-
Notifications
You must be signed in to change notification settings - Fork 82
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
diogoasouza
wants to merge
1
commit into
rancher:master
Choose a base branch
from
diogoasouza:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
pod.Spec.Volumes = append(pod.Spec.Volumes, | ||
v1.Volume{ | ||
|
@@ -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 { | ||
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should, yes. It was my mistake |
||
}, | ||
}, | ||
}) | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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