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 1 commit
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 MemberShape.redactIfNecessary(model: Model, safeToPrint: String) =
model.expectShape(this.target).redactIfNecessary(safeToPrint)

fun Shape.redactIfNecessary(safeToPrint: String) =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be error prone. For example, if someone calls Shape.redactIfNecessary on a MemberShape, which is possible since a MemberShape is a shape, then that might result in the wrong code generated behavior, right? Perhaps we should just have one function that always takes a model, and do any necessary switching logic based on the type in that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it crossed my mind that this could be error prone.

if someone calls Shape.redactIfNecessary on a MemberShape, which is possible since a MemberShape is a shape, then that might result in the wrong code generated behavior, right?

Yes. I think there's an argument to be made that it's a mistake on behalf of the caller, since sensitive cannot be applied to member shapes. I only introduced MemberShape.redactIfNecessary as syntax sugar for the caller to not have to traverse to the member's target, but I don't know if I prefer to lay that responsibility on the caller, or to have a single function that always takes in model like you suggest. All 3 ways are reasonable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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