From 0d8192a2861bf0c217efe6471a5ef973fbcb1291 Mon Sep 17 00:00:00 2001 From: Anuj Chaudhari Date: Thu, 12 Sep 2024 10:55:17 -0700 Subject: [PATCH] Fix incorrect context name being created when using new tanzu platform endpoints (#808) * Fix incorrect context name being created when using new endpoints * Fix cosign documentation link as it seems to be migrated to new location --- docs/full/README.md | 2 +- pkg/command/login.go | 2 +- pkg/command/login_test.go | 94 ++++++++++++++++++++++++++------------- 3 files changed, 64 insertions(+), 34 deletions(-) diff --git a/docs/full/README.md b/docs/full/README.md index 795f88c3d..3d7841fc0 100644 --- a/docs/full/README.md +++ b/docs/full/README.md @@ -214,7 +214,7 @@ from the repository. You can find more details in the ### User experience -CLI verifies [cosign](https://docs.sigstore.dev/signing/quickstart/) signature of +CLI verifies [cosign](https://docs.sigstore.dev/quickstart/quickstart-cosign/) signature of the plugin inventory image present in the repository. If the signature verification is successful, it would download the plugin inventory image on the user's machine and caches the verified plugin inventory image to improve the diff --git a/pkg/command/login.go b/pkg/command/login.go index 2bb895b9c..aa598aa88 100644 --- a/pkg/command/login.go +++ b/pkg/command/login.go @@ -141,7 +141,7 @@ func getString(data interface{}) string { // pre-req orgName and endpoint is non-empty string func prepareTanzuContextName(orgName, ucpEndpoint string, isStaging bool) string { var contextName string - idpType := getIDPType(ucpEndpoint) + idpType := getIDPType(endpoint) if idpType == config.UAAIdpType { contextName = "tpsm" } else { diff --git a/pkg/command/login_test.go b/pkg/command/login_test.go index 56127e0cf..3b2544d51 100644 --- a/pkg/command/login_test.go +++ b/pkg/command/login_test.go @@ -11,61 +11,91 @@ import ( func TestPrepareTanzuContextName(t *testing.T) { testCases := []struct { - orgName string - endpoint string - isStaging bool - forceCSP bool - expected string + orgName string + ucpEndpoint string + endpoint string + saasEndpoints []string + isStaging bool + forceCSP bool + expected string }{ // Test case for normal input with no staging environment and default endpoint. { - orgName: "MyOrg", - endpoint: centralconfig.DefaultTanzuPlatformEndpoint, - isStaging: false, - expected: "MyOrg", + orgName: "MyOrg", + ucpEndpoint: centralconfig.DefaultTanzuPlatformEndpoint, + endpoint: centralconfig.DefaultTanzuPlatformEndpoint, + saasEndpoints: []string{centralconfig.DefaultTanzuPlatformEndpoint}, + isStaging: false, + expected: "MyOrg", }, // Test case for normal input with staging environment and default endpoint. { - orgName: "MyOrg", - endpoint: centralconfig.DefaultTanzuPlatformEndpoint, - isStaging: true, - expected: "MyOrg-staging", + orgName: "MyOrg", + ucpEndpoint: centralconfig.DefaultTanzuPlatformEndpoint, + endpoint: centralconfig.DefaultTanzuPlatformEndpoint, + saasEndpoints: []string{centralconfig.DefaultTanzuPlatformEndpoint}, + isStaging: true, + expected: "MyOrg-staging", }, - // Test case for normal input with no staging environment and custom SaaS endpoint. + // Test case for normal input with no staging environment and custom endpoint with force CSP. { - orgName: "MyOrg", - endpoint: "https://custom-endpoint.com", - isStaging: false, - expected: "MyOrg-86fd8133", - forceCSP: true, + orgName: "MyOrg", + ucpEndpoint: "https://ucp.custom-endpoint.com", + endpoint: "https://custom-endpoint.com", + saasEndpoints: []string{centralconfig.DefaultTanzuPlatformEndpoint}, + isStaging: false, + expected: "MyOrg-70217fc3", + forceCSP: true, }, - // Test case for normal input with staging environment and custom SaaS endpoint. + // Test case for normal input with staging environment and custom endpoint with force CSP. { - orgName: "MyOrg", - endpoint: "https://custom-endpoint.com", - isStaging: true, - expected: "MyOrg-staging-86fd8133", - forceCSP: true, + orgName: "MyOrg", + ucpEndpoint: "https://ucp.custom-endpoint.com", + endpoint: "https://custom-endpoint.com", + saasEndpoints: []string{centralconfig.DefaultTanzuPlatformEndpoint}, + isStaging: true, + expected: "MyOrg-staging-70217fc3", + forceCSP: true, + }, + // Test case for normal input with new ucpEndpoint and platform endpoints which are actually SaaS endpoints. + { + orgName: "MyOrg", + ucpEndpoint: "https://ucp.platform-dev.endpoint.com", + endpoint: "https://platform-dev.endpoint.com", + saasEndpoints: []string{"https://(www.)?platform(.)*.endpoint.com"}, + expected: "MyOrg-9dfa8f6c", + }, + // Test case for normal input with new ucpEndpoint and platform endpoints which are actually SaaS endpoints. + { + orgName: "MyOrg", + isStaging: true, + ucpEndpoint: "https://ucp.platform.endpoint.com", + endpoint: "https://platform.endpoint.com", + saasEndpoints: []string{"https://(www.)?platform(.)*.endpoint.com"}, + expected: "MyOrg-staging-042532db", }, // Test case for normal input custom SM endpoint. { // org and staging values are effectively ignored - orgName: "MyOrg", - isStaging: true, - endpoint: "https://custom-endpoint.com", - expected: "tpsm-86fd8133", + orgName: "MyOrg", + isStaging: true, + ucpEndpoint: "https://ucp.custom-endpoint.com", + endpoint: "https://custom-endpoint.com", + saasEndpoints: []string{centralconfig.DefaultTanzuPlatformEndpoint}, + expected: "tpsm-70217fc3", }, } for _, tc := range testCases { + endpoint = tc.endpoint forceCSP = tc.forceCSP fakeDefaultCentralConfigReader := fakes.CentralConfig{} - fakeDefaultCentralConfigReader.GetTanzuPlatformSaaSEndpointListReturns([]string{centralconfig.DefaultTanzuPlatformEndpoint}) + fakeDefaultCentralConfigReader.GetTanzuPlatformSaaSEndpointListReturns(tc.saasEndpoints) centralconfig.DefaultCentralConfigReader = &fakeDefaultCentralConfigReader - actual := prepareTanzuContextName(tc.orgName, tc.endpoint, tc.isStaging) + actual := prepareTanzuContextName(tc.orgName, tc.ucpEndpoint, tc.isStaging) if actual != tc.expected { - t.Errorf("orgName: %s, endpoint: %s, isStaging: %t - expected: %s, got: %s", tc.orgName, tc.endpoint, tc.isStaging, tc.expected, actual) + t.Errorf("orgName: %s, ucpEndpoint: %s endpoint: %s, isStaging: %t - expected: %s, got: %s", tc.orgName, tc.ucpEndpoint, tc.endpoint, tc.isStaging, tc.expected, actual) } } }