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 4, 2024
1 parent 2506177 commit f6613db
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 69 deletions.
18 changes: 2 additions & 16 deletions internal/client/deb_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func NewDebClient(accessToken *string, httpClient IClient) *DebClient {
return &DebClient{
host: &host,
httpClient: httpClient,
accessToken: initAccessToken(accessToken),
accessToken: accessToken,
jwtToken: "",
authenticator: authenticator,
}
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
48 changes: 1 addition & 47 deletions internal/client/deb_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,35 +38,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 +186,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
3 changes: 1 addition & 2 deletions internal/client/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ type errorMessage struct {
}

func (debClient *DebClient) authenticate() error {

if debClient.accessToken != nil {
if len(*debClient.accessToken) != 0 {
return debClient.authenticateExplicitToken()
}

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 f6613db

Please sign in to comment.