-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiam.go
71 lines (58 loc) · 1.68 KB
/
iam.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
package iam
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/iam/iamiface"
log "github.com/sirupsen/logrus"
)
type PolicyDocument struct {
Version string
Statement []StatementEntry
}
type StatementEntry struct {
Sid string `json:",omitempty"`
Effect string
Action []string
Resource string
Condition Condition `json:",omitempty"`
}
// Condition maps a condition operator to the condition-key/condition-value statement
// ie. "{ "StringEquals" : { "aws:username" : "johndoe" }}"
// for more information, see https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_condition.html
type Condition map[string]ConditionStatement
// ConditionStatement maps condition-key to condition-value
// ie. "{ "aws:username" : "johndoe" }"
type ConditionStatement map[string]string
type IAM struct {
session *session.Session
Service iamiface.IAMAPI
}
type IAMOption func(*IAM)
func New(opts ...IAMOption) IAM {
i := IAM{}
for _, opt := range opts {
opt(&i)
}
if i.session != nil {
i.Service = iam.New(i.session)
}
return i
}
func WithSession(sess *session.Session) IAMOption {
return func(i *IAM) {
log.Debug("using aws session")
i.session = sess
}
}
func WithCredentials(key, secret, token, region string) IAMOption {
return func(i *IAM) {
log.Debugf("creating new session with key id %s in region %s", key, region)
sess := session.Must(session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentials(key, secret, token),
Region: aws.String(region),
}))
i.session = sess
}
}