Skip to content

backend: Fix missing k8s token when in "in-cluster" mode #2477

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jamowei
Copy link

@jamowei jamowei commented Oct 24, 2024

This PR fixes the issue #1826 that you have to provide an extra k8s token in the UI although headlamp is running inside a k8s cluster with in-cluster mode enabled.

This are the logs you get, which are also confusing then:

{"level":"info","source":"/headlamp/backend/cmd/headlamp.go","line":305,"time":"2024-03-18T22:51:07Z","message":"Creating Headlamp handler"}
{"level":"info","source":"/headlamp/backend/cmd/headlamp.go","line":306,"time":"2024-03-18T22:51:07Z","message":"Kubeconfig path: "}
{"level":"info","source":"/headlamp/backend/cmd/headlamp.go","line":307,"time":"2024-03-18T22:51:07Z","message":"Static plugin dir: /headlamp/static-plugins"}
{"level":"info","source":"/headlamp/backend/cmd/headlamp.go","line":308,"time":"2024-03-18T22:51:07Z","message":"Plugins dir: /headlamp/plugins"}
{"level":"info","source":"/headlamp/backend/cmd/headlamp.go","line":309,"time":"2024-03-18T22:51:07Z","message":"Dynamic clusters support: false"}
{"level":"info","source":"/headlamp/backend/cmd/headlamp.go","line":310,"time":"2024-03-18T22:51:07Z","message":"Helm support: false"}
{"level":"info","source":"/headlamp/backend/cmd/headlamp.go","line":311,"time":"2024-03-18T22:51:07Z","message":"Proxy URLs: []"}
{"level":"info","context":"main","clusterURL":"https://10.96.0.1:443","source":"/headlamp/backend/pkg/kubeconfig/kubeconfig.go","line":172,"time":"2024-03-18T22:51:07Z","message":"Proxy setup"}
*** Headlamp Server ***
  API Routers:
{"level":"error","source":"/headlamp/backend/cmd/headlamp.go","line":365,"error":"read /: is a directory","time":"2024-03-18T22:51:07Z","message":"loading kubeconfig"}
{"level":"error","source":"/headlamp/backend/cmd/headlamp.go","line":376,"error":"open /home/headlamp/.config/Headlamp/kubeconfigs/config: no such file or directory","time":"2024-03-18T22:51:07Z","message":"loading dynamic kubeconfig"}

The problem was that the kubeconfig.GetInClusterContext() function did not use the service-token which gets mounted to the pod when creating the cluster-credentials.

I tested the fix with minikube and now I don't have to pass an extra token when opening the UI 🙂

@dosubot dosubot bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Oct 24, 2024
@jamowei jamowei force-pushed the main branch 2 times, most recently from 088617f to d337ebe Compare October 24, 2024 21:40
@illume
Copy link
Collaborator

illume commented Oct 25, 2024

Thanks for this fix.

It looks like there's a formatting issue. Maybe this will fix it: make backend-format

@jamowei
Copy link
Author

jamowei commented Oct 25, 2024

Thanks for this fix.

It looks like there's a formatting issue. Maybe this will fix it: make backend-format

Ah ok, thanks for the hint. I fixed it 🙂

Copy link
Contributor

@joaquimrocha joaquimrocha left a comment

Choose a reason for hiding this comment

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

@jamowei , left a comment. Also, can you squash these give a title + description to the commit?
The title should be in imperative mood (e.g. 'Fix', not 'Fixed' or 'Fixes') and have a backend: prefix.

@jamowei jamowei changed the title fix missing k8s token when in "in-cluster" mode backend: Fix missing k8s token when in "in-cluster" mode Oct 25, 2024
@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:XS This PR changes 0-9 lines, ignoring generated files. labels Oct 25, 2024
@jamowei
Copy link
Author

jamowei commented Oct 25, 2024

@jamowei , left a comment. Also, can you squash these give a title + description to the commit? The title should be in imperative mood (e.g. 'Fix', not 'Fixed' or 'Fixes') and have a backend: prefix.

@joaquimrocha Ok now?

@jamowei jamowei requested a review from joaquimrocha October 25, 2024 20:14
@jamowei jamowei force-pushed the main branch 2 times, most recently from 1dc4c1c to 9ca13c4 Compare October 25, 2024 20:56
Copy link
Contributor

@joaquimrocha joaquimrocha left a comment

Choose a reason for hiding this comment

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

@jamowei The 1st commit is now in the right format, thanks! It still seems though like there are several commits, including a merge commit and they are not all related to the changes. Please squash these together, and maybe remove the changes to the initial logging, unless you want to propose those in a different commit. I asked some colleagues for help.


