@@ -21,13 +21,16 @@ import (
21
21
"testing"
22
22
23
23
"github.com/container-storage-interface/spec/lib/go/csi"
24
+ "github.com/golang/protobuf/proto"
24
25
"github.com/kubernetes-csi/csi-lib-utils/protosanitizer/test/csitest"
25
26
"github.com/stretchr/testify/assert"
26
27
)
27
28
28
29
func TestStripSecrets (t * testing.T ) {
29
30
secretName := "secret-abc"
30
31
secretValue := "123"
32
+
33
+ // Current spec.
31
34
createVolume := & csi.CreateVolumeRequest {
32
35
AccessibilityRequirements : & csi.TopologyRequirement {
33
36
Requisite : []* csi.Topology {
@@ -63,9 +66,50 @@ func TestStripSecrets(t *testing.T) {
63
66
},
64
67
}
65
68
66
- cases := []struct {
69
+ // Revised spec with more secret fields.
70
+ createVolumeFuture := & csitest.CreateVolumeRequest {
71
+ CapacityRange : & csitest.CapacityRange {
72
+ RequiredBytes : 1024 ,
73
+ },
74
+ MaybeSecretMap : map [int64 ]* csitest.VolumeCapability {
75
+ 1 : & csitest.VolumeCapability {ArraySecret : "aaa" },
76
+ 2 : & csitest.VolumeCapability {ArraySecret : "bbb" },
77
+ },
78
+ Name : "foo" ,
79
+ NewSecretInt : 42 ,
80
+ Seecreets : map [string ]string {
81
+ secretName : secretValue ,
82
+ "secret-xyz" : "987" ,
83
+ },
84
+ VolumeCapabilities : []* csitest.VolumeCapability {
85
+ & csitest.VolumeCapability {
86
+ AccessType : & csitest.VolumeCapability_Mount {
87
+ Mount : & csitest.VolumeCapability_MountVolume {
88
+ FsType : "ext4" ,
89
+ },
90
+ },
91
+ ArraySecret : "knock knock" ,
92
+ },
93
+ & csitest.VolumeCapability {
94
+ ArraySecret : "Who's there?" ,
95
+ },
96
+ },
97
+ VolumeContentSource : & csitest.VolumeContentSource {
98
+ Type : & csitest.VolumeContentSource_Volume {
99
+ Volume : & csitest.VolumeContentSource_VolumeSource {
100
+ VolumeId : "abc" ,
101
+ OneofSecretField : "hello" ,
102
+ },
103
+ },
104
+ NestedSecretField : "world" ,
105
+ },
106
+ }
107
+
108
+ type testcase struct {
67
109
original , stripped interface {}
68
- }{
110
+ }
111
+
112
+ cases := []testcase {
69
113
{nil , "null" },
70
114
{1 , "1" },
71
115
{"hello world" , `"hello world"` },
@@ -99,43 +143,7 @@ func TestStripSecrets(t *testing.T) {
99
143
}, `{"accessibility_requirements":{},"capacity_range":{"limit_bytes":1024,"required_bytes":1024},"name":"test-volume","parameters":{"param1":"param1","param2":"param2"},"secrets":"***stripped***","volume_capabilities":[{"AccessType":{"Mount":{"fs_type":"ext4","mount_flags":["flag1","flag2","flag3"]}},"access_mode":{"mode":5}}],"volume_content_source":{"Type":null}}` },
100
144
{createVolume , `{"accessibility_requirements":{"requisite":[{"segments":{"foo":"bar","x":"y"}},{"segments":{"a":"b"}}]},"capacity_range":{"required_bytes":1024},"name":"foo","secrets":"***stripped***","volume_capabilities":[{"AccessType":{"Mount":{"fs_type":"ext4"}}}]}` },
101
145
{& csitest.CreateVolumeRequest {}, `{}` },
102
- {& csitest.CreateVolumeRequest {
103
- CapacityRange : & csitest.CapacityRange {
104
- RequiredBytes : 1024 ,
105
- },
106
- MaybeSecretMap : map [int64 ]* csitest.VolumeCapability {
107
- 1 : & csitest.VolumeCapability {ArraySecret : "aaa" },
108
- 2 : & csitest.VolumeCapability {ArraySecret : "bbb" },
109
- },
110
- Name : "foo" ,
111
- NewSecretInt : 42 ,
112
- Seecreets : map [string ]string {
113
- secretName : secretValue ,
114
- "secret-xyz" : "987" ,
115
- },
116
- VolumeCapabilities : []* csitest.VolumeCapability {
117
- & csitest.VolumeCapability {
118
- AccessType : & csitest.VolumeCapability_Mount {
119
- Mount : & csitest.VolumeCapability_MountVolume {
120
- FsType : "ext4" ,
121
- },
122
- },
123
- ArraySecret : "knock knock" ,
124
- },
125
- & csitest.VolumeCapability {
126
- ArraySecret : "Who's there?" ,
127
- },
128
- },
129
- VolumeContentSource : & csitest.VolumeContentSource {
130
- Type : & csitest.VolumeContentSource_Volume {
131
- Volume : & csitest.VolumeContentSource_VolumeSource {
132
- VolumeId : "abc" ,
133
- OneofSecretField : "hello" ,
134
- },
135
- },
136
- NestedSecretField : "world" ,
137
- },
138
- },
146
+ {createVolumeFuture ,
139
147
// Secrets are *not* removed from all fields yet. This will have to be fixed one way or another
140
148
// before the CSI spec can start using secrets there (currently it doesn't).
141
149
// The test is still useful because it shows that also complicated fields get serialized.
@@ -144,6 +152,17 @@ func TestStripSecrets(t *testing.T) {
144
152
},
145
153
}
146
154
155
+ // Message from revised spec as received by a sidecar based on the current spec.
156
+ // The XXX_unrecognized field contains secrets and must not get logged.
157
+ unknownFields := & csi.CreateVolumeRequest {}
158
+ data , err := proto .Marshal (createVolumeFuture )
159
+ if assert .NoError (t , err , "marshall future message" ) &&
160
+ assert .NoError (t , proto .Unmarshal (data , unknownFields ), "unmarshal with unknown fields" ) {
161
+ cases = append (cases , testcase {unknownFields ,
162
+ `{"capacity_range":{"required_bytes":1024},"name":"foo","secrets":"***stripped***","volume_capabilities":[{"AccessType":{"Mount":{"fs_type":"ext4"}}},{"AccessType":null}],"volume_content_source":{"Type":{"Volume":{"volume_id":"abc"}}}}` ,
163
+ })
164
+ }
165
+
147
166
for _ , c := range cases {
148
167
before := fmt .Sprint (c .original )
149
168
stripped := StripSecrets (c .original )
0 commit comments