Skip to content

Commit 4a3397a

Browse files
committed
chore(core/schema): use schema log filter when available
1 parent d79dc91 commit 4a3397a

20 files changed

+125
-48
lines changed

.changeset/lemon-wasps-itch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@smithy/smithy-client": minor
3+
---
4+
5+
default schema log filter

packages/smithy-client/src/command.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import type {
2020
} from "@smithy/types";
2121
import { SMITHY_CONTEXT_KEY } from "@smithy/types";
2222

23+
import { schemaLogFilter } from "./schemaLogFilter";
24+
2325
/**
2426
* @public
2527
*/
@@ -28,7 +30,7 @@ export abstract class Command<
2830
Output extends ClientOutput,
2931
ResolvedClientConfiguration,
3032
ClientInput extends object = any,
31-
ClientOutput extends MetadataBearer = any,
33+
ClientOutput extends MetadataBearer = any
3234
> implements ICommand<ClientInput, Input, ClientOutput, Output, ResolvedClientConfiguration>
3335
{
3436
public abstract input: Input;
@@ -44,7 +46,7 @@ export abstract class Command<
4446
O extends SO,
4547
C extends { logger: Logger; requestHandler: RequestHandler<any, any, any> },
4648
SI extends object = any,
47-
SO extends MetadataBearer = any,
49+
SO extends MetadataBearer = any
4850
>() {
4951
return new ClassBuilder<I, O, C, SI, SO>();
5052
}
@@ -120,7 +122,7 @@ class ClassBuilder<
120122
O extends SO,
121123
C extends { logger: Logger; requestHandler: RequestHandler<any, any, any> },
122124
SI extends object = any,
123-
SO extends MetadataBearer = any,
125+
SO extends MetadataBearer = any
124126
> {
125127
private _init: (_: Command<I, O, C, SI, SO>) => void = () => {};
126128
private _ep: EndpointParameterInstructions = {};
@@ -130,8 +132,8 @@ class ClassBuilder<
130132
private _clientName = "";
131133
private _additionalContext = {} as HandlerExecutionContext;
132134
private _smithyContext = {} as Record<string, unknown>;
133-
private _inputFilterSensitiveLog = (_: any) => _;
134-
private _outputFilterSensitiveLog = (_: any) => _;
135+
private _inputFilterSensitiveLog: any = undefined;
136+
private _outputFilterSensitiveLog: any = undefined;
135137
private _serializer: (input: I, context: SerdeContext | any) => Promise<IHttpRequest> = null as any;
136138
private _deserializer: (output: IHttpResponse, context: SerdeContext | any) => Promise<O> = null as any;
137139
private _operationSchema?: OperationSchema;
@@ -268,8 +270,12 @@ class ClassBuilder<
268270
middlewareFn: closure._middlewareFn,
269271
clientName: closure._clientName,
270272
commandName: closure._commandName,
271-
inputFilterSensitiveLog: closure._inputFilterSensitiveLog,
272-
outputFilterSensitiveLog: closure._outputFilterSensitiveLog,
273+
inputFilterSensitiveLog:
274+
closure._inputFilterSensitiveLog ??
275+
(closure._operationSchema ? schemaLogFilter.bind(null, closure._operationSchema!.input) : (_) => _),
276+
outputFilterSensitiveLog:
277+
closure._outputFilterSensitiveLog ??
278+
(closure._operationSchema ? schemaLogFilter.bind(null, closure._operationSchema!.output) : (_) => _),
273279
smithyContext: closure._smithyContext,
274280
additionalContext: closure._additionalContext,
275281
});
@@ -299,7 +305,7 @@ export interface CommandImpl<
299305
O extends SO,
300306
C extends { logger: Logger; requestHandler: RequestHandler<any, any, any> },
301307
SI extends object = any,
302-
SO extends MetadataBearer = any,
308+
SO extends MetadataBearer = any
303309
> extends Command<I, O, C, SI, SO> {
304310
readonly input: I;
305311
resolveMiddleware(stack: IMiddlewareStack<SI, SO>, configuration: C, options: any): Handler<I, O>;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { NormalizedSchema } from "@smithy/core/schema";
2+
import type { SchemaRef } from "@smithy/types";
3+
4+
const SENSITIVE_STRING = "***SensitiveInformation***";
5+
6+
/**
7+
* Redacts sensitive parts of any data object using its schema, for logging.
8+
*
9+
* @internal
10+
* @param schema - with filtering traits.
11+
* @param data - to be logged.
12+
*/
13+
export function schemaLogFilter(schema: SchemaRef, data: unknown): any {
14+
if (data == null) {
15+
return data;
16+
}
17+
const ns = NormalizedSchema.of(schema);
18+
if (ns.getMergedTraits().sensitive) {
19+
return SENSITIVE_STRING;
20+
}
21+
22+
if (ns.isListSchema()) {
23+
const isSensitive = !!ns.getValueSchema().getMergedTraits().sensitive;
24+
if (isSensitive) {
25+
return SENSITIVE_STRING;
26+
}
27+
} else if (ns.isMapSchema()) {
28+
const isSensitive =
29+
!!ns.getKeySchema().getMergedTraits().sensitive || !!ns.getValueSchema().getMergedTraits().sensitive;
30+
if (isSensitive) {
31+
return SENSITIVE_STRING;
32+
}
33+
} else if (ns.isStructSchema() && typeof data === "object") {
34+
const object = data as Record<string, unknown>;
35+
36+
const newObject = {} as any;
37+
for (const [member, memberNs] of ns.structIterator()) {
38+
if (object[member] != null) {
39+
newObject[member] = schemaLogFilter(memberNs, object[member]);
40+
}
41+
}
42+
return newObject;
43+
}
44+
45+
return data;
46+
}

private/smithy-rpcv2-cbor-schema/src/commands/EmptyInputOutputCommand.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class EmptyInputOutputCommand extends $Command
6666
})
6767
.s("RpcV2Protocol", "EmptyInputOutput", {})
6868
.n("RpcV2ProtocolClient", "EmptyInputOutputCommand")
69-
.f(void 0, void 0)
69+
7070
.sc(EmptyInputOutput)
7171
.build() {
7272
/** @internal type navigation helper, not in runtime. */

private/smithy-rpcv2-cbor-schema/src/commands/Float16Command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export class Float16Command extends $Command
6868
})
6969
.s("RpcV2Protocol", "Float16", {})
7070
.n("RpcV2ProtocolClient", "Float16Command")
71-
.f(void 0, void 0)
71+
7272
.sc(Float16)
7373
.build() {
7474
/** @internal type navigation helper, not in runtime. */

private/smithy-rpcv2-cbor-schema/src/commands/FractionalSecondsCommand.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export class FractionalSecondsCommand extends $Command
6868
})
6969
.s("RpcV2Protocol", "FractionalSeconds", {})
7070
.n("RpcV2ProtocolClient", "FractionalSecondsCommand")
71-
.f(void 0, void 0)
71+
7272
.sc(FractionalSeconds)
7373
.build() {
7474
/** @internal type navigation helper, not in runtime. */

private/smithy-rpcv2-cbor-schema/src/commands/GreetingWithErrorsCommand.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export class GreetingWithErrorsCommand extends $Command
8181
})
8282
.s("RpcV2Protocol", "GreetingWithErrors", {})
8383
.n("RpcV2ProtocolClient", "GreetingWithErrorsCommand")
84-
.f(void 0, void 0)
84+
8585
.sc(GreetingWithErrors)
8686
.build() {
8787
/** @internal type navigation helper, not in runtime. */

private/smithy-rpcv2-cbor-schema/src/commands/NoInputOutputCommand.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export class NoInputOutputCommand extends $Command
6565
})
6666
.s("RpcV2Protocol", "NoInputOutput", {})
6767
.n("RpcV2ProtocolClient", "NoInputOutputCommand")
68-
.f(void 0, void 0)
68+
6969
.sc(NoInputOutput)
7070
.build() {
7171
/** @internal type navigation helper, not in runtime. */

private/smithy-rpcv2-cbor-schema/src/commands/OperationWithDefaultsCommand.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export class OperationWithDefaultsCommand extends $Command
134134
})
135135
.s("RpcV2Protocol", "OperationWithDefaults", {})
136136
.n("RpcV2ProtocolClient", "OperationWithDefaultsCommand")
137-
.f(void 0, void 0)
137+
138138
.sc(OperationWithDefaults)
139139
.build() {
140140
/** @internal type navigation helper, not in runtime. */

private/smithy-rpcv2-cbor-schema/src/commands/OptionalInputOutputCommand.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class OptionalInputOutputCommand extends $Command
7070
})
7171
.s("RpcV2Protocol", "OptionalInputOutput", {})
7272
.n("RpcV2ProtocolClient", "OptionalInputOutputCommand")
73-
.f(void 0, void 0)
73+
7474
.sc(OptionalInputOutput)
7575
.build() {
7676
/** @internal type navigation helper, not in runtime. */

0 commit comments

Comments
 (0)