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

Allow to specify a tanzu endpoint with no scheme (avoid panic) #811

Merged
merged 1 commit into from
Sep 13, 2024

Conversation

marckhouzam
Copy link
Contributor

What this PR does / why we need it

Creating a context through tanzu login or tanzu create for Tanzu Platform would fail if the scheme was not specified. For tanzu login it would even panic.
Running tanzu login --endpoint api.tanzu.cloud.vmware.com will panic, while running tanzu context create test --endpoint api.tanzu.cloud.vmware.com will attempt to create a TMC context.

This PR adds the https:// prefix before checking if the URL is a Tanzu Platform URL.

Which issue(s) this PR fixes

Fixes #

Describe testing done for PR

Before the PR

$ tz login --endpoint api.tanzu.cloud.vmware.com
panic: interface conversion: interface {} is nil, not string

goroutine 1 [running]:
github.com/vmware-tanzu/tanzu-cli/pkg/command.globalTanzuLoginUAA(0x14001da3e00, 0x14001bc2900?)
	/Users/kmarc/git/tanzu-cli/pkg/command/context.go:653 +0x248
github.com/vmware-tanzu/tanzu-cli/pkg/command.globalTanzuLogin(0x14001da3e00, 0xc8628c716f88aa8e?)
	/Users/kmarc/git/tanzu-cli/pkg/command/context.go:647 +0xe0
github.com/vmware-tanzu/tanzu-cli/pkg/command.login(0x14000af8300?, {0x140005a0f80?, 0x0?, 0x2?})
	/Users/kmarc/git/tanzu-cli/pkg/command/login.go:99 +0xc4
github.com/spf13/cobra.(*Command).execute(0x14000af8300, {0x140005a0f60, 0x2, 0x2})
	/Users/kmarc/go/pkg/mod/github.com/spf13/[email protected]/command.go:983 +0x6d8
github.com/spf13/cobra.(*Command).ExecuteC(0x140009d6000)
	/Users/kmarc/go/pkg/mod/github.com/spf13/[email protected]/command.go:1115 +0x348
github.com/spf13/cobra.(*Command).Execute(0x1400084deb8?)
	/Users/kmarc/go/pkg/mod/github.com/spf13/[email protected]/command.go:1039 +0x1c
github.com/vmware-tanzu/tanzu-cli/pkg/command.Execute()
	/Users/kmarc/git/tanzu-cli/pkg/command/root.go:823 +0x24
main.main()
	/Users/kmarc/git/tanzu-cli/cmd/tanzu/main.go:15 +0x1c

$ tz login --endpoint platform.tanzu.broadcom.com
panic: interface conversion: interface {} is nil, not string

goroutine 1 [running]:
github.com/vmware-tanzu/tanzu-cli/pkg/command.globalTanzuLoginUAA(0x14001d11e00, 0x14001b6e930?)
	/Users/kmarc/git/tanzu-cli/pkg/command/context.go:653 +0x248
github.com/vmware-tanzu/tanzu-cli/pkg/command.globalTanzuLogin(0x14001d11e00, 0x6cef94a6690d4391?)
	/Users/kmarc/git/tanzu-cli/pkg/command/context.go:647 +0xe0
github.com/vmware-tanzu/tanzu-cli/pkg/command.login(0x140009e6900?, {0x1400027c240?, 0x0?, 0x2?})
	/Users/kmarc/git/tanzu-cli/pkg/command/login.go:99 +0xc4
github.com/spf13/cobra.(*Command).execute(0x140009e6900, {0x1400027c200, 0x2, 0x2})
	/Users/kmarc/go/pkg/mod/github.com/spf13/[email protected]/command.go:983 +0x6d8
github.com/spf13/cobra.(*Command).ExecuteC(0x14000a28000)
	/Users/kmarc/go/pkg/mod/github.com/spf13/[email protected]/command.go:1115 +0x348
github.com/spf13/cobra.(*Command).Execute(0x14000b51eb8?)
	/Users/kmarc/go/pkg/mod/github.com/spf13/[email protected]/command.go:1039 +0x1c
github.com/vmware-tanzu/tanzu-cli/pkg/command.Execute()
	/Users/kmarc/git/tanzu-cli/pkg/command/root.go:823 +0x24
main.main()
	/Users/kmarc/git/tanzu-cli/cmd/tanzu/main.go:15 +0x1c

# Notice that with context create, if the scheme is not specified
# for a Tanzu URL, the CLI was interpreting it as a TMC URL
$ tz context create --endpoint api.tanzu.cloud.vmware.com
? Give the context a name test

[i] The API key can be provided by setting the TANZU_API_TOKEN environment variable

[i] If you don't have an API token, visit the VMware Cloud Services console, select your organization, and create an API token with the TMC service roles:
  https://console.cloud.vmware.com/csp/gateway/portal/#/user/tokens

? API Token
[x] : interrupt

$ tz context create --endpoint platform.tanzu.broadcom.com
? Give the context a name test
[x] : the 'pinniped-auth' plugin is not installed. This plugin is required to authenticate with TKG/vSphere with Kubernetes(TKGs), please install the plugin and retry

After the PR:

