From 609ccabe50e8ff17bf16d2cf7c130326b0aa172f Mon Sep 17 00:00:00 2001 From: filip Date: Thu, 31 Oct 2024 14:06:58 +0100 Subject: [PATCH 1/5] Change client<>auth dependency and add cached token use to client --- internal/auth/auth.go | 7 +++-- internal/auth/auth_test.go | 3 +-- internal/client/deb_client.go | 30 +++++++++++++++------ internal/client/deb_client_test.go | 24 ++++++++++++++--- internal/client/request.go | 18 +++++++++++++ internal/client/testdata/deb_client_mock.go | 9 +++++++ internal/cmd/auth/auth_test.go | 5 +--- internal/cmd/auth/login/login_test.go | 5 +--- internal/cmd/auth/logout/logout_test.go | 5 +--- internal/cmd/auth/token/token_test.go | 15 ++++++++--- internal/file/finder_test.go | 5 ++++ internal/upload/uploader_test.go | 5 ++++ internal/wire/cli_container.go | 2 +- internal/wire/cli_container_test.go | 2 ++ 14 files changed, 100 insertions(+), 35 deletions(-) diff --git a/internal/auth/auth.go b/internal/auth/auth.go index e0e3ea99..59d212ac 100644 --- a/internal/auth/auth.go +++ b/internal/auth/auth.go @@ -4,7 +4,6 @@ import ( "context" "strings" - "github.com/debricked/cli/internal/client" "github.com/golang-jwt/jwt" "github.com/zalando/go-keyring" "golang.org/x/oauth2" @@ -50,7 +49,7 @@ func (dsc DebrickedSecretClient) Delete(service string) error { return keyring.Delete(service, dsc.User) } -func NewDebrickedAuthenticator(client client.IDebClient) Authenticator { +func NewDebrickedAuthenticator(host string) Authenticator { return Authenticator{ SecretClient: DebrickedSecretClient{ User: "DebrickedCLI", @@ -59,8 +58,8 @@ func NewDebrickedAuthenticator(client client.IDebClient) Authenticator { ClientID: "01919462-7d6e-78e8-aa24-ba779213c90f", ClientSecret: "", Endpoint: oauth2.Endpoint{ - AuthURL: client.Host() + "/app/oauth/authorize", - TokenURL: client.Host() + "/app/oauth/token", + AuthURL: host + "/app/oauth/authorize", + TokenURL: host + "/app/oauth/token", }, RedirectURL: "http://localhost:9096/callback", Scopes: []string{"select", "profile", "basicRepo", "fullApi"}, diff --git a/internal/auth/auth_test.go b/internal/auth/auth_test.go index 53d9bdbb..f588b72f 100644 --- a/internal/auth/auth_test.go +++ b/internal/auth/auth_test.go @@ -5,7 +5,6 @@ import ( "testing" "github.com/debricked/cli/internal/auth/testdata" - clientTestdata "github.com/debricked/cli/internal/client/testdata" "github.com/stretchr/testify/assert" "github.com/zalando/go-keyring" "golang.org/x/oauth2" @@ -15,7 +14,7 @@ const windowsOS = "windows" const macOS = "darwin" func TestNewAuthenticator(t *testing.T) { - res := NewDebrickedAuthenticator(clientTestdata.NewDebClientMock()) + res := NewDebrickedAuthenticator("") assert.NotNil(t, res) } diff --git a/internal/client/deb_client.go b/internal/client/deb_client.go index dc98bbd7..15a21d6e 100644 --- a/internal/client/deb_client.go +++ b/internal/client/deb_client.go @@ -8,6 +8,8 @@ import ( "net/http" "os" + "github.com/debricked/cli/internal/auth" + "github.com/fatih/color" ) @@ -23,13 +25,15 @@ type IDebClient interface { SetAccessToken(accessToken *string) IsEnterpriseCustomer(silent bool) bool Host() string + Authenticator() auth.IAuthenticator } type DebClient struct { - host *string - httpClient IClient - accessToken *string - jwtToken string + host *string + httpClient IClient + accessToken *string + jwtToken string + authenticator auth.IAuthenticator } func NewDebClient(accessToken *string, httpClient IClient) *DebClient { @@ -37,12 +41,14 @@ func NewDebClient(accessToken *string, httpClient IClient) *DebClient { if len(host) == 0 { host = DefaultDebrickedUri } + authenticator := auth.NewDebrickedAuthenticator(host) return &DebClient{ - host: &host, - httpClient: httpClient, - accessToken: initAccessToken(accessToken), - jwtToken: "", + host: &host, + httpClient: httpClient, + accessToken: initAccessToken(accessToken), + jwtToken: "", + authenticator: authenticator, } } @@ -66,6 +72,10 @@ func (debClient *DebClient) SetAccessToken(accessToken *string) { debClient.accessToken = initAccessToken(accessToken) } +func (debClient *DebClient) Authenticator() auth.IAuthenticator { + return debClient.authenticator +} + func initAccessToken(accessToken *string) *string { if accessToken == nil { accessToken = new(string) @@ -74,6 +84,10 @@ func initAccessToken(accessToken *string) *string { *accessToken = os.Getenv("DEBRICKED_TOKEN") } + if len(*accessToken) == 0 { + return nil + } + return accessToken } diff --git a/internal/client/deb_client_test.go b/internal/client/deb_client_test.go index f3882026..aa4f0e5d 100644 --- a/internal/client/deb_client_test.go +++ b/internal/client/deb_client_test.go @@ -33,9 +33,6 @@ func TestNewDebClientWithNilToken(t *testing.T) { if *debClient.host != DefaultDebrickedUri { t.Error("failed to assert that host was set properly") } - if debClient.accessToken == nil { - t.Error("failed to assert that access token was set properly") - } } const debrickedTknEnvVar = "DEBRICKED_TOKEN" @@ -95,6 +92,16 @@ func TestClientUnauthorized(t *testing.T) { } } +func TestHost(t *testing.T) { + debClient := NewDebClient(&tkn, nil) + assert.Equal(t, *debClient.host, debClient.Host()) +} + +func TestAuthenticator(t *testing.T) { + debClient := NewDebClient(&tkn, nil) + assert.NotNil(t, debClient.Authenticator()) +} + func TestGet(t *testing.T) { clientMock := testdataClient.NewMock() clientMock.AddMockResponse(testdataClient.MockResponse{ @@ -172,7 +179,7 @@ func TestPostWithTimeout(t *testing.T) { } } -func TestAuthenticate(t *testing.T) { +func TestAuthenticateExplicitToken(t *testing.T) { tkn = "0501ac404fd1823d0d4c047f957637a912d3b94713ee32a6" jwtTkn := "jwt-tkn" clientMock := testdataClient.NewMock() @@ -191,6 +198,15 @@ func TestAuthenticate(t *testing.T) { } } +func TestAuthenticateCachedToken(t *testing.T) { + clientMock := testdataClient.NewMock() + client = NewDebClient(nil, clientMock) + err := client.authenticate() + if err != nil { + t.Fatal("failed to assert that no error occurred") + } +} + func TestSetAccessToken(t *testing.T) { debClient := NewDebClient(nil, testdataClient.NewMock()) debClient.accessToken = nil diff --git a/internal/client/request.go b/internal/client/request.go index d999d2d2..f87d0309 100644 --- a/internal/client/request.go +++ b/internal/client/request.go @@ -117,6 +117,24 @@ type errorMessage struct { } func (debClient *DebClient) authenticate() error { + + if debClient.accessToken != nil { + return debClient.authenticateExplicitToken() + } + + return debClient.authenticateCachedToken() +} + +func (debClient *DebClient) authenticateCachedToken() error { + token, err := debClient.authenticator.Token() + if err == nil { + debClient.jwtToken = token.AccessToken + } + + return err +} + +func (debClient *DebClient) authenticateExplicitToken() error { uri := "/api/login_refresh" data := map[string]string{"refresh_token": *debClient.accessToken} diff --git a/internal/client/testdata/deb_client_mock.go b/internal/client/testdata/deb_client_mock.go index 60feb184..362561dd 100644 --- a/internal/client/testdata/deb_client_mock.go +++ b/internal/client/testdata/deb_client_mock.go @@ -7,6 +7,8 @@ import ( "net/http" "net/url" + "github.com/debricked/cli/internal/auth" + authTestdata "github.com/debricked/cli/internal/auth/testdata" "github.com/debricked/cli/internal/client" ) @@ -146,3 +148,10 @@ func (mock *DebClientMock) IsEnterpriseCustomer(silent bool) bool { return mock.isEnterprise } + +func (mock *DebClientMock) Authenticator() auth.IAuthenticator { + return auth.Authenticator{ + SecretClient: authTestdata.MockInvalidSecretClient{}, + OAuthConfig: nil, + } +} diff --git a/internal/cmd/auth/auth_test.go b/internal/cmd/auth/auth_test.go index b10f08f6..5a79f7b1 100644 --- a/internal/cmd/auth/auth_test.go +++ b/internal/cmd/auth/auth_test.go @@ -4,14 +4,11 @@ import ( "testing" "github.com/debricked/cli/internal/auth" - "github.com/debricked/cli/internal/client" "github.com/stretchr/testify/assert" ) func TestNewAuthCmd(t *testing.T) { - token := "token" - deb_client := client.NewDebClient(&token, nil) - authenticator := auth.NewDebrickedAuthenticator(deb_client) + authenticator := auth.NewDebrickedAuthenticator("") cmd := NewAuthCmd(authenticator) commands := cmd.Commands() nbrOfCommands := 3 diff --git a/internal/cmd/auth/login/login_test.go b/internal/cmd/auth/login/login_test.go index bba8f0cc..913a8fa8 100644 --- a/internal/cmd/auth/login/login_test.go +++ b/internal/cmd/auth/login/login_test.go @@ -5,14 +5,11 @@ import ( "github.com/debricked/cli/internal/auth" "github.com/debricked/cli/internal/auth/testdata" - "github.com/debricked/cli/internal/client" "github.com/stretchr/testify/assert" ) func TestNewLoginCmd(t *testing.T) { - token := "token" - deb_client := client.NewDebClient(&token, nil) - authenticator := auth.NewDebrickedAuthenticator(deb_client) + authenticator := auth.NewDebrickedAuthenticator("") cmd := NewLoginCmd(authenticator) commands := cmd.Commands() nbrOfCommands := 0 diff --git a/internal/cmd/auth/logout/logout_test.go b/internal/cmd/auth/logout/logout_test.go index 5bb95f0e..5f89140d 100644 --- a/internal/cmd/auth/logout/logout_test.go +++ b/internal/cmd/auth/logout/logout_test.go @@ -5,14 +5,11 @@ import ( "github.com/debricked/cli/internal/auth" "github.com/debricked/cli/internal/auth/testdata" - "github.com/debricked/cli/internal/client" "github.com/stretchr/testify/assert" ) func TestNewLogoutCmd(t *testing.T) { - token := "token" - deb_client := client.NewDebClient(&token, nil) - authenticator := auth.NewDebrickedAuthenticator(deb_client) + authenticator := auth.NewDebrickedAuthenticator("") cmd := NewLogoutCmd(authenticator) commands := cmd.Commands() nbrOfCommands := 0 diff --git a/internal/cmd/auth/token/token_test.go b/internal/cmd/auth/token/token_test.go index b684ef1f..4371daf8 100644 --- a/internal/cmd/auth/token/token_test.go +++ b/internal/cmd/auth/token/token_test.go @@ -5,15 +5,12 @@ import ( "github.com/debricked/cli/internal/auth" "github.com/debricked/cli/internal/auth/testdata" - "github.com/debricked/cli/internal/client" "github.com/spf13/viper" "github.com/stretchr/testify/assert" ) func TestNewTokenCmd(t *testing.T) { - token := "token" - deb_client := client.NewDebClient(&token, nil) - authenticator := auth.NewDebrickedAuthenticator(deb_client) + authenticator := auth.NewDebrickedAuthenticator("") cmd := NewTokenCmd(authenticator) commands := cmd.Commands() nbrOfCommands := 0 @@ -57,6 +54,16 @@ func TestRunE(t *testing.T) { assert.NoError(t, err) } +func TestRunEJSONFlag(t *testing.T) { + a := testdata.MockAuthenticator{} + jsonFormat = true + runE := RunE(a) + + err := runE(nil, []string{}) + + assert.NoError(t, err) +} + func TestRunEError(t *testing.T) { a := testdata.ErrorMockAuthenticator{} runE := RunE(a) diff --git a/internal/file/finder_test.go b/internal/file/finder_test.go index 381e9d87..be92edda 100644 --- a/internal/file/finder_test.go +++ b/internal/file/finder_test.go @@ -13,6 +13,7 @@ import ( "strings" "testing" + "github.com/debricked/cli/internal/auth" "github.com/debricked/cli/internal/client/testdata" ioFs "github.com/debricked/cli/internal/io" "github.com/stretchr/testify/assert" @@ -68,6 +69,10 @@ func (mock *debClientMock) IsEnterpriseCustomer(silent bool) bool { return true } +func (mock *debClientMock) Authenticator() auth.IAuthenticator { + return nil +} + var finder *Finder func setUp(auth bool) { diff --git a/internal/upload/uploader_test.go b/internal/upload/uploader_test.go index 8a452773..c6c613e0 100644 --- a/internal/upload/uploader_test.go +++ b/internal/upload/uploader_test.go @@ -8,6 +8,7 @@ import ( "strings" "testing" + "github.com/debricked/cli/internal/auth" "github.com/debricked/cli/internal/client" "github.com/debricked/cli/internal/client/testdata" "github.com/debricked/cli/internal/file" @@ -159,3 +160,7 @@ func (mock *debClientMock) SetAccessToken(_ *string) {} func (mock *debClientMock) IsEnterpriseCustomer(silent bool) bool { return true } + +func (mock *debClientMock) Authenticator() auth.IAuthenticator { + return nil +} diff --git a/internal/wire/cli_container.go b/internal/wire/cli_container.go index fda5ad80..9e68094b 100644 --- a/internal/wire/cli_container.go +++ b/internal/wire/cli_container.go @@ -94,7 +94,7 @@ func (cc *CliContainer) wire() error { cc.licenseReporter = licenseReport.Reporter{DebClient: cc.debClient} cc.vulnerabilityReporter = vulnerabilityReport.Reporter{DebClient: cc.debClient} cc.sbomReporter = sbomReport.Reporter{DebClient: cc.debClient, FileWriter: io.FileWriter{}} - cc.authenticator = auth.NewDebrickedAuthenticator(cc.debClient) + cc.authenticator = cc.debClient.Authenticator() return nil } diff --git a/internal/wire/cli_container_test.go b/internal/wire/cli_container_test.go index a1d85ec2..08acef6e 100644 --- a/internal/wire/cli_container_test.go +++ b/internal/wire/cli_container_test.go @@ -40,4 +40,6 @@ func assertCliContainer(t *testing.T, cc *CliContainer) { assert.NotNil(t, cc.LicenseReporter()) assert.NotNil(t, cc.VulnerabilityReporter()) assert.NotNil(t, cc.Fingerprinter()) + assert.NotNil(t, cc.Authenticator()) + assert.NotNil(t, cc.SBOMReporter()) } From cee93b18707c2944dc1fc7c9ce9a3033455cd6bc Mon Sep 17 00:00:00 2001 From: filip Date: Fri, 1 Nov 2024 11:48:44 +0100 Subject: [PATCH 2/5] trying to fix env token --- internal/client/deb_client.go | 11 +++++------ internal/client/deb_client_test.go | 13 +++++++++---- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/internal/client/deb_client.go b/internal/client/deb_client.go index 15a21d6e..3e4004c0 100644 --- a/internal/client/deb_client.go +++ b/internal/client/deb_client.go @@ -81,13 +81,12 @@ func initAccessToken(accessToken *string) *string { accessToken = new(string) } if len(*accessToken) == 0 { - *accessToken = os.Getenv("DEBRICKED_TOKEN") + ok := false + *accessToken, ok = os.LookupEnv("DEBRICKED_TOKEN") + if !ok { + accessToken = nil + } } - - if len(*accessToken) == 0 { - return nil - } - return accessToken } diff --git a/internal/client/deb_client_test.go b/internal/client/deb_client_test.go index aa4f0e5d..37c2f078 100644 --- a/internal/client/deb_client_test.go +++ b/internal/client/deb_client_test.go @@ -39,17 +39,22 @@ const debrickedTknEnvVar = "DEBRICKED_TOKEN" func TestNewDebClientWithTokenEnvVariable(t *testing.T) { envVarTkn := "env-tkn" - oldEnvValue := os.Getenv(debrickedTknEnvVar) + oldEnvValue, ok := os.LookupEnv(debrickedTknEnvVar) err := os.Setenv(debrickedTknEnvVar, "env-tkn") if err != nil { t.Fatalf("failed to set env var %s", debrickedTknEnvVar) } - defer func(key, value string) { - err := os.Setenv(key, value) + defer func(key, value string, ok bool) { + var err error = nil + if ok { + err = os.Setenv(key, value) + } else { + err = os.Unsetenv(key) + } if err != nil { t.Fatalf("failed to reset env var %s", debrickedTknEnvVar) } - }(debrickedTknEnvVar, oldEnvValue) + }(debrickedTknEnvVar, oldEnvValue, ok) accessToken := "" debClient := NewDebClient(&accessToken, nil) From a3571d7c11d3c0230d6855fd44e8981f93d1374b Mon Sep 17 00:00:00 2001 From: filip Date: Fri, 1 Nov 2024 12:29:38 +0100 Subject: [PATCH 3/5] Mock cached credentials in debclient test --- internal/client/deb_client_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/internal/client/deb_client_test.go b/internal/client/deb_client_test.go index 37c2f078..45692891 100644 --- a/internal/client/deb_client_test.go +++ b/internal/client/deb_client_test.go @@ -10,6 +10,7 @@ import ( "strings" "testing" + testdataAuth "github.com/debricked/cli/internal/auth/testdata" testdataClient "github.com/debricked/cli/internal/client/testdata/client" "github.com/stretchr/testify/assert" ) @@ -206,6 +207,13 @@ func TestAuthenticateExplicitToken(t *testing.T) { func TestAuthenticateCachedToken(t *testing.T) { clientMock := testdataClient.NewMock() client = NewDebClient(nil, clientMock) + client = &DebClient{ + host: nil, + accessToken: nil, + httpClient: clientMock, + jwtToken: "", + authenticator: testdataAuth.MockAuthenticator{}, + } err := client.authenticate() if err != nil { t.Fatal("failed to assert that no error occurred") From 2506177587ad10a6510f0253f627eb09c55ac089 Mon Sep 17 00:00:00 2001 From: filip Date: Mon, 4 Nov 2024 13:58:27 +0100 Subject: [PATCH 4/5] fix test and set token --- internal/client/deb_client.go | 1 - internal/client/deb_client_test.go | 20 +++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/internal/client/deb_client.go b/internal/client/deb_client.go index 3e4004c0..d003eba3 100644 --- a/internal/client/deb_client.go +++ b/internal/client/deb_client.go @@ -42,7 +42,6 @@ func NewDebClient(accessToken *string, httpClient IClient) *DebClient { host = DefaultDebrickedUri } authenticator := auth.NewDebrickedAuthenticator(host) - return &DebClient{ host: &host, httpClient: httpClient, diff --git a/internal/client/deb_client_test.go b/internal/client/deb_client_test.go index 45692891..99e350ca 100644 --- a/internal/client/deb_client_test.go +++ b/internal/client/deb_client_test.go @@ -214,7 +214,25 @@ func TestAuthenticateCachedToken(t *testing.T) { jwtToken: "", authenticator: testdataAuth.MockAuthenticator{}, } - err := client.authenticate() + + oldEnvValue, ok := os.LookupEnv(debrickedTknEnvVar) + err := os.Unsetenv(debrickedTknEnvVar) + if err != nil { + t.Fatalf("failed to set env var %s", debrickedTknEnvVar) + } + defer func(key, value string, ok bool) { + var err error = nil + if ok { + err = os.Setenv(key, value) + } else { + err = os.Unsetenv(key) + } + if err != nil { + t.Fatalf("failed to reset env var %s", debrickedTknEnvVar) + } + }(debrickedTknEnvVar, oldEnvValue, ok) + + err = client.authenticate() if err != nil { t.Fatal("failed to assert that no error occurred") } From 2c66d3ea720c035149751302d84e96df209bfbdd Mon Sep 17 00:00:00 2001 From: filip Date: Mon, 4 Nov 2024 15:41:16 +0100 Subject: [PATCH 5/5] Let viper handle env variable for token --- internal/client/deb_client.go | 22 +++---------- internal/client/deb_client_test.go | 50 +----------------------------- internal/client/request.go | 7 +++-- internal/cmd/root/root.go | 9 ++++-- internal/cmd/root/root_test.go | 4 +-- 5 files changed, 17 insertions(+), 75 deletions(-) diff --git a/internal/client/deb_client.go b/internal/client/deb_client.go index d003eba3..a1714cef 100644 --- a/internal/client/deb_client.go +++ b/internal/client/deb_client.go @@ -41,13 +41,13 @@ func NewDebClient(accessToken *string, httpClient IClient) *DebClient { if len(host) == 0 { host = DefaultDebrickedUri } - authenticator := auth.NewDebrickedAuthenticator(host) + return &DebClient{ host: &host, httpClient: httpClient, - accessToken: initAccessToken(accessToken), + accessToken: accessToken, jwtToken: "", - authenticator: authenticator, + authenticator: auth.NewDebrickedAuthenticator(host), } } @@ -68,27 +68,13 @@ func (debClient *DebClient) Get(uri string, format string) (*http.Response, erro } func (debClient *DebClient) SetAccessToken(accessToken *string) { - debClient.accessToken = initAccessToken(accessToken) + debClient.accessToken = accessToken } func (debClient *DebClient) Authenticator() auth.IAuthenticator { return debClient.authenticator } -func initAccessToken(accessToken *string) *string { - if accessToken == nil { - accessToken = new(string) - } - if len(*accessToken) == 0 { - ok := false - *accessToken, ok = os.LookupEnv("DEBRICKED_TOKEN") - if !ok { - accessToken = nil - } - } - return accessToken -} - type BillingPlan struct { SCA string `json:"sca"` Select string `json:"select"` diff --git a/internal/client/deb_client_test.go b/internal/client/deb_client_test.go index 99e350ca..5097ee95 100644 --- a/internal/client/deb_client_test.go +++ b/internal/client/deb_client_test.go @@ -36,37 +36,6 @@ func TestNewDebClientWithNilToken(t *testing.T) { } } -const debrickedTknEnvVar = "DEBRICKED_TOKEN" - -func TestNewDebClientWithTokenEnvVariable(t *testing.T) { - envVarTkn := "env-tkn" - oldEnvValue, ok := os.LookupEnv(debrickedTknEnvVar) - err := os.Setenv(debrickedTknEnvVar, "env-tkn") - if err != nil { - t.Fatalf("failed to set env var %s", debrickedTknEnvVar) - } - defer func(key, value string, ok bool) { - var err error = nil - if ok { - err = os.Setenv(key, value) - } else { - err = os.Unsetenv(key) - } - if err != nil { - t.Fatalf("failed to reset env var %s", debrickedTknEnvVar) - } - }(debrickedTknEnvVar, oldEnvValue, ok) - - accessToken := "" - debClient := NewDebClient(&accessToken, nil) - if *debClient.host != DefaultDebrickedUri { - t.Error("failed to assert that host was set properly") - } - if *debClient.accessToken != envVarTkn { - t.Errorf("failed to assert that access token was set to %s. Got %s", envVarTkn, *debClient.accessToken) - } -} - func TestNewDebClientWithWithURI(t *testing.T) { accessToken := "" os.Setenv("DEBRICKED_URI", "https://subdomain.debricked.com") @@ -215,24 +184,7 @@ func TestAuthenticateCachedToken(t *testing.T) { authenticator: testdataAuth.MockAuthenticator{}, } - oldEnvValue, ok := os.LookupEnv(debrickedTknEnvVar) - err := os.Unsetenv(debrickedTknEnvVar) - if err != nil { - t.Fatalf("failed to set env var %s", debrickedTknEnvVar) - } - defer func(key, value string, ok bool) { - var err error = nil - if ok { - err = os.Setenv(key, value) - } else { - err = os.Unsetenv(key) - } - if err != nil { - t.Fatalf("failed to reset env var %s", debrickedTknEnvVar) - } - }(debrickedTknEnvVar, oldEnvValue, ok) - - err = client.authenticate() + err := client.authenticate() if err != nil { t.Fatal("failed to assert that no error occurred") } diff --git a/internal/client/request.go b/internal/client/request.go index f87d0309..d352377f 100644 --- a/internal/client/request.go +++ b/internal/client/request.go @@ -117,9 +117,10 @@ type errorMessage struct { } func (debClient *DebClient) authenticate() error { - - if debClient.accessToken != nil { - return debClient.authenticateExplicitToken() + if debClient.accessToken != nil { // To avoid segfault + if len(*debClient.accessToken) != 0 { + return debClient.authenticateExplicitToken() + } } return debClient.authenticateCachedToken() diff --git a/internal/cmd/root/root.go b/internal/cmd/root/root.go index 51a9c481..d51ad765 100644 --- a/internal/cmd/root/root.go +++ b/internal/cmd/root/root.go @@ -15,7 +15,8 @@ import ( var accessToken string -const AccessTokenFlag = "access-token" +const AccessTokenFlag = "token" +const OldAccessTokenFlag = "access-token" func NewRootCmd(version string, container *wire.CliContainer) *cobra.Command { rootCmd := &cobra.Command{ @@ -29,12 +30,14 @@ Complete documentation is available at https://docs.debricked.com/tools-and-inte Version: version, } viper.SetEnvPrefix("DEBRICKED") + viper.AutomaticEnv() viper.MustBindEnv(AccessTokenFlag) + rootCmd.PersistentFlags().StringVarP( &accessToken, - AccessTokenFlag, + OldAccessTokenFlag, "t", - "", + viper.GetString(AccessTokenFlag), `Debricked access token. Read more: https://docs.debricked.com/product/administration/generate-access-token`, ) diff --git a/internal/cmd/root/root_test.go b/internal/cmd/root/root_test.go index 52e32cb8..5541b033 100644 --- a/internal/cmd/root/root_test.go +++ b/internal/cmd/root/root_test.go @@ -21,7 +21,7 @@ func TestNewRootCmd(t *testing.T) { } flags := cmd.PersistentFlags() - flag := flags.Lookup(AccessTokenFlag) + flag := flags.Lookup(OldAccessTokenFlag) assert.NotNil(t, flag) assert.Equal(t, "t", flag.Shorthand) @@ -34,7 +34,7 @@ func TestNewRootCmd(t *testing.T) { break } } - assert.Truef(t, match, "failed to assert that flag was present: "+AccessTokenFlag) + assert.Truef(t, match, "failed to assert that flag was present: "+OldAccessTokenFlag) assert.Len(t, viperKeys, 22) }