Skip to content

Commit

Permalink
[sc-11670] - add call to createclienttoken (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxlevinps authored Nov 15, 2023
1 parent cd022c1 commit 488c5c1
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 29 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ We use the following categories for changes:
## [Unreleased]

### Added

- added get client token command [#sc-11670]
### Changed

### Fixed
Expand Down
52 changes: 52 additions & 0 deletions cmd/generate_client_token.go
Original file line number Diff line number Diff line change
@@ -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)
}
54 changes: 30 additions & 24 deletions pkg/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package api

import (
"io"
"io/ioutil"
"net/http"
"net/url"

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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))
}
2 changes: 1 addition & 1 deletion pkg/api/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/api/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/auth/client_token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package auth

const (
GENERATE_CLIENT_TOKEN_API_KEY_ENDPOINT = "system/generate-client-token"
)

0 comments on commit 488c5c1

Please sign in to comment.