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

Add support to generate API token with tanzu api-token create #820

Merged
merged 2 commits into from
Oct 8, 2024

Conversation

anujc25
Copy link
Contributor

@anujc25 anujc25 commented Oct 3, 2024

What this PR does / why we need it

  • Add support to generate API token with the tanzu api-token create command
  • Add unit tests

Sample output:

> tz api-token create 
[i] Opening the browser window to complete the login 
Log in by visiting this link: https://wdc-10-206-98-55.eng.vmware.com/auth/oauth/authorize?client_id=tp_cli_app&code_challenge=XfCGpFSLGz5x_qYhXfsjYkXpfF6865RUh32wVd20-Ys&code_challe pe=code&state=1f5e2644c6e07bf7ff00cb5064bfabbe 

Optionally, paste your authorization code: [...] 

===

API Token Generation Successful! Your generated API token is: cle82767a13748debd53e33b2d46d92b-r

For non-interactive login use the API token as follows: TANZU_API_TOKEN=cle82767a13748debd53e33b2d46d92b-r tanzu login --endpoint https://wdc-10-206-98-55.eng.vmware.com

Please copy and save your token securely. Note that you will need to regenerate a new token before expiration time and login again to continue using the CLI.

Which issue(s) this PR fixes

Fixes #

Describe testing done for PR

  • Manually tests the UAA login workflow. See the above screenshot

Release note

Add support to generate API token with `tanzu api-token create` (supported for UAA as IDP)

Additional information

Special notes for your reviewer

@anujc25 anujc25 force-pushed the support-api-token-create branch from 3a0faa6 to 60a3bf0 Compare October 4, 2024 16:16
@anujc25 anujc25 changed the title Add support to generate API token with tz api-token create Add support to generate API token with tanzu api-token create Oct 4, 2024
@anujc25 anujc25 marked this pull request as ready for review October 4, 2024 21:02
@anujc25 anujc25 requested a review from a team as a code owner October 4, 2024 21:02
@anujc25 anujc25 force-pushed the support-api-token-create branch 3 times, most recently from a4d8c6d to 5bde1ca Compare October 5, 2024 22:37
Copy link
Contributor

@vuil vuil left a comment

Choose a reason for hiding this comment

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

Some comments about the output as I continue the review

pkg/command/apitoken.go Outdated Show resolved Hide resolved
pkg/command/apitoken.go Outdated Show resolved Hide resolved
pkg/command/apitoken.go Outdated Show resolved Hide resolved
pkg/command/apitoken.go Outdated Show resolved Hide resolved
Copy link
Contributor

@vuil vuil left a comment

Choose a reason for hiding this comment

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

looking good in general, thanks the for changes.
I have a few questions and suggestions.
And some UX output needs to be updated

pkg/utils/time/time.go Show resolved Hide resolved
pkg/command/apitoken.go Outdated Show resolved Hide resolved
pkg/command/apitoken_test.go Show resolved Hide resolved
@anujc25 anujc25 force-pushed the support-api-token-create branch 4 times, most recently from 7d63052 to 328a84c Compare October 7, 2024 16:42
pkg/command/apitoken.go Outdated Show resolved Hide resolved
pkg/command/apitoken.go Outdated Show resolved Hide resolved
pkg/command/apitoken.go Outdated Show resolved Hide resolved
pkg/command/apitoken.go Outdated Show resolved Hide resolved
pkg/command/apitoken.go Outdated Show resolved Hide resolved
pkg/command/apitoken.go Outdated Show resolved Hide resolved

fmt.Fprint(cmd.OutOrStdout(), bold.Sprint("==\n\n"))
fmt.Fprintf(cmd.OutOrStdout(), "%s Your generated API token is: %s\n\n", bold.Sprint("API Token Generation Successful!"), cyanBold.Sprint(token.RefreshToken))
fmt.Fprintf(cmd.OutOrStdout(), "For non-interactive login use the API token as follow: %s\n\n", cyanBold.Sprint("TANZU_API_TOKEN=<token> tanzu login --endpoint <tanzu-platform-endpoint>"))
Copy link
Contributor

Choose a reason for hiding this comment

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

This token can only be used for UAA, correct?
If so, instead of <tanzu-platform-endpoint>, should we put something with uaa in it?

Copy link
Contributor

Choose a reason for hiding this comment

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

That UAA is used is implementation details that may be lost on many users. In a comment earlier, I suggested using terminology that is more comprehensible. Still thinking of options...

Copy link
Contributor

Choose a reason for hiding this comment

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

