Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Sync Run API Token Type #109

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions auth/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/url"
"os"
"strings"
"time"

"github.com/cloudquery/cloudquery-api-go/config"
Expand All @@ -18,6 +19,7 @@ const (
EnvVarCloudQueryAPIKey = "CLOUDQUERY_API_KEY"
ExpiryBuffer = 60 * time.Second
tokenFilePath = "cloudquery/token"
syncRunAPIKeyPrefix = "cqsr_"
)

type tokenResponse struct {
Expand All @@ -36,6 +38,7 @@ const (
Undefined TokenType = iota
BearerToken
APIKey
SyncRunAPIKey
)

var UndefinedToken = Token{Type: Undefined, Value: ""}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions auth/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Loading