kubernetes-client icon indicating copy to clipboard operation
kubernetes-client copied to clipboard

Alternative to oc whoami --show-token

Open Sparker0i opened this issue 2 years ago • 1 comments

When I login to my cluster using a username+password combo in the OC CLI tool, I am able to do oc whoami --show-token and it displays the OAuth Bearer token with which I am logged in and which is used by the tool to make requests to the cluster.

Using this Openshift-Client library v5, I write a code like this to login to the cluster:

val config = new OpenShiftConfigBuilder()
  .withMasterUrl(clusterUrl)
  .withTrustCerts(true)
  .withUsername(apiUser)
  .withPassword(apiPassword)
  .withNamespace(namespace)
  .build()

val _ocClient = new DefaultOpenShiftClient(config)

Is there a way we can programatically get access to the token being used just like the command used in the oc cli?

Sparker0i avatar Sep 01 '22 10:09 Sparker0i

Have you tried this:

client.getConfiguration().getOauthToken();
// Or
client.getConfiguration().getPassword();

Maybe the token is accessible in one of those.

manusa avatar Sep 20 '22 09:09 manusa

Sorry for the late response, getPassword works, but it just prints the same apiPassword that I had passed. Doing with getOauthToken() is returning null unfortunately.

But surprisingly I'm able to perform other operations on the cluster, so surprising how the oauthtoken is returning null..

EDIT: We tried this with another cluster and it worked. Thanks for the help. Closing this now.

Sparker0i avatar Oct 26 '22 09:10 Sparker0i

Sorry I've got to open this up again. So we have two kinds of clusters. One where we can login with a service account token directly and another one where we have to give a user name and password.

When we login to both kind of clusters using this library, we are able to perform operations. However when we try to run the getOauthToken() for the second cluster, it shows null, despite being able to perform other kind of operations with the cluster like listing pods in a namespace, creating jobs etc..

Sparker0i avatar Oct 26 '22 13:10 Sparker0i

This issue has been automatically marked as stale because it has not had any activity since 90 days. It will be closed if no further activity occurs within 7 days. Thank you for your contributions!

stale[bot] avatar Jan 24 '23 23:01 stale[bot]