Skip to content

Commit

Permalink
Experimental: Set the Tanzu Hub endpoint when creating tanzu context (
Browse files Browse the repository at this point in the history
#734)

* Fetch Tanzu Hub endpoint from CSP when creating the `tanzu` context
  • Loading branch information
anujc25 authored and vuil committed May 1, 2024
1 parent 66dc2e1 commit 5e73d1e
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 20 deletions.
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/google/gnostic v0.6.9
github.com/google/go-containerregistry v0.15.2
github.com/google/uuid v1.3.0
github.com/google/uuid v1.6.0
github.com/gorilla/mux v1.8.0
github.com/imdario/mergo v0.3.13
github.com/juju/fslock v0.0.0-20160525022230-4d5c94c67b4b
Expand All @@ -35,18 +35,18 @@ require (
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.3
github.com/stretchr/testify v1.8.4
github.com/tj/assert v0.0.3
github.com/verybluebot/tarinator-go v0.0.0-20190613183509-5ab4e1193986
github.com/vmware-tanzu/carvel-imgpkg v0.36.1
github.com/vmware-tanzu/carvel-ytt v0.40.0
github.com/vmware-tanzu/tanzu-cli/test/e2e/framework v0.0.0-00010101000000-000000000000
github.com/vmware-tanzu/tanzu-framework/capabilities/client v0.0.0-20230523145612-1c6fbba34686
github.com/vmware-tanzu/tanzu-plugin-runtime v1.3.0-dev.0.20240426182051-2590b16de5ae
github.com/vmware-tanzu/tanzu-plugin-runtime v1.3.0-dev.0.20240501223830-ad724a20da91
go.pinniped.dev v0.20.0
golang.org/x/mod v0.12.0
golang.org/x/mod v0.15.0
golang.org/x/oauth2 v0.8.0
golang.org/x/sync v0.3.0
golang.org/x/sync v0.6.0
golang.org/x/term v0.18.0
google.golang.org/grpc v1.56.3
gopkg.in/yaml.v2 v2.4.0
Expand Down Expand Up @@ -221,7 +221,7 @@ require (
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.12.0 // indirect
golang.org/x/tools v0.18.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
Expand Down
24 changes: 12 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,8 @@ github.com/google/trillian v1.5.2 h1:roGP6G8aaAch7vP08+oitPkvmZzxjTfIkguozqJ04Ok
github.com/google/trillian v1.5.2/go.mod h1:H8vOoa2dxd3xCdMzOOwt9kIz/3MSoJhcqLJGG8iRwbg=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/enterprise-certificate-proxy v0.0.0-20220520183353-fd19c99a87aa/go.mod h1:17drOmN3MwGY7t0e+Ei9b45FFGA3fBs3x36SsCg1hq8=
github.com/googleapis/enterprise-certificate-proxy v0.2.3 h1:yk9/cqRKtT9wXZSsRH9aurXEpJX+U6FLtpYTdC3R06k=
github.com/googleapis/enterprise-certificate-proxy v0.2.3/go.mod h1:AwSRAtLfXpU5Nm3pW+v7rGDHp09LsPtGY9MduiEsR9k=
Expand Down Expand Up @@ -696,8 +696,8 @@ github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1F
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8=
github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d h1:vfofYNRScrDdvS342BElfbETmL1Aiz3i2t0zfRj16Hs=
Expand Down Expand Up @@ -738,8 +738,8 @@ github.com/vmware-tanzu/tanzu-framework/apis/run v0.0.0-20230419030809-7081502eb
github.com/vmware-tanzu/tanzu-framework/apis/run v0.0.0-20230419030809-7081502ebf68/go.mod h1:e1Uef+Ux5BIHpYwqbeP2ZZmOzehBcez2vUEWXHe+xHE=
github.com/vmware-tanzu/tanzu-framework/capabilities/client v0.0.0-20230523145612-1c6fbba34686 h1:VcuXqUXFxm5WDqWkzAlU/6cJXua0ozELnqD59fy7J6E=
github.com/vmware-tanzu/tanzu-framework/capabilities/client v0.0.0-20230523145612-1c6fbba34686/go.mod h1:AFGOXZD4tH+KhpmtV0VjWjllXhr8y57MvOsIxTtywc4=
github.com/vmware-tanzu/tanzu-plugin-runtime v1.3.0-dev.0.20240426182051-2590b16de5ae h1:/queEiLNkAA2NdeNfAaBTnJTZ7x7bsn1txV2B+90GK8=
github.com/vmware-tanzu/tanzu-plugin-runtime v1.3.0-dev.0.20240426182051-2590b16de5ae/go.mod h1:5m73y796B4EoeXZtvkq8jbQPPQXeYkLPLS2BBbxZp7o=
github.com/vmware-tanzu/tanzu-plugin-runtime v1.3.0-dev.0.20240501223830-ad724a20da91 h1:JzjL1CKFEwa+WhpyGr8U7J3CEVDriFDTAFbp/QK67HY=
github.com/vmware-tanzu/tanzu-plugin-runtime v1.3.0-dev.0.20240501223830-ad724a20da91/go.mod h1:FtRiwn4qIsx4yQUbKqcv33Sx5x1c+Cil30/K/3FJ4co=
github.com/xanzy/go-gitlab v0.83.0 h1:37p0MpTPNbsTMKX/JnmJtY8Ch1sFiJzVF342+RvZEGw=
github.com/xanzy/go-gitlab v0.83.0/go.mod h1:5ryv+MnpZStBH8I/77HuQBsMbBGANtVpLWC15qOjWAw=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
Expand Down Expand Up @@ -815,8 +815,8 @@ golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8=
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -877,8 +877,8 @@ golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down Expand Up @@ -982,8 +982,8 @@ golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4f
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss=
golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM=
golang.org/x/tools v0.18.0 h1:k8NLag8AGHnn+PHbl7g43CtqZAwG60vZkLqgyZgIHgQ=
golang.org/x/tools v0.18.0/go.mod h1:GL7B4CwcLLeo59yx/9UWWuNOW1n3VZ4f5axWfML7Lcg=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
59 changes: 59 additions & 0 deletions pkg/auth/csp/tanzu.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package csp

import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -427,3 +428,61 @@ func GetOrgNameFromOrgID(orgID, accessToken, issuer string) (string, error) {

return org.Name, nil
}

// GetTanzuHubEndpointForTAP retrieves Tanzu Hub Endpoint For TAP SaaS through the CSP API
func GetTanzuHubEndpointForTAP(orgID, accessToken string, useStagingIssuer bool) (string, error) {
// CSPServiceURLs stores the CSP service URL information
type CSPServiceURLs struct {
ServiceHome string `json:"serviceHome"`
}

// CSPService stores the CSP service details
type CSPService struct {
DisplayName string `json:"displayName"`
ProductIdentifier string `json:"productIdentifier"`
ServiceDefinitionID string `json:"serviceDefinitionId"`
ServiceUrls CSPServiceURLs `json:"serviceUrls"`
}

// CSPServices stores the CSP services list
type CSPServices struct {
ServicesList []CSPService `json:"servicesList"`
}

endpoint := "https://console.cloud.vmware.com"
if useStagingIssuer {
endpoint = "https://console-stg.cloud.vmware.com"
}
api := fmt.Sprintf("%s/csp/gateway/slc/api/v2/ui/definitions/?orgId=%s", endpoint, orgID)

data := url.Values{}
req, _ := http.NewRequestWithContext(context.Background(), "GET", api, bytes.NewBufferString(data.Encode()))
req.Header.Set("authorization", "Bearer "+accessToken)

resp, err := httpRestClient.Do(req)
if err != nil {
return "", errors.WithMessage(err, "Failed to obtain available services for the specified organization")
}
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return "", errors.Errorf("Failed to obtain available services for the specified organization. %s", string(body))
}

defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
services := CSPServices{}

if err = json.Unmarshal(body, &services); err != nil {
return "", errors.Wrap(err, "could not unmarshal the services response")
}

for _, s := range services.ServicesList {
if s.ProductIdentifier == "TANZU-SAAS" && strings.Contains(s.DisplayName, "Tanzu Application Platform") { // TODO: Can this be improved to use some unique id?
// Remove `www.` if present from the endpoint. Because when invoking directly through API it does not work
tanzuHubEndpoint := strings.Replace(s.ServiceUrls.ServiceHome, "www.", "", 1)
return tanzuHubEndpoint, nil
}
}

return "", errors.New("could not find 'Tanzu Application Platform' service associated with the specified organization")
}
13 changes: 11 additions & 2 deletions pkg/command/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,13 +652,20 @@ func globalTanzuLogin(c *configtypes.Context, generateContextNameFunc func(orgNa
if err != nil {
return err
}

// Fetch the Tanzu Hub endpoint for the Tanzu context as a best case effort
tanzuHubEndpoint, err := csp.GetTanzuHubEndpointForTAP(claims.OrgID, c.GlobalOpts.Auth.AccessToken, staging)
if err != nil {
log.V(7).Infof("unable to get Tanzu Hub endpoint. Error: %v", err.Error())
}

// update the context name using the context name generator
if generateContextNameFunc != nil {
c.Name = generateContextNameFunc(orgName, endpoint, staging)
}

// update the context metadata
if err := updateTanzuContextMetadata(c, claims.OrgID, orgName); err != nil {
if err := updateTanzuContextMetadata(c, claims.OrgID, orgName, tanzuHubEndpoint); err != nil {
return err
}

Expand Down Expand Up @@ -702,14 +709,15 @@ the Tanzu Application Platform service. Please ensure correct organization authe
}

// updateTanzuContextMetadata updates the context additional metadata
func updateTanzuContextMetadata(c *configtypes.Context, orgID, orgName string) error {
func updateTanzuContextMetadata(c *configtypes.Context, orgID, orgName, tanzuHubEndpoint string) error {
exists, err := config.ContextExists(c.Name)
if err != nil {
return err
}
if !exists {
c.AdditionalMetadata[config.OrgIDKey] = orgID
c.AdditionalMetadata[config.OrgNameKey] = orgName
c.AdditionalMetadata[config.TanzuHubEndpointKey] = tanzuHubEndpoint
return nil
}
// This is possible only for contexts created using "tanzu login" command because
Expand All @@ -721,6 +729,7 @@ func updateTanzuContextMetadata(c *configtypes.Context, orgID, orgName string) e
// If the context exists with the same name, honor the users current context additional metadata
// which includes the org/project/space details.
c.AdditionalMetadata = existingContext.AdditionalMetadata
c.AdditionalMetadata[config.TanzuHubEndpointKey] = tanzuHubEndpoint

return nil
}
Expand Down

0 comments on commit 5e73d1e

Please sign in to comment.