-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
190 lines (170 loc) · 5.37 KB
/
main.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
/*
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package main
import (
"bytes"
"errors"
"flag"
"fmt"
"log"
"strings"
"text/template"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/eks"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/sts"
)
const (
defaultNamespace = "prometheus"
defaultIAMRole = "EKS-AMP-ServiceAccount"
defaultIAMRoleDescription = "IAM role to be used by a K8s service account with write access to AMP"
ampRemoteWritePolicy = "arn:aws:iam::aws:policy/AmazonPrometheusRemoteWriteAccess"
)
var (
cluster string // EKS cluster
region string // AWS region, optional
namespace string // Kubernetes namespace, optional
serviceAccount string // Kubernetes service account, optional
role string // IAM role name to create, optional
)
func main() {
flag.StringVar(&cluster, "cluster", "", "EKS cluster name")
flag.StringVar(®ion, "region", "", "EKS cluster's region")
flag.StringVar(&namespace, "namespace", defaultNamespace, "EKS namespace to restrict the IAM policy for")
flag.StringVar(&serviceAccount, "service-account", "", "EKS service account")
flag.StringVar(&role, "role", "", "IAM role to be created or updated")
flag.Usage = func() {
usageText(0)
}
flag.Parse()
if cluster == "" {
usageText(1)
}
if region == "" {
reg, err := defaultRegion()
if err != nil {
log.Fatalf("Cannot identify the default region: %v", err)
}
region = reg
}
if serviceAccount == "" {
serviceAccount = namespace
}
if role == "" {
role = fmt.Sprintf("%s-%s-%s-%s-%s",
defaultIAMRole, region, cluster, namespace, serviceAccount)
}
cfg := &aws.Config{}
if region != "" {
cfg.Region = aws.String(region)
}
sess, err := session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
AssumeRoleTokenProvider: stscreds.StdinTokenProvider,
Config: *cfg,
})
if err != nil {
log.Fatalf("Cannot create session: %v", err)
}
if err := createRole(sess); err != nil {
log.Fatalf("Cannot create IAM role: %v", err)
}
log.Printf("Role %q is created.", role)
}
func createRole(sess *session.Session) error {
iamSvc := iam.New(sess)
eksSvc := eks.New(sess)
stsSvc := sts.New(sess)
clusterOut, err := eksSvc.DescribeCluster(&eks.DescribeClusterInput{
Name: aws.String(cluster),
})
if err != nil {
return fmt.Errorf("failed to find the EKS cluster: %v", err)
}
callerIdentityOut, err := stsSvc.GetCallerIdentity(&sts.GetCallerIdentityInput{})
if err != nil {
return fmt.Errorf("failed to get caller's identity: %v", err)
}
trustDoc := bytes.NewBuffer(nil)
if err := trustDocTmpl.Execute(trustDoc, &trustDocVars{
Account: *callerIdentityOut.Account,
Namespace: namespace,
ServiceAccount: serviceAccount,
OIDC: strings.ReplaceAll(*clusterOut.Cluster.Identity.Oidc.Issuer, "https://", ""),
}); err != nil {
return fmt.Errorf("failed to generate the trust relationship document: %v", err)
}
_, err = iamSvc.CreateRole(&iam.CreateRoleInput{
RoleName: aws.String(role),
AssumeRolePolicyDocument: aws.String(trustDoc.String()),
Description: aws.String(defaultIAMRoleDescription),
})
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
if aerr.Code() != iam.ErrCodeEntityAlreadyExistsException {
return fmt.Errorf("failed to create the IAM role: %v", err)
}
// TODO(jbd): Instead of returning an error, validate the document.
return fmt.Errorf("role %q already exists, delete it manually to recreate", role)
}
}
_, err = iamSvc.AttachRolePolicy(&iam.AttachRolePolicyInput{
PolicyArn: aws.String(ampRemoteWritePolicy),
RoleName: aws.String(role),
})
if err != nil {
return fmt.Errorf("failed to attach the policy to the role: %v", err)
}
return nil
}
func defaultRegion() (string, error) {
sess, err := session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
})
if err != nil {
return "", fmt.Errorf("failed to create session: %v", err)
}
defaultRegion := sess.Config.Region
if defaultRegion == nil {
return "", errors.New("no default region is set")
}
return *defaultRegion, nil
}
var trustDocTmpl = template.Must(template.New("trust-doc").Parse(`{
"Version": "2012-10-17",
"Statement": [
{
"Effect":"Allow",
"Principal":{
"Federated": "arn:aws:iam::{{.Account}}:oidc-provider/{{.OIDC}}"
},
"Action":"sts:AssumeRoleWithWebIdentity",
"Condition":{
"StringEquals": {
"{{.OIDC}}:sub": "system:serviceaccount:{{.Namespace}}:{{.ServiceAccount}}"
}
}
}
]
}`))
type trustDocVars struct {
Account string
Namespace string
ServiceAccount string
OIDC string
}