From 488c5c17b5de569201dceab13f8503a65710d6a6 Mon Sep 17 00:00:00 2001 From: Max Levin <86924752+maxlevinps@users.noreply.github.com> Date: Wed, 15 Nov 2023 10:49:04 +0200 Subject: [PATCH] [sc-11670] - add call to createclienttoken (#267) --- CHANGELOG.md | 2 +- cmd/generate_client_token.go | 52 ++++++++++++++++++++++++++++++++++ pkg/api/client.go | 54 ++++++++++++++++++++---------------- pkg/api/cluster.go | 2 +- pkg/api/tenant.go | 6 ++-- pkg/auth/client_token.go | 5 ++++ 6 files changed, 92 insertions(+), 29 deletions(-) create mode 100644 cmd/generate_client_token.go create mode 100644 pkg/auth/client_token.go diff --git a/CHANGELOG.md b/CHANGELOG.md index d495e81..ccbfd5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +17,7 @@ We use the following categories for changes: ## [Unreleased] ### Added - +- added get client token command [#sc-11670] ### Changed ### Fixed diff --git a/cmd/generate_client_token.go b/cmd/generate_client_token.go new file mode 100644 index 0000000..3cebdcf --- /dev/null +++ b/cmd/generate_client_token.go @@ -0,0 +1,52 @@ +package cmd + +import ( + "github.com/spf13/cobra" + "groundcover.com/pkg/api" + "groundcover.com/pkg/auth" + "groundcover.com/pkg/ui" +) + +var generateClientTokenCmd = &cobra.Command{ + Use: "generate-client-token", + Short: "Get Client Token for Grafana API", + RunE: func(cmd *cobra.Command, args []string) error { + var err error + + var tenant *api.TenantInfo + if tenant, err = fetchTenant(); err != nil { + return err + } + + var apiToken *auth.ApiKey + if apiToken, err = fetchClientToken(tenant); err != nil { + return err + } + + ui.QuietWriter.Println(apiToken.ApiKey) + + return nil + }, +} + +func fetchClientToken(tenant *api.TenantInfo) (*auth.ApiKey, error) { + var err error + + var auth0Token *auth.Auth0Token + if auth0Token, err = auth.LoadAuth0Token(); err != nil { + return nil, err + } + + apiClient := api.NewClient(auth0Token) + + var clientToken *auth.ApiKey + if clientToken, err = apiClient.GetOrCreateClientToken(tenant); err != nil { + return nil, err + } + + return clientToken, nil +} + +func init() { + AuthCmd.AddCommand(generateClientTokenCmd) +} diff --git a/pkg/api/client.go b/pkg/api/client.go index 08eefc3..f33cf52 100644 --- a/pkg/api/client.go +++ b/pkg/api/client.go @@ -2,7 +2,6 @@ package api import ( "io" - "io/ioutil" "net/http" "net/url" @@ -56,7 +55,7 @@ func (client *Client) ApiKey(tenantUUID string) (*auth.ApiKey, error) { } var request *http.Request - if request, err = http.NewRequest("POST", url.String(), nil); err != nil { + if request, err = http.NewRequest(http.MethodPost, url.String(), nil); err != nil { return nil, err } @@ -84,7 +83,7 @@ func (client *Client) ServiceAccountToken(tenantUUID string) (*auth.SAToken, err } var request *http.Request - if request, err = http.NewRequest("POST", url.String(), nil); err != nil { + if request, err = http.NewRequest(http.MethodPost, url.String(), nil); err != nil { return nil, err } @@ -103,36 +102,43 @@ func (client *Client) ServiceAccountToken(tenantUUID string) (*auth.SAToken, err return saToken, nil } -func (client *Client) JoinPath(endpoint string) (*url.URL, error) { - return client.baseUrl.Parse(endpoint) -} - -func (client *Client) do(request *http.Request) ([]byte, error) { +func (client *Client) GetOrCreateClientToken(tenant *TenantInfo) (*auth.ApiKey, error) { var err error - var response *http.Response - if response, err = client.httpClient.Do(request); err != nil { + var url *url.URL + if url, err = client.JoinPath(auth.GENERATE_CLIENT_TOKEN_API_KEY_ENDPOINT); err != nil { return nil, err } - defer response.Body.Close() - if response.StatusCode != http.StatusOK { - return nil, NewResponseError(response) + var request *http.Request + if request, err = http.NewRequest(http.MethodPost, url.String(), nil); err != nil { + return nil, err } - return ioutil.ReadAll(response.Body) -} + request.Header.Add(TenantUUIDHeader, tenant.UUID) -func (client *Client) get(endpoint string) ([]byte, error) { - var err error + var body []byte + if body, err = client.do(request); err != nil { + return nil, err + } - var url *url.URL - if url, err = client.JoinPath(endpoint); err != nil { + clientToken := &auth.ApiKey{} + if err = clientToken.ParseBody(body); err != nil { return nil, err } + return clientToken, nil +} + +func (client *Client) JoinPath(endpoint string) (*url.URL, error) { + return client.baseUrl.Parse(endpoint) +} + +func (client *Client) do(request *http.Request) ([]byte, error) { + var err error var response *http.Response - if response, err = client.httpClient.Get(url.String()); err != nil { + + if response, err = client.httpClient.Do(request); err != nil { return nil, err } defer response.Body.Close() @@ -141,10 +147,10 @@ func (client *Client) get(endpoint string) ([]byte, error) { return nil, NewResponseError(response) } - return ioutil.ReadAll(response.Body) + return io.ReadAll(io.Reader(response.Body)) } -func (client *Client) post(endpoint, contentType string, payload io.Reader) ([]byte, error) { +func (client *Client) get(endpoint string) ([]byte, error) { var err error var url *url.URL @@ -153,7 +159,7 @@ func (client *Client) post(endpoint, contentType string, payload io.Reader) ([]b } var response *http.Response - if response, err = client.httpClient.Post(url.String(), contentType, payload); err != nil { + if response, err = client.httpClient.Get(url.String()); err != nil { return nil, err } defer response.Body.Close() @@ -162,5 +168,5 @@ func (client *Client) post(endpoint, contentType string, payload io.Reader) ([]b return nil, NewResponseError(response) } - return ioutil.ReadAll(response.Body) + return io.ReadAll(io.Reader(response.Body)) } diff --git a/pkg/api/cluster.go b/pkg/api/cluster.go index c88513d..421350d 100644 --- a/pkg/api/cluster.go +++ b/pkg/api/cluster.go @@ -75,7 +75,7 @@ func (client *Client) ClusterList(tenantUUID string) ([]ClusterInfo, error) { } var request *http.Request - if request, err = http.NewRequest("GET", url.String(), nil); err != nil { + if request, err = http.NewRequest(http.MethodGet, url.String(), nil); err != nil { return nil, err } diff --git a/pkg/api/tenant.go b/pkg/api/tenant.go index 96aa16f..80ce971 100644 --- a/pkg/api/tenant.go +++ b/pkg/api/tenant.go @@ -5,8 +5,8 @@ import ( ) const ( - TenantUUIDHeader = "X-Tenant-UUID" - TENANT_LIST_ENDPOINT = "rbac/member/tenants" + TenantUUIDHeader = "X-Tenant-UUID" + TenantsListEndpoint = "rbac/member/tenants" ) type TenantListResponse struct { @@ -23,7 +23,7 @@ func (client *Client) TenantList() ([]*TenantInfo, error) { var err error var body []byte - if body, err = client.get(TENANT_LIST_ENDPOINT); err != nil { + if body, err = client.get(TenantsListEndpoint); err != nil { return nil, err } diff --git a/pkg/auth/client_token.go b/pkg/auth/client_token.go new file mode 100644 index 0000000..7c3c08d --- /dev/null +++ b/pkg/auth/client_token.go @@ -0,0 +1,5 @@ +package auth + +const ( + GENERATE_CLIENT_TOKEN_API_KEY_ENDPOINT = "system/generate-client-token" +)