@@ -1171,208 +1171,6 @@ func TestDropRunAsGroup(t *testing.T) {
1171
1171
}
1172
1172
}
1173
1173
1174
- func TestDropGMSAFields (t * testing.T ) {
1175
- defaultContainerSecurityContextFactory := func () * api.SecurityContext {
1176
- defaultProcMount := api .DefaultProcMount
1177
- return & api.SecurityContext {ProcMount : & defaultProcMount }
1178
- }
1179
- podWithoutWindowsOptionsFactory := func () * api.Pod {
1180
- return & api.Pod {
1181
- Spec : api.PodSpec {
1182
- RestartPolicy : api .RestartPolicyNever ,
1183
- SecurityContext : & api.PodSecurityContext {},
1184
- Containers : []api.Container {{Name : "container1" , Image : "testimage" , SecurityContext : defaultContainerSecurityContextFactory ()}},
1185
- InitContainers : []api.Container {{Name : "initContainer1" , Image : "testimage" , SecurityContext : defaultContainerSecurityContextFactory ()}},
1186
- },
1187
- }
1188
- }
1189
-
1190
- type podFactoryInfo struct {
1191
- description string
1192
- hasGMSAField bool
1193
- // this factory should generate the input pod whose spec will be fed to dropDisabledFields
1194
- podFactory func () * api.Pod
1195
- // this factory should generate the expected pod after the GMSA fields have been dropped
1196
- // we can't just use podWithoutWindowsOptionsFactory as is for this, since in some cases
1197
- // we'll be left with a WindowsSecurityContextOptions struct with no GMSA field set, as opposed
1198
- // to a nil pointer in the pod generated by podWithoutWindowsOptionsFactory
1199
- // if this field is not set, it will default to the podFactory
1200
- strippedPodFactory func () * api.Pod
1201
- }
1202
- podFactoryInfos := []podFactoryInfo {
1203
- {
1204
- description : "does not have any GMSA field set" ,
1205
- hasGMSAField : false ,
1206
- podFactory : podWithoutWindowsOptionsFactory ,
1207
- },
1208
- {
1209
- description : "has a pod-level WindowsSecurityContextOptions struct with no GMSA field set" ,
1210
- hasGMSAField : false ,
1211
- podFactory : func () * api.Pod {
1212
- pod := podWithoutWindowsOptionsFactory ()
1213
- pod .Spec .SecurityContext .WindowsOptions = & api.WindowsSecurityContextOptions {}
1214
- return pod
1215
- },
1216
- },
1217
- {
1218
- description : "has a WindowsSecurityContextOptions struct with no GMSA field set on a container" ,
1219
- hasGMSAField : false ,
1220
- podFactory : func () * api.Pod {
1221
- pod := podWithoutWindowsOptionsFactory ()
1222
- pod .Spec .Containers [0 ].SecurityContext .WindowsOptions = & api.WindowsSecurityContextOptions {}
1223
- return pod
1224
- },
1225
- },
1226
- {
1227
- description : "has a WindowsSecurityContextOptions struct with no GMSA field set on an init container" ,
1228
- hasGMSAField : false ,
1229
- podFactory : func () * api.Pod {
1230
- pod := podWithoutWindowsOptionsFactory ()
1231
- pod .Spec .InitContainers [0 ].SecurityContext .WindowsOptions = & api.WindowsSecurityContextOptions {}
1232
- return pod
1233
- },
1234
- },
1235
- {
1236
- description : "is nil" ,
1237
- hasGMSAField : false ,
1238
- podFactory : func () * api.Pod { return nil },
1239
- },
1240
- }
1241
-
1242
- toPtr := func (s string ) * string {
1243
- return & s
1244
- }
1245
- addGMSACredentialSpecName := func (windowsOptions * api.WindowsSecurityContextOptions ) {
1246
- windowsOptions .GMSACredentialSpecName = toPtr ("dummy-gmsa-cred-spec-name" )
1247
- }
1248
- addGMSACredentialSpec := func (windowsOptions * api.WindowsSecurityContextOptions ) {
1249
- windowsOptions .GMSACredentialSpec = toPtr ("dummy-gmsa-cred-spec-contents" )
1250
- }
1251
- addBothGMSAFields := func (windowsOptions * api.WindowsSecurityContextOptions ) {
1252
- addGMSACredentialSpecName (windowsOptions )
1253
- addGMSACredentialSpec (windowsOptions )
1254
- }
1255
-
1256
- for fieldName , windowsOptionsTransformingFunc := range map [string ]func (* api.WindowsSecurityContextOptions ){
1257
- "GMSACredentialSpecName field" : addGMSACredentialSpecName ,
1258
- "GMSACredentialSpec field" : addGMSACredentialSpec ,
1259
- "both GMSA fields" : addBothGMSAFields ,
1260
- } {
1261
- // yes, these variables are indeed needed for the closure to work
1262
- // properly, please do NOT remove them
1263
- name := fieldName
1264
- transformingFunc := windowsOptionsTransformingFunc
1265
-
1266
- windowsOptionsWithGMSAFieldFactory := func () * api.WindowsSecurityContextOptions {
1267
- windowsOptions := & api.WindowsSecurityContextOptions {}
1268
- transformingFunc (windowsOptions )
1269
- return windowsOptions
1270
- }
1271
-
1272
- podFactoryInfos = append (podFactoryInfos ,
1273
- podFactoryInfo {
1274
- description : fmt .Sprintf ("has %s in Pod" , name ),
1275
- hasGMSAField : true ,
1276
- podFactory : func () * api.Pod {
1277
- pod := podWithoutWindowsOptionsFactory ()
1278
- pod .Spec .SecurityContext .WindowsOptions = windowsOptionsWithGMSAFieldFactory ()
1279
- return pod
1280
- },
1281
- strippedPodFactory : func () * api.Pod {
1282
- pod := podWithoutWindowsOptionsFactory ()
1283
- pod .Spec .SecurityContext .WindowsOptions = & api.WindowsSecurityContextOptions {}
1284
- return pod
1285
- },
1286
- },
1287
- podFactoryInfo {
1288
- description : fmt .Sprintf ("has %s in Container" , name ),
1289
- hasGMSAField : true ,
1290
- podFactory : func () * api.Pod {
1291
- pod := podWithoutWindowsOptionsFactory ()
1292
- pod .Spec .Containers [0 ].SecurityContext .WindowsOptions = windowsOptionsWithGMSAFieldFactory ()
1293
- return pod
1294
- },
1295
- strippedPodFactory : func () * api.Pod {
1296
- pod := podWithoutWindowsOptionsFactory ()
1297
- pod .Spec .Containers [0 ].SecurityContext .WindowsOptions = & api.WindowsSecurityContextOptions {}
1298
- return pod
1299
- },
1300
- },
1301
- podFactoryInfo {
1302
- description : fmt .Sprintf ("has %s in InitContainer" , name ),
1303
- hasGMSAField : true ,
1304
- podFactory : func () * api.Pod {
1305
- pod := podWithoutWindowsOptionsFactory ()
1306
- pod .Spec .InitContainers [0 ].SecurityContext .WindowsOptions = windowsOptionsWithGMSAFieldFactory ()
1307
- return pod
1308
- },
1309
- strippedPodFactory : func () * api.Pod {
1310
- pod := podWithoutWindowsOptionsFactory ()
1311
- pod .Spec .InitContainers [0 ].SecurityContext .WindowsOptions = & api.WindowsSecurityContextOptions {}
1312
- return pod
1313
- },
1314
- })
1315
- }
1316
-
1317
- for _ , enabled := range []bool {true , false } {
1318
- for _ , oldPodFactoryInfo := range podFactoryInfos {
1319
- for _ , newPodFactoryInfo := range podFactoryInfos {
1320
- newPodHasGMSAField , newPod := newPodFactoryInfo .hasGMSAField , newPodFactoryInfo .podFactory ()
1321
- if newPod == nil {
1322
- continue
1323
- }
1324
- oldPodHasGMSAField , oldPod := oldPodFactoryInfo .hasGMSAField , oldPodFactoryInfo .podFactory ()
1325
-
1326
- t .Run (fmt .Sprintf ("feature enabled=%v, old pod %s, new pod %s" , enabled , oldPodFactoryInfo .description , newPodFactoryInfo .description ), func (t * testing.T ) {
1327
- defer featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .WindowsGMSA , enabled )()
1328
-
1329
- var oldPodSpec * api.PodSpec
1330
- if oldPod != nil {
1331
- oldPodSpec = & oldPod .Spec
1332
- }
1333
- dropDisabledFields (& newPod .Spec , nil , oldPodSpec , nil )
1334
-
1335
- // old pod should never be changed
1336
- if ! reflect .DeepEqual (oldPod , oldPodFactoryInfo .podFactory ()) {
1337
- t .Errorf ("old pod changed: %v" , diff .ObjectReflectDiff (oldPod , oldPodFactoryInfo .podFactory ()))
1338
- }
1339
-
1340
- switch {
1341
- case enabled || oldPodHasGMSAField :
1342
- // new pod should not be changed if the feature is enabled, or if the old pod had any GMSA field set
1343
- if ! reflect .DeepEqual (newPod , newPodFactoryInfo .podFactory ()) {
1344
- t .Errorf ("new pod changed: %v" , diff .ObjectReflectDiff (newPod , newPodFactoryInfo .podFactory ()))
1345
- }
1346
- case newPodHasGMSAField :
1347
- // new pod should be changed
1348
- if reflect .DeepEqual (newPod , newPodFactoryInfo .podFactory ()) {
1349
- t .Errorf ("%v" , oldPod )
1350
- t .Errorf ("%v" , newPod )
1351
- t .Errorf ("new pod was not changed" )
1352
- }
1353
- // new pod should not have any GMSA field set
1354
- var expectedStrippedPod * api.Pod
1355
- if newPodFactoryInfo .strippedPodFactory == nil {
1356
- expectedStrippedPod = newPodFactoryInfo .podFactory ()
1357
- } else {
1358
- expectedStrippedPod = newPodFactoryInfo .strippedPodFactory ()
1359
- }
1360
-
1361
- if ! reflect .DeepEqual (newPod , expectedStrippedPod ) {
1362
- t .Errorf ("new pod had some GMSA field set: %v" , diff .ObjectReflectDiff (newPod , expectedStrippedPod ))
1363
- }
1364
- default :
1365
- // new pod should not need to be changed
1366
- if ! reflect .DeepEqual (newPod , newPodFactoryInfo .podFactory ()) {
1367
- t .Errorf ("new pod changed: %v" , diff .ObjectReflectDiff (newPod , newPodFactoryInfo .podFactory ()))
1368
- }
1369
- }
1370
- })
1371
- }
1372
- }
1373
- }
1374
- }
1375
-
1376
1174
func TestDropPodSysctls (t * testing.T ) {
1377
1175
podWithSysctls := func () * api.Pod {
1378
1176
return & api.Pod {
0 commit comments