Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor redactIfNecessary function #1746

Merged
merged 4 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,14 @@ import software.amazon.smithy.rust.codegen.core.smithy.traits.SyntheticInputTrai
import software.amazon.smithy.rust.codegen.core.util.dq
import software.amazon.smithy.rust.codegen.core.util.getTrait
import software.amazon.smithy.rust.codegen.core.util.hasTrait
import software.amazon.smithy.rust.codegen.core.util.redactIfNecessary

fun RustWriter.implBlock(structureShape: Shape, symbolProvider: SymbolProvider, block: RustWriter.() -> Unit) {
rustBlock("impl ${symbolProvider.toSymbol(structureShape).name}") {
block(this)
}
}

fun redactIfNecessary(member: MemberShape, model: Model, safeToPrint: String): String {
return if (member.getMemberTrait(model, SensitiveTrait::class.java).isPresent) {
"*** Sensitive Data Redacted ***".dq()
} else {
safeToPrint
}
}

open class StructureGenerator(
val model: Model,
private val symbolProvider: RustSymbolProvider,
Expand Down Expand Up @@ -114,9 +107,7 @@ open class StructureGenerator(
rust("""let mut formatter = f.debug_struct(${name.dq()});""")
members.forEach { member ->
val memberName = symbolProvider.toMemberName(member)
val fieldValue = redactIfNecessary(
member, model, "self.$memberName",
)
val fieldValue = member.redactIfNecessary(model, "self.$memberName")
rust(
"formatter.field(${memberName.dq()}, &$fieldValue);",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import software.amazon.smithy.rust.codegen.client.smithy.CoreCodegenContext
import software.amazon.smithy.rust.codegen.client.smithy.RuntimeType
import software.amazon.smithy.rust.codegen.client.smithy.generators.CodegenTarget
import software.amazon.smithy.rust.codegen.client.smithy.generators.operationBuildError
import software.amazon.smithy.rust.codegen.client.smithy.generators.redactIfNecessary
import software.amazon.smithy.rust.codegen.client.smithy.makeOptional
import software.amazon.smithy.rust.codegen.client.smithy.mapRustType
import software.amazon.smithy.rust.codegen.client.smithy.protocols.HttpBindingDescriptor
Expand All @@ -54,6 +53,7 @@ import software.amazon.smithy.rust.codegen.core.util.inputShape
import software.amazon.smithy.rust.codegen.core.util.isPrimitive
import software.amazon.smithy.rust.codegen.core.util.isStreaming
import software.amazon.smithy.rust.codegen.core.util.outputShape
import software.amazon.smithy.rust.codegen.core.util.redactIfNecessary
import software.amazon.smithy.rust.codegen.core.util.toSnakeCase

/**
Expand Down Expand Up @@ -492,11 +492,7 @@ class HttpBindingGenerator(
let header_value = $safeName;
let header_value = http::header::HeaderValue::try_from(&*header_value).map_err(|err| {
#{build_error}::InvalidField { field: "$memberName", details: format!("`{}` cannot be used as a header value: {}", &${
redactIfNecessary(
memberShape,
model,
"header_value",
)
memberShape.redactIfNecessary(model, "header_value")
}, err)}
})?;
builder = builder.header("${httpBinding.locationName}", header_value);
Expand Down Expand Up @@ -532,11 +528,7 @@ class HttpBindingGenerator(
#{build_error}::InvalidField {
field: "$memberName",
details: format!("`{}` cannot be used as a header value: {}", ${
redactIfNecessary(
memberShape,
model,
"v",
)
memberShape.redactIfNecessary(model, "v")
}, err)}
})?;
builder = builder.header(header_name, header_value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import software.amazon.smithy.model.shapes.Shape
import software.amazon.smithy.model.shapes.ShapeId
import software.amazon.smithy.model.shapes.StructureShape
import software.amazon.smithy.model.shapes.UnionShape
import software.amazon.smithy.model.traits.SensitiveTrait
import software.amazon.smithy.model.traits.StreamingTrait
import software.amazon.smithy.model.traits.Trait
import software.amazon.smithy.rust.codegen.core.smithy.traits.SyntheticInputTrait
Expand Down Expand Up @@ -81,6 +82,16 @@ fun ServiceShape.hasEventStreamOperations(model: Model): Boolean = operations.an
model.expectShape(id, OperationShape::class.java).isEventStream(model)
}

fun Shape.redactIfNecessary(model: Model, safeToPrint: String): String =
when (this) {
is MemberShape -> model.expectShape(this.target).redactIfNecessary(model, safeToPrint)
else -> if (this.hasTrait<SensitiveTrait>()) {
"*** Sensitive Data Redacted ***".dq()
} else {
safeToPrint
}
}

/*
* Returns the member of this structure targeted with streaming trait (if it exists).
*
Expand Down