/
creds.go
194 lines (178 loc) · 5.04 KB
/
creds.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package cmd
import (
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/iam"
"github.com/mxk/go-cli"
"github.com/mxk/go-cloud/aws/arn"
"github.com/mxk/go-cloud/aws/awsx"
"github.com/mxk/go-cloud/aws/iamx"
"github.com/mxk/oktapus/op"
"github.com/mxk/oktapus/table"
)
var credsCli = cli.Main.Add(&cli.Info{
Name: "creds",
Usage: "[options] account-spec",
Summary: "Get account credentials",
MinArgs: 1,
MaxArgs: 1,
New: func() cli.Cmd {
return &credsCmd{
Dur: 5 * time.Minute,
Policy: "AdministratorAccess",
}
},
})
type credsCmd struct {
OutFmt
Dur time.Duration `flag:"Minimum credential validity <duration>"`
Policy string `flag:"Attach managed policy <name> or ARN to user"`
Tmp bool `flag:"Delete user automatically when the account is freed"`
User string `flag:"Create new access keys for the <name>d IAM user"`
Spec string
}
func (*credsCmd) Info() *cli.Info { return credsCli }
func (*credsCmd) Help(w *cli.Writer) {
w.Text(`
Get account credentials.
By default, this command returns temporary credentials for all accounts that
match the spec. Credentials are cached and renewed automatically when they
are set to expire within 5 minutes. You can increase this duration with -dur
(e.g. -dur=30m) or force unconditional renewal with a negative duration
(e.g. -dur=-1s).
If -user is specified, the command creates long-term IAM access keys for new
or existing IAM users. If -tmp is specified, the users will be automatically
deleted when the account is freed. If -policy is empty, user policies are
not modified (default is to attach AdministratorAccess policy).
`)
accountSpecHelp(w)
}
func (cmd *credsCmd) Main(args []string) error {
cmd.Spec = args[0]
// TODO: Set non-zero exit code for any error
return op.RunAndPrint(cmd)
}
func (cmd *credsCmd) Run(ctx *op.Ctx) (interface{}, error) {
path, name, err := splitPathName(cmd.User, cmd.Tmp)
if err != nil {
return nil, err
}
attachPolicy, err := getManagedPolicy(ctx.Ident().Partition(), cmd.Policy)
if err != nil {
return nil, err
}
acs, err := ctx.Match(cmd.Spec)
if err != nil {
return nil, err
}
if cmd.User != "" {
cmd.Dur = minDur
}
out := listCreds(acs.EnsureCreds(cmd.Dur))
if cmd.User == "" {
return out, nil
}
km := newKeyMaker(path, name, attachPolicy)
acs.Map(func(i int, ac *op.Account) error {
co := out[i]
if co.Error != "" {
return nil
} else if !ac.CredsValid() {
co.Error = explainError(op.ErrNoAccess)
return nil
}
out, err := km.exec(ac.IAM)
*co = credsOutput{
Account: co.Account,
Name: co.Name,
Error: explainError(err),
}
if err == nil {
co.AccessKeyID = aws.StringValue(out.AccessKey.AccessKeyId)
co.SecretAccessKey = aws.StringValue(out.AccessKey.SecretAccessKey)
}
return nil
})
return out, nil
}
// credsOutput provides account credentials to the user.
type credsOutput struct {
Account string
Name string
Expires expTime
AccessKeyID string
SecretAccessKey string
SessionToken string `json:",omitempty" printer:",width=1,last"`
Error string `json:",omitempty"`
}
func listCreds(acs op.Accounts) []*credsOutput {
out := make([]*credsOutput, len(acs))
for i, ac := range acs.CredsOrErr() {
err := ac.Err
if ac.CredsValid() {
err = nil
}
co := &credsOutput{
Account: ac.ID,
Name: ac.Name,
Error: explainError(err),
}
if err == nil {
cr, _ := ac.CredsProvider().Creds()
co.Expires = expTime{cr.Expires}
co.AccessKeyID = cr.AccessKeyID
co.SecretAccessKey = cr.SecretAccessKey
co.SessionToken = cr.SessionToken
}
out[i] = co
}
return out
}
func (o *credsOutput) PrintRow(p *table.Printer) {
if o.Error == "" {
table.PrintRow(p, o)
} else {
p.PrintCol(0, o.Account, true)
p.PrintCol(1, o.Name, true)
p.PrintErr(o.Error)
}
}
// keyMaker creates new IAM user access keys.
type keyMaker struct {
user iam.CreateUserInput
pol iam.AttachUserPolicyInput
key iam.CreateAccessKeyInput
}
func newKeyMaker(path, name string, attachPolicy arn.ARN) *keyMaker {
userName := aws.String(name)
return &keyMaker{
iam.CreateUserInput{Path: aws.String(path), UserName: userName},
iam.AttachUserPolicyInput{
PolicyArn: arn.String(attachPolicy),
UserName: userName,
},
iam.CreateAccessKeyInput{UserName: userName},
}
}
func (m *keyMaker) exec(c iamx.Client) (*iam.CreateAccessKeyOutput, error) {
if _, err := c.CreateUserRequest(&m.user).Send(); err != nil {
if awsx.ErrCode(err) != iam.ErrCodeEntityAlreadyExistsException {
return nil, err
}
// Existing user must have identical path to create new access key
in := iam.GetUserInput{UserName: m.user.UserName}
u, err := c.GetUserRequest(&in).Send()
if err != nil {
return nil, err
}
if aws.StringValue(u.User.Path) != aws.StringValue(m.user.Path) {
return nil, op.Error("user path mismatch")
}
}
if arn.Value(m.pol.PolicyArn) != "" {
if _, err := c.AttachUserPolicyRequest(&m.pol).Send(); err != nil {
return nil, err
}
}
return c.CreateAccessKeyRequest(&m.key).Send()
}