Skip to content

Commit b1b0280

Browse files
committed
Ensure secretID starts with "testsecrets/" prefix and improve success message for AWS Secrets Manager
1 parent 4164fbf commit b1b0280

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

tools/ghsecrets/main.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ func main() {
3232
Use: "set",
3333
Short: "Set test secrets in GitHub or AWS",
3434
Run: func(cmd *cobra.Command, args []string) {
35-
// Validate file
3635
if err := validateFile(filePath); err != nil {
3736
exitWithError(err, "Failed to validate file")
3837
return
@@ -51,6 +50,9 @@ func main() {
5150
}
5251
}
5352

53+
// Ensure secretID starts with "testsecrets/"
54+
secretID = ensurePrefix(secretID, "testsecrets/")
55+
5456
switch strings.ToLower(backend) {
5557
case "github":
5658
if err := setGitHubSecret(filePath, secretID); err != nil {
@@ -134,29 +136,27 @@ func setGitHubSecret(filePath, secretID string) error {
134136

135137
// setAWSSecret creates or updates a secret in AWS Secrets Manager
136138
func setAWSSecret(filePath, secretID string, sharedWith []string) error {
139+
secretID = ensurePrefix(secretID, "testsecrets/") // Ensure prefix
140+
137141
data, err := os.ReadFile(filePath)
138142
if err != nil {
139143
return fmt.Errorf("failed to read file: %w", err)
140144
}
141145
encoded := base64.StdEncoding.EncodeToString(data)
142146

143-
// 1) Load AWS config
144147
cfg, err := config.LoadDefaultConfig(context.TODO())
145148
if err != nil {
146149
return fmt.Errorf("failed to load AWS config: %w", err)
147150
}
148151

149-
// 2) Create Secrets Manager client
150152
smClient := secretsmanager.NewFromConfig(cfg)
151153

152-
// 3) Try creating the secret
153154
_, err = smClient.CreateSecret(context.TODO(), &secretsmanager.CreateSecretInput{
154155
Name: aws.String(secretID),
155156
SecretString: aws.String(encoded),
156157
Description: aws.String("Chainlink Test Secret created by CTF/ghsecrets CLI"),
157158
})
158159
if err != nil {
159-
// If the secret already exists, update it instead
160160
var resourceExistsErr *types.ResourceExistsException
161161
if errors.As(err, &resourceExistsErr) {
162162
fmt.Printf("Secret %s already exists, updating its value...\n", secretID)
@@ -166,7 +166,6 @@ func setAWSSecret(filePath, secretID string, sharedWith []string) error {
166166
Description: aws.String("Secret updated by ghsecrets CLI"),
167167
})
168168
if err != nil {
169-
// Check for the SSO token expiration error
170169
if strings.Contains(err.Error(), "InvalidGrantException") {
171170
return fmt.Errorf(
172171
"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",
@@ -175,9 +174,7 @@ func setAWSSecret(filePath, secretID string, sharedWith []string) error {
175174
}
176175
return fmt.Errorf("failed to update AWS secret: %w", err)
177176
}
178-
fmt.Printf("Secret %s updated successfully.\n", secretID)
179177
} else {
180-
// Check for the SSO token expiration error
181178
if strings.Contains(err.Error(), "InvalidGrantException") {
182179
return fmt.Errorf(
183180
"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",
@@ -186,17 +183,22 @@ func setAWSSecret(filePath, secretID string, sharedWith []string) error {
186183
}
187184
return fmt.Errorf("failed to create AWS secret: %w", err)
188185
}
189-
} else {
190-
fmt.Printf("Secret %s created successfully.\n", secretID)
191186
}
192187

193-
// 4) Update sharing policy if necessary
194188
if len(sharedWith) > 0 {
195189
err = updateAWSSecretAccessPolicy(secretID, sharedWith)
196190
if err != nil {
197191
return fmt.Errorf("failed to update secret sharing policy: %w", err)
198192
}
199193
}
194+
195+
// Success message with AWS-specific instructions
196+
fmt.Printf(
197+
"Test secret set successfully in AWS Secrets Manager with key: %s\n\n"+
198+
"To use this secret in a GitHub workflow, set the 'test_secrets_override_key' flag with the 'aws:' prefix. Example:\n"+
199+
"gh workflow run ${workflow_name} -f test_secrets_override_key=aws:%s\n",
200+
secretID, secretID,
201+
)
200202
return nil
201203
}
202204

@@ -333,6 +335,13 @@ func generateSecretIDFromGithubUsername() (string, error) {
333335
return strings.ToUpper(secretID), nil
334336
}
335337

338+
func ensurePrefix(secretID, prefix string) string {
339+
if !strings.HasPrefix(secretID, prefix) {
340+
return prefix + secretID
341+
}
342+
return secretID
343+
}
344+
336345
func exitWithError(err error, msg string) {
337346
if err != nil {
338347
fmt.Fprintf(os.Stderr, "%s: %v\n", msg, err)

0 commit comments

Comments
 (0)