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

fix #4698: refining authentication mechanisms to be clearer #4702

Merged
merged 3 commits into from
Jan 27, 2023

Conversation

shawkins
Copy link
Contributor

@shawkins shawkins commented Dec 20, 2022

Description

Fix #4698

Addresses #4698: this makes auth mechanisms more distinct. It gives full control over setting the auth header to the token interceptors (which could be renamed).

The proposed behavior:

For kubernetes: if username and password is supplied, then only basic auth will be used. If not, then the token logic will be used.

For openshift: there is some confusing overlap between the usage of username / password and setting the token / token provider. It makes sense to me to keep these distinct. So if a username and password are provided, then we'll use that generate a token - after the first request(s) fail, once there has been a successful refresh the token will be reused. If a username / password are not set, then we'll use the token from the config, which may also be provided by a token provider.

All of this would be added to appropriate documentation if it seems good.

There is additional logic to guard against sharing the token with requests where the user has used withRequestConfig to override the username / password. @manusa seemed open to another issue that would deprecate withRequestConfig (either completely or specific fields).

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • Feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change
  • Chore (non-breaking change which doesn't affect codebase;
    test, version modification, documentation, etc.)

Checklist

  • Code contributed by me aligns with current project license: Apache 2.0
  • I Added CHANGELOG entry regarding this change
  • I have implemented unit tests to cover my changes
  • I have added/updated the javadocs and other documentation accordingly
  • No new bugs, code smells, etc. in SonarCloud report
  • I tested my code in Kubernetes
  • I tested my code in OpenShift

@shawkins
Copy link
Contributor Author

shawkins commented Jan 3, 2023

@manusa and others. Upon further review the openshift logic probably does need to stay somewhat intertwined.

So with this change the openshift logic will try to use the token (which could also be from a tokenprovider). If that fails, it will then see if a username and password are supplied in the config passed into the interceptor. If they are supplied, then we'll authenticate to get a fresh token - if we get a one, it will be persisted in the kubeconfig.

If a username and password aren't supplied we'll try to refresh the config (just like the base kubernetes logic) and use the updated token (we don't bother detecting if it hasn't actually changed).

Just like the kuberentes token refresh we set the token on the current config when there's been a possible change - this is an alternative to the atomicreference approach - I've modified here so that it's a volatile field for better multi-threaded behavior.

This also refactored the kuberentes token refresh such that the config passed into the interceptor is used to determine key details of the kubeconfig - what file it came from, and what the current context is. Without that, we were relying on the static / default logic.

What's not to like:

  • While the kubernetes logic clearly separates how username and password are used, the openshift logic does not.
  • Being able to override the username / password in the requestconfig complicates things, seems like we shouldn't allow that with Refine what is possible via withRequestConfig #4708. In particular I'm a little fuzzy about what it should mean to openshift when change the username, but don't change the current context.
  • Setting the token on the current Config seems a little hackish. It does partially address Share openshift tokens across openshift clients #4701. However we still aren't officially "sharing" tokens across different openshift clients if they are configured to use a username / password because there's no refreshing of the config in that case. A workaround is that if you pass an OpenShiftConfig to the KuberentesClientBuilder that will be used by all of the OpenShiftClients created by adapt calls. Alternatives could include refining the OAuthTokenProvider for our internal usage as well.

@shawkins
Copy link
Contributor Author

oc behavior:

https://github.com/openshift/oc/blob/a4cfdc79461db44d482b315e4e894f32d6c68ae2/pkg/cli/login/loginoptions.go#L197

  • if we have a token try that first.
  • if not and there's no password, then match the username against the kubeconfig (we are already doing this the first time we load the config)
  • otherwise use the password to obtain a token

After the config is updated, then it is saved here https://github.com/openshift/oc/blob/a4cfdc79461db44d482b315e4e894f32d6c68ae2/pkg/cli/login/login.go#L179

We seem to be reasonable mimicking this behavior.

The only additional corner case I can think of is that the existing code was somehow trying to account for the case of using the OpenShiftClient against a non-openshift server that was configured to use basic auth, in which case it was trying to do the basic auth first. However this does not seem to be supported by oc, so it should be acceptable to not do this.

Also oc does support a few other auth schemes - https://github.com/openshift/oc/blob/33f8aed30246e726258c51be27c21c60a2b005f4/pkg/helpers/tokencmd/request_token.go#L92 - but that is beyond the scope of this issue.

@shawkins
Copy link
Contributor Author

Test runs not withstanding this should be ready...

openshift logic now functions more like the kube logic and will persist
in the kubeconfig, update the current config, and use the config refresh
logic to check for updated tokens.
@sonarcloud
Copy link

sonarcloud bot commented Jan 17, 2023

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

48.2% 48.2% Coverage
0.0% 0.0% Duplication

Copy link
Member

@manusa manusa left a comment

Choose a reason for hiding this comment

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

LGTM, thx 🙌

The analysis here is 🔝 #4702 (comment)

@manusa manusa added this to the 6.5.0 milestone Jan 27, 2023
@manusa manusa merged commit 188bd37 into fabric8io:master Jan 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove unnecessary Basic Auth in Openshift requests.
2 participants