Skip to content

Commit

Permalink
using login environments now
Browse files Browse the repository at this point in the history
  • Loading branch information
rsds143 committed Mar 1, 2022
1 parent d44fa71 commit 745aa22
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 11 deletions.
4 changes: 2 additions & 2 deletions cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func executeLogin(args []string, getHome func() (string, pkg.ConfFiles, error),
}
switch {
case authToken != "":
if err := makeConf(confDir, confFiles.TokenPath, authToken); err != nil {
if err := makeConf(confDir, pkg.PathWithEnv(confFiles.TokenPath), authToken); err != nil {
return WriteError, err
}
return 0, nil
Expand All @@ -133,7 +133,7 @@ func executeLogin(args []string, getHome func() (string, pkg.ConfFiles, error),
}
}
clientJSON = fmt.Sprintf("{\"clientId\":\"%v\",\"clientName\":\"%v\",\"clientSecret\":\"%v\"}", clientID, clientName, clientSecret)
if err := makeConf(confDir, confFiles.SaPath, clientJSON); err != nil {
if err := makeConf(confDir, pkg.PathWithEnv(confFiles.SaPath), clientJSON); err != nil {
return WriteError, err
}
return 0, nil
Expand Down
8 changes: 4 additions & 4 deletions cmd/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func TestArgs(t *testing.T) {
if exitCode != 0 {
t.Fatalf("unexpected exit code %v", exitCode)
}
fd, err := os.Open(f)
fd, err := os.Open(pkg.PathWithEnv(f))
if err != nil {
t.Fatalf("unexpected error %v", err)
}
Expand Down Expand Up @@ -175,7 +175,7 @@ func TestArgsWithNoPermission(t *testing.T) {
if exitCode != WriteError {
t.Errorf("unexpected exit code %v", exitCode)
}
expected := fmt.Sprintf("unable to create the login file due to error open %v: permission denied", f)
expected := fmt.Sprintf("unable to create the login file due to error open %v: permission denied", pkg.PathWithEnv(f))
if err.Error() != expected {
t.Errorf("expected '%v' but was '%v'", expected, err.Error())
}
Expand Down Expand Up @@ -390,7 +390,7 @@ func TestLoginToken(t *testing.T) {
if exitCode != 0 {
t.Fatalf("unexpected exit code %v", exitCode)
}
fd, err := os.Open(f)
fd, err := os.Open(pkg.PathWithEnv(f))
if err != nil {
t.Fatalf("unexpected error %v", err)
}
Expand Down Expand Up @@ -431,7 +431,7 @@ func TestLoginTokenInvalidPerms(t *testing.T) {
if exitCode != WriteError {
t.Errorf("unexpected exit code %v", exitCode)
}
expected := fmt.Sprintf("unable to create the login file due to error open %v: permission denied", f)
expected := fmt.Sprintf("unable to create the login file due to error open %v: permission denied", pkg.PathWithEnv(f))
if err.Error() != expected {
t.Errorf("expected '%v' but was '%v'", expected, err.Error())
}
Expand Down
17 changes: 15 additions & 2 deletions pkg/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,27 @@ func GetHome(getHome func() (string, error)) (confDir string, confFiles ConfFile
}
confDir = path.Join(home, ".config", "astra")

tokenFile := path.Join(confDir, "token")
saFile := path.Join(confDir, "sa.json")
tokenFile := path.Join(confDir, PathWithEnv("token"))
saFile := path.Join(confDir, PathWithEnv("sa.json"))
return confDir, ConfFiles{
TokenPath: tokenFile,
SaPath: saFile,
}, nil
}

func PathWithEnv(f string) string {
if strings.Contains(f, string(os.PathSeparator)) {
tokens := strings.Split(f, string(os.PathSeparator))
tokenLen := len(tokens)
if tokenLen > 0 {
last := tokens[tokenLen-1]
tokens[tokenLen-1] = Env + "_" + last
return strings.Join(tokens, string(os.PathSeparator))
}
}
return Env + "_" + f
}

// ReadToken retrieves the login from the specified json file
func ReadToken(tokenFile string) (string, error) {
f, err := os.Open(tokenFile)
Expand Down
14 changes: 14 additions & 0 deletions pkg/conf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ import (
"testing"
)

func TestPathWithEnvWhenPath(t *testing.T) {
newPath := PathWithEnv("/test/sa.json")
if newPath != "/test/prod_sa.json" {
t.Errorf("expected path of /test/prod_sa.json but was %v", newPath)
}
}

func TestPathWithEnvWhenNoPath(t *testing.T) {
newPath := PathWithEnv("sa.json")
if newPath != "prod_sa.json" {
t.Errorf("expected path of prod_sa.json but was %v", newPath)
}
}

func TestReadLogin(t *testing.T) {
c, err := ReadLogin("testdata/sa.json")
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestLoginWithInvalidTokenFile(t *testing.T) {
if err == nil {
t.Fatal("expected an error on an empty path")
}
expected := "found token at 'testdata/with_invalid_token/.config/astra/token' but unable to read token with error 'missing prefix 'AstraCS' in token file 'testdata/with_invalid_token/.config/astra/token''"
expected := "found token at 'testdata/with_invalid_token/.config/astra/prod_token' but unable to read token with error 'missing prefix 'AstraCS' in token file 'testdata/with_invalid_token/.config/astra/prod_token''"
if err.Error() != expected {
t.Errorf("expected '%v' but was '%v'", expected, err.Error())
}
Expand All @@ -74,7 +74,7 @@ func TestLoginWithEmptyTokenFile(t *testing.T) {
if err == nil {
t.Fatal("expected an error on an empty path")
}
expected := "found token at 'testdata/with_empty_token/.config/astra/token' but unable to read token with error 'token file 'testdata/with_empty_token/.config/astra/token' is empty'"
expected := "found token at 'testdata/with_empty_token/.config/astra/prod_token' but unable to read token with error 'token file 'testdata/with_empty_token/.config/astra/prod_token' is empty'"
if err.Error() != expected {
t.Errorf("expected '%v' but was '%v'", expected, err.Error())
}
Expand All @@ -100,7 +100,7 @@ func TestLoginWithInvalidSA(t *testing.T) {
if err == nil {
t.Fatal("expected an error on an empty path")
}
expected := "Invalid service account: Client ID for service account is empty for file 'testdata/with_invalid_sa/.config/astra/sa.json'"
expected := "Invalid service account: Client ID for service account is empty for file 'testdata/with_invalid_sa/.config/astra/prod_sa.json'"
if err.Error() != expected {
t.Errorf("expected '%v' but was '%v'", expected, err.Error())
}
Expand Down
File renamed without changes.

0 comments on commit 745aa22

Please sign in to comment.