2017-05-30 19:15:38 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2017-11-20 17:57:24 +00:00
|
|
|
"fmt"
|
2017-05-30 19:15:38 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-01-22 04:06:17 +00:00
|
|
|
storagev1beta1 "k8s.io/api/storage/v1beta1"
|
2017-05-30 19:15:38 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
"k8s.io/apimachinery/pkg/api/resource"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-11-20 17:57:24 +00:00
|
|
|
"k8s.io/apimachinery/pkg/types"
|
2018-01-09 21:44:55 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
2017-07-17 22:59:15 +00:00
|
|
|
"k8s.io/apiserver/pkg/authentication/request/bearertoken"
|
|
|
|
"k8s.io/apiserver/pkg/authentication/token/tokenfile"
|
|
|
|
"k8s.io/apiserver/pkg/authentication/user"
|
2017-11-20 17:57:24 +00:00
|
|
|
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
|
|
|
utilfeaturetesting "k8s.io/apiserver/pkg/util/feature/testing"
|
2018-01-17 04:24:20 +00:00
|
|
|
versionedinformers "k8s.io/client-go/informers"
|
2018-01-17 04:39:11 +00:00
|
|
|
externalclientset "k8s.io/client-go/kubernetes"
|
2017-05-30 19:15:38 +00:00
|
|
|
restclient "k8s.io/client-go/rest"
|
2017-10-16 11:41:50 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/legacyscheme"
|
2017-11-08 22:34:54 +00:00
|
|
|
api "k8s.io/kubernetes/pkg/apis/core"
|
2017-07-11 18:53:12 +00:00
|
|
|
"k8s.io/kubernetes/pkg/apis/policy"
|
2017-05-30 19:15:38 +00:00
|
|
|
"k8s.io/kubernetes/pkg/auth/nodeidentifier"
|
|
|
|
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
|
|
|
informers "k8s.io/kubernetes/pkg/client/informers/informers_generated/internalversion"
|
2017-11-20 17:57:24 +00:00
|
|
|
"k8s.io/kubernetes/pkg/features"
|
2017-05-30 19:15:38 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubeapiserver/authorizer"
|
|
|
|
"k8s.io/kubernetes/plugin/pkg/admission/noderestriction"
|
|
|
|
"k8s.io/kubernetes/test/integration/framework"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNodeAuthorizer(t *testing.T) {
|
|
|
|
// Start the server so we know the address
|
|
|
|
h := &framework.MasterHolder{Initialized: make(chan struct{})}
|
|
|
|
apiServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
<-h.Initialized
|
|
|
|
h.M.GenericAPIServer.Handler.ServeHTTP(w, req)
|
|
|
|
}))
|
|
|
|
|
2017-07-17 22:59:15 +00:00
|
|
|
const (
|
|
|
|
// Define credentials
|
|
|
|
tokenMaster = "master-token"
|
|
|
|
tokenNodeUnknown = "unknown-token"
|
|
|
|
tokenNode1 = "node1-token"
|
|
|
|
tokenNode2 = "node2-token"
|
|
|
|
)
|
|
|
|
|
|
|
|
authenticator := bearertoken.New(tokenfile.New(map[string]*user.DefaultInfo{
|
|
|
|
tokenMaster: {Name: "admin", Groups: []string{"system:masters"}},
|
|
|
|
tokenNodeUnknown: {Name: "unknown", Groups: []string{"system:nodes"}},
|
|
|
|
tokenNode1: {Name: "system:node:node1", Groups: []string{"system:nodes"}},
|
|
|
|
tokenNode2: {Name: "system:node:node2", Groups: []string{"system:nodes"}},
|
|
|
|
}))
|
|
|
|
|
2017-05-30 19:15:38 +00:00
|
|
|
// Build client config, clientset, and informers
|
2017-10-16 11:41:50 +00:00
|
|
|
clientConfig := &restclient.Config{Host: apiServer.URL, ContentConfig: restclient.ContentConfig{NegotiatedSerializer: legacyscheme.Codecs}}
|
2018-01-17 04:24:20 +00:00
|
|
|
superuserClient, superuserClientExternal := clientsetForToken(tokenMaster, clientConfig)
|
2017-05-30 19:15:38 +00:00
|
|
|
informerFactory := informers.NewSharedInformerFactory(superuserClient, time.Minute)
|
2018-01-17 04:24:20 +00:00
|
|
|
versionedInformerFactory := versionedinformers.NewSharedInformerFactory(superuserClientExternal, time.Minute)
|
2017-05-30 19:15:38 +00:00
|
|
|
|
2018-01-17 04:39:11 +00:00
|
|
|
// Enabled CSIPersistentVolume feature at startup so volumeattachments get watched
|
|
|
|
defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CSIPersistentVolume, true)()
|
|
|
|
|
2018-02-20 19:28:28 +00:00
|
|
|
// Enable DynamicKubeletConfig feature so that Node.Spec.ConfigSource can be set
|
|
|
|
defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.DynamicKubeletConfig, true)()
|
|
|
|
|
2017-05-30 19:15:38 +00:00
|
|
|
// Set up Node+RBAC authorizer
|
|
|
|
authorizerConfig := &authorizer.AuthorizationConfig{
|
2018-01-17 04:24:20 +00:00
|
|
|
AuthorizationModes: []string{"Node", "RBAC"},
|
|
|
|
InformerFactory: informerFactory,
|
|
|
|
VersionedInformerFactory: versionedInformerFactory,
|
2017-05-30 19:15:38 +00:00
|
|
|
}
|
2017-07-14 03:24:27 +00:00
|
|
|
nodeRBACAuthorizer, _, err := authorizerConfig.New()
|
2017-05-30 19:15:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up NodeRestriction admission
|
|
|
|
nodeRestrictionAdmission := noderestriction.NewPlugin(nodeidentifier.NewDefaultNodeIdentifier())
|
2018-05-09 16:23:46 +00:00
|
|
|
nodeRestrictionAdmission.SetInternalKubeInformerFactory(informerFactory)
|
2017-10-24 13:33:28 +00:00
|
|
|
if err := nodeRestrictionAdmission.ValidateInitialization(); err != nil {
|
2017-05-30 19:15:38 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the server
|
|
|
|
masterConfig := framework.NewIntegrationTestMasterConfig()
|
2018-01-31 15:17:48 +00:00
|
|
|
masterConfig.GenericConfig.Authentication.Authenticator = authenticator
|
|
|
|
masterConfig.GenericConfig.Authorization.Authorizer = nodeRBACAuthorizer
|
2017-05-30 19:15:38 +00:00
|
|
|
masterConfig.GenericConfig.AdmissionControl = nodeRestrictionAdmission
|
2018-01-17 04:39:11 +00:00
|
|
|
|
2017-05-30 19:15:38 +00:00
|
|
|
_, _, closeFn := framework.RunAMasterUsingServer(masterConfig, apiServer, h)
|
|
|
|
defer closeFn()
|
|
|
|
|
|
|
|
// Start the informers
|
|
|
|
stopCh := make(chan struct{})
|
|
|
|
defer close(stopCh)
|
|
|
|
informerFactory.Start(stopCh)
|
2018-01-17 04:24:20 +00:00
|
|
|
versionedInformerFactory.Start(stopCh)
|
2017-05-30 19:15:38 +00:00
|
|
|
|
|
|
|
// Wait for a healthy server
|
|
|
|
for {
|
|
|
|
result := superuserClient.Core().RESTClient().Get().AbsPath("/healthz").Do()
|
|
|
|
_, err := result.Raw()
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
t.Log(err)
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create objects
|
|
|
|
if _, err := superuserClient.Core().Secrets("ns").Create(&api.Secret{ObjectMeta: metav1.ObjectMeta{Name: "mysecret"}}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := superuserClient.Core().Secrets("ns").Create(&api.Secret{ObjectMeta: metav1.ObjectMeta{Name: "mypvsecret"}}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := superuserClient.Core().ConfigMaps("ns").Create(&api.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: "myconfigmap"}}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-02-20 19:28:28 +00:00
|
|
|
if _, err := superuserClient.Core().ConfigMaps("ns").Create(&api.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: "myconfigmapconfigsource"}}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-01-17 04:39:11 +00:00
|
|
|
pvName := "mypv"
|
2018-01-22 04:06:17 +00:00
|
|
|
if _, err := superuserClientExternal.StorageV1beta1().VolumeAttachments().Create(&storagev1beta1.VolumeAttachment{
|
2018-01-17 04:39:11 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "myattachment"},
|
2018-01-22 04:06:17 +00:00
|
|
|
Spec: storagev1beta1.VolumeAttachmentSpec{
|
2018-01-17 04:39:11 +00:00
|
|
|
Attacher: "foo",
|
2018-01-22 04:06:17 +00:00
|
|
|
Source: storagev1beta1.VolumeAttachmentSource{PersistentVolumeName: &pvName},
|
2018-01-17 04:39:11 +00:00
|
|
|
NodeName: "node2",
|
|
|
|
},
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-05-30 19:15:38 +00:00
|
|
|
if _, err := superuserClient.Core().PersistentVolumeClaims("ns").Create(&api.PersistentVolumeClaim{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "mypvc"},
|
|
|
|
Spec: api.PersistentVolumeClaimSpec{
|
|
|
|
AccessModes: []api.PersistentVolumeAccessMode{api.ReadOnlyMany},
|
|
|
|
Resources: api.ResourceRequirements{Requests: api.ResourceList{api.ResourceStorage: resource.MustParse("1")}},
|
|
|
|
},
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-11-20 17:57:24 +00:00
|
|
|
|
2017-05-30 19:15:38 +00:00
|
|
|
if _, err := superuserClient.Core().PersistentVolumes().Create(&api.PersistentVolume{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "mypv"},
|
|
|
|
Spec: api.PersistentVolumeSpec{
|
|
|
|
AccessModes: []api.PersistentVolumeAccessMode{api.ReadOnlyMany},
|
|
|
|
Capacity: api.ResourceList{api.ResourceStorage: resource.MustParse("1")},
|
|
|
|
ClaimRef: &api.ObjectReference{Namespace: "ns", Name: "mypvc"},
|
2017-06-16 15:23:22 +00:00
|
|
|
PersistentVolumeSource: api.PersistentVolumeSource{AzureFile: &api.AzureFilePersistentVolumeSource{ShareName: "default", SecretName: "mypvsecret"}},
|
2017-05-30 19:15:38 +00:00
|
|
|
},
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-01-09 21:44:55 +00:00
|
|
|
getSecret := func(client clientset.Interface) func() error {
|
|
|
|
return func() error {
|
|
|
|
_, err := client.Core().Secrets("ns").Get("mysecret", metav1.GetOptions{})
|
|
|
|
return err
|
|
|
|
}
|
2017-05-30 19:15:38 +00:00
|
|
|
}
|
2018-01-09 21:44:55 +00:00
|
|
|
getPVSecret := func(client clientset.Interface) func() error {
|
|
|
|
return func() error {
|
|
|
|
_, err := client.Core().Secrets("ns").Get("mypvsecret", metav1.GetOptions{})
|
|
|
|
return err
|
|
|
|
}
|
2017-05-30 19:15:38 +00:00
|
|
|
}
|
2018-01-09 21:44:55 +00:00
|
|
|
getConfigMap := func(client clientset.Interface) func() error {
|
|
|
|
return func() error {
|
|
|
|
_, err := client.Core().ConfigMaps("ns").Get("myconfigmap", metav1.GetOptions{})
|
|
|
|
return err
|
|
|
|
}
|
2017-05-30 19:15:38 +00:00
|
|
|
}
|
2018-02-20 19:28:28 +00:00
|
|
|
getConfigMapConfigSource := func(client clientset.Interface) func() error {
|
|
|
|
return func() error {
|
|
|
|
_, err := client.Core().ConfigMaps("ns").Get("myconfigmapconfigsource", metav1.GetOptions{})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-01-09 21:44:55 +00:00
|
|
|
getPVC := func(client clientset.Interface) func() error {
|
|
|
|
return func() error {
|
|
|
|
_, err := client.Core().PersistentVolumeClaims("ns").Get("mypvc", metav1.GetOptions{})
|
|
|
|
return err
|
|
|
|
}
|
2017-05-30 19:15:38 +00:00
|
|
|
}
|
2018-01-09 21:44:55 +00:00
|
|
|
getPV := func(client clientset.Interface) func() error {
|
|
|
|
return func() error {
|
|
|
|
_, err := client.Core().PersistentVolumes().Get("mypv", metav1.GetOptions{})
|
|
|
|
return err
|
|
|
|
}
|
2017-05-30 19:15:38 +00:00
|
|
|
}
|
2018-01-17 04:39:11 +00:00
|
|
|
getVolumeAttachment := func(client externalclientset.Interface) func() error {
|
|
|
|
return func() error {
|
2018-01-22 04:06:17 +00:00
|
|
|
_, err := client.StorageV1beta1().VolumeAttachments().Get("myattachment", metav1.GetOptions{})
|
2018-01-17 04:39:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-05-30 19:15:38 +00:00
|
|
|
|
2018-01-09 21:44:55 +00:00
|
|
|
createNode2NormalPod := func(client clientset.Interface) func() error {
|
|
|
|
return func() error {
|
|
|
|
_, err := client.Core().Pods("ns").Create(&api.Pod{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "node2normalpod"},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
NodeName: "node2",
|
|
|
|
Containers: []api.Container{{Name: "image", Image: "busybox"}},
|
|
|
|
Volumes: []api.Volume{
|
|
|
|
{Name: "secret", VolumeSource: api.VolumeSource{Secret: &api.SecretVolumeSource{SecretName: "mysecret"}}},
|
|
|
|
{Name: "cm", VolumeSource: api.VolumeSource{ConfigMap: &api.ConfigMapVolumeSource{LocalObjectReference: api.LocalObjectReference{Name: "myconfigmap"}}}},
|
|
|
|
{Name: "pvc", VolumeSource: api.VolumeSource{PersistentVolumeClaim: &api.PersistentVolumeClaimVolumeSource{ClaimName: "mypvc"}}},
|
|
|
|
},
|
2017-05-30 19:15:38 +00:00
|
|
|
},
|
2018-01-09 21:44:55 +00:00
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
2017-05-30 19:15:38 +00:00
|
|
|
}
|
2018-01-09 21:44:55 +00:00
|
|
|
updateNode2NormalPodStatus := func(client clientset.Interface) func() error {
|
|
|
|
return func() error {
|
|
|
|
startTime := metav1.NewTime(time.Now())
|
|
|
|
_, err := client.Core().Pods("ns").UpdateStatus(&api.Pod{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "node2normalpod"},
|
|
|
|
Status: api.PodStatus{StartTime: &startTime},
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
2017-05-30 19:15:38 +00:00
|
|
|
}
|
2018-01-09 21:44:55 +00:00
|
|
|
deleteNode2NormalPod := func(client clientset.Interface) func() error {
|
|
|
|
return func() error {
|
|
|
|
zero := int64(0)
|
|
|
|
return client.Core().Pods("ns").Delete("node2normalpod", &metav1.DeleteOptions{GracePeriodSeconds: &zero})
|
|
|
|
}
|
2017-05-30 19:15:38 +00:00
|
|
|
}
|
|
|
|
|
2018-01-09 21:44:55 +00:00
|
|
|
createNode2MirrorPod := func(client clientset.Interface) func() error {
|
|
|
|
return func() error {
|
|
|
|
_, err := client.Core().Pods("ns").Create(&api.Pod{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "node2mirrorpod",
|
|
|
|
Annotations: map[string]string{api.MirrorPodAnnotationKey: "true"},
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
NodeName: "node2",
|
|
|
|
Containers: []api.Container{{Name: "image", Image: "busybox"}},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
2017-05-30 19:15:38 +00:00
|
|
|
}
|
2018-01-09 21:44:55 +00:00
|
|
|
deleteNode2MirrorPod := func(client clientset.Interface) func() error {
|
|
|
|
return func() error {
|
|
|
|
zero := int64(0)
|
|
|
|
return client.Core().Pods("ns").Delete("node2mirrorpod", &metav1.DeleteOptions{GracePeriodSeconds: &zero})
|
|
|
|
}
|
2017-05-30 19:15:38 +00:00
|
|
|
}
|
|
|
|
|
2018-01-09 21:44:55 +00:00
|
|
|
createNode2 := func(client clientset.Interface) func() error {
|
|
|
|
return func() error {
|
|
|
|
_, err := client.Core().Nodes().Create(&api.Node{ObjectMeta: metav1.ObjectMeta{Name: "node2"}})
|
|
|
|
return err
|
|
|
|
}
|
2017-05-30 19:15:38 +00:00
|
|
|
}
|
2018-02-20 19:28:28 +00:00
|
|
|
setNode2ConfigSource := func(client clientset.Interface) func() error {
|
|
|
|
return func() error {
|
|
|
|
node2, err := client.Core().Nodes().Get("node2", metav1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
node2.Spec.ConfigSource = &api.NodeConfigSource{
|
2018-01-29 17:32:48 +00:00
|
|
|
ConfigMap: &api.ConfigMapNodeConfigSource{
|
2018-04-16 22:15:03 +00:00
|
|
|
Namespace: "ns",
|
|
|
|
Name: "myconfigmapconfigsource",
|
2018-01-29 17:32:48 +00:00
|
|
|
KubeletConfigKey: "kubelet",
|
2018-02-20 19:28:28 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
_, err = client.Core().Nodes().Update(node2)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unsetNode2ConfigSource := func(client clientset.Interface) func() error {
|
|
|
|
return func() error {
|
|
|
|
node2, err := client.Core().Nodes().Get("node2", metav1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
node2.Spec.ConfigSource = nil
|
|
|
|
_, err = client.Core().Nodes().Update(node2)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-01-09 21:44:55 +00:00
|
|
|
updateNode2Status := func(client clientset.Interface) func() error {
|
|
|
|
return func() error {
|
|
|
|
_, err := client.Core().Nodes().UpdateStatus(&api.Node{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "node2"},
|
|
|
|
Status: api.NodeStatus{},
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
2017-05-30 19:15:38 +00:00
|
|
|
}
|
2018-01-09 21:44:55 +00:00
|
|
|
deleteNode2 := func(client clientset.Interface) func() error {
|
|
|
|
return func() error {
|
|
|
|
return client.Core().Nodes().Delete("node2", nil)
|
|
|
|
}
|
2017-05-30 19:15:38 +00:00
|
|
|
}
|
2018-01-09 21:44:55 +00:00
|
|
|
createNode2NormalPodEviction := func(client clientset.Interface) func() error {
|
|
|
|
return func() error {
|
|
|
|
return client.Policy().Evictions("ns").Evict(&policy.Eviction{
|
|
|
|
TypeMeta: metav1.TypeMeta{
|
|
|
|
APIVersion: "policy/v1beta1",
|
|
|
|
Kind: "Eviction",
|
|
|
|
},
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "node2normalpod",
|
|
|
|
Namespace: "ns",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2017-07-11 18:53:12 +00:00
|
|
|
}
|
2018-01-09 21:44:55 +00:00
|
|
|
createNode2MirrorPodEviction := func(client clientset.Interface) func() error {
|
|
|
|
return func() error {
|
|
|
|
return client.Policy().Evictions("ns").Evict(&policy.Eviction{
|
|
|
|
TypeMeta: metav1.TypeMeta{
|
|
|
|
APIVersion: "policy/v1beta1",
|
|
|
|
Kind: "Eviction",
|
|
|
|
},
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "node2mirrorpod",
|
|
|
|
Namespace: "ns",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2017-07-11 18:53:12 +00:00
|
|
|
}
|
2017-05-30 19:15:38 +00:00
|
|
|
|
2017-11-20 17:57:24 +00:00
|
|
|
capacity := 50
|
2018-01-09 21:44:55 +00:00
|
|
|
updatePVCCapacity := func(client clientset.Interface) func() error {
|
|
|
|
return func() error {
|
|
|
|
capacity++
|
|
|
|
statusString := fmt.Sprintf("{\"status\": {\"capacity\": {\"storage\": \"%dG\"}}}", capacity)
|
|
|
|
patchBytes := []byte(statusString)
|
|
|
|
_, err := client.Core().PersistentVolumeClaims("ns").Patch("mypvc", types.StrategicMergePatchType, patchBytes, "status")
|
|
|
|
return err
|
|
|
|
}
|
2017-11-20 17:57:24 +00:00
|
|
|
}
|
|
|
|
|
2018-01-09 21:44:55 +00:00
|
|
|
updatePVCPhase := func(client clientset.Interface) func() error {
|
|
|
|
return func() error {
|
|
|
|
patchBytes := []byte(`{"status":{"phase": "Bound"}}`)
|
|
|
|
_, err := client.Core().PersistentVolumeClaims("ns").Patch("mypvc", types.StrategicMergePatchType, patchBytes, "status")
|
|
|
|
return err
|
|
|
|
}
|
2017-11-20 17:57:24 +00:00
|
|
|
}
|
|
|
|
|
2018-01-17 04:24:20 +00:00
|
|
|
nodeanonClient, _ := clientsetForToken(tokenNodeUnknown, clientConfig)
|
2018-01-17 04:39:11 +00:00
|
|
|
node1Client, node1ClientExternal := clientsetForToken(tokenNode1, clientConfig)
|
|
|
|
node2Client, node2ClientExternal := clientsetForToken(tokenNode2, clientConfig)
|
2017-05-30 19:15:38 +00:00
|
|
|
|
|
|
|
// all node requests from node1 and unknown node fail
|
|
|
|
expectForbidden(t, getSecret(nodeanonClient))
|
|
|
|
expectForbidden(t, getPVSecret(nodeanonClient))
|
|
|
|
expectForbidden(t, getConfigMap(nodeanonClient))
|
|
|
|
expectForbidden(t, getPVC(nodeanonClient))
|
|
|
|
expectForbidden(t, getPV(nodeanonClient))
|
|
|
|
expectForbidden(t, createNode2NormalPod(nodeanonClient))
|
|
|
|
expectForbidden(t, createNode2MirrorPod(nodeanonClient))
|
2017-07-11 18:53:12 +00:00
|
|
|
expectForbidden(t, deleteNode2NormalPod(nodeanonClient))
|
2017-05-30 19:15:38 +00:00
|
|
|
expectForbidden(t, deleteNode2MirrorPod(nodeanonClient))
|
2017-07-11 18:53:12 +00:00
|
|
|
expectForbidden(t, createNode2MirrorPodEviction(nodeanonClient))
|
2017-05-30 19:15:38 +00:00
|
|
|
expectForbidden(t, createNode2(nodeanonClient))
|
|
|
|
expectForbidden(t, updateNode2Status(nodeanonClient))
|
|
|
|
expectForbidden(t, deleteNode2(nodeanonClient))
|
|
|
|
|
|
|
|
expectForbidden(t, getSecret(node1Client))
|
|
|
|
expectForbidden(t, getPVSecret(node1Client))
|
|
|
|
expectForbidden(t, getConfigMap(node1Client))
|
|
|
|
expectForbidden(t, getPVC(node1Client))
|
|
|
|
expectForbidden(t, getPV(node1Client))
|
|
|
|
expectForbidden(t, createNode2NormalPod(nodeanonClient))
|
|
|
|
expectForbidden(t, createNode2MirrorPod(node1Client))
|
2017-07-11 18:53:12 +00:00
|
|
|
expectNotFound(t, deleteNode2MirrorPod(node1Client))
|
|
|
|
expectNotFound(t, createNode2MirrorPodEviction(node1Client))
|
2017-05-30 19:15:38 +00:00
|
|
|
expectForbidden(t, createNode2(node1Client))
|
|
|
|
expectForbidden(t, updateNode2Status(node1Client))
|
|
|
|
expectForbidden(t, deleteNode2(node1Client))
|
|
|
|
|
|
|
|
// related object requests from node2 fail
|
|
|
|
expectForbidden(t, getSecret(node2Client))
|
|
|
|
expectForbidden(t, getPVSecret(node2Client))
|
|
|
|
expectForbidden(t, getConfigMap(node2Client))
|
|
|
|
expectForbidden(t, getPVC(node2Client))
|
|
|
|
expectForbidden(t, getPV(node2Client))
|
2017-11-20 17:57:24 +00:00
|
|
|
|
2017-05-30 19:15:38 +00:00
|
|
|
expectForbidden(t, createNode2NormalPod(nodeanonClient))
|
|
|
|
// mirror pod and self node lifecycle is allowed
|
|
|
|
expectAllowed(t, createNode2MirrorPod(node2Client))
|
|
|
|
expectAllowed(t, deleteNode2MirrorPod(node2Client))
|
2017-07-11 18:53:12 +00:00
|
|
|
expectAllowed(t, createNode2MirrorPod(node2Client))
|
|
|
|
expectAllowed(t, createNode2MirrorPodEviction(node2Client))
|
2017-05-30 19:15:38 +00:00
|
|
|
expectAllowed(t, createNode2(node2Client))
|
|
|
|
expectAllowed(t, updateNode2Status(node2Client))
|
2018-05-11 13:37:21 +00:00
|
|
|
expectAllowed(t, deleteNode2(node2Client))
|
2017-05-30 19:15:38 +00:00
|
|
|
|
|
|
|
// create a pod as an admin to add object references
|
|
|
|
expectAllowed(t, createNode2NormalPod(superuserClient))
|
|
|
|
|
|
|
|
// unidentifiable node and node1 are still forbidden
|
|
|
|
expectForbidden(t, getSecret(nodeanonClient))
|
|
|
|
expectForbidden(t, getPVSecret(nodeanonClient))
|
|
|
|
expectForbidden(t, getConfigMap(nodeanonClient))
|
|
|
|
expectForbidden(t, getPVC(nodeanonClient))
|
|
|
|
expectForbidden(t, getPV(nodeanonClient))
|
|
|
|
expectForbidden(t, createNode2NormalPod(nodeanonClient))
|
|
|
|
expectForbidden(t, updateNode2NormalPodStatus(nodeanonClient))
|
|
|
|
expectForbidden(t, deleteNode2NormalPod(nodeanonClient))
|
2017-07-11 18:53:12 +00:00
|
|
|
expectForbidden(t, createNode2NormalPodEviction(nodeanonClient))
|
2017-05-30 19:15:38 +00:00
|
|
|
expectForbidden(t, createNode2MirrorPod(nodeanonClient))
|
|
|
|
expectForbidden(t, deleteNode2MirrorPod(nodeanonClient))
|
2017-07-11 18:53:12 +00:00
|
|
|
expectForbidden(t, createNode2MirrorPodEviction(nodeanonClient))
|
2017-05-30 19:15:38 +00:00
|
|
|
|
|
|
|
expectForbidden(t, getSecret(node1Client))
|
|
|
|
expectForbidden(t, getPVSecret(node1Client))
|
|
|
|
expectForbidden(t, getConfigMap(node1Client))
|
|
|
|
expectForbidden(t, getPVC(node1Client))
|
|
|
|
expectForbidden(t, getPV(node1Client))
|
|
|
|
expectForbidden(t, createNode2NormalPod(node1Client))
|
|
|
|
expectForbidden(t, updateNode2NormalPodStatus(node1Client))
|
|
|
|
expectForbidden(t, deleteNode2NormalPod(node1Client))
|
2017-07-11 18:53:12 +00:00
|
|
|
expectForbidden(t, createNode2NormalPodEviction(node1Client))
|
2017-05-30 19:15:38 +00:00
|
|
|
expectForbidden(t, createNode2MirrorPod(node1Client))
|
2017-07-11 18:53:12 +00:00
|
|
|
expectNotFound(t, deleteNode2MirrorPod(node1Client))
|
|
|
|
expectNotFound(t, createNode2MirrorPodEviction(node1Client))
|
2017-05-30 19:15:38 +00:00
|
|
|
|
|
|
|
// node2 can get referenced objects now
|
|
|
|
expectAllowed(t, getSecret(node2Client))
|
|
|
|
expectAllowed(t, getPVSecret(node2Client))
|
|
|
|
expectAllowed(t, getConfigMap(node2Client))
|
|
|
|
expectAllowed(t, getPVC(node2Client))
|
|
|
|
expectAllowed(t, getPV(node2Client))
|
2017-11-20 17:57:24 +00:00
|
|
|
|
2017-05-30 19:15:38 +00:00
|
|
|
expectForbidden(t, createNode2NormalPod(node2Client))
|
|
|
|
expectAllowed(t, updateNode2NormalPodStatus(node2Client))
|
|
|
|
expectAllowed(t, deleteNode2NormalPod(node2Client))
|
|
|
|
expectAllowed(t, createNode2MirrorPod(node2Client))
|
|
|
|
expectAllowed(t, deleteNode2MirrorPod(node2Client))
|
2018-02-20 19:28:28 +00:00
|
|
|
|
2017-07-11 18:53:12 +00:00
|
|
|
// recreate as an admin to test eviction
|
|
|
|
expectAllowed(t, createNode2NormalPod(superuserClient))
|
|
|
|
expectAllowed(t, createNode2MirrorPod(superuserClient))
|
|
|
|
expectAllowed(t, createNode2NormalPodEviction(node2Client))
|
|
|
|
expectAllowed(t, createNode2MirrorPodEviction(node2Client))
|
2017-11-20 17:57:24 +00:00
|
|
|
|
|
|
|
// re-create a pod as an admin to add object references
|
|
|
|
expectAllowed(t, createNode2NormalPod(superuserClient))
|
2018-01-17 04:39:11 +00:00
|
|
|
|
|
|
|
// ExpandPersistentVolumes feature disabled
|
2017-11-20 17:57:24 +00:00
|
|
|
defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ExpandPersistentVolumes, false)()
|
|
|
|
expectForbidden(t, updatePVCCapacity(node1Client))
|
|
|
|
expectForbidden(t, updatePVCCapacity(node2Client))
|
|
|
|
|
2018-01-17 04:39:11 +00:00
|
|
|
// ExpandPersistentVolumes feature enabled
|
2017-11-20 17:57:24 +00:00
|
|
|
defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ExpandPersistentVolumes, true)()
|
|
|
|
expectForbidden(t, updatePVCCapacity(node1Client))
|
|
|
|
expectAllowed(t, updatePVCCapacity(node2Client))
|
|
|
|
expectForbidden(t, updatePVCPhase(node2Client))
|
2018-01-17 04:39:11 +00:00
|
|
|
|
|
|
|
// Disabled CSIPersistentVolume feature
|
|
|
|
defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CSIPersistentVolume, false)()
|
|
|
|
expectForbidden(t, getVolumeAttachment(node1ClientExternal))
|
|
|
|
expectForbidden(t, getVolumeAttachment(node2ClientExternal))
|
|
|
|
// Enabled CSIPersistentVolume feature
|
|
|
|
defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CSIPersistentVolume, true)()
|
|
|
|
expectForbidden(t, getVolumeAttachment(node1ClientExternal))
|
|
|
|
expectAllowed(t, getVolumeAttachment(node2ClientExternal))
|
2018-02-23 19:24:43 +00:00
|
|
|
|
2018-02-20 19:28:28 +00:00
|
|
|
// create node2 again
|
|
|
|
expectAllowed(t, createNode2(node2Client))
|
|
|
|
// node2 can not set its own config source
|
|
|
|
expectForbidden(t, setNode2ConfigSource(node2Client))
|
|
|
|
// node2 can not access the configmap config source yet
|
|
|
|
expectForbidden(t, getConfigMapConfigSource(node2Client))
|
|
|
|
// superuser can access the configmap config source
|
|
|
|
expectAllowed(t, getConfigMapConfigSource(superuserClient))
|
|
|
|
// superuser can set node2's config source
|
|
|
|
expectAllowed(t, setNode2ConfigSource(superuserClient))
|
|
|
|
// node2 can now get the configmap assigned as its config source
|
|
|
|
expectAllowed(t, getConfigMapConfigSource(node2Client))
|
|
|
|
// superuser can unset node2's config source
|
|
|
|
expectAllowed(t, unsetNode2ConfigSource(superuserClient))
|
|
|
|
// node2 can no longer get the configmap after it is unassigned as its config source
|
|
|
|
expectForbidden(t, getConfigMapConfigSource(node2Client))
|
|
|
|
// clean up node2
|
2018-05-11 13:37:21 +00:00
|
|
|
expectAllowed(t, deleteNode2(node2Client))
|
2018-02-20 19:28:28 +00:00
|
|
|
|
2018-02-23 19:24:43 +00:00
|
|
|
//TODO(mikedanese): integration test node restriction of TokenRequest
|
2017-05-30 19:15:38 +00:00
|
|
|
}
|
|
|
|
|
2018-01-09 21:44:55 +00:00
|
|
|
// expect executes a function a set number of times until it either returns the
|
|
|
|
// expected error or executes too many times. It returns if the retries timed
|
|
|
|
// out and the last error returned by the method.
|
2018-01-17 04:39:11 +00:00
|
|
|
func expect(t *testing.T, f func() error, wantErr func(error) bool) (timeout bool, lastErr error) {
|
|
|
|
t.Helper()
|
2018-01-09 21:44:55 +00:00
|
|
|
err := wait.PollImmediate(time.Second, 30*time.Second, func() (bool, error) {
|
2018-01-17 04:39:11 +00:00
|
|
|
t.Helper()
|
2018-01-09 21:44:55 +00:00
|
|
|
lastErr = f()
|
|
|
|
if wantErr(lastErr) {
|
|
|
|
return true, nil
|
|
|
|
}
|
2018-01-17 04:39:11 +00:00
|
|
|
t.Logf("unexpected response, will retry: %v", lastErr)
|
2018-01-09 21:44:55 +00:00
|
|
|
return false, nil
|
|
|
|
})
|
|
|
|
return err == nil, lastErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func expectForbidden(t *testing.T, f func() error) {
|
|
|
|
t.Helper()
|
2018-01-17 04:39:11 +00:00
|
|
|
if ok, err := expect(t, f, errors.IsForbidden); !ok {
|
2018-01-09 21:44:55 +00:00
|
|
|
t.Errorf("Expected forbidden error, got %v", err)
|
2017-05-30 19:15:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-09 21:44:55 +00:00
|
|
|
func expectNotFound(t *testing.T, f func() error) {
|
|
|
|
t.Helper()
|
2018-01-17 04:39:11 +00:00
|
|
|
if ok, err := expect(t, f, errors.IsNotFound); !ok {
|
2018-01-09 21:44:55 +00:00
|
|
|
t.Errorf("Expected notfound error, got %v", err)
|
2017-07-11 18:53:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-09 21:44:55 +00:00
|
|
|
func expectAllowed(t *testing.T, f func() error) {
|
|
|
|
t.Helper()
|
2018-01-17 04:39:11 +00:00
|
|
|
if ok, err := expect(t, f, func(e error) bool { return e == nil }); !ok {
|
2018-01-09 21:44:55 +00:00
|
|
|
t.Errorf("Expected no error, got %v", err)
|
2017-05-30 19:15:38 +00:00
|
|
|
}
|
|
|
|
}
|