-
Notifications
You must be signed in to change notification settings - Fork 0
/
iam.go
152 lines (117 loc) · 3.85 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
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
package certutils
import (
"fmt"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/olekukonko/tablewriter"
)
type IAM struct {
client *iam.IAM
}
type IAMDescription struct {
name string
id string
path string
arn string
}
func NewIAM(sess *session.Session) *IAM {
return &IAM{
client: iam.New(sess),
}
}
func createIAMUploadServerCertificateInput(body, chain, pkey, path, name string) *iam.UploadServerCertificateInput {
input := &iam.UploadServerCertificateInput{}
input.SetCertificateBody(body)
input.SetCertificateChain(chain)
input.SetPath(path)
input.SetPrivateKey(pkey)
input.SetServerCertificateName(name)
return input
}
func (i *IAM) Upload(cert, chain, pkey []byte, path, name string) (string, error) {
out, err := i.client.UploadServerCertificate(createIAMUploadServerCertificateInput(string(cert), string(chain), string(pkey), path, name))
return fmt.Sprintf("Uploaded %s %s", name, *out.ServerCertificateMetadata.Arn), err
}
func createIAMListServerCertificatesInput(marker string, maxItems int64, path string) *iam.ListServerCertificatesInput {
input := &iam.ListServerCertificatesInput{}
if marker != "" {
input.SetMarker(marker)
}
if maxItems > 0 {
input.SetMaxItems(maxItems)
}
if path != "" {
input.SetPathPrefix(path)
}
return input
}
func (i *IAM) List(marker string, maxItems int64, path string) ([]IAMDescription, error) {
out, err := i.client.ListServerCertificates(createIAMListServerCertificatesInput(marker, maxItems, path))
if err != nil {
return []IAMDescription{}, err
}
descs := make([]IAMDescription, 0, len(out.ServerCertificateMetadataList))
for _, metadata := range out.ServerCertificateMetadataList {
desc := IAMDescription{
name: *metadata.ServerCertificateName,
id: *metadata.ServerCertificateId,
path: *metadata.Path,
arn: *metadata.Arn,
}
descs = append(descs, desc)
}
return descs, err
}
func (i *IAM) ListMap(marker string, maxItems int64, path string) (map[string]IAMDescription, error) {
out, err := i.client.ListServerCertificates(createIAMListServerCertificatesInput(marker, maxItems, path))
if err != nil {
return map[string]IAMDescription{}, err
}
certs := make(map[string]IAMDescription, 0)
for _, metadata := range out.ServerCertificateMetadataList {
certs[*metadata.ServerCertificateId] = IAMDescription{
name: *metadata.ServerCertificateName,
id: *metadata.ServerCertificateId,
path: *metadata.Path,
arn: *metadata.Arn,
}
}
return certs, err
}
func (i *IAM) ListNames(marker string, maxItems int64, path string) ([]string, error) {
descs, err := i.List(marker, maxItems, path)
if err != nil {
return []string{}, err
}
names := make([]string, 0, len(descs))
for _, desc := range descs {
names = append(names, desc.name)
}
return names, err
}
func createIAMUpdateServerCertificateInput(newPath, newName, name string) *iam.UpdateServerCertificateInput {
input := &iam.UpdateServerCertificateInput{}
input.SetNewPath(newPath)
input.SetNewServerCertificateName(newName)
input.SetServerCertificateName(name)
return input
}
func (i *IAM) Update(newPath, newName, name string) (string, error) {
_, err := i.client.UpdateServerCertificate(createIAMUpdateServerCertificateInput(newPath, newName, name))
return fmt.Sprintf("Updated %s -> %s", name, newName), err
}
func (i *IAM) Delete(name string) (string, error) {
input := &iam.DeleteServerCertificateInput{ServerCertificateName: aws.String(name)}
_, err := i.client.DeleteServerCertificate(input)
return fmt.Sprintf("Deleted %s", name), err
}
func (i *IAM) ReadableList(descs []IAMDescription) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "ID", "Path", "Arn"})
for _, desc := range descs {
table.Append([]string{desc.name, desc.id, desc.path, desc.arn})
}
table.Render()
}