-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
132 lines (120 loc) · 3.78 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"os/user"
"path/filepath"
"github.com/alyu/configparser"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/secretsmanager"
)
func main() {
sourceProfile := flag.String("p", "default", "Profile to use")
secret := flag.String("s", "secret", "Secret To Fetch")
version := flag.String("v", "version", "Version of secret To Fetch")
skipProfile := flag.Bool("k", false, "Skip profile check and just use default for use when no cred file and default will work")
credFile := flag.String("c", filepath.Join(getCredentialPath(), ".aws", "credentials"), "Full path to credentials file")
flag.Parse()
if *secret == "secret" {
fmt.Println("You must specify a secret name to fetch")
return
}
var sess *session.Session
if *skipProfile {
//Use Default Credentials
sess = session.Must(session.NewSession())
} else {
//Get Specified Credentials
exists, err := checkProfileExists(credFile, sourceProfile)
if err != nil || !exists {
fmt.Println(err.Error())
return
}
sess = CreateSession(sourceProfile)
}
getSecret(sess, secret, version)
}
// CreateSession Creates AWS Session with specified profile
func CreateSession(profileName *string) *session.Session {
profileNameValue := *profileName
sess := session.Must(session.NewSessionWithOptions(session.Options{
Profile: profileNameValue,
Config: aws.Config{Region: aws.String("us-east-1")},
}))
return sess
}
// getCredentialPath returns the users home directory path as a string
func getCredentialPath() string {
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
return usr.HomeDir
}
// checkProfileExists takes path to the credentials file and profile name to search for
// Returns bool and any errors
func checkProfileExists(credFile *string, profileName *string) (bool, error) {
config, err := configparser.Read(*credFile)
if err != nil {
fmt.Println("Could not find credentials file")
fmt.Println(err.Error())
return false, err
}
section, err := config.Section(*profileName)
if err != nil {
fmt.Println("Could not find profile in credentials file")
return false, nil
}
if !section.Exists("aws_access_key_id") {
fmt.Println("Could not find access key in profile")
return false, nil
}
return true, nil
}
func getSecret(sess *session.Session, secretName *string, secretVersion *string) {
svc := secretsmanager.New(sess)
var versionID string
if *secretVersion == "version" {
versionID = "AWSCURRENT"
} else {
versionID = *secretVersion
}
input := &secretsmanager.GetSecretValueInput{
SecretId: aws.String(*secretName),
VersionStage: aws.String(versionID),
}
result, err := svc.GetSecretValue(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case secretsmanager.ErrCodeResourceNotFoundException:
fmt.Println(secretsmanager.ErrCodeResourceNotFoundException, aerr.Error())
case secretsmanager.ErrCodeInvalidParameterException:
fmt.Println(secretsmanager.ErrCodeInvalidParameterException, aerr.Error())
case secretsmanager.ErrCodeInvalidRequestException:
fmt.Println(secretsmanager.ErrCodeInvalidRequestException, aerr.Error())
case secretsmanager.ErrCodeDecryptionFailure:
fmt.Println(secretsmanager.ErrCodeDecryptionFailure, aerr.Error())
case secretsmanager.ErrCodeInternalServiceError:
fmt.Println(secretsmanager.ErrCodeInternalServiceError, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
// Convert structs to JSON.
data, err := json.Marshal(result)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", data)
}