$ tz login --endpoint api.tanzu.cloud.vmware.com
[i] Opening the browser window to complete the login
Log in by visiting this link:

    https://console.tanzu.broadcom.com/csp/gateway/discovery?client_id=tanzu-cli-client-id&code_challenge=PI2yOXeb-svqCPvQjnYfKUYn7HteOOfRGP2oh-I_aIA&code_challenge_method=S256&redirect_uri=http%3A%2F%2F127.0.0.1%3A57399%2Fcallback&response_type=code&state=c2a0c44f82ecd914cf0bb13b79c6adae

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


[ok] Successfully logged into 'Tanzu Platform for K8s SaaS - Internal 1' organization and created a tanzu context
$ tz context current --short
Tanzu_Platform_for_K8s_SaaS_-_Internal_1:mfine:tjwa2

$ tz login --endpoint platform.tanzu.broadcom.com
[i] Opening the browser window to complete the login
Log in by visiting this link:

    https://console.tanzu.broadcom.com/csp/gateway/discovery?client_id=tanzu-cli-client-id&code_challenge=QtgqCTxRTvQ_vlWPnZNBMYDufn43yyrtLG_PB7k0ItA&code_challenge_method=S256&redirect_uri=http%3A%2F%2F127.0.0.1%3A57426%2Fcallback&response_type=code&state=82bd0d302150195aefd1b34baeeb19cf

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


[ok] Successfully logged into 'Tanzu Platform for K8s SaaS - Internal 1' organization and created a tanzu context
$ tz context current --short
Tanzu_Platform_for_K8s_SaaS_-_Internal_1-2dfd3362

$ tz context create --endpoint api.tanzu.cloud.vmware.com
? Give the context a name test
[i] Opening the browser window to complete the login
Log in by visiting this link:

    https://console.tanzu.broadcom.com/csp/gateway/discovery?client_id=tanzu-cli-client-id&code_challenge=DHt5mTvnxbrgnQdQTF1bwMqN1wpaS6jb3MtY8ZZhq1o&code_challenge_method=S256&redirect_uri=http%3A%2F%2F127.0.0.1%3A57452%2Fcallback&response_type=code&state=a3970984f223ced278a9b73d9406629d

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


[ok] Successfully logged into 'Tanzu Platform for K8s SaaS - Internal 1' organization and created a tanzu context
$ tz context current --short
test

$ tz context create testing --endpoint platform.tanzu.broadcom.com
[i] Opening the browser window to complete the login
Log in by visiting this link:

    https://console.tanzu.broadcom.com/csp/gateway/discovery?client_id=tanzu-cli-client-id&code_challenge=Ise4Ak6Q7-J1vmLD2BktAmEglyzI64PpLYCIM1Ff_Mk&code_challenge_method=S256&redirect_uri=http%3A%2F%2F127.0.0.1%3A57481%2Fcallback&response_type=code&state=120d04cbd1238a8a87f4d4b4f9ab1868

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


[ok] Successfully logged into 'Tanzu Platform for K8s SaaS - Internal 1' organization and created a tanzu context
$ tz context current --short
testing

# Try with staging
$ tz login --endpoint api.tanzu-stable.cloud.vmware.com --staging
[i] Opening the browser window to complete the login
Log in by visiting this link:

    https://console-stg.tanzu.broadcom.com/csp/gateway/discovery?client_id=tanzu-cli-client-id&code_challenge=G8bKm5rpZliPtqCoTME3KzA_8RcbCvj3SXjJKBMcrM4&code_challenge_method=S256&redirect_uri=http%3A%2F%2F127.0.0.1%3A57511%2Fcallback&response_type=code&state=06c60882e69ffecc316e53a7ef05c040

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


[ok] Successfully logged into 'Falcons' organization and created a tanzu context
$ tz context current --short
Falcons-staging-2761fd6c

$ tz login --endpoint platform-verify.tanzu.broadcom.com --staging
[i] Opening the browser window to complete the login
Log in by visiting this link:

    https://console-stg.tanzu.broadcom.com/csp/gateway/discovery?client_id=tanzu-cli-client-id&code_challenge=sNpH2kgGxsd4OwgiDkX5vzBEsbwvD1lyWEFzz9bN4n8&code_challenge_method=S256&redirect_uri=http%3A%2F%2F127.0.0.1%3A57529%2Fcallback&response_type=code&state=b8c99149f239b993319b2f0c7f297136

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


[ok] Successfully logged into 'Falcons' organization and created a tanzu context
$ tz context current --short
Falcons-staging-3fb994fb

Release note

Allow to specify a tanzu endpoint with no scheme

Additional information

Special notes for your reviewer

I was unsure how to create a tanzu context for TPSM, so I still need to test that.

tanzu login --endpoint api.tanzu.cloud.vmware.com
tanzu context create --endpoint api.tanzu.cloud.vmware.com

Signed-off-by: Marc Khouzam <[email protected]>
@marckhouzam marckhouzam requested a review from a team as a code owner September 13, 2024 14:47
Copy link
Contributor

@anujc25 anujc25 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 fixing this @marckhouzam

Copy link
Contributor

@prkalle prkalle 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 @marckhouzam !

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.

thanks for the fix!

@marckhouzam marckhouzam merged commit 565f847 into vmware-tanzu:main Sep 13, 2024
8 checks passed
@marckhouzam marckhouzam deleted the fix/panic branch September 13, 2024 18:41
@marckhouzam marckhouzam added this to the v1.5.0 milestone Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants