@@ -9,12 +9,14 @@ import (
9
9
securityapi "github.com/openshift/origin/pkg/security/apis/security"
10
10
)
11
11
12
- // mustRunAs implements the RunAsUserSecurityContextConstraintsStrategy interface
12
+ // mustRunAsRange implements the RunAsUserSecurityContextConstraintsStrategy interface
13
13
type mustRunAsRange struct {
14
14
opts * securityapi.RunAsUserStrategyOptions
15
15
}
16
16
17
- // NewMustRunAs provides a strategy that requires the container to run as a specific UID in a range.
17
+ var _ RunAsUserSecurityContextConstraintsStrategy = & mustRunAsRange {}
18
+
19
+ // NewMustRunAsRange provides a strategy that requires the container to run as a specific UID in a range.
18
20
func NewMustRunAsRange (options * securityapi.RunAsUserStrategyOptions ) (RunAsUserSecurityContextConstraintsStrategy , error ) {
19
21
if options == nil {
20
22
return nil , fmt .Errorf ("MustRunAsRange requires run as user options" )
@@ -36,28 +38,18 @@ func (s *mustRunAsRange) Generate(pod *api.Pod, container *api.Container) (*int6
36
38
}
37
39
38
40
// Validate ensures that the specified values fall within the range of the strategy.
39
- func (s * mustRunAsRange ) Validate (pod * api.Pod , container * api.Container ) field.ErrorList {
41
+ func (s * mustRunAsRange ) Validate (fldPath * field. Path , _ * api.Pod , _ * api.Container , runAsNonRoot * bool , runAsUser * int64 ) field.ErrorList {
40
42
allErrs := field.ErrorList {}
41
43
42
- securityContextPath := field .NewPath ("securityContext" )
43
- if container .SecurityContext == nil {
44
- detail := fmt .Sprintf ("unable to validate nil security context for container %s" , container .Name )
45
- allErrs = append (allErrs , field .Invalid (securityContextPath , container .SecurityContext , detail ))
46
- return allErrs
47
- }
48
- if container .SecurityContext .RunAsUser == nil {
49
- detail := fmt .Sprintf ("unable to validate nil RunAsUser for container %s" , container .Name )
50
- allErrs = append (allErrs , field .Invalid (securityContextPath .Child ("runAsUser" ), container .SecurityContext .RunAsUser , detail ))
44
+ if runAsUser == nil {
45
+ allErrs = append (allErrs , field .Required (fldPath .Child ("runAsUser" ), "" ))
51
46
return allErrs
52
47
}
53
48
54
- if * container .SecurityContext .RunAsUser < * s .opts .UIDRangeMin || * container .SecurityContext .RunAsUser > * s .opts .UIDRangeMax {
55
- detail := fmt .Sprintf ("UID on container %s does not match required range. Found %d, required min: %d max: %d" ,
56
- container .Name ,
57
- * container .SecurityContext .RunAsUser ,
58
- * s .opts .UIDRangeMin ,
59
- * s .opts .UIDRangeMax )
60
- allErrs = append (allErrs , field .Invalid (securityContextPath .Child ("runAsUser" ), * container .SecurityContext .RunAsUser , detail ))
49
+ if * runAsUser < * s .opts .UIDRangeMin || * runAsUser > * s .opts .UIDRangeMax {
50
+ detail := fmt .Sprintf ("must be in the ranges: [%v, %v]" , * s .opts .UIDRangeMin , * s .opts .UIDRangeMax )
51
+ allErrs = append (allErrs , field .Invalid (fldPath .Child ("runAsUser" ), * runAsUser , detail ))
52
+ return allErrs
61
53
}
62
54
63
55
return allErrs
0 commit comments