-
Notifications
You must be signed in to change notification settings - Fork 155
/
role.go
262 lines (210 loc) · 7.74 KB
/
role.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
Copyright 2021 The Kubermatic Kubernetes Platform contributors.
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 aws
import (
"context"
"fmt"
"path"
"github.com/aws/aws-sdk-go-v2/aws/arn"
"github.com/aws/aws-sdk-go-v2/service/iam"
iamtypes "github.com/aws/aws-sdk-go-v2/service/iam/types"
kubermaticv1 "k8c.io/kubermatic/v2/pkg/apis/kubermatic/v1"
"k8c.io/kubermatic/v2/pkg/provider"
"k8s.io/utils/ptr"
)
const (
workerPolicyName = "kubernetes-worker"
controlPlanePolicyName = "kubernetes-control-plane"
)
// /////////////////////////
// worker role (part of the worker instance profile)
func workerRoleName(clusterName string) string {
return fmt.Sprintf("%s%s-worker", resourceNamePrefix, clusterName)
}
func reconcileWorkerRole(ctx context.Context, client *iam.Client, cluster *kubermaticv1.Cluster) (string, error) {
policy, err := getWorkerPolicy(cluster.Name)
if err != nil {
return "", fmt.Errorf("failed to build the worker policy: %w", err)
}
policies := map[string]string{workerPolicyName: policy}
roleName := workerRoleName(cluster.Name)
return ensureRole(ctx, client, cluster, roleName, policies)
}
// /////////////////////////
// control plane role
func controlPlaneRoleName(clusterName string) string {
return fmt.Sprintf("%s%s-control-plane", resourceNamePrefix, clusterName)
}
func reconcileControlPlaneRole(ctx context.Context, client *iam.Client, cluster *kubermaticv1.Cluster, update provider.ClusterUpdater) (*kubermaticv1.Cluster, error) {
policy, err := getControlPlanePolicy(cluster.Name)
if err != nil {
return cluster, fmt.Errorf("failed to build the control plane policy: %w", err)
}
policies := map[string]string{controlPlanePolicyName: policy}
// default the role name
roleNameOrARN := cluster.Spec.Cloud.AWS.ControlPlaneRoleARN
if roleNameOrARN == "" {
roleNameOrARN = controlPlaneRoleName(cluster.Name)
}
// ensure role exists and is assigned to the given policies
roleARN, err := ensureRole(ctx, client, cluster, roleNameOrARN, policies)
if err != nil {
return cluster, err
}
return update(ctx, cluster.Name, func(cluster *kubermaticv1.Cluster) {
cluster.Spec.Cloud.AWS.ControlPlaneRoleARN = roleARN
})
}
func cleanUpControlPlaneRole(ctx context.Context, client *iam.Client, cluster *kubermaticv1.Cluster) error {
// default the role name
roleNameOrARN := cluster.Spec.Cloud.AWS.ControlPlaneRoleARN
if roleNameOrARN == "" {
roleNameOrARN = controlPlaneRoleName(cluster.Name)
}
return deleteRole(ctx, client, cluster, roleNameOrARN, []string{controlPlanePolicyName})
}
// /////////////////////////
// commonly shared functions
func getRole(ctx context.Context, client *iam.Client, roleNameOrARN string) (*iamtypes.Role, error) {
roleName, err := decodeRoleARN(roleNameOrARN)
if err != nil {
return nil, err
}
getRoleInput := &iam.GetRoleInput{
RoleName: ptr.To(roleName),
}
out, err := client.GetRole(ctx, getRoleInput)
if err != nil {
return nil, err
}
return out.Role, nil
}
func decodeRoleARN(nameOrARN string) (string, error) {
if !arn.IsARN(nameOrARN) {
return nameOrARN, nil
}
parsed, err := arn.Parse(nameOrARN)
if err != nil {
return "", fmt.Errorf("invalid role ARN %q: %w", nameOrARN, err)
}
// Roles resource names are stored as "role/...." in the ARN and we need
// to strip the prefix.
return path.Base(parsed.Resource), nil
}
func ensureRole(ctx context.Context, client *iam.Client, cluster *kubermaticv1.Cluster, roleNameOrARN string, policies map[string]string) (string, error) {
// When ensuring an existing role, we are usually called with a full ARN,
// but when creating a role initially, we are called with just the name.
// To create/check the role, we first need to parse the nameOrARN.
roleName, err := decodeRoleARN(roleNameOrARN)
if err != nil {
return "", err
}
// check if it still exists
existingRole, err := getRole(ctx, client, roleName)
if err != nil && !isNotFound(err) {
return "", fmt.Errorf("failed to get role: %w", err)
}
// create missing role
var roleARN string
if isNotFound(err) {
createRoleInput := &iam.CreateRoleInput{
AssumeRolePolicyDocument: ptr.To(assumeRolePolicy),
RoleName: ptr.To(roleName),
Tags: []iamtypes.Tag{iamOwnershipTag(cluster.Name)},
}
output, err := client.CreateRole(ctx, createRoleInput)
if err != nil {
return "", fmt.Errorf("failed to create role: %w", err)
}
roleARN = *output.Role.Arn
} else {
roleARN = *existingRole.Arn
}
// attach policies
for policyName, policyTpl := range policies {
// The AWS API allows us to issue a PUT request, which has the create-or-update/upsert semantics
putRolePolicyInput := &iam.PutRolePolicyInput{
RoleName: ptr.To(roleName),
PolicyName: ptr.To(policyName),
PolicyDocument: ptr.To(policyTpl),
}
if _, err := client.PutRolePolicy(ctx, putRolePolicyInput); err != nil {
return "", fmt.Errorf("failed to ensure policy %q for role %q: %w", policyName, roleNameOrARN, err)
}
}
return roleARN, nil
}
func deleteRole(ctx context.Context, client *iam.Client, cluster *kubermaticv1.Cluster, roleNameOrARN string, policies []string) error {
roleName, err := decodeRoleARN(roleNameOrARN)
if err != nil {
return err
}
// check if it still exists
role, err := getRole(ctx, client, roleName)
if err != nil {
// nothing more to do here
if isNotFound(err) {
return nil
}
return fmt.Errorf("failed to get role: %w", err)
}
owned := hasIAMTag(iamOwnershipTag(cluster.Name), role.Tags)
// delete policies; by default we only delete those that are specified, but when
// we fully own the role, we must remove all policies, regardless of the policies
// parameter
if policies == nil || owned {
// list all custom policies
listPoliciesOut, err := client.ListRolePolicies(ctx, &iam.ListRolePoliciesInput{
RoleName: ptr.To(roleName),
})
if err != nil {
return fmt.Errorf("failed to list policies for role %q: %w", roleNameOrARN, err)
}
policies = listPoliciesOut.PolicyNames
}
// delete policies from role
for _, policyName := range policies {
deleteRolePolicyInput := &iam.DeleteRolePolicyInput{
PolicyName: ptr.To(policyName),
RoleName: ptr.To(roleName),
}
if _, err = client.DeleteRolePolicy(ctx, deleteRolePolicyInput); err != nil {
return fmt.Errorf("failed to delete role policy %q: %w", policyName, err)
}
}
// Deleting the cluster policy above always needs to happen,
// but unless we actually own the role, we must stop here and not
// detach AWS policies or even delete the role entirely.
if !owned {
return nil
}
// detach potential AWS managed policies
listAttachedPoliciesOut, err := client.ListAttachedRolePolicies(ctx, &iam.ListAttachedRolePoliciesInput{
RoleName: ptr.To(roleName),
})
if err != nil {
return fmt.Errorf("failed to list attached policies for role %q: %w", roleNameOrARN, err)
}
for _, policy := range listAttachedPoliciesOut.AttachedPolicies {
detachRolePolicyInput := &iam.DetachRolePolicyInput{
RoleName: ptr.To(roleName),
PolicyArn: policy.PolicyArn,
}
if _, err := client.DetachRolePolicy(ctx, detachRolePolicyInput); err != nil {
return fmt.Errorf("failed to detach policy %q: %w", *policy.PolicyName, err)
}
}
// delete the role itself
_, err = client.DeleteRole(ctx, &iam.DeleteRoleInput{RoleName: ptr.To(roleName)})
return err
}