You are right. Maybe "TPSM" is more appropriate, or the long form of it?

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe the way it is implemented is reasonable enough. Users who have to use the form "tanzu login --endpoint ..." and who has successfully use "api-token create" to get to this point should know what tanzu-platform-endpoint refers to.

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 have updated the log message to mention actual Tanzu Platform endpoint that needs to be used instead of <tanzu-platform-endpoint>. Let me know if any other changes are needed here.

pkg/command/apitoken.go Outdated Show resolved Hide resolved
Copy link
Contributor

@marckhouzam marckhouzam left a comment

Choose a reason for hiding this comment

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

Nice.
Minor suggestions to take or leave.

@anujc25 anujc25 force-pushed the support-api-token-create branch from f718d5d to 5ab4d76 Compare October 7, 2024 18:31
@anujc25
Copy link
Contributor Author

anujc25 commented Oct 7, 2024

Do we know the expiration time easily?
Since we tell the user regenerate a new token before expiration, I find it nice to tell them when that will be. If not, is there another way for them to avoid their usage failing when expiration happens?

@marckhouzam regarding this, I discussed with @vuil and the conclusion is we do not have a way to know the expiration time for the Refresh Token at the moment. So, if needed we need to document the expiration time separately in the docs.

@anujc25 anujc25 force-pushed the support-api-token-create branch from 5ab4d76 to f008087 Compare October 7, 2024 18:46
@marckhouzam
Copy link
Contributor

@marckhouzam regarding this, I discussed with @vuil and the conclusion is we do not have a way to know the expiration time for the Refresh Token at the moment. So, if needed we need to document the expiration time separately in the docs.

So we will document that the token has a lifetime of X time?
Do we prefer only putting it in the docs so we can more easily change it, instead of printing the value we currently know?

I assume there is no advanced notification for the user to know the token is about to expire?

@anujc25
Copy link
Contributor Author

anujc25 commented Oct 7, 2024

So we will document that the token has a lifetime of X time? Do we prefer only putting it in the docs so we can more easily change it, instead of printing the value we currently know?

Yes. We do not know the lifetime of this refresh token at the moment. And the time can be updated without CLI's knowledge from the backend so it is safe to put that information in the docs.

I assume there is no advanced notification for the user to know the token is about to expire?

Correct, there isn't any advanced notification for the user to know the token is about to expire.

Please note that this API token generation from the CLI is a temporary workaround for supporting the non- interactive login with UAA because UAA does not have any UI to generate this token. There will be updates and enhancement in this area in future that would likely make CLI more user friendly.

pkg/auth/uaa/tanzu.go Outdated Show resolved Hide resolved
@marckhouzam marckhouzam added this to the v1.5.1 milestone Oct 7, 2024
@anujc25 anujc25 force-pushed the support-api-token-create branch from ed7ae50 to e1aa16b Compare October 7, 2024 21:43
Copy link
Contributor

@vuil vuil left a comment

Choose a reason for hiding this comment

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

I think we may still want to tweak the messages but the rest lgtm, thanks!

pkg/command/apitoken.go Outdated Show resolved Hide resolved
bold := color.New(color.Bold)

fmt.Fprint(cmd.OutOrStdout(), bold.Sprint("==\n\n"))
fmt.Fprintf(cmd.OutOrStdout(), "%s Your generated API token is: %s\n\n", bold.Sprint("API Token Generation Successful!"), cyanBold.Sprint(token.RefreshToken))
Copy link
Contributor

Choose a reason for hiding this comment

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

We may have to workshop the err/output messages in a followup. Some thoughts

  • Is it better to output the token in stdout, and show the rest in stderr?
  • do we want to advocate TANZU_API_TOKEN=%s tanzu login, or mention the config file way to persist the env var?

The messages in these function

  • may confuse users who are interacting with the public endpoint, who ends up creating a valid tanzu endpoint and still get a surprise when this fails for him
  • the instructions for regenerate and re-login might be confusing to the user of the current CLI instance (who has obviously logged in)

Copy link
Contributor Author

@anujc25 anujc25 Oct 7, 2024

Choose a reason for hiding this comment

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

Is it better to output the token in stdout, and show the rest in stderr?

I would prefer using stdout for both the token and the rest of the output. Since this is an output message intended for the user, it's more conventional to keep all output in stdout.

@anujc25 anujc25 force-pushed the support-api-token-create branch from e1aa16b to 25baf0d Compare October 7, 2024 21:46
@anujc25 anujc25 force-pushed the support-api-token-create branch from 25baf0d to b3f007f Compare October 7, 2024 22:32
@anujc25 anujc25 merged commit e7a3e56 into vmware-tanzu:main Oct 8, 2024
7 checks passed
anujc25 added a commit that referenced this pull request Oct 10, 2024
* Add support to generate API token with `tanzu api-token create`

Signed-off-by: Anuj Chaudhari <[email protected]>
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.

3 participants