Skip to content

Commit

Permalink
Let viper handle env variable for token
Browse files Browse the repository at this point in the history
  • Loading branch information
filip-debricked committed Nov 5, 2024
1 parent 2506177 commit 5fff911
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 74 deletions.
22 changes: 4 additions & 18 deletions internal/client/deb_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand All @@ -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"`
Expand Down
50 changes: 1 addition & 49 deletions internal/client/deb_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
}
Expand Down
7 changes: 4 additions & 3 deletions internal/client/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 6 additions & 3 deletions internal/cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -29,15 +30,17 @@ Complete documentation is available at https://docs.debricked.com/tools-and-inte
Version: version,
}
viper.SetEnvPrefix("DEBRICKED")
viper.MustBindEnv(AccessTokenFlag)
viper.AutomaticEnv()
rootCmd.PersistentFlags().StringVarP(
&accessToken,
AccessTokenFlag,
"t",
"",
viper.GetString(AccessTokenFlag),
`Debricked access token.
Read more: https://docs.debricked.com/product/administration/generate-access-token`,
)
viper.MustBindEnv(AccessTokenFlag)
viper.RegisterAlias(OldAccessTokenFlag, AccessTokenFlag) // To avoid breaking change

var debClient = container.DebClient()
debClient.SetAccessToken(&accessToken)
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/root/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestNewRootCmd(t *testing.T) {
}
}
assert.Truef(t, match, "failed to assert that flag was present: "+AccessTokenFlag)
assert.Len(t, viperKeys, 22)
assert.Len(t, viperKeys, 23)
}

func TestPreRun(t *testing.T) {
Expand Down

0 comments on commit 5fff911

Please sign in to comment.