forked from pingcap/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iam.go
236 lines (196 loc) · 6.2 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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License 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 model
import (
"fmt"
"strings"
"text/template"
"k8s.io/klog"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/model/iam"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/awstasks"
)
// IAMModelBuilder configures IAM objects
type IAMModelBuilder struct {
*KopsModelContext
Lifecycle *fi.Lifecycle
}
var _ fi.ModelBuilder = &IAMModelBuilder{}
const RolePolicyTemplate = `{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "Service": "{{ IAMServiceEC2 }}"},
"Action": "sts:AssumeRole"
}
]
}`
func (b *IAMModelBuilder) Build(c *fi.ModelBuilderContext) error {
// Collect managed Instance Group roles
managedRoles := make(map[kops.InstanceGroupRole]bool)
// Collect Instance Profile ARNs and their associated Instance Group roles
sharedProfileARNsToIGRole := make(map[string]kops.InstanceGroupRole)
for _, ig := range b.InstanceGroups {
if ig.Spec.IAM != nil && ig.Spec.IAM.Profile != nil {
specProfile := fi.StringValue(ig.Spec.IAM.Profile)
if matchingRole, ok := sharedProfileARNsToIGRole[specProfile]; ok {
if matchingRole != ig.Spec.Role {
return fmt.Errorf("Found IAM instance profile assigned to multiple Instance Group roles %v and %v: %v",
ig.Spec.Role, sharedProfileARNsToIGRole[specProfile], specProfile)
}
} else {
sharedProfileARNsToIGRole[specProfile] = ig.Spec.Role
}
} else {
managedRoles[ig.Spec.Role] = true
}
}
// Generate IAM tasks for each shared role
for profileARN, igRole := range sharedProfileARNsToIGRole {
iamName, err := findCustomAuthNameFromArn(profileARN)
if err != nil {
return fmt.Errorf("unable to parse instance profile name from arn %q: %v", profileARN, err)
}
err = b.buildIAMTasks(igRole, iamName, c, true)
if err != nil {
return err
}
}
// Generate IAM tasks for each managed role
for igRole := range managedRoles {
iamName := b.IAMName(igRole)
err := b.buildIAMTasks(igRole, iamName, c, false)
if err != nil {
return err
}
}
return nil
}
func (b *IAMModelBuilder) buildIAMTasks(igRole kops.InstanceGroupRole, iamName string, c *fi.ModelBuilderContext, shared bool) error {
{ // To minimize diff for easier code review
var iamRole *awstasks.IAMRole
{
rolePolicy, err := b.buildAWSIAMRolePolicy()
if err != nil {
return err
}
iamRole = &awstasks.IAMRole{
Name: s(iamName),
Lifecycle: b.Lifecycle,
RolePolicyDocument: fi.WrapResource(rolePolicy),
ExportWithID: s(strings.ToLower(string(igRole)) + "s"),
}
c.AddTask(iamRole)
}
{
iamPolicy := &iam.PolicyResource{
Builder: &iam.PolicyBuilder{
Cluster: b.Cluster,
Role: igRole,
Region: b.Region,
},
}
// This is slightly tricky; we need to know the hosted zone id,
// but we might be creating the hosted zone dynamically.
// TODO: I don't love this technique for finding the task by name & modifying it
dnsZoneTask, found := c.Tasks["DNSZone/"+b.NameForDNSZone()]
if found {
iamPolicy.DNSZone = dnsZoneTask.(*awstasks.DNSZone)
} else {
klog.V(2).Infof("Task %q not found; won't set route53 permissions in IAM", "DNSZone/"+b.NameForDNSZone())
}
t := &awstasks.IAMRolePolicy{
Name: s(iamName),
Lifecycle: b.Lifecycle,
Role: iamRole,
PolicyDocument: iamPolicy,
}
c.AddTask(t)
}
var iamInstanceProfile *awstasks.IAMInstanceProfile
{
iamInstanceProfile = &awstasks.IAMInstanceProfile{
Name: s(iamName),
Lifecycle: b.Lifecycle,
Shared: fi.Bool(shared),
}
c.AddTask(iamInstanceProfile)
}
{
iamInstanceProfileRole := &awstasks.IAMInstanceProfileRole{
Name: s(iamName),
Lifecycle: b.Lifecycle,
InstanceProfile: iamInstanceProfile,
Role: iamRole,
}
c.AddTask(iamInstanceProfileRole)
}
// Generate additional policies if needed, and attach to existing role
{
additionalPolicy := ""
if b.Cluster.Spec.AdditionalPolicies != nil {
additionalPolicies := *(b.Cluster.Spec.AdditionalPolicies)
additionalPolicy = additionalPolicies[strings.ToLower(string(igRole))]
}
additionalPolicyName := "additional." + iamName
t := &awstasks.IAMRolePolicy{
Name: s(additionalPolicyName),
Lifecycle: b.Lifecycle,
Role: iamRole,
}
if additionalPolicy != "" {
p := &iam.Policy{
Version: iam.PolicyDefaultVersion,
}
statements, err := iam.ParseStatements(additionalPolicy)
if err != nil {
return fmt.Errorf("additionalPolicy %q is invalid: %v", strings.ToLower(string(igRole)), err)
}
p.Statement = append(p.Statement, statements...)
policy, err := p.AsJSON()
if err != nil {
return fmt.Errorf("error building IAM policy: %v", err)
}
t.PolicyDocument = fi.WrapResource(fi.NewStringResource(policy))
} else {
t.PolicyDocument = fi.WrapResource(fi.NewStringResource(""))
}
c.AddTask(t)
}
}
return nil
}
// buildAWSIAMRolePolicy produces the AWS IAM role policy for the given role
func (b *IAMModelBuilder) buildAWSIAMRolePolicy() (fi.Resource, error) {
functions := template.FuncMap{
"IAMServiceEC2": func() string {
// IAMServiceEC2 returns the name of the IAM service for EC2 in the current region
// it is ec2.amazonaws.com everywhere but in cn-north, where it is ec2.amazonaws.com.cn
switch b.Region {
case "cn-north-1":
return "ec2.amazonaws.com.cn"
case "cn-northwest-1":
return "ec2.amazonaws.com.cn"
default:
return "ec2.amazonaws.com"
}
},
}
templateResource, err := NewTemplateResource("AWSIAMRolePolicy", RolePolicyTemplate, functions, nil)
if err != nil {
return nil, err
}
return templateResource, nil
}