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 for interactive login on terminal based hosts(without browser) for tanzu context #645

Merged

Conversation

prkalle
Copy link
Contributor

@prkalle prkalle commented Jan 12, 2024

What this PR does / why we need it

This PR adds support for interactive login on terminal based hosts for tanzu context

Changes summary:

  • Add support for interactive login on terminal based hosts for tanzu context. CLI would prompt the authorization URL so that user can open the URL in the host having browser and then copy the authcode manually to complete the login.
  • User can choose the local listener port for callback URLs during OAuth authorization flow by setting the TANZU_CLI_OAUTH_LOCAL_LISTENER_PORT environment variable with port number user want to use on the local host.

Which issue(s) this PR fixes

Fixes #

Describe testing done for PR

Copied the tanzu binary to terminal based host and created tanzu context successfully( opened the auth URL posted on the terminal on the local machine browser and copied the auth code from the browser URL)

kubo@xGgQ2fHmfJ4uz:~$ export HOME=/tmp/Prem-CLIHome/
kubo@xGgQ2fHmfJ4uz:/home/kubo$ /tmp/tanzu-cli-linux_amd64 context create prem-ucp-intg-ctx5 --type tanzu --endpoint https://api.tanzu-dev.cloud.vmware.com --staging
[i] Opening the browser window to complete the login
[!] failed to open the browser for login:exec: "xdg-open,x-www-browser,www-browser": executable file not found in $PATH
Log in by visiting this link:

    https://console-stg.cloud.vmware.com/csp/gateway/discovery?client_id=tanzu-cli-client-id&code_challenge=G2cpk7jR0snIb1drJ9VX_4skPAss81ySQQ1LuiorFFc&code_challenge_method=S256&redirect_uri=http%3A%2F%2F127.0.0.1%3A40391%2Fcallback&response_type=code&state=c8f2497ed53e91def558490211a85d24

    Optionally, paste your authorization code: b2tRay1HN0RvNXFpWFc4Zlduc1RyRmZ0ZzRUTU8taHM6dXMtd2VzdC0y


[ok] Successfully logged into 'One Tanzu Integration' organization and created a tanzu context
[i] Checking for required plugins for context 'prem-ucp-intg-ctx5'...
[i] All required plugins are already installed and up-to-date

Tested copying invlaid auth code , and CLI errors ourt as expected.

kubo@xGgQ2fHmfJ4uz:/home/kubo$ /tmp/tanzu-cli-linux_amd64 context create prem-ucp-intg-ctx6 --type tanzu --endpoint https://api.tanzu-dev.cloud.vmware.com --staging
[i] Opening the browser window to complete the login
[!] failed to open the browser for login:exec: "xdg-open,x-www-browser,www-browser": executable file not found in $PATH
Log in by visiting this link:

    https://console-stg.cloud.vmware.com/csp/gateway/discovery?client_id=tanzu-cli-client-id&code_challenge=yROGaa0RWBgLzqTCvN7dtyonnnEVmH9EvsJjyL7NiQ8&code_challenge_method=S256&redirect_uri=http%3A%2F%2F127.0.0.1%3A46859%2Fcallback&response_type=code&state=873ccb739415f510a235c19e7f787489

    Optionally, paste your authorization code: b2tRay1HN0RvNXFpWFc4Zlduc1RyRmZ0ZzRUTU8taHM6dXMtd2VzdC0yInvalid
[i] failed to exchange auth code for oauth tokens, err=oauth2: cannot fetch token: 400 Bad Request
Response: {"requestId":"c2a540ca88556f42","errorCode":null,"statusCode":400,"traceId":"8c06b00497f68403","cspErrorCode":"540.324-340.800","metadata":null,"moduleCode":540,"message":"invalid_grant: Invalid authorization code"}

[x] : failed to get the token from CSP: token issuer https://console-stg.cloud.vmware.com/csp/gateway/am/api did not return expected tokens

Verified the kubectl triggers the interactive login and allows user to copy the auth code, and once the auth code is pasted the login flow was successful.

kubo@xGgQ2fHmfJ4uz:/home/kubo$ kubectl get projects
[i] Opening the browser window to complete the login
[!] failed to open the browser for login:exec: "xdg-open,x-www-browser,www-browser": executable file not found in $PATH
Log in by visiting this link:

    https://console-stg.cloud.vmware.com/csp/gateway/discovery?client_id=tanzu-cli-client-id&code_challenge=PLG3eZM6MfByUJnucuZDvHAEM32tRPTFt0IKlOPVT8M&code_challenge_method=S256&orgId=b1d48027-bb69-4a56-a5b8-e941ef29fa4b&redirect_uri=http%3A%2F%2F127.0.0.1%3A44705%2Fcallback&response_type=code&state=23bf9d874597ccbf11ae01b80d494a72

    Optionally, paste your authorization code: dmFNR1JLMWR0cmc4QmpIWUdvbmpVU09FY0EyR1YxbEI6dXMtd2VzdC0y

NAME
abhisheks-project
adib-project
aditya-project
...
yuva-project
kubo@xGgQ2fHmfJ4uz:/home/kubo$

create a tanzu context on my mac (with browser) without setting the environment variable for local host listener port and it was successful( CLI opens a random port)

