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

issue-42861#Not using auto generated secret tokens #129

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
51 changes: 5 additions & 46 deletions pkg/podimpersonation/podimpersonation.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,34 +334,7 @@ func (s *PodImpersonation) createPod(ctx context.Context, user user.Info, role *
return nil, err
}

sc := v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: sa.Name + "-token",
OwnerReferences: ref(role),
Annotations: map[string]string{
"kubernetes.io/service-account.name": sa.Name,
},
},
Type: v1.SecretTypeServiceAccountToken,
}
tokenSecret, err := client.CoreV1().Secrets(sa.Namespace).Create(ctx, &sc, metav1.CreateOptions{})
if err != nil {
return nil, err
}
if _, ok := tokenSecret.Data[v1.ServiceAccountTokenKey]; !ok {
for {
logrus.Debugf("wait for svc account secret to be populated with token %s", tokenSecret.Name)
time.Sleep(2 * time.Second)
tokenSecret, err = client.CoreV1().Secrets(sa.Namespace).Get(ctx, sc.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
if _, ok := tokenSecret.Data[v1.ServiceAccountTokenKey]; ok {
break
}
}
}
pod = s.augmentPod(pod, sa, tokenSecret, podOptions.ImageOverride)
pod = s.augmentPod(pod, sa, podOptions.ImageOverride)

if err := s.createConfigMaps(ctx, user, role, pod, podOptions, client); err != nil {
return nil, err
Expand Down Expand Up @@ -510,17 +483,16 @@ func (s *PodImpersonation) adminKubeConfig(user user.Info, role *rbacv1.ClusterR
}, nil
}

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

pod = pod.DeepCopy()

pod.Spec.ServiceAccountName = ""
pod.Spec.ServiceAccountName = sa.Name
pod.Spec.AutomountServiceAccountToken = &f
Comment on lines +495 to 496
Copy link
Member

Choose a reason for hiding this comment

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

Should we consider using the AutomountServiceAccountToken feature and let k8s manage the token inject (volumes)?
If I understand correctly, it was disabled because we created the Secret and would like to use one Secret for all pods.
Now, We no longer need to maintain the Secret, so can we leave it to K8s?
WDYT? Discussions are welcomed!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tested with that before, and it works, but I'm unsure if it handles rotating the tokens.

Copy link
Member

@jiaqiluo jiaqiluo Sep 22, 2023

Choose a reason for hiding this comment

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

Yes, K8s handles the rotation.

To assign a ServiceAccount to a Pod, you set the spec.serviceAccountName field in the Pod specification. Kubernetes then automatically provides the credentials for that ServiceAccount to the Pod. In v1.22 and later, Kubernetes gets a short-lived, automatically rotating token using the TokenRequest API and mounts the token as a projected volume.

Ref: https://kubernetes.io/docs/concepts/security/service-accounts/#assign-to-pod

I would like to hear more thoughts from the maintainer of the repo @rancher/rancher-team-1-neo-dev

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd agree with @jiaqiluo here, there's a lot of extra code add to project volumes that I would expect K8s to do for us. If there's a reason to manually set this up, happy to consider it, but for now I think we should default to the automounted value recommended by upstream.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree as well, I wasn't aware that kubernetes would rotate the tokens automatically if using that flag, just that it does that when using a projected volume

pod.Spec.Volumes = append(pod.Spec.Volumes,
v1.Volume{
Expand All @@ -543,15 +515,7 @@ func (s *PodImpersonation) augmentPod(pod *v1.Pod, sa *v1.ServiceAccount, secret
},
},
},
v1.Volume{
Name: secret.Name,
VolumeSource: v1.VolumeSource{
Secret: &v1.SecretVolumeSource{
SecretName: secret.Name,
DefaultMode: &m,
},
},
})
)

for i, container := range pod.Spec.Containers {
for _, envvar := range container.Env {
Expand Down Expand Up @@ -597,11 +561,6 @@ func (s *PodImpersonation) augmentPod(pod *v1.Pod, sa *v1.ServiceAccount, secret
MountPath: "/root/.kube/config",
SubPath: "config",
},
{
Name: secret.Name,
MountPath: "/var/run/secrets/kubernetes.io/serviceaccount",
ReadOnly: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you elaborate on why this was removed? I would expect us to still want this to be read-only

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It should, yes. It was my mistake

},
},
})

Expand Down