// load kubeConfig clusters
err := kubeconfig.LoadAndStoreKubeConfigs(config.kubeConfigStore, kubeConfigPath, kubeconfig.KubeConfig)
if err != nil {
if err != nil && !config.useInCluster {
Copy link
Contributor

Choose a reason for hiding this comment

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

@illume I think you did some changes related to this in the past, in case you still remember and this is related.

Comment on lines +840 to +843
inClusterAuthInfo := &api.AuthInfo{
Token: clusterConfig.BearerToken,
TokenFile: clusterConfig.BearerTokenFile,
}
Copy link
Contributor

Choose a reason for hiding this comment

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

@yolossn , can you help me understand if this is fine?

Copy link
Collaborator

Choose a reason for hiding this comment

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

This change will make the in-cluster deployments open without any kind of authentication. This is a major change in the existing functionality.

@joaquimrocha
Copy link
Contributor

BTW, each commit (although I think this PR should just be one) needs to be signed off. That's done by running git commit -s or, if you have already your commit done, git commit -s --amend.

@dosubot dosubot bot added size:XS This PR changes 0-9 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Oct 29, 2024
…unning inside k8s cluster with `in-cluster` mode enabled

Signed-off-by: Jan <[email protected]>
@illume
Copy link
Collaborator

illume commented Nov 1, 2024

I reran this job that failed... https://github.com/headlamp-k8s/headlamp/actions/runs/11580255626/job/32326141784?pr=2477

(Maybe it was an intermittent error.)

@illume
Copy link
Collaborator

illume commented Nov 1, 2024

hrmmm. The Build Container and test job still seems to be failing. I'll try it again later, and if it's still failing I'll look into it further.

@mdoerries
Copy link

@illume @joaquimrocha Is there any update? We also urgently need this function!

@joaquimrocha
Copy link
Contributor

@mdoerries We are now prioritizing this.

@joaquimrocha joaquimrocha added this to the v0.27.0 milestone Nov 27, 2024
@jamowei
Copy link
Author

jamowei commented Nov 27, 2024

hrmmm. The Build Container and test job still seems to be failing. I'll try it again later, and if it's still failing I'll look into it further.

I think you have to fix the E2E tests with playwright. There it looks like a token is expected, when running in "in-cluster" mode... Which in my opinion is wrong 😉

@joaquimrocha
Copy link
Contributor

@jamowei , I got @yolossn (one of our team's engineers) to take a look and he's raised the concern that getting the token into the config like that, when in-cluster, may lead to a security problem because you are going to configure the access for all users that way, i.e. if Headlamp successfully accesses the cluster, it will not prompt the user for a token, and this in a shared environment can be a security problem, at least for those who do not expect this behavior.

AFAIU, you can accomplish the same by using a kubeconfig with the cluster's data (including token) and using the -kubeconfig even if you are deploying Headlamp. WDYT?

@joaquimrocha
Copy link
Contributor

@jamowei Given my previous comment, I don't think we can merge this PR. At least not as is, we'd require a flag like -in-cluster-shared-token=true + docs about it for this to feel safe enough.

@joaquimrocha joaquimrocha marked this pull request as draft December 4, 2024 16:58
@jamowei
Copy link
Author

jamowei commented Dec 5, 2024

@jamowei , I got @yolossn (one of our team's engineers) to take a look and he's raised the concern that getting the token into the config like that, when in-cluster, may lead to a security problem because you are going to configure the access for all users that way, i.e. if Headlamp successfully accesses the cluster, it will not prompt the user for a token, and this in a shared environment can be a security problem, at least for those who do not expect this behavior.

AFAIU, you can accomplish the same by using a kubeconfig with the cluster's data (including token) and using the -kubeconfig even if you are deploying Headlamp. WDYT?

Ok, I understand this point und I will try the -kubeconfig parameter instead. But for easy use and easy setup an extra parameter would be great! Maybe I have time to extend my PR for that...

@jamowei
Copy link
Author

jamowei commented Dec 5, 2024

@jamowei Given my previous comment, I don't think we can merge this PR. At least not as is, we'd require a flag like -in-cluster-shared-token=true + docs about it for this to feel safe enough.

I would name it -use-service-account in combination with -in-cluster parameter, which then will enables the auto-login via the mounted service account token 😉

What do you think @joaquimrocha @mdoerries ?

@jamowei jamowei requested a review from yolossn December 5, 2024 08:16
@joaquimrocha
Copy link
Contributor

joaquimrocha commented Dec 5, 2024

I would name it -use-service-account in combination with -in-cluster parameter, which then will enables the auto-login via the mounted service account token 😉

What do you think @joaquimrocha @mdoerries ?

I think we need an arg name that clearly states what it is and what context, because maybe some admins would think use-service-account is for prompting users on the client side to use an SA token. I am not 100% happy with my previous suggestion for a name, but something along those lines, where it's clearly what we are turning on/off would be better IMO.
More suggestions are welcome!

@k8s-triage-robot
Copy link

Unknown CLA label state. Rechecking for CLA labels.

Send feedback to sig-contributor-experience at kubernetes/community.

/check-cla
/easycla

Copy link

CLA Not Signed

@k8s-ci-robot k8s-ci-robot added the cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. label Apr 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. size:XS This PR changes 0-9 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants