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
f94df597
Unverified
Commit
f94df597
authored
Aug 16, 2016
by
Tim St. Clair
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remove apparmor dependency on pkg/kubelet/lifecycle
parent
41367711
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
57 additions
and
55 deletions
+57
-55
kubelet.go
pkg/kubelet/kubelet.go
+1
-2
handlers.go
pkg/kubelet/lifecycle/handlers.go
+23
-0
validate.go
pkg/security/apparmor/validate.go
+21
-26
validate_test.go
pkg/security/apparmor/validate_test.go
+12
-27
No files found.
pkg/kubelet/kubelet.go
View file @
f94df597
...
@@ -73,7 +73,6 @@ import (
...
@@ -73,7 +73,6 @@ import (
"k8s.io/kubernetes/pkg/kubelet/util/queue"
"k8s.io/kubernetes/pkg/kubelet/util/queue"
"k8s.io/kubernetes/pkg/kubelet/volumemanager"
"k8s.io/kubernetes/pkg/kubelet/volumemanager"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/security/apparmor"
"k8s.io/kubernetes/pkg/securitycontext"
"k8s.io/kubernetes/pkg/securitycontext"
"k8s.io/kubernetes/pkg/types"
"k8s.io/kubernetes/pkg/types"
"k8s.io/kubernetes/pkg/util/bandwidth"
"k8s.io/kubernetes/pkg/util/bandwidth"
...
@@ -601,7 +600,7 @@ func NewMainKubelet(
...
@@ -601,7 +600,7 @@ func NewMainKubelet(
klet
.
AddPodSyncLoopHandler
(
activeDeadlineHandler
)
klet
.
AddPodSyncLoopHandler
(
activeDeadlineHandler
)
klet
.
AddPodSyncHandler
(
activeDeadlineHandler
)
klet
.
AddPodSyncHandler
(
activeDeadlineHandler
)
klet
.
AddPodAdmitHandler
(
apparmor
.
NewValidato
r
(
containerRuntime
))
klet
.
AddPodAdmitHandler
(
lifecycle
.
NewAppArmorAdmitHandle
r
(
containerRuntime
))
// apply functional Option's
// apply functional Option's
for
_
,
opt
:=
range
kubeOptions
{
for
_
,
opt
:=
range
kubeOptions
{
...
...
pkg/kubelet/lifecycle/handlers.go
View file @
f94df597
...
@@ -30,6 +30,7 @@ import (
...
@@ -30,6 +30,7 @@ import (
kubetypes
"k8s.io/kubernetes/pkg/kubelet/types"
kubetypes
"k8s.io/kubernetes/pkg/kubelet/types"
"k8s.io/kubernetes/pkg/kubelet/util/format"
"k8s.io/kubernetes/pkg/kubelet/util/format"
"k8s.io/kubernetes/pkg/kubelet/util/ioutils"
"k8s.io/kubernetes/pkg/kubelet/util/ioutils"
"k8s.io/kubernetes/pkg/security/apparmor"
"k8s.io/kubernetes/pkg/types"
"k8s.io/kubernetes/pkg/types"
"k8s.io/kubernetes/pkg/util/intstr"
"k8s.io/kubernetes/pkg/util/intstr"
)
)
...
@@ -142,3 +143,25 @@ func getHttpRespBody(resp *http.Response) string {
...
@@ -142,3 +143,25 @@ func getHttpRespBody(resp *http.Response) string {
}
}
return
""
return
""
}
}
func
NewAppArmorAdmitHandler
(
runtime
string
)
PodAdmitHandler
{
return
&
appArmorAdmitHandler
{
Validator
:
apparmor
.
NewValidator
(
runtime
),
}
}
type
appArmorAdmitHandler
struct
{
apparmor
.
Validator
}
func
(
a
*
appArmorAdmitHandler
)
Admit
(
attrs
*
PodAdmitAttributes
)
PodAdmitResult
{
err
:=
a
.
Validate
(
attrs
.
Pod
)
if
err
==
nil
{
return
PodAdmitResult
{
Admit
:
true
}
}
return
PodAdmitResult
{
Admit
:
false
,
Reason
:
"AppArmor"
,
Message
:
fmt
.
Sprintf
(
"Cannot enforce AppArmor: %v"
,
err
),
}
}
pkg/security/apparmor/validate.go
View file @
f94df597
...
@@ -26,7 +26,6 @@ import (
...
@@ -26,7 +26,6 @@ import (
"strings"
"strings"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/kubelet/lifecycle"
"k8s.io/kubernetes/pkg/util"
"k8s.io/kubernetes/pkg/util"
)
)
...
@@ -34,11 +33,12 @@ import (
...
@@ -34,11 +33,12 @@ import (
// Set to true if the wrong build tags are set (see validate_disabled.go).
// Set to true if the wrong build tags are set (see validate_disabled.go).
var
isDisabledBuild
bool
var
isDisabledBuild
bool
const
(
// Interface for validating that a pod with with an AppArmor profile can be run by a Node.
rejectReason
=
"AppArmor"
type
Validator
interface
{
)
Validate
(
pod
*
api
.
Pod
)
error
}
func
NewValidator
(
runtime
string
)
lifecycle
.
PodAdmitHandle
r
{
func
NewValidator
(
runtime
string
)
Validato
r
{
if
err
:=
validateHost
(
runtime
);
err
!=
nil
{
if
err
:=
validateHost
(
runtime
);
err
!=
nil
{
return
&
validator
{
validateHostErr
:
err
}
return
&
validator
{
validateHostErr
:
err
}
}
}
...
@@ -58,21 +58,7 @@ type validator struct {
...
@@ -58,21 +58,7 @@ type validator struct {
appArmorFS
string
appArmorFS
string
}
}
// TODO(timstclair): Refactor the PodAdmitInterface to return a (Admit, Reason Message) rather than
func
(
v
*
validator
)
Validate
(
pod
*
api
.
Pod
)
error
{
// the PodAdmitResult struct so that the interface can be implemented without importing lifecycle.
func
(
v
*
validator
)
Admit
(
attrs
*
lifecycle
.
PodAdmitAttributes
)
lifecycle
.
PodAdmitResult
{
err
:=
v
.
validate
(
attrs
.
Pod
)
if
err
==
nil
{
return
lifecycle
.
PodAdmitResult
{
Admit
:
true
}
}
return
lifecycle
.
PodAdmitResult
{
Admit
:
false
,
Reason
:
rejectReason
,
Message
:
fmt
.
Sprintf
(
"Cannot enforce AppArmor: %v"
,
err
),
}
}
func
(
v
*
validator
)
validate
(
pod
*
api
.
Pod
)
error
{
if
!
isRequired
(
pod
)
{
if
!
isRequired
(
pod
)
{
return
nil
return
nil
}
}
...
@@ -122,18 +108,27 @@ func validateHost(runtime string) error {
...
@@ -122,18 +108,27 @@ func validateHost(runtime string) error {
// Verify that the profile is valid and loaded.
// Verify that the profile is valid and loaded.
func
validateProfile
(
profile
string
,
loadedProfiles
map
[
string
]
bool
)
error
{
func
validateProfile
(
profile
string
,
loadedProfiles
map
[
string
]
bool
)
error
{
if
err
:=
ValidateProfileFormat
(
profile
);
err
!=
nil
{
return
err
}
if
strings
.
HasPrefix
(
profile
,
ProfileNamePrefix
)
{
profileName
:=
strings
.
TrimPrefix
(
profile
,
ProfileNamePrefix
)
if
!
loadedProfiles
[
profileName
]
{
return
fmt
.
Errorf
(
"profile %q is not loaded"
,
profileName
)
}
}
return
nil
}
func
ValidateProfileFormat
(
profile
string
)
error
{
if
profile
==
""
||
profile
==
ProfileRuntimeDefault
{
if
profile
==
""
||
profile
==
ProfileRuntimeDefault
{
return
nil
return
nil
}
}
if
!
strings
.
HasPrefix
(
profile
,
ProfileNamePrefix
)
{
if
!
strings
.
HasPrefix
(
profile
,
ProfileNamePrefix
)
{
return
fmt
.
Errorf
(
"invalid AppArmor profile name: %q"
,
profile
)
return
fmt
.
Errorf
(
"invalid AppArmor profile name: %q"
,
profile
)
}
}
profileName
:=
strings
.
TrimPrefix
(
profile
,
ProfileNamePrefix
)
if
!
loadedProfiles
[
profileName
]
{
return
fmt
.
Errorf
(
"profile %q is not loaded"
,
profileName
)
}
return
nil
return
nil
}
}
...
...
pkg/security/apparmor/validate_test.go
View file @
f94df597
...
@@ -21,7 +21,6 @@ import (
...
@@ -21,7 +21,6 @@ import (
"testing"
"testing"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/kubelet/lifecycle"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
)
)
...
@@ -37,7 +36,7 @@ func TestGetAppArmorFS(t *testing.T) {
...
@@ -37,7 +36,7 @@ func TestGetAppArmorFS(t *testing.T) {
assert
.
Equal
(
t
,
expectedPath
,
actualPath
)
assert
.
Equal
(
t
,
expectedPath
,
actualPath
)
}
}
func
Test
Admit
Host
(
t
*
testing
.
T
)
{
func
Test
Validate
Host
(
t
*
testing
.
T
)
{
// This test only passes on systems running AppArmor with the default configuration.
// This test only passes on systems running AppArmor with the default configuration.
// The test should be manually run if modifying the getAppArmorFS function.
// The test should be manually run if modifying the getAppArmorFS function.
t
.
Skip
()
t
.
Skip
()
...
@@ -46,7 +45,7 @@ func TestAdmitHost(t *testing.T) {
...
@@ -46,7 +45,7 @@ func TestAdmitHost(t *testing.T) {
assert
.
Error
(
t
,
validateHost
(
"rkt"
))
assert
.
Error
(
t
,
validateHost
(
"rkt"
))
}
}
func
Test
Admit
Profile
(
t
*
testing
.
T
)
{
func
Test
Validate
Profile
(
t
*
testing
.
T
)
{
loadedProfiles
:=
map
[
string
]
bool
{
loadedProfiles
:=
map
[
string
]
bool
{
"docker-default"
:
true
,
"docker-default"
:
true
,
"foo-bar"
:
true
,
"foo-bar"
:
true
,
...
@@ -79,7 +78,7 @@ func TestAdmitProfile(t *testing.T) {
...
@@ -79,7 +78,7 @@ func TestAdmitProfile(t *testing.T) {
}
}
}
}
func
Test
Admit
BadHost
(
t
*
testing
.
T
)
{
func
Test
Validate
BadHost
(
t
*
testing
.
T
)
{
hostErr
:=
errors
.
New
(
"expected host error"
)
hostErr
:=
errors
.
New
(
"expected host error"
)
v
:=
&
validator
{
v
:=
&
validator
{
validateHostErr
:
hostErr
,
validateHostErr
:
hostErr
,
...
@@ -95,20 +94,16 @@ func TestAdmitBadHost(t *testing.T) {
...
@@ -95,20 +94,16 @@ func TestAdmitBadHost(t *testing.T) {
}
}
for
_
,
test
:=
range
tests
{
for
_
,
test
:=
range
tests
{
result
:=
v
.
Admit
(
&
lifecycle
.
PodAdmitAttributes
{
err
:=
v
.
Validate
(
getPodWithProfile
(
test
.
profile
))
Pod
:
getPodWithProfile
(
test
.
profile
),
})
if
test
.
expectValid
{
if
test
.
expectValid
{
assert
.
True
(
t
,
result
.
Admit
,
"Pod with profile %q should be admitte
d"
,
test
.
profile
)
assert
.
NoError
(
t
,
err
,
"Pod with profile %q should be vali
d"
,
test
.
profile
)
}
else
{
}
else
{
assert
.
False
(
t
,
result
.
Admit
,
"Pod with profile %q should be rejected"
,
test
.
profile
)
assert
.
Equal
(
t
,
hostErr
,
err
,
"Pod with profile %q should trigger a host validation error"
,
test
.
profile
)
assert
.
Equal
(
t
,
rejectReason
,
result
.
Reason
,
"Pod with profile %q"
,
test
.
profile
)
assert
.
Contains
(
t
,
result
.
Message
,
hostErr
.
Error
(),
"Pod with profile %q"
,
test
.
profile
)
}
}
}
}
}
}
func
Test
Admit
ValidHost
(
t
*
testing
.
T
)
{
func
Test
Validate
ValidHost
(
t
*
testing
.
T
)
{
v
:=
&
validator
{
v
:=
&
validator
{
appArmorFS
:
"./testdata/"
,
appArmorFS
:
"./testdata/"
,
}
}
...
@@ -128,15 +123,11 @@ func TestAdmitValidHost(t *testing.T) {
...
@@ -128,15 +123,11 @@ func TestAdmitValidHost(t *testing.T) {
}
}
for
_
,
test
:=
range
tests
{
for
_
,
test
:=
range
tests
{
result
:=
v
.
Admit
(
&
lifecycle
.
PodAdmitAttributes
{
err
:=
v
.
Validate
(
getPodWithProfile
(
test
.
profile
))
Pod
:
getPodWithProfile
(
test
.
profile
),
})
if
test
.
expectValid
{
if
test
.
expectValid
{
assert
.
True
(
t
,
result
.
Admit
,
"Pod with profile %q should be admitte
d"
,
test
.
profile
)
assert
.
NoError
(
t
,
err
,
"Pod with profile %q should be vali
d"
,
test
.
profile
)
}
else
{
}
else
{
assert
.
False
(
t
,
result
.
Admit
,
"Pod with profile %q should be rejected"
,
test
.
profile
)
assert
.
Error
(
t
,
err
,
"Pod with profile %q should trigger a validation error"
,
test
.
profile
)
assert
.
Equal
(
t
,
rejectReason
,
result
.
Reason
,
"Pod with profile %q"
,
test
.
profile
)
assert
.
NotEmpty
(
t
,
result
.
Message
,
"Pod with profile %q"
,
test
.
profile
)
}
}
}
}
...
@@ -160,16 +151,10 @@ func TestAdmitValidHost(t *testing.T) {
...
@@ -160,16 +151,10 @@ func TestAdmitValidHost(t *testing.T) {
},
},
},
},
}
}
assert
.
True
(
t
,
v
.
Admit
(
&
lifecycle
.
PodAdmitAttributes
{
Pod
:
pod
})
.
Admit
,
assert
.
NoError
(
t
,
v
.
Validate
(
pod
),
"Multi-container pod should validate"
)
"Multi-container pod should be admitted"
)
for
k
,
val
:=
range
pod
.
Annotations
{
for
k
,
val
:=
range
pod
.
Annotations
{
pod
.
Annotations
[
k
]
=
val
+
"-bad"
pod
.
Annotations
[
k
]
=
val
+
"-bad"
assert
.
Error
(
t
,
v
.
Validate
(
pod
),
"Multi-container pod with invalid profile %s:%s"
,
k
,
pod
.
Annotations
[
k
])
result
:=
v
.
Admit
(
&
lifecycle
.
PodAdmitAttributes
{
Pod
:
pod
})
assert
.
False
(
t
,
result
.
Admit
,
"Multi-container pod with invalid profile should be rejected"
)
assert
.
Equal
(
t
,
rejectReason
,
result
.
Reason
,
"Multi-container pod with invalid profile"
)
assert
.
NotEmpty
(
t
,
result
.
Message
,
"Multi-container pod with invalid profile"
)
pod
.
Annotations
[
k
]
=
val
// Restore.
pod
.
Annotations
[
k
]
=
val
// Restore.
}
}
}
}
...
...
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