From a610d0f9bf1ccdb27a925bd3803db7f44f45f407 Mon Sep 17 00:00:00 2001 From: erezrokah Date: Tue, 23 Jan 2024 12:16:36 +0000 Subject: [PATCH] feat: Add Sync Run API Token Type --- auth/token.go | 11 +++++++++-- auth/token_test.go | 8 ++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/auth/token.go b/auth/token.go index 4966427..2fd26b5 100644 --- a/auth/token.go +++ b/auth/token.go @@ -7,6 +7,7 @@ import ( "net/http" "net/url" "os" + "strings" "time" "github.com/cloudquery/cloudquery-api-go/config" @@ -18,6 +19,7 @@ const ( EnvVarCloudQueryAPIKey = "CLOUDQUERY_API_KEY" ExpiryBuffer = 60 * time.Second tokenFilePath = "cloudquery/token" + syncRunAPIKeyPrefix = "cqsr_" ) type tokenResponse struct { @@ -36,6 +38,7 @@ const ( Undefined TokenType = iota BearerToken APIKey + SyncRunAPIKey ) var UndefinedToken = Token{Type: Undefined, Value: ""} @@ -66,8 +69,9 @@ func NewTokenClient() *TokenClient { // GetToken returns the ID token // If CLOUDQUERY_API_KEY is set, it returns that value, otherwise it returns an ID token generated from the refresh token. func (tc *TokenClient) GetToken() (Token, error) { - if tc.GetTokenType() == APIKey { - return Token{Type: APIKey, Value: os.Getenv(EnvVarCloudQueryAPIKey)}, nil + tokenType := tc.GetTokenType() + if tokenType != BearerToken { + return Token{Type: tokenType, Value: os.Getenv(EnvVarCloudQueryAPIKey)}, nil } // If the token is not expired, return it @@ -104,6 +108,9 @@ func (tc *TokenClient) GetToken() (Token, error) { // GetTokenType returns the type of token that will be returned by GetToken func (tc *TokenClient) GetTokenType() TokenType { if token := os.Getenv(EnvVarCloudQueryAPIKey); token != "" { + if strings.HasPrefix(token, syncRunAPIKeyPrefix) { + return SyncRunAPIKey + } return APIKey } return BearerToken diff --git a/auth/token_test.go b/auth/token_test.go index dbc8e56..483d8cc 100644 --- a/auth/token_test.go +++ b/auth/token_test.go @@ -118,6 +118,14 @@ func TestTokenClient_APIKeyTokenType(t *testing.T) { assert.Equal(t, APIKey, tc.GetTokenType()) } +func TestTokenClient_SyncRunAPIKeyTokenType(t *testing.T) { + t.Setenv(EnvVarCloudQueryAPIKey, "cqsr_my_token") + + tc := NewTokenClient() + + assert.Equal(t, SyncRunAPIKey, tc.GetTokenType()) +} + func overrideEnvironmentVariable(t *testing.T, key, value string) func() { originalValue := os.Getenv(key) resetFn := func() {