❯ ./bin/tanzu context create prem-ucp-intg-ctx6 --type tanzu --endpoint https://api.tanzu-dev.cloud.vmware.com --staging
[i] Opening the browser window to complete the login
Log in by visiting this link:

    https://console-stg.cloud.vmware.com/csp/gateway/discovery?client_id=tanzu-cli-client-id&code_challenge=tRabEloiPE3bj65VM9Q-rQE3Yy-PfIHqXNEybwkTGgc&code_challenge_method=S256&redirect_uri=http%3A%2F%2F127.0.0.1%3A54215%2Fcallback&response_type=code&state=ccf5af586459e0b4ddb5e27923346cb7

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


[ok] Successfully logged into 'One Tanzu Integration' organization and created a tanzu context
[i] Checking for required plugins for context 'prem-ucp-intg-ctx6'...
[i] All required plugins are already installed and up-to-date

created tanzu context successfully with local listener port set using the environment variable TANZU_CLI_OAUTH_LOCAL_LISTENER_PORT with 8645

❯ export TANZU_CLI_OAUTH_LOCAL_LISTENER_PORT=8645
❯ ./bin/tanzu context create prem-ucp-intg-ctx6 --type tanzu --endpoint https://api.tanzu-dev.cloud.vmware.com --staging
[i] Opening the browser window to complete the login
Log in by visiting this link:

    https://console-stg.cloud.vmware.com/csp/gateway/discovery?client_id=tanzu-cli-client-id&code_challenge=FjTuhkeY1bN4AgHWUPD7L06FGefFgDDdQWCrK7ptCcM&code_challenge_method=S256&redirect_uri=http%3A%2F%2F127.0.0.1%3A8645%2Fcallback&response_type=code&state=c7cb77f60a0c4953f3253950e62e0fd5

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

        context: tanzu-cli-one-tanzu-demo-ctx2

[ok] Successfully logged into 'One Tanzu Integration' organization and created a tanzu context
[i] Checking for required plugins for context 'prem-ucp-intg-ctx6'...
[i] All required plugins are already installed and up-to-date

Tested with invalid port and CLI errors out as expected.

❯ export TANZU_CLI_OAUTH_LOCAL_LISTENER_PORT=8645777
❯ ./bin/tanzu context create prem-ucp-intg-ctx6 --type tanzu --endpoint https://api.tanzu-dev.cloud.vmware.com --staging
[x] : failed to get the token from CSP: failed to parse TANZU_CLI_OAUTH_LOCAL_LISTENER_PORT as uint16: strconv.ParseUint: parsing "8645777": value out of range

Release note

Add support for interactive login on terminal based hosts for creating 'tanzu' context. User can choose the local listener port for callback URL during OAuth authorization flow by setting the TANZU_CLI_OAUTH_LOCAL_LISTENER_PORT environment variable.

Additional information

Special notes for your reviewer

@prkalle prkalle requested a review from a team as a code owner January 12, 2024 16:53
@prkalle prkalle force-pushed the feature/intc_tanzu_login_terminalhost branch 2 times, most recently from d21b40b to 674f3aa Compare January 12, 2024 19:21
@prkalle prkalle added the documentation Improvements or additions to documentation label Jan 12, 2024
@prkalle prkalle force-pushed the feature/intc_tanzu_login_terminalhost branch from 674f3aa to 9319ab2 Compare January 12, 2024 21:38
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.

Nice set of changes!
Have a few nits on naming consistency stuff.
Also the release notes has a typo. You should also mention the env var introduced there as well.

I suggest we add a section in our markdown docs to describe the interactive login flow, in which the env var can also be mentioned. With the section available, it would be a good idea in the context create --help output to reference said section.

pkg/auth/csp/tanzu.go Outdated Show resolved Hide resolved
pkg/auth/csp/tanzu.go Outdated Show resolved Hide resolved
@prkalle prkalle force-pushed the feature/intc_tanzu_login_terminalhost branch from 9319ab2 to f88c5ac Compare January 19, 2024 17:46
…wser) for tanzu context

- Add support for interactive login on terminal based hosts for tanzu context. CLI would prompt the authorization URL so that user can open the URL in the host having browser and then copy the authcode manually to complete the login.
- User can choose the local listener port for callback URLs during OAuth authorization flow by setting the TANZU_CLI_OAUTH_LOCAL_LISTENER_PORT environment variable with port number user want to use on the local host.

Signed-off-by: Prem Kumar Kalle <pkalle@vmware.com>
@prkalle prkalle force-pushed the feature/intc_tanzu_login_terminalhost branch from f88c5ac to 20cd318 Compare January 19, 2024 18:37
@prkalle
Copy link
Contributor Author

prkalle commented Jan 19, 2024

I suggest we add a section in our markdown docs to describe the interactive login flow, in which the env var can also be mentioned. With the section available, it would be a good idea in the context create --help output to reference said section.

Thanks @vuil . I will add the markdown docs in a follow up PR.

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.

lgtm, thanks for the updates and a nice set of changes!

@prkalle prkalle merged commit 4d846ee into vmware-tanzu:main Jan 19, 2024
7 checks passed
@marckhouzam marckhouzam added the kind/documentation Categorizes issue or PR as related to documentation label Jan 22, 2024
@marckhouzam marckhouzam added this to the v1.2.0 milestone Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-not-required documentation Improvements or additions to documentation kind/documentation Categorizes issue or PR as related to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants