|
| 1 | +/* |
| 2 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +you may not use this file except in compliance with the License. |
| 4 | +You may obtain a copy of the License at |
| 5 | +
|
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +
|
| 8 | +Unless required by applicable law or agreed to in writing, software |
| 9 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +See the License for the specific language governing permissions and |
| 12 | +limitations under the License. |
| 13 | +*/ |
| 14 | + |
| 15 | +package iam |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + |
| 21 | + "github.com/aws/aws-sdk-go/aws" |
| 22 | + "github.com/aws/aws-sdk-go/service/iam" |
| 23 | + apis "github.com/awslabs/kit/operator/pkg/apis/controlplane/v1alpha1" |
| 24 | + "github.com/awslabs/kit/operator/pkg/awsprovider" |
| 25 | + "github.com/awslabs/kit/operator/pkg/errors" |
| 26 | + "github.com/awslabs/kit/operator/pkg/kubeprovider" |
| 27 | + "go.uber.org/zap" |
| 28 | + |
| 29 | + "knative.dev/pkg/ptr" |
| 30 | +) |
| 31 | + |
| 32 | +type Controller struct { |
| 33 | + iam *awsprovider.IAM |
| 34 | + kubeClient *kubeprovider.Client |
| 35 | +} |
| 36 | + |
| 37 | +// NewController returns a controller for managing IAM resources in AWS |
| 38 | +func NewController(iam *awsprovider.IAM, client *kubeprovider.Client) *Controller { |
| 39 | + return &Controller{iam: iam, kubeClient: client} |
| 40 | +} |
| 41 | + |
| 42 | +var ( |
| 43 | + kitNodeRolePolicies = []string{ |
| 44 | + "arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy", |
| 45 | + "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore", |
| 46 | + "arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy", |
| 47 | + "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly", |
| 48 | + } |
| 49 | +) |
| 50 | + |
| 51 | +func (c *Controller) Reconcile(ctx context.Context, controlPlane *apis.ControlPlane) error { |
| 52 | + role, err := c.getRole(ctx, KitNodeRoleNameFor(controlPlane.ClusterName())) |
| 53 | + if err != nil && !errors.IsIAMObjectDoNotExist(err) { |
| 54 | + return fmt.Errorf("getting IAM role for %v, %w", controlPlane.ClusterName(), err) |
| 55 | + } |
| 56 | + if role == nil { |
| 57 | + role, err = c.createRole(ctx, &iam.CreateRoleInput{ |
| 58 | + AssumeRolePolicyDocument: aws.String(assumeRolePolicyDocument), |
| 59 | + Description: aws.String("Role assumed by dataplane nodes created by KIT operated"), |
| 60 | + RoleName: aws.String(KitNodeRoleNameFor(controlPlane.ClusterName())), |
| 61 | + Tags: generateRoleTags(controlPlane.ClusterName()), |
| 62 | + }) |
| 63 | + if err != nil { |
| 64 | + return fmt.Errorf("creating IAM role for %v, %w", controlPlane.ClusterName(), err) |
| 65 | + } |
| 66 | + zap.S().Infof("[%s] Created IAM Role %v", controlPlane.ClusterName(), aws.StringValue(role.RoleName)) |
| 67 | + } |
| 68 | + if err := role.addRoleToInstanceProfile(ctx, KitNodeRoleNameFor(controlPlane.ClusterName()), |
| 69 | + KitNodeInstanceProfileNameFor(controlPlane.ClusterName())); err != nil { |
| 70 | + return fmt.Errorf("adding instance profile to role, %w", err) |
| 71 | + } |
| 72 | + for _, policy := range kitNodeRolePolicies { |
| 73 | + if err := role.attachPolicy(ctx, policy, KitNodeRoleNameFor(controlPlane.ClusterName())); err != nil { |
| 74 | + return fmt.Errorf("attaching policies to role, %w", err) |
| 75 | + } |
| 76 | + } |
| 77 | + return nil |
| 78 | +} |
| 79 | + |
| 80 | +func (c *Controller) Finalize(ctx context.Context, controlPlane *apis.ControlPlane) error { |
| 81 | + _, err := c.iam.RemoveRoleFromInstanceProfileWithContext(ctx, &iam.RemoveRoleFromInstanceProfileInput{ |
| 82 | + InstanceProfileName: aws.String(KitNodeInstanceProfileNameFor(controlPlane.ClusterName())), |
| 83 | + RoleName: aws.String(KitNodeRoleNameFor(controlPlane.ClusterName())), |
| 84 | + }) |
| 85 | + if err != nil && !errors.IsIAMObjectDoNotExist(err) { |
| 86 | + return fmt.Errorf("removing role from instance profile, %w", err) |
| 87 | + } |
| 88 | + _, err = c.iam.DeleteInstanceProfileWithContext(ctx, &iam.DeleteInstanceProfileInput{ |
| 89 | + InstanceProfileName: aws.String(KitNodeInstanceProfileNameFor(controlPlane.ClusterName())), |
| 90 | + }) |
| 91 | + if err != nil && !errors.IsIAMObjectDoNotExist(err) { |
| 92 | + return fmt.Errorf("deleting instance profile, %w", err) |
| 93 | + } |
| 94 | + |
| 95 | + for _, policy := range kitNodeRolePolicies { |
| 96 | + if _, err = c.iam.DetachRolePolicyWithContext(ctx, &iam.DetachRolePolicyInput{ |
| 97 | + PolicyArn: aws.String(policy), |
| 98 | + RoleName: aws.String(KitNodeRoleNameFor(controlPlane.ClusterName())), |
| 99 | + }); err != nil { |
| 100 | + return fmt.Errorf("detaching policy from role, %w", err) |
| 101 | + } |
| 102 | + } |
| 103 | + _, err = c.iam.DeleteRoleWithContext(ctx, &iam.DeleteRoleInput{ |
| 104 | + RoleName: aws.String(KitNodeRoleNameFor(controlPlane.ClusterName())), |
| 105 | + }) |
| 106 | + if err != nil && !errors.IsIAMObjectDoNotExist(err) { |
| 107 | + return fmt.Errorf("deleting role, %w", err) |
| 108 | + } |
| 109 | + zap.S().Infof("[%s] Deleted IAM Role %v and instance profile", |
| 110 | + controlPlane.ClusterName(), KitNodeRoleNameFor(controlPlane.ClusterName())) |
| 111 | + return nil |
| 112 | +} |
| 113 | + |
| 114 | +func (c *Controller) getRole(ctx context.Context, roleName string) (*role, error) { |
| 115 | + roleOutput, err := c.iam.GetRoleWithContext(ctx, &iam.GetRoleInput{ |
| 116 | + RoleName: aws.String(roleName), |
| 117 | + }) |
| 118 | + if err != nil { |
| 119 | + return nil, fmt.Errorf("getting iam role %v, %w", roleName, err) |
| 120 | + } |
| 121 | + return &role{iam: c.iam, Role: roleOutput.Role}, nil |
| 122 | +} |
| 123 | + |
| 124 | +func (c *Controller) createRole(ctx context.Context, roleInput *iam.CreateRoleInput) (*role, error) { |
| 125 | + roleOutput, err := c.iam.CreateRoleWithContext(ctx, roleInput) |
| 126 | + return &role{iam: c.iam, Role: roleOutput.Role}, err |
| 127 | +} |
| 128 | + |
| 129 | +type role struct { |
| 130 | + iam *awsprovider.IAM |
| 131 | + *iam.Role |
| 132 | +} |
| 133 | + |
| 134 | +func (r *role) addRoleToInstanceProfile(ctx context.Context, roleName, profileName string) error { |
| 135 | + profile, err := createInstanceProfile(ctx, r.iam, profileName) |
| 136 | + if err != nil { |
| 137 | + return fmt.Errorf("creating instance profile, %w", err) |
| 138 | + } |
| 139 | + if profile == nil { |
| 140 | + return fmt.Errorf("instance profile is NIL") |
| 141 | + } |
| 142 | + for _, role := range profile.Roles { |
| 143 | + if aws.StringValue(role.RoleName) == roleName { |
| 144 | + return nil |
| 145 | + } |
| 146 | + } |
| 147 | + _, err = r.iam.AddRoleToInstanceProfile(&iam.AddRoleToInstanceProfileInput{ |
| 148 | + InstanceProfileName: aws.String(profileName), |
| 149 | + RoleName: aws.String(roleName), |
| 150 | + }) |
| 151 | + return err |
| 152 | +} |
| 153 | + |
| 154 | +func (r *role) attachPolicy(ctx context.Context, policyARN, roleName string) error { |
| 155 | + _, err := r.iam.AttachRolePolicy(&iam.AttachRolePolicyInput{ |
| 156 | + PolicyArn: aws.String(policyARN), |
| 157 | + RoleName: aws.String(roleName), |
| 158 | + }) |
| 159 | + if errors.IsIAMObjectAlreadyExist(err) { |
| 160 | + return nil |
| 161 | + } |
| 162 | + return err |
| 163 | +} |
| 164 | + |
| 165 | +func createInstanceProfile(ctx context.Context, iamAPI *awsprovider.IAM, profileName string) (*iam.InstanceProfile, error) { |
| 166 | + profile, err := iamAPI.CreateInstanceProfileWithContext(ctx, &iam.CreateInstanceProfileInput{ |
| 167 | + InstanceProfileName: aws.String(profileName), |
| 168 | + }) |
| 169 | + if errors.IsIAMObjectAlreadyExist(err) { |
| 170 | + output, err := iamAPI.GetInstanceProfileWithContext(ctx, &iam.GetInstanceProfileInput{ |
| 171 | + InstanceProfileName: aws.String(profileName), |
| 172 | + }) |
| 173 | + if err != nil { |
| 174 | + return nil, err |
| 175 | + } |
| 176 | + return output.InstanceProfile, nil |
| 177 | + } |
| 178 | + if err != nil { |
| 179 | + return nil, fmt.Errorf("creating instance profile, %w", err) |
| 180 | + } |
| 181 | + return profile.InstanceProfile, nil |
| 182 | +} |
| 183 | + |
| 184 | +func KitNodeRoleNameFor(clusterName string) string { |
| 185 | + return fmt.Sprintf("KitDataplaneNodes-%s-cluster", clusterName) |
| 186 | +} |
| 187 | + |
| 188 | +func KitNodeInstanceProfileNameFor(clusterName string) string { |
| 189 | + return fmt.Sprintf("KitDataplaneNodesProfile-%s-cluster", clusterName) |
| 190 | +} |
| 191 | + |
| 192 | +func generateRoleTags(clusterName string) []*iam.Tag { |
| 193 | + return []*iam.Tag{{ |
| 194 | + Key: ptr.String(fmt.Sprintf("kubernetes.io/cluster/%s", clusterName)), |
| 195 | + Value: ptr.String("owned"), |
| 196 | + }} |
| 197 | +} |
| 198 | + |
| 199 | +// KitNodeRole is assumed by the nodes provisioned by kit-operator for dataplane |
| 200 | +const assumeRolePolicyDocument = `{ |
| 201 | + "Version": "2012-10-17", |
| 202 | + "Statement": [ |
| 203 | + { |
| 204 | + "Action": "sts:AssumeRole", |
| 205 | + "Effect": "Allow", |
| 206 | + "Principal": { |
| 207 | + "Service": "ec2.amazonaws.com" |
| 208 | + } |
| 209 | + } |
| 210 | + ] |
| 211 | +}` |
0 commit comments