Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate EKS-related IAM resources #2662

Merged
merged 1 commit into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
320 changes: 164 additions & 156 deletions cmd/clusterawsadm/cloudformation/bootstrap/cluster_api_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,190 +235,198 @@ func (t Template) ControllersPolicy() *infrav1.PolicyDocument {
})
}
}
if !t.Spec.EKS.Disable {
allowedIAMActions := infrav1.Actions{
"iam:GetRole",
"iam:ListAttachedRolePolicies",
}
if t.Spec.EventBridge.Enable {
statement = append(statement, infrav1.StatementEntry{
Effect: infrav1.EffectAllow,
Resource: infrav1.Resources{
"arn:*:ssm:*:*:parameter/aws/service/eks/optimized-ami/*",
},
Effect: infrav1.EffectAllow,
Resource: infrav1.Resources{infrav1.Any},
Action: infrav1.Actions{
"ssm:GetParameter",
"events:DeleteRule",
"events:DescribeRule",
"events:ListTargetsByRule",
"events:PutRule",
"events:PutTargets",
"events:RemoveTargets",
"sqs:CreateQueue",
"sqs:DeleteMessage",
"sqs:DeleteQueue",
"sqs:GetQueueAttributes",
"sqs:GetQueueUrl",
"sqs:ReceiveMessage",
"sqs:SetQueueAttributes",
},
})
}

return &infrav1.PolicyDocument{
Version: infrav1.CurrentVersion,
Statement: statement,
}
}

// ControllersPolicyEKS creates a policy from a template for AWS Controllers.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@johananl I think it would be nice to document the 10 policies limit somewhere in here, to indicate why other resources has not been put in separate policies. We could as well do this in #2271 though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's ok for now. The 10 policies limit should be relatively well understood by folk in the IAM space.

