Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
fef3b031
Unverified
Commit
fef3b031
authored
Oct 06, 2017
by
Jordan Liggitt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PodSecurityPolicy: pass effective runAsNonRoot and runAsUser to user validation interface
parent
e34a00d1
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
25 additions
and
47 deletions
+25
-47
provider.go
pkg/security/podsecuritypolicy/provider.go
+1
-1
provider_test.go
pkg/security/podsecuritypolicy/provider_test.go
+1
-1
mustrunas.go
pkg/security/podsecuritypolicy/user/mustrunas.go
+6
-16
mustrunas_test.go
pkg/security/podsecuritypolicy/user/mustrunas_test.go
+3
-9
nonroot.go
pkg/security/podsecuritypolicy/user/nonroot.go
+7
-13
nonroot_test.go
pkg/security/podsecuritypolicy/user/nonroot_test.go
+4
-4
runasany.go
pkg/security/podsecuritypolicy/user/runasany.go
+1
-1
runasany_test.go
pkg/security/podsecuritypolicy/user/runasany_test.go
+1
-1
types.go
pkg/security/podsecuritypolicy/user/types.go
+1
-1
No files found.
pkg/security/podsecuritypolicy/provider.go
View file @
fef3b031
...
@@ -267,7 +267,7 @@ func (s *simpleProvider) ValidateContainerSecurityContext(pod *api.Pod, containe
...
@@ -267,7 +267,7 @@ func (s *simpleProvider) ValidateContainerSecurityContext(pod *api.Pod, containe
}
}
sc
:=
container
.
SecurityContext
sc
:=
container
.
SecurityContext
allErrs
=
append
(
allErrs
,
s
.
strategies
.
RunAsUserStrategy
.
Validate
(
pod
,
contain
er
)
...
)
allErrs
=
append
(
allErrs
,
s
.
strategies
.
RunAsUserStrategy
.
Validate
(
fldPath
.
Child
(
"securityContext"
),
pod
,
container
,
sc
.
RunAsNonRoot
,
sc
.
RunAsUs
er
)
...
)
allErrs
=
append
(
allErrs
,
s
.
strategies
.
SELinuxStrategy
.
Validate
(
fldPath
.
Child
(
"seLinuxOptions"
),
pod
,
container
,
sc
.
SELinuxOptions
)
...
)
allErrs
=
append
(
allErrs
,
s
.
strategies
.
SELinuxStrategy
.
Validate
(
fldPath
.
Child
(
"seLinuxOptions"
),
pod
,
container
,
sc
.
SELinuxOptions
)
...
)
allErrs
=
append
(
allErrs
,
s
.
strategies
.
AppArmorStrategy
.
Validate
(
pod
,
container
)
...
)
allErrs
=
append
(
allErrs
,
s
.
strategies
.
AppArmorStrategy
.
Validate
(
pod
,
container
)
...
)
allErrs
=
append
(
allErrs
,
s
.
strategies
.
SeccompStrategy
.
ValidateContainer
(
pod
,
container
)
...
)
allErrs
=
append
(
allErrs
,
s
.
strategies
.
SeccompStrategy
.
ValidateContainer
(
pod
,
container
)
...
)
...
...
pkg/security/podsecuritypolicy/provider_test.go
View file @
fef3b031
...
@@ -455,7 +455,7 @@ func TestValidateContainerSecurityContextFailures(t *testing.T) {
...
@@ -455,7 +455,7 @@ func TestValidateContainerSecurityContextFailures(t *testing.T) {
"failUserPSP"
:
{
"failUserPSP"
:
{
pod
:
failUserPod
,
pod
:
failUserPod
,
psp
:
failUserPSP
,
psp
:
failUserPSP
,
expectedError
:
"
does not match required rang
e"
,
expectedError
:
"
runAsUser: Invalid valu
e"
,
},
},
"failSELinuxPSP"
:
{
"failSELinuxPSP"
:
{
pod
:
failSELinuxPod
,
pod
:
failSELinuxPod
,
...
...
pkg/security/podsecuritypolicy/user/mustrunas.go
View file @
fef3b031
...
@@ -49,27 +49,17 @@ func (s *mustRunAs) Generate(pod *api.Pod, container *api.Container) (*int64, er
...
@@ -49,27 +49,17 @@ func (s *mustRunAs) Generate(pod *api.Pod, container *api.Container) (*int64, er
}
}
// Validate ensures that the specified values fall within the range of the strategy.
// Validate ensures that the specified values fall within the range of the strategy.
func
(
s
*
mustRunAs
)
Validate
(
pod
*
api
.
Pod
,
container
*
api
.
Container
)
field
.
ErrorList
{
func
(
s
*
mustRunAs
)
Validate
(
fldPath
*
field
.
Path
,
_
*
api
.
Pod
,
_
*
api
.
Container
,
runAsNonRoot
*
bool
,
runAsUser
*
int64
)
field
.
ErrorList
{
allErrs
:=
field
.
ErrorList
{}
allErrs
:=
field
.
ErrorList
{}
securityContextPath
:=
field
.
NewPath
(
"securityContext"
)
if
runAsUser
==
nil
{
if
container
.
SecurityContext
==
nil
{
allErrs
=
append
(
allErrs
,
field
.
Required
(
fldPath
.
Child
(
"runAsUser"
),
""
))
detail
:=
fmt
.
Sprintf
(
"unable to validate nil security context for container %s"
,
container
.
Name
)
allErrs
=
append
(
allErrs
,
field
.
Invalid
(
securityContextPath
,
container
.
SecurityContext
,
detail
))
return
allErrs
}
if
container
.
SecurityContext
.
RunAsUser
==
nil
{
detail
:=
fmt
.
Sprintf
(
"unable to validate nil RunAsUser for container %s"
,
container
.
Name
)
allErrs
=
append
(
allErrs
,
field
.
Invalid
(
securityContextPath
.
Child
(
"runAsUser"
),
container
.
SecurityContext
.
RunAsUser
,
detail
))
return
allErrs
return
allErrs
}
}
if
!
s
.
isValidUID
(
*
container
.
SecurityContext
.
RunAsUser
)
{
if
!
s
.
isValidUID
(
*
runAsUser
)
{
detail
:=
fmt
.
Sprintf
(
"UID on container %s does not match required range. Found %d, allowed: %v"
,
detail
:=
fmt
.
Sprintf
(
"must be in the ranges: %v"
,
s
.
opts
.
Ranges
)
container
.
Name
,
allErrs
=
append
(
allErrs
,
field
.
Invalid
(
fldPath
.
Child
(
"runAsUser"
),
*
runAsUser
,
detail
))
*
container
.
SecurityContext
.
RunAsUser
,
s
.
opts
.
Ranges
)
allErrs
=
append
(
allErrs
,
field
.
Invalid
(
securityContextPath
.
Child
(
"runAsUser"
),
*
container
.
SecurityContext
.
RunAsUser
,
detail
))
}
}
return
allErrs
return
allErrs
}
}
...
...
pkg/security/podsecuritypolicy/user/mustrunas_test.go
View file @
fef3b031
...
@@ -98,19 +98,13 @@ func TestValidate(t *testing.T) {
...
@@ -98,19 +98,13 @@ func TestValidate(t *testing.T) {
},
},
},
},
},
},
"nil security context"
:
{
container
:
&
api
.
Container
{
SecurityContext
:
nil
,
},
expectedMsg
:
"unable to validate nil security context for container"
,
},
"nil run as user"
:
{
"nil run as user"
:
{
container
:
&
api
.
Container
{
container
:
&
api
.
Container
{
SecurityContext
:
&
api
.
SecurityContext
{
SecurityContext
:
&
api
.
SecurityContext
{
RunAsUser
:
nil
,
RunAsUser
:
nil
,
},
},
},
},
expectedMsg
:
"
unable to validate nil RunAsUser for container
"
,
expectedMsg
:
"
runAsUser: Required
"
,
},
},
"invalid id"
:
{
"invalid id"
:
{
container
:
&
api
.
Container
{
container
:
&
api
.
Container
{
...
@@ -118,7 +112,7 @@ func TestValidate(t *testing.T) {
...
@@ -118,7 +112,7 @@ func TestValidate(t *testing.T) {
RunAsUser
:
&
invalidID
,
RunAsUser
:
&
invalidID
,
},
},
},
},
expectedMsg
:
"
does not match required range
"
,
expectedMsg
:
"
runAsUser: Invalid
"
,
},
},
}
}
...
@@ -128,7 +122,7 @@ func TestValidate(t *testing.T) {
...
@@ -128,7 +122,7 @@ func TestValidate(t *testing.T) {
t
.
Errorf
(
"unexpected error initializing NewMustRunAs for testcase %s: %#v"
,
name
,
err
)
t
.
Errorf
(
"unexpected error initializing NewMustRunAs for testcase %s: %#v"
,
name
,
err
)
continue
continue
}
}
errs
:=
mustRunAs
.
Validate
(
nil
,
tc
.
contain
er
)
errs
:=
mustRunAs
.
Validate
(
nil
,
nil
,
nil
,
tc
.
container
.
SecurityContext
.
RunAsNonRoot
,
tc
.
container
.
SecurityContext
.
RunAsUs
er
)
//should've passed but didn't
//should've passed but didn't
if
len
(
tc
.
expectedMsg
)
==
0
&&
len
(
errs
)
>
0
{
if
len
(
tc
.
expectedMsg
)
==
0
&&
len
(
errs
)
>
0
{
t
.
Errorf
(
"%s expected no errors but received %v"
,
name
,
errs
)
t
.
Errorf
(
"%s expected no errors but received %v"
,
name
,
errs
)
...
...
pkg/security/podsecuritypolicy/user/nonroot.go
View file @
fef3b031
...
@@ -17,8 +17,6 @@ limitations under the License.
...
@@ -17,8 +17,6 @@ limitations under the License.
package
user
package
user
import
(
import
(
"fmt"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/apis/extensions"
"k8s.io/kubernetes/pkg/apis/extensions"
...
@@ -43,22 +41,18 @@ func (s *nonRoot) Generate(pod *api.Pod, container *api.Container) (*int64, erro
...
@@ -43,22 +41,18 @@ func (s *nonRoot) Generate(pod *api.Pod, container *api.Container) (*int64, erro
// or if the UID is set it is not root. Validation will fail if RunAsNonRoot is set to false.
// or if the UID is set it is not root. Validation will fail if RunAsNonRoot is set to false.
// In order to work properly this assumes that the kubelet performs a final check on runAsUser
// In order to work properly this assumes that the kubelet performs a final check on runAsUser
// or the image UID when runAsUser is nil.
// or the image UID when runAsUser is nil.
func
(
s
*
nonRoot
)
Validate
(
pod
*
api
.
Pod
,
container
*
api
.
Container
)
field
.
ErrorList
{
func
(
s
*
nonRoot
)
Validate
(
fldPath
*
field
.
Path
,
_
*
api
.
Pod
,
_
*
api
.
Container
,
runAsNonRoot
*
bool
,
runAsUser
*
int64
)
field
.
ErrorList
{
allErrs
:=
field
.
ErrorList
{}
allErrs
:=
field
.
ErrorList
{}
securityContextPath
:=
field
.
NewPath
(
"securityContext"
)
if
runAsNonRoot
==
nil
&&
runAsUser
==
nil
{
if
container
.
SecurityContext
==
nil
{
allErrs
=
append
(
allErrs
,
field
.
Required
(
fldPath
.
Child
(
"runAsNonRoot"
),
"must be true"
))
detail
:=
fmt
.
Sprintf
(
"unable to validate nil security context for container %s"
,
container
.
Name
)
allErrs
=
append
(
allErrs
,
field
.
Invalid
(
securityContextPath
,
container
.
SecurityContext
,
detail
))
return
allErrs
return
allErrs
}
}
if
container
.
SecurityContext
.
RunAsNonRoot
!=
nil
&&
*
container
.
SecurityContext
.
RunAsNonRoot
==
false
{
if
runAsNonRoot
!=
nil
&&
*
runAsNonRoot
==
false
{
detail
:=
fmt
.
Sprintf
(
"RunAsNonRoot must be true for container %s"
,
container
.
Name
)
allErrs
=
append
(
allErrs
,
field
.
Invalid
(
fldPath
.
Child
(
"runAsNonRoot"
),
*
runAsNonRoot
,
"must be true"
))
allErrs
=
append
(
allErrs
,
field
.
Invalid
(
securityContextPath
.
Child
(
"runAsNonRoot"
),
*
container
.
SecurityContext
.
RunAsNonRoot
,
detail
))
return
allErrs
return
allErrs
}
}
if
container
.
SecurityContext
.
RunAsUser
!=
nil
&&
*
container
.
SecurityContext
.
RunAsUser
==
0
{
if
runAsUser
!=
nil
&&
*
runAsUser
==
0
{
detail
:=
fmt
.
Sprintf
(
"running with the root UID is forbidden by the pod security policy for container %s"
,
container
.
Name
)
allErrs
=
append
(
allErrs
,
field
.
Invalid
(
fldPath
.
Child
(
"runAsUser"
),
*
runAsUser
,
"running with the root UID is forbidden"
))
allErrs
=
append
(
allErrs
,
field
.
Invalid
(
securityContextPath
.
Child
(
"runAsUser"
),
*
container
.
SecurityContext
.
RunAsUser
,
detail
))
return
allErrs
return
allErrs
}
}
return
allErrs
return
allErrs
...
...
pkg/security/podsecuritypolicy/user/nonroot_test.go
View file @
fef3b031
...
@@ -102,17 +102,17 @@ func TestNonRootValidate(t *testing.T) {
...
@@ -102,17 +102,17 @@ func TestNonRootValidate(t *testing.T) {
{
{
container
:
&
api
.
Container
{
container
:
&
api
.
Container
{
SecurityContext
:
&
api
.
SecurityContext
{
SecurityContext
:
&
api
.
SecurityContext
{
RunAsNonRoot
:
&
unfalse
,
RunAsNonRoot
:
nil
,
RunAsUser
:
&
badUID
,
RunAsUser
:
nil
,
},
},
},
},
expectedErr
:
true
,
expectedErr
:
true
,
msg
:
"in test case %d, expected errors from
root uid
but got %v"
,
msg
:
"in test case %d, expected errors from
nil runAsNonRoot and nil runAsUser
but got %v"
,
},
},
}
}
for
i
,
tc
:=
range
tests
{
for
i
,
tc
:=
range
tests
{
errs
:=
s
.
Validate
(
nil
,
tc
.
contain
er
)
errs
:=
s
.
Validate
(
nil
,
nil
,
nil
,
tc
.
container
.
SecurityContext
.
RunAsNonRoot
,
tc
.
container
.
SecurityContext
.
RunAsUs
er
)
if
(
len
(
errs
)
==
0
)
==
tc
.
expectedErr
{
if
(
len
(
errs
)
==
0
)
==
tc
.
expectedErr
{
t
.
Errorf
(
tc
.
msg
,
i
,
errs
)
t
.
Errorf
(
tc
.
msg
,
i
,
errs
)
}
}
...
...
pkg/security/podsecuritypolicy/user/runasany.go
View file @
fef3b031
...
@@ -38,6 +38,6 @@ func (s *runAsAny) Generate(pod *api.Pod, container *api.Container) (*int64, err
...
@@ -38,6 +38,6 @@ func (s *runAsAny) Generate(pod *api.Pod, container *api.Container) (*int64, err
}
}
// Validate ensures that the specified values fall within the range of the strategy.
// Validate ensures that the specified values fall within the range of the strategy.
func
(
s
*
runAsAny
)
Validate
(
pod
*
api
.
Pod
,
container
*
api
.
Container
)
field
.
ErrorList
{
func
(
s
*
runAsAny
)
Validate
(
fldPath
*
field
.
Path
,
_
*
api
.
Pod
,
_
*
api
.
Container
,
runAsNonRoot
*
bool
,
runAsUser
*
int64
)
field
.
ErrorList
{
return
field
.
ErrorList
{}
return
field
.
ErrorList
{}
}
}
pkg/security/podsecuritypolicy/user/runasany_test.go
View file @
fef3b031
...
@@ -52,7 +52,7 @@ func TestRunAsAnyValidate(t *testing.T) {
...
@@ -52,7 +52,7 @@ func TestRunAsAnyValidate(t *testing.T) {
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"unexpected error initializing NewRunAsAny %v"
,
err
)
t
.
Fatalf
(
"unexpected error initializing NewRunAsAny %v"
,
err
)
}
}
errs
:=
s
.
Validate
(
nil
,
nil
)
errs
:=
s
.
Validate
(
nil
,
nil
,
nil
,
nil
,
nil
)
if
len
(
errs
)
!=
0
{
if
len
(
errs
)
!=
0
{
t
.
Errorf
(
"unexpected errors validating with "
)
t
.
Errorf
(
"unexpected errors validating with "
)
}
}
...
...
pkg/security/podsecuritypolicy/user/types.go
View file @
fef3b031
...
@@ -26,5 +26,5 @@ type RunAsUserStrategy interface {
...
@@ -26,5 +26,5 @@ type RunAsUserStrategy interface {
// Generate creates the uid based on policy rules.
// Generate creates the uid based on policy rules.
Generate
(
pod
*
api
.
Pod
,
container
*
api
.
Container
)
(
*
int64
,
error
)
Generate
(
pod
*
api
.
Pod
,
container
*
api
.
Container
)
(
*
int64
,
error
)
// Validate ensures that the specified values fall within the range of the strategy.
// Validate ensures that the specified values fall within the range of the strategy.
Validate
(
pod
*
api
.
Pod
,
container
*
api
.
Container
)
field
.
ErrorList
Validate
(
fldPath
*
field
.
Path
,
pod
*
api
.
Pod
,
container
*
api
.
Container
,
runAsNonRoot
*
bool
,
runAsUser
*
int64
)
field
.
ErrorList
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment