Skip to content

Commit a02ea89

Browse files
committed
fix: run error cmds
1 parent c11118e commit a02ea89

File tree

7 files changed

+33
-26
lines changed

7 files changed

+33
-26
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
OWNER := dnitsch
22
NAME := aws-cli-auth
3-
VERSION := v0.8.1
3+
VERSION := v0.9.0
44
REVISION := $(shell git rev-parse --short HEAD)
55

66
LDFLAGS := -ldflags="-s -w -X \"github.com/dnitsch/aws-cli-auth/cmd.Version=$(VERSION)\" -X \"github.com/dnitsch/aws-cli-auth/cmd.Revision=$(REVISION)\" -extldflags -static"

cmd/clear.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var (
1313
clearCmd = &cobra.Command{
1414
Use: "clear-cache <flags>",
1515
Short: "Clears any stored credentials in the OS secret store",
16-
Run: clear,
16+
RunE: clear,
1717
}
1818
)
1919

@@ -23,7 +23,7 @@ func init() {
2323
rootCmd.AddCommand(clearCmd)
2424
}
2525

26-
func clear(cmd *cobra.Command, args []string) {
26+
func clear(cmd *cobra.Command, args []string) error {
2727
web := web.New()
2828
secretStore := util.NewSecretStore("")
2929

@@ -37,8 +37,8 @@ func clear(cmd *cobra.Command, args []string) {
3737
secretStore.ClearAll()
3838

3939
if err := os.Remove(util.ConfigIniFile("")); err != nil {
40-
util.Exit(err)
40+
return err
4141
}
4242

43-
util.CleanExit()
43+
return nil
4444
}

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func Execute() {
2727
if err := rootCmd.Execute(); err != nil {
2828
util.Exit(err)
2929
}
30+
util.CleanExit()
3031
}
3132

3233
func init() {

cmd/saml.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
"github.com/dnitsch/aws-cli-auth/internal/auth"
77
"github.com/dnitsch/aws-cli-auth/internal/config"
8-
"github.com/dnitsch/aws-cli-auth/internal/util"
98
"github.com/spf13/cobra"
109
)
1110

@@ -20,7 +19,7 @@ var (
2019
Use: "saml <SAML ProviderUrl>",
2120
Short: "Get AWS credentials and out to stdout",
2221
Long: `Get AWS credentials and out to stdout through your SAML provider authentication.`,
23-
Run: getSaml,
22+
RunE: getSaml,
2423
PreRunE: func(cmd *cobra.Command, args []string) error {
2524
if reloadBeforeTime != 0 && reloadBeforeTime > duration {
2625
return fmt.Errorf("reload-before: %v, must be less than duration (-d): %v", reloadBeforeTime, duration)
@@ -42,7 +41,7 @@ func init() {
4241
rootCmd.AddCommand(samlCmd)
4342
}
4443

45-
func getSaml(cmd *cobra.Command, args []string) {
44+
func getSaml(cmd *cobra.Command, args []string) error {
4645
conf := config.SamlConfig{
4746
ProviderUrl: providerUrl,
4847
PrincipalArn: principalArn,
@@ -57,6 +56,8 @@ func getSaml(cmd *cobra.Command, args []string) {
5756
},
5857
}
5958

60-
auth.GetSamlCreds(conf)
61-
util.CleanExit()
59+
if err := auth.GetSamlCreds(conf); err != nil {
60+
return err
61+
}
62+
return nil
6263
}

cmd/specific.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var (
1818
Long: `Initiates a specific crednetial provider [WEB_ID] as opposed to relying on the defaultCredentialChain provider.
1919
This is useful in CI situations where various authentication forms maybe present from AWS_ACCESS_KEY as env vars to metadata of the node.
2020
Returns the same JSON object as the call to the AWS cli for any of the sts AssumeRole* commands`,
21-
Run: specific,
21+
RunE: specific,
2222
}
2323
)
2424

@@ -27,28 +27,31 @@ func init() {
2727
rootCmd.AddCommand(specificCmd)
2828
}
2929

30-
func specific(cmd *cobra.Command, args []string) {
30+
func specific(cmd *cobra.Command, args []string) error {
3131
var awsCreds *util.AWSCredentials
3232
var err error
3333
if method != "" {
3434
switch method {
3535
case "WEB_ID":
3636
awsCreds, err = auth.LoginAwsWebToken(os.Getenv("USER")) // TODO: redo this getUser implementation
3737
if err != nil {
38-
util.Exit(err)
38+
return err
3939
}
4040
default:
41-
util.Exit(fmt.Errorf("Unsupported Method: %s", method))
41+
return fmt.Errorf("unsupported Method: %s", method)
4242
}
4343
}
4444
config := config.SamlConfig{BaseConfig: config.BaseConfig{StoreInProfile: storeInProfile}}
4545

4646
if role != "" {
4747
awsCreds, err = auth.AssumeRoleWithCreds(awsCreds, os.Getenv("USER"), role)
4848
if err != nil {
49-
util.Exit(err)
49+
return err
5050
}
5151
}
5252

53-
util.SetCredentials(awsCreds, config)
53+
if err := util.SetCredentials(awsCreds, config); err != nil {
54+
return err
55+
}
56+
return nil
5457
}

internal/auth/saml.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
// GetSamlCreds
12-
func GetSamlCreds(conf config.SamlConfig) {
12+
func GetSamlCreds(conf config.SamlConfig) error {
1313
if conf.BaseConfig.CfgSectionName == "" && conf.BaseConfig.StoreInProfile {
1414
util.Writeln("Config-Section name must be provided if store-profile is enabled")
1515
util.Exit(nil)
@@ -28,23 +28,24 @@ func GetSamlCreds(conf config.SamlConfig) {
2828

2929
t, err := webBrowser.GetSamlLogin(conf)
3030
if err != nil {
31-
util.Exit(err)
31+
return err
3232
}
3333
user, err := user.Current()
3434
if err != nil {
35-
util.Exit(err)
35+
return err
3636
}
3737

3838
roleObj := &util.AWSRole{RoleARN: conf.BaseConfig.Role, PrincipalARN: conf.PrincipalArn, Name: util.SessionName(user.Username, config.SELF_NAME), Duration: conf.Duration}
3939

4040
awsCreds, err = LoginStsSaml(t, roleObj)
4141
if err != nil {
42-
util.Exit(err)
42+
return err
4343
}
4444

4545
awsCreds.Version = 1
4646
secretStore.SaveAWSCredential(awsCreds)
4747
}
4848

4949
util.SetCredentials(awsCreds, conf)
50+
return nil
5051
}

internal/util/helper.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ func SessionName(username, self_name string) string {
4343
return fmt.Sprintf("%s-%s", username, self_name)
4444
}
4545

46-
func SetCredentials(creds *AWSCredentials, config config.SamlConfig) {
46+
func SetCredentials(creds *AWSCredentials, config config.SamlConfig) error {
4747

4848
if config.BaseConfig.StoreInProfile {
4949
if err := storeCredentialsInProfile(*creds, config.BaseConfig.CfgSectionName); err != nil {
5050
Traceln("Error: %s", err.Error())
51-
Exit(err)
51+
return err
5252
}
53-
return
53+
return nil
5454
}
55-
returnStdOutAsJson(*creds)
55+
return returnStdOutAsJson(*creds)
5656
}
5757

5858
func storeCredentialsInProfile(creds AWSCredentials, configSection string) error {
@@ -82,15 +82,16 @@ func storeCredentialsInProfile(creds AWSCredentials, configSection string) error
8282
return nil
8383
}
8484

85-
func returnStdOutAsJson(creds AWSCredentials) {
85+
func returnStdOutAsJson(creds AWSCredentials) error {
8686
creds.Version = 1
8787

8888
jsonBytes, err := json.Marshal(creds)
8989
if err != nil {
9090
Writeln("Unexpected AWS credential response")
91-
Exit(err)
91+
return err
9292
}
9393
fmt.Println(string(jsonBytes))
94+
return nil
9495
}
9596

9697
func GetWebIdTokenFileContents() (string, error) {

0 commit comments

Comments
 (0)