@@ -8,19 +8,19 @@ import (
88 "os/exec"
99 "path/filepath"
1010 "strings"
11-
1211 "time"
1312
1413 "github.com/smartcontractkit/chainlink-testing-framework/framework"
15-
1614 "github.com/spf13/cobra"
1715)
1816
1917func main () {
2018 var filePath string
21- var customSecretID string
19+ var secretID string
2220 var backend string // Backend: GitHub or AWS
21+ var decode bool // Decode flag for `get`
2322
23+ // Set Command
2424 var setCmd = & cobra.Command {
2525 Use : "set" ,
2626 Short : "Set test secrets in GitHub or AWS" ,
@@ -31,16 +31,12 @@ func main() {
3131 return
3232 }
3333
34- var secretID string
35- var err error
36-
37- if customSecretID != "" {
38- secretID = customSecretID
39- } else {
34+ if secretID == "" {
4035 if ! isGHInstalled () {
4136 fmt .Println ("GitHub CLI not found. Please go to https://cli.github.com/ and install it to use this tool." )
4237 return
4338 }
39+ var err error
4440 secretID , err = generateSecretIDFromGithubUsername ()
4541 if err != nil {
4642 log .Fatalf ("Failed to generate secret ID: %s" , err )
@@ -63,17 +59,42 @@ func main() {
6359 },
6460 }
6561
62+ // Get Command
63+ var getCmd = & cobra.Command {
64+ Use : "get" ,
65+ Short : "Retrieve a secret from AWS Secrets Manager" ,
66+ Run : func (cmd * cobra.Command , args []string ) {
67+ if strings .ToLower (backend ) != "aws" {
68+ log .Fatalf ("The 'get' command only supports the AWS backend." )
69+ }
70+
71+ if secretID == "" {
72+ log .Fatalf ("You must specify a secret ID using the --secret-id flag." )
73+ }
74+
75+ // Retrieve the secret from AWS Secrets Manager
76+ if err := getAWSSecret (secretID , decode ); err != nil {
77+ log .Fatalf ("Failed to retrieve AWS secret: %s" , err )
78+ }
79+ },
80+ }
81+
6682 var rootCmd = & cobra.Command {
6783 Use : "ghsecrets" ,
6884 Short : "A tool for managing GitHub or AWS test secrets" ,
6985 }
7086
7187 rootCmd .AddCommand (setCmd )
88+ rootCmd .AddCommand (getCmd )
7289
7390 setCmd .PersistentFlags ().StringVarP (& filePath , "file" , "f" , defaultSecretsPath (), "path to file with test secrets" )
74- setCmd .PersistentFlags ().StringVarP (& customSecretID , "secret-id" , "s" , "" , "custom secret ID " )
91+ setCmd .PersistentFlags ().StringVarP (& secretID , "secret-id" , "s" , "" , "ID of the secret to set " )
7592 setCmd .PersistentFlags ().StringVarP (& backend , "backend" , "b" , "aws" , "Backend to use for storing secrets. Options: github, aws" )
7693
94+ getCmd .PersistentFlags ().StringVarP (& secretID , "secret-id" , "s" , "" , "ID of the secret to retrieve" )
95+ getCmd .PersistentFlags ().StringVarP (& backend , "backend" , "b" , "aws" , "Backend to use for retrieving secrets. Only 'aws' is supported for this command." )
96+ getCmd .PersistentFlags ().BoolVarP (& decode , "decode" , "d" , false , "Decode the Base64-encoded secret value" )
97+
7798 if err := rootCmd .Execute (); err != nil {
7899 fmt .Println (err )
79100 os .Exit (1 )
@@ -116,9 +137,7 @@ func generateSecretIDFromGithubUsername() (string, error) {
116137 return strings .ToUpper (secretID ), nil
117138}
118139
119- // ===========================
120- // GitHub Secrets Logic
121- // ===========================
140+ // setGitHubSecret sets a test secret in GitHub Secrets
122141func setGitHubSecret (filePath , secretID string ) error {
123142 data , err := os .ReadFile (filePath )
124143 if err != nil {
@@ -148,31 +167,43 @@ func setGitHubSecret(filePath, secretID string) error {
148167 return nil
149168}
150169
151- // ===========================
152- // AWS Secrets Manager Logic
153- // ===========================
170+ // getAWSSecret retrieves a test secret from AWS Secrets Manager
154171func setAWSSecret (filePath , secretID string ) error {
155- // 1. Read the file content
156172 data , err := os .ReadFile (filePath )
157173 if err != nil {
158174 return fmt .Errorf ("failed to read file: %w" , err )
159175 }
160-
161- // 2. Base64 encode the file content (or skip if you prefer raw)
162176 encoded := base64 .StdEncoding .EncodeToString (data )
163-
164- // 3. Create a new AWS Secrets Manager client
165177 sm , err := framework .NewAWSSecretsManager (10 * time .Second )
166178 if err != nil {
167179 return fmt .Errorf ("failed to initialize AWS Secrets Manager: %w" , err )
168180 }
169-
170- // 4. Create (or override) the secret
171181 err = sm .CreateSecret (secretID , encoded , true )
172182 if err != nil {
173183 return fmt .Errorf ("failed to create (or override) AWS secret: %w" , err )
174184 }
175-
176185 fmt .Printf ("Test secret set successfully in AWS with key: %s\n " , secretID )
177186 return nil
178187}
188+
189+ // getAWSSecret retrieves a test secret from AWS Secrets Manager
190+ func getAWSSecret (secretID string , decode bool ) error {
191+ sm , err := framework .NewAWSSecretsManager (10 * time .Second )
192+ if err != nil {
193+ return fmt .Errorf ("failed to initialize AWS Secrets Manager: %w" , err )
194+ }
195+ secret , err := sm .GetSecret (secretID )
196+ if err != nil {
197+ return fmt .Errorf ("failed to retrieve AWS secret: %w" , err )
198+ }
199+ value := secret .Value ()
200+ if decode {
201+ decoded , err := base64 .StdEncoding .DecodeString (value )
202+ if err != nil {
203+ return fmt .Errorf ("failed to decode secret value: %w" , err )
204+ }
205+ value = string (decoded )
206+ }
207+ fmt .Println (value )
208+ return nil
209+ }
0 commit comments