Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changing permission for kubeconfig file inside the shell pod #166

Merged
merged 2 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 44 additions & 10 deletions pkg/podimpersonation/podimpersonation.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,10 +512,12 @@ func (s *PodImpersonation) adminKubeConfig(user user.Info, role *rbacv1.ClusterR

func (s *PodImpersonation) augmentPod(pod *v1.Pod, sa *v1.ServiceAccount, secret *v1.Secret, imageOverride string) *v1.Pod {
var (
zero = int64(0)
t = true
f = false
m = int32(420)
zero = int64(0)
t = true
f = false
m = int32(0o644)
m2 = int32(0o600)
shellUser = 1000
)

pod = pod.DeepCopy()
Expand All @@ -535,11 +537,18 @@ func (s *PodImpersonation) augmentPod(pod *v1.Pod, sa *v1.ServiceAccount, secret
},
v1.Volume{
Name: "user-kubeconfig",
VolumeSource: v1.VolumeSource{
EmptyDir: &v1.EmptyDirVolumeSource{},
},
},
v1.Volume{
Name: "user-kube-configmap",
VolumeSource: v1.VolumeSource{
ConfigMap: &v1.ConfigMapVolumeSource{
LocalObjectReference: v1.LocalObjectReference{
Name: s.userConfigName(),
},
DefaultMode: &m2,
},
},
},
Expand All @@ -553,27 +562,52 @@ func (s *PodImpersonation) augmentPod(pod *v1.Pod, sa *v1.ServiceAccount, secret
},
})

image := imageOverride
if image == "" {
image = s.imageName()
}

for i, container := range pod.Spec.Containers {
for _, envvar := range container.Env {
if envvar.Name != "KUBECONFIG" {
continue
}

//This mounts two volumes, one configMap and one emptyDir.
//The reason for this is that we need to change the permissions on the kubeconfig file
//and, since a configMap volume is always read-only, we need an emptyDir volume as well.
vmount := v1.VolumeMount{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Could add a comment to explain why we're doing this since it's not obvious at first

Name: "user-kubeconfig",
MountPath: "/tmp/.kube",
}
cfgVMount := v1.VolumeMount{
Name: "user-kube-configmap",
MountPath: "/home/.kube/config",
SubPath: "config",
}

pod.Spec.InitContainers = append(pod.Spec.InitContainers, v1.Container{
Name: "init-kubeconfig-volume",
Image: image,
Command: []string{"sh", "-c", fmt.Sprintf("cp %s %s && chown %d %s/config", cfgVMount.MountPath, vmount.MountPath, shellUser, vmount.MountPath)},
ImagePullPolicy: v1.PullIfNotPresent,
SecurityContext: &v1.SecurityContext{
RunAsUser: &zero,
RunAsGroup: &zero,
},
VolumeMounts: []v1.VolumeMount{cfgVMount, vmount},
},
)

pod.Spec.Containers[i].VolumeMounts = append(container.VolumeMounts, v1.VolumeMount{
Name: "user-kubeconfig",
ReadOnly: true,
MountPath: envvar.Value,
SubPath: "config",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this subpath?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only use what's inside the config key of the ConfigMap. We don't need it, but I think it's good to specify a SubPath if we know exactly what we need

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@diogoasouza this is not a configmap. This is only emptyDir volume.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rohitsakala Yes, sorry if I wasn't clear. I meant that we only copy the config from the configMap to this volume and that's why we only need to mount this path to the container

})
break
}
}

image := imageOverride
if image == "" {
image = s.imageName()
}

pod.Spec.Containers = append(pod.Spec.Containers, v1.Container{
Name: "proxy",
Image: image,
Expand Down
91 changes: 91 additions & 0 deletions pkg/podimpersonation/podimpersonation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package podimpersonation

import (
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"testing"
"time"
)

func TestAugmentPod(t *testing.T) {
var (
zero = int64(0)
)
testCases := []struct {
name string
imageOverride string
envVars []v1.EnvVar
}{
{
name: "Should mount volume to container, create an init container and use regular image",
imageOverride: "",
envVars: []v1.EnvVar{{Name: "KUBECONFIG", Value: ".kube/config"}},
},
{
name: "Should mount volume to container, create an init container and use overridden image",
imageOverride: "rancher/notShell:v1.0.0",
envVars: []v1.EnvVar{{Name: "KUBECONFIG", Value: ".kube/config"}},
},
{
name: "Should not create init container if there's no KUBECONFIG envVar",
imageOverride: "",
envVars: []v1.EnvVar{},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
p := newPod(tc.envVars)
impersonator := New("", nil, time.Minute, func() string { return "rancher/shell:v0.1.22" })
pod := impersonator.augmentPod(p, nil, &v1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "s"}}, tc.imageOverride)

assert.Len(t, pod.Spec.Volumes, len(p.Spec.Volumes)+4, "expected four new volumes")
if len(tc.envVars) != 0 {
assert.Len(t, pod.Spec.Containers[0].VolumeMounts, len(p.Spec.Containers[0].VolumeMounts)+1, "expected kubeconfig volume to be mounted")
assert.Len(t, pod.Spec.InitContainers, len(p.Spec.InitContainers)+1, "expected an init container to be created")
if tc.imageOverride != "" {
assert.Equal(t, pod.Spec.InitContainers[len(pod.Spec.InitContainers)-1].Image, tc.imageOverride, "expected image to be the one received as parameter")
} else {
assert.Equal(t, pod.Spec.InitContainers[len(pod.Spec.InitContainers)-1].Image, impersonator.imageName(), "expected image to be the impersonator image")
}
assert.Equal(t, pod.Spec.InitContainers[len(pod.Spec.InitContainers)-1].SecurityContext.RunAsUser, &zero, "expected init container to run as user zero")
assert.Equal(t, pod.Spec.InitContainers[len(pod.Spec.InitContainers)-1].SecurityContext.RunAsGroup, &zero, "expected init container to run as group zero")
} else {
assert.Len(t, pod.Spec.InitContainers, len(p.Spec.InitContainers), "expected no init container to be created")
}
assert.Equal(t, pod.Spec.Containers[len(pod.Spec.Containers)-1].Name, "proxy", "expected the container proxy to be created")
})
}
}

func newPod(env []v1.EnvVar) *v1.Pod {
return &v1.Pod{
Spec: v1.PodSpec{
Volumes: []v1.Volume{{
Name: "volume1",
VolumeSource: v1.VolumeSource{
ConfigMap: &v1.ConfigMapVolumeSource{
LocalObjectReference: v1.LocalObjectReference{
Name: "cfgMap",
},
},
},
}},
Containers: []v1.Container{
{
Name: "shell",
Image: "rancher/shell:v0.1.22",
Env: env,
VolumeMounts: []v1.VolumeMount{{
Name: "volume1",
MountPath: "/home/vol",
}},
},
},
ServiceAccountName: "svc-account-1",
AutomountServiceAccountToken: nil,
SecurityContext: nil,
},
}
}