@@ -306,9 +306,15 @@ func (c *criService) containerSpecOpts(config *runtime.ContainerConfig, imageCon
306306 }
307307 specOpts = append (specOpts , customopts .WithAdditionalGIDs (userstr ))
308308
309+ asp := securityContext .GetApparmor ()
310+ if asp == nil {
311+ asp , err = generateApparmorSecurityProfile (securityContext .GetApparmorProfile ()) // nolint:staticcheck deprecated but we don't want to remove yet
312+ if err != nil {
313+ return nil , errors .Wrap (err , "failed to generate apparmor spec opts" )
314+ }
315+ }
309316 apparmorSpecOpts , err := generateApparmorSpecOpts (
310- securityContext .GetApparmor (),
311- securityContext .GetApparmorProfile (), // nolint:staticcheck deprecated but we don't want to remove yet
317+ asp ,
312318 securityContext .GetPrivileged (),
313319 c .apparmorEnabled ())
314320 if err != nil {
@@ -318,9 +324,17 @@ func (c *criService) containerSpecOpts(config *runtime.ContainerConfig, imageCon
318324 specOpts = append (specOpts , apparmorSpecOpts )
319325 }
320326
327+ ssp := securityContext .GetSeccomp ()
328+ if ssp == nil {
329+ ssp , err = generateSeccompSecurityProfile (
330+ securityContext .GetSeccompProfilePath (), // nolint:staticcheck deprecated but we don't want to remove yet
331+ c .config .UnsetSeccompProfile )
332+ if err != nil {
333+ return nil , errors .Wrap (err , "failed to generate seccomp spec opts" )
334+ }
335+ }
321336 seccompSpecOpts , err := c .generateSeccompSpecOpts (
322- securityContext .GetSeccomp (),
323- securityContext .GetSeccompProfilePath (), // nolint:staticcheck deprecated but we don't want to remove yet
337+ ssp ,
324338 securityContext .GetPrivileged (),
325339 c .seccompEnabled ())
326340 if err != nil {
@@ -332,24 +346,51 @@ func (c *criService) containerSpecOpts(config *runtime.ContainerConfig, imageCon
332346 return specOpts , nil
333347}
334348
349+ func generateSeccompSecurityProfile (profilePath string , unsetProfilePath string ) (* runtime.SecurityProfile , error ) {
350+ if profilePath != "" {
351+ return generateSecurityProfile (profilePath )
352+ }
353+ if unsetProfilePath != "" {
354+ return generateSecurityProfile (unsetProfilePath )
355+ }
356+ return nil , nil
357+ }
358+ func generateApparmorSecurityProfile (profilePath string ) (* runtime.SecurityProfile , error ) {
359+ if profilePath != "" {
360+ return generateSecurityProfile (profilePath )
361+ }
362+ return nil , nil
363+ }
364+
365+ func generateSecurityProfile (profilePath string ) (* runtime.SecurityProfile , error ) {
366+ switch profilePath {
367+ case runtimeDefault , dockerDefault , "" :
368+ return & runtime.SecurityProfile {
369+ ProfileType : runtime .SecurityProfile_RuntimeDefault ,
370+ }, nil
371+ case unconfinedProfile :
372+ return & runtime.SecurityProfile {
373+ ProfileType : runtime .SecurityProfile_Unconfined ,
374+ }, nil
375+ default :
376+ // Require and Trim default profile name prefix
377+ if ! strings .HasPrefix (profilePath , profileNamePrefix ) {
378+ return nil , errors .Errorf ("invalid profile %q" , profilePath )
379+ }
380+ return & runtime.SecurityProfile {
381+ ProfileType : runtime .SecurityProfile_Localhost ,
382+ LocalhostRef : strings .TrimPrefix (profilePath , profileNamePrefix ),
383+ }, nil
384+ }
385+ }
386+
335387// generateSeccompSpecOpts generates containerd SpecOpts for seccomp.
336- func (c * criService ) generateSeccompSpecOpts (sp * runtime.SecurityProfile , seccompProf string , privileged , seccompEnabled bool ) (oci.SpecOpts , error ) {
388+ func (c * criService ) generateSeccompSpecOpts (sp * runtime.SecurityProfile , privileged , seccompEnabled bool ) (oci.SpecOpts , error ) {
337389 if privileged {
338390 // Do not set seccomp profile when container is privileged
339391 return nil , nil
340392 }
341- if seccompProf == "" && sp == nil {
342- seccompProf = c .config .UnsetSeccompProfile
343- }
344- // Set seccomp profile
345- if seccompProf == runtimeDefault || seccompProf == dockerDefault {
346- // use correct default profile (Eg. if not configured otherwise, the default is docker/default)
347- seccompProf = seccompDefaultProfile
348- }
349393 if ! seccompEnabled {
350- if seccompProf != "" && seccompProf != unconfinedProfile {
351- return nil , errors .New ("seccomp is not supported" )
352- }
353394 if sp != nil {
354395 if sp .ProfileType != runtime .SecurityProfile_Unconfined {
355396 return nil , errors .New ("seccomp is not supported" )
@@ -358,49 +399,33 @@ func (c *criService) generateSeccompSpecOpts(sp *runtime.SecurityProfile, seccom
358399 return nil , nil
359400 }
360401
361- if sp != nil {
362- if sp .ProfileType != runtime .SecurityProfile_Localhost && sp .LocalhostRef != "" {
363- return nil , errors .New ("seccomp config invalid LocalhostRef must only be set if ProfileType is Localhost" )
364- }
365- switch sp .ProfileType {
366- case runtime .SecurityProfile_Unconfined :
367- // Do not set seccomp profile.
368- return nil , nil
369- case runtime .SecurityProfile_RuntimeDefault :
370- return seccomp .WithDefaultProfile (), nil
371- case runtime .SecurityProfile_Localhost :
372- // trimming the localhost/ prefix just in case even through it should not
373- // be necessary with the new SecurityProfile struct
374- return seccomp .WithProfile (strings .TrimPrefix (sp .LocalhostRef , profileNamePrefix )), nil
375- default :
376- return nil , errors .New ("seccomp unknown ProfileType" )
377- }
402+ if sp == nil {
403+ return nil , nil
378404 }
379405
380- switch seccompProf {
381- case "" , unconfinedProfile :
406+ if sp .ProfileType != runtime .SecurityProfile_Localhost && sp .LocalhostRef != "" {
407+ return nil , errors .New ("seccomp config invalid LocalhostRef must only be set if ProfileType is Localhost" )
408+ }
409+ switch sp .ProfileType {
410+ case runtime .SecurityProfile_Unconfined :
382411 // Do not set seccomp profile.
383412 return nil , nil
384- case dockerDefault :
385- // Note: WithDefaultProfile specOpts must be added after capabilities
413+ case runtime .SecurityProfile_RuntimeDefault :
386414 return seccomp .WithDefaultProfile (), nil
415+ case runtime .SecurityProfile_Localhost :
416+ // trimming the localhost/ prefix just in case even though it should not
417+ // be necessary with the new SecurityProfile struct
418+ return seccomp .WithProfile (strings .TrimPrefix (sp .LocalhostRef , profileNamePrefix )), nil
387419 default :
388- // Require and Trim default profile name prefix
389- if ! strings .HasPrefix (seccompProf , profileNamePrefix ) {
390- return nil , errors .Errorf ("invalid seccomp profile %q" , seccompProf )
391- }
392- return seccomp .WithProfile (strings .TrimPrefix (seccompProf , profileNamePrefix )), nil
420+ return nil , errors .New ("seccomp unknown ProfileType" )
393421 }
394422}
395423
396424// generateApparmorSpecOpts generates containerd SpecOpts for apparmor.
397- func generateApparmorSpecOpts (sp * runtime.SecurityProfile , apparmorProf string , privileged , apparmorEnabled bool ) (oci.SpecOpts , error ) {
425+ func generateApparmorSpecOpts (sp * runtime.SecurityProfile , privileged , apparmorEnabled bool ) (oci.SpecOpts , error ) {
398426 if ! apparmorEnabled {
399427 // Should fail loudly if user try to specify apparmor profile
400428 // but we don't support it.
401- if apparmorProf != "" && apparmorProf != unconfinedProfile {
402- return nil , errors .New ("apparmor is not supported" )
403- }
404429 if sp != nil {
405430 if sp .ProfileType != runtime .SecurityProfile_Unconfined {
406431 return nil , errors .New ("apparmor is not supported" )
@@ -409,62 +434,40 @@ func generateApparmorSpecOpts(sp *runtime.SecurityProfile, apparmorProf string,
409434 return nil , nil
410435 }
411436
412- if sp != nil {
413- if sp .ProfileType != runtime .SecurityProfile_Localhost && sp .LocalhostRef != "" {
414- return nil , errors .New ("apparmor config invalid LocalhostRef must only be set if ProfileType is Localhost" )
415- }
437+ if sp == nil {
438+ // Based on kubernetes#51746, default apparmor profile should be applied
439+ // for when apparmor is not specified.
440+ sp , _ = generateSecurityProfile ("" )
441+ }
416442
417- switch sp .ProfileType {
418- case runtime .SecurityProfile_Unconfined :
419- // Do not set apparmor profile.
420- return nil , nil
421- case runtime .SecurityProfile_RuntimeDefault :
422- if privileged {
423- // Do not set apparmor profile when container is privileged
424- return nil , nil
425- }
426- return apparmor .WithDefaultProfile (appArmorDefaultProfileName ), nil
427- case runtime .SecurityProfile_Localhost :
428- // trimming the localhost/ prefix just in case even through it should not
429- // be necessary with the new SecurityProfile struct
430- appArmorProfile := strings .TrimPrefix (sp .LocalhostRef , profileNamePrefix )
431- if profileExists , err := appArmorProfileExists (appArmorProfile ); ! profileExists {
432- if err != nil {
433- return nil , errors .Wrap (err , "failed to generate apparmor spec opts" )
434- }
435- return nil , errors .Errorf ("apparmor profile not found %s" , appArmorProfile )
436- }
437- return apparmor .WithProfile (appArmorProfile ), nil
438- default :
439- return nil , errors .New ("apparmor unknown ProfileType" )
440- }
443+ if sp .ProfileType != runtime .SecurityProfile_Localhost && sp .LocalhostRef != "" {
444+ return nil , errors .New ("apparmor config invalid LocalhostRef must only be set if ProfileType is Localhost" )
441445 }
442446
443- switch apparmorProf {
444- // Based on kubernetes#51746, default apparmor profile should be applied
445- // for when apparmor is not specified.
446- case runtimeDefault , "" :
447+ switch sp .ProfileType {
448+ case runtime .SecurityProfile_Unconfined :
449+ // Do not set apparmor profile.
450+ return nil , nil
451+ case runtime .SecurityProfile_RuntimeDefault :
447452 if privileged {
448453 // Do not set apparmor profile when container is privileged
449454 return nil , nil
450455 }
451456 // TODO (mikebrow): delete created apparmor default profile
452457 return apparmor .WithDefaultProfile (appArmorDefaultProfileName ), nil
453- case unconfinedProfile :
454- return nil , nil
455- default :
456- // Require and Trim default profile name prefix
457- if ! strings .HasPrefix (apparmorProf , profileNamePrefix ) {
458- return nil , errors .Errorf ("invalid apparmor profile %q" , apparmorProf )
459- }
460- appArmorProfile := strings .TrimPrefix (apparmorProf , profileNamePrefix )
458+ case runtime .SecurityProfile_Localhost :
459+ // trimming the localhost/ prefix just in case even through it should not
460+ // be necessary with the new SecurityProfile struct
461+ appArmorProfile := strings .TrimPrefix (sp .LocalhostRef , profileNamePrefix )
461462 if profileExists , err := appArmorProfileExists (appArmorProfile ); ! profileExists {
462463 if err != nil {
463464 return nil , errors .Wrap (err , "failed to generate apparmor spec opts" )
464465 }
465466 return nil , errors .Errorf ("apparmor profile not found %s" , appArmorProfile )
466467 }
467468 return apparmor .WithProfile (appArmorProfile ), nil
469+ default :
470+ return nil , errors .New ("apparmor unknown ProfileType" )
468471 }
469472}
470473
0 commit comments