Skip to content

Commit 4164fbf

Browse files
committed
Handle not authenticated
1 parent 699d16b commit 4164fbf

File tree

1 file changed

+48
-19
lines changed

1 file changed

+48
-19
lines changed

tools/ghsecrets/main.go

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,33 +34,37 @@ func main() {
3434
Run: func(cmd *cobra.Command, args []string) {
3535
// Validate file
3636
if err := validateFile(filePath); err != nil {
37-
fmt.Println(err)
37+
exitWithError(err, "Failed to validate file")
3838
return
3939
}
4040

4141
if secretID == "" {
4242
if !isGHInstalled() {
43-
fmt.Println("GitHub CLI not found. Please go to https://cli.github.com/ and install it to use this tool.")
43+
exitWithError(nil, "GitHub CLI not found. Please go to https://cli.github.com/ and install it to use this tool.")
4444
return
4545
}
4646
var err error
4747
secretID, err = generateSecretIDFromGithubUsername()
4848
if err != nil {
49-
log.Fatalf("Failed to generate secret ID: %s", err)
49+
exitWithError(err, "Failed to generate secret ID")
50+
return
5051
}
5152
}
5253

5354
switch strings.ToLower(backend) {
5455
case "github":
5556
if err := setGitHubSecret(filePath, secretID); err != nil {
56-
log.Fatalf("Failed to set GitHub secret: %s", err)
57+
exitWithError(err, "Failed to set GitHub secret")
58+
return
5759
}
5860
case "aws":
5961
if err := setAWSSecret(filePath, secretID, sharedWith); err != nil {
60-
log.Fatalf("Failed to set AWS secret: %s", err)
62+
exitWithError(err, "Failed to set AWS secret")
63+
return
6164
}
6265
default:
63-
log.Fatalf("Unsupported backend: %s. Valid backends are 'github' or 'aws'.", backend)
66+
exitWithError(nil, "Unsupported backend. Valid backends are 'github' or 'aws'.")
67+
return
6468
}
6569
},
6670
}
@@ -70,16 +74,8 @@ func main() {
7074
Use: "get",
7175
Short: "Retrieve a secret from AWS Secrets Manager",
7276
Run: func(cmd *cobra.Command, args []string) {
73-
if strings.ToLower(backend) != "aws" {
74-
log.Fatalf("The 'get' command only supports the AWS backend.")
75-
}
76-
77-
if secretID == "" {
78-
log.Fatalf("You must specify a secret ID using the --secret-id flag.")
79-
}
80-
8177
if err := getAWSSecret(secretID, decode); err != nil {
82-
log.Fatalf("Failed to retrieve AWS secret: %s", err)
78+
exitWithError(err, "Failed to retrieve AWS secret")
8379
}
8480
},
8581
}
@@ -98,12 +94,13 @@ func main() {
9894
setCmd.PersistentFlags().StringSliceVar(&sharedWith, "shared-with", []string{}, "Comma-separated list of IAM ARNs to share the secret with")
9995

10096
getCmd.PersistentFlags().StringVarP(&secretID, "secret-id", "s", "", "ID of the secret to retrieve")
101-
getCmd.PersistentFlags().StringVarP(&backend, "backend", "b", "aws", "Backend to use for retrieving secrets. Only 'aws' is supported for this command.")
10297
getCmd.PersistentFlags().BoolVarP(&decode, "decode", "d", false, "Decode the Base64-encoded secret value")
10398

99+
// Make secretID a required flag for the set command
100+
getCmd.MarkPersistentFlagRequired("secret-id")
101+
104102
if err := rootCmd.Execute(); err != nil {
105-
fmt.Println(err)
106-
os.Exit(1)
103+
exitWithError(err, "Failed to execute command")
107104
}
108105
}
109106

@@ -156,7 +153,7 @@ func setAWSSecret(filePath, secretID string, sharedWith []string) error {
156153
_, err = smClient.CreateSecret(context.TODO(), &secretsmanager.CreateSecretInput{
157154
Name: aws.String(secretID),
158155
SecretString: aws.String(encoded),
159-
Description: aws.String("Secret created by ghsecrets CLI"),
156+
Description: aws.String("Chainlink Test Secret created by CTF/ghsecrets CLI"),
160157
})
161158
if err != nil {
162159
// If the secret already exists, update it instead
@@ -169,9 +166,24 @@ func setAWSSecret(filePath, secretID string, sharedWith []string) error {
169166
Description: aws.String("Secret updated by ghsecrets CLI"),
170167
})
171168
if err != nil {
169+
// Check for the SSO token expiration error
170+
if strings.Contains(err.Error(), "InvalidGrantException") {
171+
return fmt.Errorf(
172+
"Your AWS SSO session has likely expired. Please re-authenticate by running:\n\n aws sso login --profile <my-profile>\n\nThen try again.\n\nOriginal error: %w",
173+
err,
174+
)
175+
}
172176
return fmt.Errorf("failed to update AWS secret: %w", err)
173177
}
178+
fmt.Printf("Secret %s updated successfully.\n", secretID)
174179
} else {
180+
// Check for the SSO token expiration error
181+
if strings.Contains(err.Error(), "InvalidGrantException") {
182+
return fmt.Errorf(
183+
"Your AWS SSO session has likely expired. Please re-authenticate by running:\n\n aws sso login --profile <my-profile>\n\nThen try again.\n\nOriginal error: %w",
184+
err,
185+
)
186+
}
175187
return fmt.Errorf("failed to create AWS secret: %w", err)
176188
}
177189
} else {
@@ -247,6 +259,7 @@ func updateAWSSecretAccessPolicy(secretID string, sharedWith []string) error {
247259
return nil
248260
}
249261

262+
// getAWSSecret retrieves a test secret from AWS Secrets Manager
250263
// getAWSSecret retrieves a test secret from AWS Secrets Manager
251264
func getAWSSecret(secretID string, decode bool) error {
252265
cfg, err := config.LoadDefaultConfig(context.TODO())
@@ -259,6 +272,13 @@ func getAWSSecret(secretID string, decode bool) error {
259272
SecretId: aws.String(secretID),
260273
})
261274
if err != nil {
275+
// Check if the error is due to an expired SSO token
276+
if strings.Contains(err.Error(), "InvalidGrantException") {
277+
return fmt.Errorf(
278+
"Your AWS SSO session has likely expired. Please re-authenticate by running:\n\n aws sso login --profile <my-profile>\n\nThen try again.\n\nOriginal error: %w",
279+
err,
280+
)
281+
}
262282
return fmt.Errorf("failed to retrieve AWS secret: %w", err)
263283
}
264284

@@ -312,3 +332,12 @@ func generateSecretIDFromGithubUsername() (string, error) {
312332
secretID := fmt.Sprintf("BASE64_TESTSECRETS_%s", trimmedUsername)
313333
return strings.ToUpper(secretID), nil
314334
}
335+
336+
func exitWithError(err error, msg string) {
337+
if err != nil {
338+
fmt.Fprintf(os.Stderr, "%s: %v\n", msg, err)
339+
} else {
340+
fmt.Fprintf(os.Stderr, "%s\n", msg)
341+
}
342+
os.Exit(1)
343+
}

0 commit comments

Comments
 (0)