forked from unprofession-al/scum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_aws.go
175 lines (142 loc) · 4.36 KB
/
type_aws.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"bufio"
"encoding/json"
"fmt"
"os"
"strings"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/sts"
)
const awsprofiletype = "aws"
func init() {
RegisterProfileType(awsprofiletype, NewAWSProfile)
}
type AWSProfile struct {
Profile string `json:"profile"`
AWSAccessKeyID string `json:"aws_access_key_id"`
AWSSecretAccessKey string `json:"aws_secret_access_key"`
}
func NewAWSProfile() Profile {
return &AWSProfile{}
}
func (p *AWSProfile) Describe() string {
return `This profile handles your AWS Access Keys. For details about AWS Access Keys see:
(see https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys)
`
}
func (p *AWSProfile) Capabilities() ProfileCapabilities {
return ProfileCapabilities{
Mount: true,
Rotate: true,
Verify: true,
}
}
func (p *AWSProfile) Type() string {
return awsprofiletype
}
func (p *AWSProfile) Prompt() error {
var err error
reader := bufio.NewReader(os.Stdin)
fmt.Fprintf(os.Stderr, "Profile Name: ")
p.Profile, err = reader.ReadString('\n')
if err != nil {
return nil
}
p.Profile = strings.TrimSpace(p.Profile)
fmt.Fprintf(os.Stderr, "AWSAccessKeyID: ")
p.AWSAccessKeyID, err = reader.ReadString('\n')
if err != nil {
return nil
}
p.AWSAccessKeyID = strings.TrimSpace(p.AWSAccessKeyID)
fmt.Fprintf(os.Stderr, "AWSSecretAccessKey: ")
p.AWSSecretAccessKey, err = reader.ReadString('\n')
if err != nil {
return nil
}
p.AWSSecretAccessKey = strings.TrimSpace(p.AWSSecretAccessKey)
return nil
}
func (p *AWSProfile) Serialize() ([]byte, error) {
return json.MarshalIndent(p, "", " ")
}
func (p *AWSProfile) Deserialize(in []byte) error {
return json.Unmarshal(in, p)
}
func (p *AWSProfile) String() string {
return fmt.Sprintf("[%s]\naws_access_key_id=%s\naws_secret_access_key=%s\n\n", p.Profile, p.AWSAccessKeyID, p.AWSSecretAccessKey)
}
func (p *AWSProfile) SetName(name string) {
p.Profile = name
}
func (p *AWSProfile) Name() string {
return p.Profile
}
func (p *AWSProfile) MountSnippet() (string, string) {
return ".awscredentials", p.String()
}
func (p *AWSProfile) RotateCredentials() ([]byte, error) {
sess, _, err := p.getSession()
if err != nil {
return []byte{}, err
}
iamClient := iam.New(sess)
respListAccessKeys, err := iamClient.ListAccessKeys(&iam.ListAccessKeysInput{})
if err != nil {
return []byte{}, err
}
// Delete Old Access Key
if len(respListAccessKeys.AccessKeyMetadata) == 2 {
keyIndex := 0
if *respListAccessKeys.AccessKeyMetadata[0].AccessKeyId == p.AWSAccessKeyID {
keyIndex = 1
}
// fmt.Println("You have two access keys, which is the max number of access keys.")
_, err := iamClient.DeleteAccessKey(&iam.DeleteAccessKeyInput{
AccessKeyId: respListAccessKeys.AccessKeyMetadata[keyIndex].AccessKeyId,
})
if err != nil {
return []byte{}, err
}
// fmt.Printf("Deleted access key %s.\n", *respListAccessKeys.AccessKeyMetadata[keyIndex].AccessKeyId)
}
// Create the new access key
respCreateAccessKey, err := iamClient.CreateAccessKey(&iam.CreateAccessKeyInput{})
if err != nil {
return []byte{}, err
}
// fmt.Printf("Created access key %s.\n", *respCreateAccessKey.AccessKey.AccessKeyId)
// Todo: Verify
// delete old access key
_, err = iamClient.DeleteAccessKey(&iam.DeleteAccessKeyInput{
AccessKeyId: &p.AWSAccessKeyID,
})
if err != nil {
return []byte{}, err
}
// Update data in memory
p.AWSAccessKeyID = *respCreateAccessKey.AccessKey.AccessKeyId
p.AWSSecretAccessKey = *respCreateAccessKey.AccessKey.SecretAccessKey
return p.Serialize()
}
func (p *AWSProfile) VerifyCredentials() (string, bool) {
_, info, err := p.getSession()
if err != nil {
return err.Error(), false
}
return info, true
}
func (p *AWSProfile) getSession() (*session.Session, string, error) {
os.Setenv("AWS_ACCESS_KEY_ID", p.AWSAccessKeyID)
os.Setenv("AWS_SECRET_ACCESS_KEY", p.AWSSecretAccessKey)
sess := session.Must(session.NewSessionWithOptions(session.Options{}))
// sts get-caller-identity
stsClient := sts.New(sess)
respGetCallerIdentity, err := stsClient.GetCallerIdentity(&sts.GetCallerIdentityInput{})
if err != nil {
return sess, "", fmt.Errorf("error getting caller identity: %s. Is the key disabled?", err.Error())
}
return sess, fmt.Sprintf("Your user ARN is: %s", *respGetCallerIdentity.Arn), nil
}