Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.

Commit 730d7e3

Browse files
committed
aws/awsutils: Update not to render sensitive fields for StringValue
Updates the StringValue utility to not render fields decorated with sensitive fields. Instead, sensitive fields are outputted as "<sensitive>". This change is limited to the output of StringValue utility method.
1 parent dbd6841 commit 730d7e3

File tree

3 files changed

+70
-13
lines changed

3 files changed

+70
-13
lines changed

aws/awsutil/string_value.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,27 @@ func stringValue(v reflect.Value, indent int, buf *bytes.Buffer) {
2323
case reflect.Struct:
2424
buf.WriteString("{\n")
2525

26-
names := []string{}
2726
for i := 0; i < v.Type().NumField(); i++ {
28-
name := v.Type().Field(i).Name
29-
f := v.Field(i)
30-
if name[0:1] == strings.ToLower(name[0:1]) {
27+
ft := v.Type().Field(i)
28+
fv := v.Field(i)
29+
30+
if ft.Name[0:1] == strings.ToLower(ft.Name[0:1]) {
3131
continue // ignore unexported fields
3232
}
33-
if (f.Kind() == reflect.Ptr || f.Kind() == reflect.Slice) && f.IsNil() {
33+
if (fv.Kind() == reflect.Ptr || fv.Kind() == reflect.Slice) && fv.IsNil() {
3434
continue // ignore unset fields
3535
}
36-
names = append(names, name)
37-
}
3836

39-
for i, n := range names {
40-
val := v.FieldByName(n)
4137
buf.WriteString(strings.Repeat(" ", indent+2))
42-
buf.WriteString(n + ": ")
43-
stringValue(val, indent+2, buf)
38+
buf.WriteString(ft.Name + ": ")
4439

45-
if i < len(names)-1 {
46-
buf.WriteString(",\n")
40+
if tag := ft.Tag.Get("sensitive"); tag == "true" {
41+
buf.WriteString("<sensitive>")
42+
} else {
43+
stringValue(fv, indent+2, buf)
4744
}
45+
46+
buf.WriteString(",\n")
4847
}
4948

5049
buf.WriteString("\n" + strings.Repeat(" ", indent) + "}")

aws/awsutil/string_value_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// +build go1.7
2+
3+
package awsutil
4+
5+
import (
6+
"testing"
7+
8+
"github.com/aws/aws-sdk-go/aws"
9+
)
10+
11+
type testStruct struct {
12+
Field1 string
13+
Field2 *string
14+
Field3 []byte `sensitive:"true"`
15+
Value []*string
16+
}
17+
18+
func TestStringValue(t *testing.T) {
19+
cases := map[string]struct {
20+
Value interface{}
21+
Expect string
22+
}{
23+
"general": {
24+
Value: testStruct{
25+
Field1: "abc123",
26+
Field2: aws.String("abc123"),
27+
Field3: []byte("don't show me"),
28+
Value: []*string{
29+
aws.String("first"),
30+
aws.String("second"),
31+
},
32+
},
33+
Expect: `{
34+
Field1: "abc123",
35+
Field2: "abc123",
36+
Field3: <sensitive>,
37+
Value: ["first","second"],
38+
39+
}`,
40+
},
41+
}
42+
43+
for d, c := range cases {
44+
t.Run(d, func(t *testing.T) {
45+
actual := StringValue(c.Value)
46+
if e, a := c.Expect, actual; e != a {
47+
t.Errorf("expect:\n%v\nactual:\n%v\n", e, a)
48+
}
49+
})
50+
}
51+
}

private/model/api/shape.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ type Shape struct {
110110
// Flags that the shape cannot be rename. Prevents the shape from being
111111
// renamed further by the Input/Output.
112112
AliasedShapeName bool
113+
114+
// Sensitive types should not be logged by SDK type loggers.
115+
Sensitive bool `json:"sensitive"`
113116
}
114117

115118
// ErrorCodeName will return the error shape's name formated for
@@ -502,6 +505,10 @@ func (ref *ShapeRef) GoTags(toplevel bool, isRequired bool) string {
502505
tags = append(tags, ShapeTag{"ignore", "true"})
503506
}
504507

508+
if ref.Shape.Sensitive {
509+
tags = append(tags, ShapeTag{"sensitive", "true"})
510+
}
511+
505512
return fmt.Sprintf("`%s`", tags)
506513
}
507514

0 commit comments

Comments
 (0)