-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcredentials.go
More file actions
128 lines (101 loc) · 2.97 KB
/
credentials.go
File metadata and controls
128 lines (101 loc) · 2.97 KB
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
package awsprofile
import (
"errors"
"os"
homedir "github.com/mitchellh/go-homedir"
ini "gopkg.in/ini.v1"
)
// constant
const (
AwsSharedCredentialsFile string = "AWS_SHARED_CREDENTIALS_FILE"
AwsCredentials string = "~/.aws/credentials"
AwsAccessKeyID string = "aws_access_key_id"
AwsSecretAccessKey string = "aws_secret_access_key"
)
// error messages
var (
ErrorNotFoundAwsAccessKeyID = errors.New(AwsAccessKeyID + ErrorNotFound)
ErrorNotFoundAwsSecretAccessKey = errors.New(AwsSecretAccessKey + ErrorNotFound)
)
// Credential provide credentials
type Credential struct {
ProfileName string
AwsAccessKeyID string
AwsSecretAccessKey string
}
// Credentials has many Credential
type Credentials []Credential
// NewCredentials create a new Credentials instance
func NewCredentials() *Credentials {
return new(Credentials)
}
// Parse credential file
func (c *Credentials) Parse(credentialsFile string) error {
data, err := ini.Load(credentialsFile)
if err != nil {
return err
}
for _, section := range data.Sections() {
if section.Name() == "DEFAULT" {
continue
}
credential := Credential{}
credential.ProfileName = section.Name()
if section.HasKey(AwsAccessKeyID) {
credential.AwsAccessKeyID = section.Key(AwsAccessKeyID).String()
}
if section.HasKey(AwsSecretAccessKey) {
credential.AwsSecretAccessKey = section.Key(AwsSecretAccessKey).String()
}
*c = append(*c, credential)
}
return nil
}
// ProfileNames get name of profiles
func (c *Credentials) ProfileNames() ([]string, error) {
var profileNames []string
for _, credential := range *c {
profileNames = append(profileNames, credential.ProfileName)
}
return profileNames, nil
}
// GetAwsAccessKeyID get aws_access_key_id
func (c *Credentials) GetAwsAccessKeyID(profileName string) (string, error) {
for _, credential := range *c {
if credential.ProfileName == profileName {
return credential.AwsAccessKeyID, nil
}
}
return EmptyString, ErrorNotFoundAwsAccessKeyID
}
// GetAwsSecretAccessKey get aws_secret_access_key
func (c *Credentials) GetAwsSecretAccessKey(profileName string) (string, error) {
for _, credential := range *c {
if credential.ProfileName == profileName {
return credential.AwsSecretAccessKey, nil
}
}
return EmptyString, ErrorNotFoundAwsSecretAccessKey
}
// GetAwsAccessKeyID get aws_access_key_id
func (c *Credential) GetAwsAccessKeyID() string {
return c.AwsAccessKeyID
}
// GetAwsSecretAccessKey get aws_secret_access_key
func (c *Credential) GetAwsSecretAccessKey() string {
return c.AwsSecretAccessKey
}
// GetCredentialsPath provide file path to credentials
func GetCredentialsPath() (string, error) {
credentialsFile, err := homedir.Expand(AwsCredentials)
if err != nil {
return EmptyString, err
}
if os.Getenv(AwsSharedCredentialsFile) != EmptyString {
credentialsFile, err = homedir.Expand(os.Getenv(AwsSharedCredentialsFile))
if err != nil {
return EmptyString, err
}
}
return credentialsFile, nil
}