func (t Template) ControllersPolicyEKS() *infrav1.PolicyDocument {
statement := []infrav1.StatementEntry{}

allowedIAMActions := infrav1.Actions{
"iam:GetRole",
"iam:ListAttachedRolePolicies",
}
statement = append(statement, infrav1.StatementEntry{
Effect: infrav1.EffectAllow,
Resource: infrav1.Resources{
"arn:*:ssm:*:*:parameter/aws/service/eks/optimized-ami/*",
},
Action: infrav1.Actions{
"ssm:GetParameter",
},
})

statement = append(statement, infrav1.StatementEntry{
Effect: infrav1.EffectAllow,
Action: infrav1.Actions{
"iam:CreateServiceLinkedRole",
},
Resource: infrav1.Resources{
"arn:*:iam::*:role/aws-service-role/eks.amazonaws.com/AWSServiceRoleForAmazonEKS",
},
Condition: infrav1.Conditions{
infrav1.StringLike: map[string]string{"iam:AWSServiceName": "eks.amazonaws.com"},
},
})

statement = append(statement, infrav1.StatementEntry{
Effect: infrav1.EffectAllow,
Action: infrav1.Actions{
"iam:CreateServiceLinkedRole",
},
Resource: infrav1.Resources{
"arn:*:iam::*:role/aws-service-role/eks-nodegroup.amazonaws.com/AWSServiceRoleForAmazonEKSNodegroup",
},
Condition: infrav1.Conditions{
infrav1.StringLike: map[string]string{"iam:AWSServiceName": "eks-nodegroup.amazonaws.com"},
},
})

statement = append(statement, infrav1.StatementEntry{
Effect: infrav1.EffectAllow,
Action: infrav1.Actions{
"iam:CreateServiceLinkedRole",
},
Resource: infrav1.Resources{
"arn:aws:iam::*:role/aws-service-role/eks-fargate-pods.amazonaws.com/AWSServiceRoleForAmazonEKSForFargate",
},
Condition: infrav1.Conditions{
infrav1.StringLike: map[string]string{"iam:AWSServiceName": "eks-fargate.amazonaws.com"},
},
})

if t.Spec.EKS.AllowIAMRoleCreation {
allowedIAMActions = append(allowedIAMActions, infrav1.Actions{
"iam:DetachRolePolicy",
"iam:DeleteRole",
"iam:CreateRole",
"iam:TagRole",
"iam:AttachRolePolicy",
}...)

statement = append(statement, infrav1.StatementEntry{
Effect: infrav1.EffectAllow,
Action: infrav1.Actions{
"iam:CreateServiceLinkedRole",
"iam:ListOpenIDConnectProviders",
"iam:CreateOpenIDConnectProvider",
"iam:AddClientIDToOpenIDConnectProvider",
"iam:UpdateOpenIDConnectProviderThumbprint",
"iam:DeleteOpenIDConnectProvider",
},
Resource: infrav1.Resources{
"arn:*:iam::*:role/aws-service-role/eks.amazonaws.com/AWSServiceRoleForAmazonEKS",
},
Condition: infrav1.Conditions{
infrav1.StringLike: map[string]string{"iam:AWSServiceName": "eks.amazonaws.com"},
"*",
},
Effect: infrav1.EffectAllow,
})

statement = append(statement, infrav1.StatementEntry{
}
statement = append(statement, []infrav1.StatementEntry{
{
Action: allowedIAMActions,
Resource: infrav1.Resources{
"arn:*:iam::*:role/*",
},
Effect: infrav1.EffectAllow,
}, {
Action: infrav1.Actions{
"iam:CreateServiceLinkedRole",
"iam:GetPolicy",
},
Resource: infrav1.Resources{
"arn:*:iam::*:role/aws-service-role/eks-nodegroup.amazonaws.com/AWSServiceRoleForAmazonEKSNodegroup",
t.generateAWSManagedPolicyARN(eksClusterPolicyName),
},
Condition: infrav1.Conditions{
infrav1.StringLike: map[string]string{"iam:AWSServiceName": "eks-nodegroup.amazonaws.com"},
Effect: infrav1.EffectAllow,
}, {
Action: infrav1.Actions{
"eks:DescribeCluster",
"eks:ListClusters",
"eks:CreateCluster",
"eks:TagResource",
"eks:UpdateClusterVersion",
"eks:DeleteCluster",
"eks:UpdateClusterConfig",
"eks:UntagResource",
"eks:UpdateNodegroupVersion",
"eks:DescribeNodegroup",
"eks:DeleteNodegroup",
"eks:UpdateNodegroupConfig",
"eks:CreateNodegroup",
"eks:AssociateEncryptionConfig",
},
Resource: infrav1.Resources{
"arn:*:eks:*:*:cluster/*",
"arn:*:eks:*:*:nodegroup/*/*/*",
},
})

statement = append(statement, infrav1.StatementEntry{
Effect: infrav1.EffectAllow,
}, {
Action: infrav1.Actions{
"iam:CreateServiceLinkedRole",
"eks:ListAddons",
"eks:CreateAddon",
"eks:DescribeAddonVersions",
"eks:DescribeAddon",
"eks:DeleteAddon",
"eks:UpdateAddon",
"eks:TagResource",
"eks:DescribeFargateProfile",
"eks:CreateFargateProfile",
"eks:DeleteFargateProfile",
},
Resource: infrav1.Resources{
"arn:aws:iam::*:role/aws-service-role/eks-fargate-pods.amazonaws.com/AWSServiceRoleForAmazonEKSForFargate",
"*",
},
Condition: infrav1.Conditions{
infrav1.StringLike: map[string]string{"iam:AWSServiceName": "eks-fargate.amazonaws.com"},
Effect: infrav1.EffectAllow,
}, {
Action: infrav1.Actions{
"iam:PassRole",
},
})

if t.Spec.EKS.AllowIAMRoleCreation {
allowedIAMActions = append(allowedIAMActions, infrav1.Actions{
"iam:DetachRolePolicy",
"iam:DeleteRole",
"iam:CreateRole",
"iam:TagRole",
"iam:AttachRolePolicy",
}...)

statement = append(statement, infrav1.StatementEntry{
Action: infrav1.Actions{
"iam:ListOpenIDConnectProviders",
"iam:CreateOpenIDConnectProvider",
"iam:AddClientIDToOpenIDConnectProvider",
"iam:UpdateOpenIDConnectProviderThumbprint",
"iam:DeleteOpenIDConnectProvider",
},
Resource: infrav1.Resources{
"*",
},
Effect: infrav1.EffectAllow,
})
}
statement = append(statement, []infrav1.StatementEntry{
{
Action: allowedIAMActions,
Resource: infrav1.Resources{
"arn:*:iam::*:role/*",
},
Effect: infrav1.EffectAllow,
}, {
Action: infrav1.Actions{
"iam:GetPolicy",
},
Resource: infrav1.Resources{
t.generateAWSManagedPolicyARN(eksClusterPolicyName),
},
Effect: infrav1.EffectAllow,
}, {
Action: infrav1.Actions{
"eks:DescribeCluster",
"eks:ListClusters",
"eks:CreateCluster",
"eks:TagResource",
"eks:UpdateClusterVersion",
"eks:DeleteCluster",
"eks:UpdateClusterConfig",
"eks:UntagResource",
"eks:UpdateNodegroupVersion",
"eks:DescribeNodegroup",
"eks:DeleteNodegroup",
"eks:UpdateNodegroupConfig",
"eks:CreateNodegroup",
"eks:AssociateEncryptionConfig",
},
Resource: infrav1.Resources{
"arn:*:eks:*:*:cluster/*",
"arn:*:eks:*:*:nodegroup/*/*/*",
},
Effect: infrav1.EffectAllow,
}, {
Action: infrav1.Actions{
"eks:ListAddons",
"eks:CreateAddon",
"eks:DescribeAddonVersions",
"eks:DescribeAddon",
"eks:DeleteAddon",
"eks:UpdateAddon",
"eks:TagResource",
"eks:DescribeFargateProfile",
"eks:CreateFargateProfile",
"eks:DeleteFargateProfile",
},
Resource: infrav1.Resources{
"*",
},
Effect: infrav1.EffectAllow,
}, {
Action: infrav1.Actions{
"iam:PassRole",
},
Resource: infrav1.Resources{
"*",
},
Condition: infrav1.Conditions{
"StringEquals": map[string]string{
"iam:PassedToService": "eks.amazonaws.com",
},
},
Effect: infrav1.EffectAllow,
Resource: infrav1.Resources{
"*",
},
{
Action: infrav1.Actions{
"kms:CreateGrant",
"kms:DescribeKey",
},
Resource: infrav1.Resources{
"*",
},
Effect: infrav1.EffectAllow,
Condition: infrav1.Conditions{
"ForAnyValue:StringLike": map[string]string{
"kms:ResourceAliases": fmt.Sprintf("alias/%s", t.Spec.EKS.KMSAliasPrefix),
},
Condition: infrav1.Conditions{
"StringEquals": map[string]string{
"iam:PassedToService": "eks.amazonaws.com",
},
},
}...)
}

if t.Spec.EventBridge.Enable {
statement = append(statement, infrav1.StatementEntry{
Effect: infrav1.EffectAllow,
Resource: infrav1.Resources{infrav1.Any},
Effect: infrav1.EffectAllow,
},
{
Action: infrav1.Actions{
"events:DeleteRule",
"events:DescribeRule",
"events:ListTargetsByRule",
"events:PutRule",
"events:PutTargets",
"events:RemoveTargets",
"sqs:CreateQueue",
"sqs:DeleteMessage",
"sqs:DeleteQueue",
"sqs:GetQueueAttributes",
"sqs:GetQueueUrl",
"sqs:ReceiveMessage",
"sqs:SetQueueAttributes",
"kms:CreateGrant",
"kms:DescribeKey",
},
})
}
Resource: infrav1.Resources{
"*",
},
Effect: infrav1.EffectAllow,
Condition: infrav1.Conditions{
"ForAnyValue:StringLike": map[string]string{
"kms:ResourceAliases": fmt.Sprintf("alias/%s", t.Spec.EKS.KMSAliasPrefix),
},
},
},
}...)

return &infrav1.PolicyDocument{
Version: infrav1.CurrentVersion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,17 @@ Resources:
Effect: Allow
Resource:
- arn:*:secretsmanager:*:*:secret:aws.cluster.x-k8s.io/*
Version: 2012-10-17
Roles:
- Ref: AWSIAMRoleControllers
- Ref: AWSIAMRoleControlPlane
Type: AWS::IAM::ManagedPolicy
AWSIAMManagedPolicyControllersEKS:
Properties:
Description: For the Kubernetes Cluster API Provider AWS Controllers
ManagedPolicyName: controllers-eks.custom-suffix.com
PolicyDocument:
Statement:
- Action:
- ssm:GetParameter
Effect: Allow
Expand Down
11 changes: 11 additions & 0 deletions cmd/clusterawsadm/cloudformation/bootstrap/fixtures/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,17 @@ Resources:
Effect: Allow
Resource:
- arn:*:secretsmanager:*:*:secret:aws.cluster.x-k8s.io/*
Version: 2012-10-17
Roles:
- Ref: AWSIAMRoleControllers
- Ref: AWSIAMRoleControlPlane
Type: AWS::IAM::ManagedPolicy
AWSIAMManagedPolicyControllersEKS:
Properties:
Description: For the Kubernetes Cluster API Provider AWS Controllers
ManagedPolicyName: controllers-eks.cluster-api-provider-aws.sigs.k8s.io
PolicyDocument:
Statement:
- Action:
- ssm:GetParameter
Effect: Allow
Expand Down
Loading