Skip to content

Migrate EBS CSI policy to AmazonEBSCSIDriverPolicy managed policy #8422

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion pkg/actions/addon/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,7 @@ var _ = Describe("Create", func() {
Expect(rsr).To(BeAssignableToTypeOf(&builder.IAMRoleResourceSet{}))
output, err := rsr.(*builder.IAMRoleResourceSet).RenderJSON()
Expect(err).NotTo(HaveOccurred())
Expect(string(output)).To(ContainSubstring("PolicyEBSCSIController"))
Expect(string(output)).To(ContainSubstring("service-role/AmazonEBSCSIDriverPolicy"))
rsr.(*builder.IAMRoleResourceSet).OutputRole = "arn:aws:iam::111122223333:role/role-name-1"
return nil
}
Expand Down
1 change: 1 addition & 0 deletions pkg/cfn/builder/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
iamPolicyAmazonEC2ContainerRegistryPullOnly = "AmazonEC2ContainerRegistryPullOnly"
iamPolicyCloudWatchAgentServerPolicy = "CloudWatchAgentServerPolicy"
iamPolicyAmazonSSMManagedInstanceCore = "AmazonSSMManagedInstanceCore"
iamPolicyAmazonEBSCSIDriverPolicy = "service-role/AmazonEBSCSIDriverPolicy"

iamPolicyAmazonEKSFargatePodExecutionRolePolicy = "AmazonEKSFargatePodExecutionRolePolicy"
)
Expand Down
12 changes: 6 additions & 6 deletions pkg/cfn/builder/iam_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ func createWellKnownPolicies(wellKnownPolicies api.WellKnownPolicies) ([]managed
)
}
if wellKnownPolicies.EBSCSIController {
customPolicies = append(customPolicies,
customPolicyForRole{Name: "PolicyEBSCSIController", Statements: ebsStatements()},
managedPolicies = append(managedPolicies,
managedPolicyForRole{name: iamPolicyAmazonEBSCSIDriverPolicy},
)
}
if wellKnownPolicies.EFSCSIController {
Expand Down Expand Up @@ -125,10 +125,6 @@ func createRole(cfnTemplate cfnTemplate, clusterIAMConfig *api.ClusterIAM, iamCo
cfnTemplate.attachAllowPolicy("PolicyAppMeshPreview", refIR, appMeshStatements("appmesh-preview:*"))
}

if api.IsEnabled(iamConfig.WithAddonPolicies.EBS) {
cfnTemplate.attachAllowPolicy("PolicyEBS", refIR, ebsStatements())
}

if api.IsEnabled(iamConfig.WithAddonPolicies.FSX) {
cfnTemplate.attachAllowPolicy("PolicyFSX", refIR, fsxStatements())
cfnTemplate.attachAllowPolicy("PolicyServiceLinkRole", refIR, serviceLinkRoleStatements())
Expand Down Expand Up @@ -178,6 +174,10 @@ func makeManagedPolicies(iamCluster *api.ClusterIAM, iamConfig *api.NodeGroupIAM
managedPolicyNames.Insert(iamPolicyCloudWatchAgentServerPolicy)
}

if api.IsEnabled(iamConfig.WithAddonPolicies.EBS) {
managedPolicyNames.Insert(iamPolicyAmazonEBSCSIDriverPolicy)
}

for _, policyARN := range iamConfig.AttachPolicyARNs {
parsedARN, err := arn.Parse(policyARN)
if err != nil {
Expand Down
152 changes: 6 additions & 146 deletions pkg/cfn/builder/iam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,20 +283,22 @@ var _ = Describe("template builder for IAM", func() {

Expect(t.Description).To(Equal("IAM role for serviceaccount \"default/sa-1\" [created and managed by eksctl]"))

Expect(t.Resources).To(HaveLen(10))
Expect(t.Resources).To(HaveLen(9))
Expect(t.Outputs).To(HaveLen(1))

Expect(t).To(HaveResource(outputs.IAMServiceAccountRoleName, "AWS::IAM::Role"))

Expect(t).To(HaveResourceWithPropertyValue(outputs.IAMServiceAccountRoleName, "AssumeRolePolicyDocument", expectedServiceAccountAssumeRolePolicyDocument))
Expect(t).To(HaveResourceWithPropertyValue(outputs.IAMServiceAccountRoleName, "ManagedPolicyArns", `[
{
{
"Fn::Sub": "arn:${AWS::Partition}:iam::aws:policy/AmazonEC2ContainerRegistryPowerUser"
}
},
{
"Fn::Sub": "arn:${AWS::Partition}:iam::aws:policy/service-role/AmazonEBSCSIDriverPolicy"
}
]`))
Expect(t).To(HaveOutputWithValue(outputs.IAMServiceAccountRoleName, `{ "Fn::GetAtt": "Role1.Arn" }`))
Expect(t).To(HaveResourceWithPropertyValue("PolicyAWSLoadBalancerController", "PolicyDocument", expectedAWSLoadBalancerControllerPolicyDocument))
Expect(t).To(HaveResourceWithPropertyValue("PolicyEBSCSIController", "PolicyDocument", expectedEbsPolicyDocument))
})

It("can parse an iamserviceaccount addon template", func() {
Expand Down Expand Up @@ -744,145 +746,3 @@ const expectedAWSLoadBalancerControllerPolicyDocument = `{
],
"Version": "2012-10-17"
}`

const expectedEbsPolicyDocument = `{
"Statement": [
{
"Action": [
"ec2:CreateSnapshot",
"ec2:AttachVolume",
"ec2:DetachVolume",
"ec2:ModifyVolume",
"ec2:DescribeAvailabilityZones",
"ec2:DescribeInstances",
"ec2:DescribeSnapshots",
"ec2:DescribeTags",
"ec2:DescribeVolumes",
"ec2:DescribeVolumesModifications"
],
"Effect": "Allow",
"Resource": "*"
},
{
"Action": [
"ec2:CreateTags"
],
"Condition": {
"StringEquals": {
"ec2:CreateAction": [
"CreateVolume",
"CreateSnapshot"
]
}
},
"Effect": "Allow",
"Resource": [
{
"Fn::Sub": "arn:${AWS::Partition}:ec2:*:*:volume/*"
},
{
"Fn::Sub": "arn:${AWS::Partition}:ec2:*:*:snapshot/*"
}
]
},
{
"Action": [
"ec2:DeleteTags"
],
"Effect": "Allow",
"Resource": [
{
"Fn::Sub": "arn:${AWS::Partition}:ec2:*:*:volume/*"
},
{
"Fn::Sub": "arn:${AWS::Partition}:ec2:*:*:snapshot/*"
}
]
},
{
"Action": [
"ec2:CreateVolume"
],
"Condition": {
"StringLike": {
"aws:RequestTag/ebs.csi.aws.com/cluster": "true"
}
},
"Effect": "Allow",
"Resource": "*"
},
{
"Action": [
"ec2:CreateVolume"
],
"Condition": {
"StringLike": {
"aws:RequestTag/CSIVolumeName": "*"
}
},
"Effect": "Allow",
"Resource": "*"
},
{
"Action": [
"ec2:DeleteVolume"
],
"Condition": {
"StringLike": {
"ec2:ResourceTag/ebs.csi.aws.com/cluster": "true"
}
},
"Effect": "Allow",
"Resource": "*"
},
{
"Action": [
"ec2:DeleteVolume"
],
"Condition": {
"StringLike": {
"ec2:ResourceTag/CSIVolumeName": "*"
}
},
"Effect": "Allow",
"Resource": "*"
},
{
"Action": [
"ec2:DeleteVolume"
],
"Condition": {
"StringLike": {
"ec2:ResourceTag/kubernetes.io/created-for/pvc/name": "*"
}
},
"Effect": "Allow",
"Resource": "*"
},
{
"Action": [
"ec2:DeleteSnapshot"
],
"Condition": {
"StringLike": {
"ec2:ResourceTag/CSIVolumeSnapshotName": "*"
}
},
"Effect": "Allow",
"Resource": "*"
},
{
"Action": [
"ec2:DeleteSnapshot"
],
"Condition": {
"StringLike": {
"ec2:ResourceTag/ebs.csi.aws.com/cluster": "true"
}
},
"Effect": "Allow",
"Resource": "*"
}
],
"Version": "2012-10-17"
}`
8 changes: 3 additions & 5 deletions pkg/cfn/builder/nodegroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,11 +467,9 @@ var _ = Describe("Unmanaged NodeGroup Template Builder", func() {
ng.IAM.WithAddonPolicies.EBS = aws.Bool(true)
})

It("adds PolicyEBS to the role", func() {
Expect(ngTemplate.Resources).To(HaveKey("PolicyEBS"))

Expect(ngTemplate.Resources["PolicyEBS"].Properties.Roles).To(HaveLen(1))
Expect(isRefTo(ngTemplate.Resources["PolicyEBS"].Properties.Roles[0], "NodeInstanceRole")).To(BeTrue())
It("adds the AmazonEBSCSIDriverPolicy managed policy to the role", func() {
Expect(ngTemplate.Resources["NodeInstanceRole"].Properties.ManagedPolicyArns).To(HaveLen(5))
Expect(ngTemplate.Resources["NodeInstanceRole"].Properties.ManagedPolicyArns).To(ContainElement(makePolicyARNRef("service-role/AmazonEBSCSIDriverPolicy")))
})
})

Expand Down
136 changes: 0 additions & 136 deletions pkg/cfn/builder/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,142 +368,6 @@ func appMeshStatements(appendAction string) []cft.MapOfInterfaces {
}
}

func ebsStatements() []cft.MapOfInterfaces {
return []cft.MapOfInterfaces{
{
"Effect": "Allow",
"Action": []string{
"ec2:CreateSnapshot",
"ec2:AttachVolume",
"ec2:DetachVolume",
"ec2:ModifyVolume",
"ec2:DescribeAvailabilityZones",
"ec2:DescribeInstances",
"ec2:DescribeSnapshots",
"ec2:DescribeTags",
"ec2:DescribeVolumes",
"ec2:DescribeVolumesModifications",
},
"Resource": "*",
},
{
"Effect": "Allow",
"Action": []string{
"ec2:CreateTags",
},
"Resource": []*gfnt.Value{
addARNPartitionPrefix("ec2:*:*:volume/*"),
addARNPartitionPrefix("ec2:*:*:snapshot/*"),
},
"Condition": cft.MapOfInterfaces{
"StringEquals": cft.MapOfInterfaces{
"ec2:CreateAction": []string{
"CreateVolume",
"CreateSnapshot",
},
},
},
},
{
"Effect": "Allow",
"Action": []string{
"ec2:DeleteTags",
},
"Resource": []*gfnt.Value{
addARNPartitionPrefix("ec2:*:*:volume/*"),
addARNPartitionPrefix("ec2:*:*:snapshot/*"),
},
},
{
"Effect": "Allow",

"Action": []string{

"ec2:CreateVolume",
},
"Resource": "*",
"Condition": cft.MapOfInterfaces{
"StringLike": cft.MapOfInterfaces{
"aws:RequestTag/ebs.csi.aws.com/cluster": "true",
},
},
},
{
"Effect": "Allow",
"Action": []string{
"ec2:CreateVolume",
},
"Resource": "*",
"Condition": cft.MapOfInterfaces{
"StringLike": cft.MapOfInterfaces{
"aws:RequestTag/CSIVolumeName": "*",
},
},
},
{
"Effect": "Allow",
"Action": []string{
"ec2:DeleteVolume",
},
"Resource": "*",
"Condition": cft.MapOfInterfaces{
"StringLike": cft.MapOfInterfaces{
"ec2:ResourceTag/ebs.csi.aws.com/cluster": "true",
},
},
},
{
"Effect": "Allow",

"Action": []string{
"ec2:DeleteVolume",
},
"Resource": "*",
"Condition": cft.MapOfInterfaces{
"StringLike": cft.MapOfInterfaces{
"ec2:ResourceTag/CSIVolumeName": "*",
},
},
},
{
"Effect": "Allow",
"Action": []string{
"ec2:DeleteVolume",
},
"Resource": "*",
"Condition": cft.MapOfInterfaces{
"StringLike": cft.MapOfInterfaces{
"ec2:ResourceTag/kubernetes.io/created-for/pvc/name": "*",
},
},
},
{
"Effect": "Allow",
"Action": []string{
"ec2:DeleteSnapshot",
},
"Resource": "*",
"Condition": cft.MapOfInterfaces{
"StringLike": cft.MapOfInterfaces{
"ec2:ResourceTag/CSIVolumeSnapshotName": "*",
},
},
},
{
"Effect": "Allow",
"Action": []string{
"ec2:DeleteSnapshot",
},
"Resource": "*",
"Condition": cft.MapOfInterfaces{
"StringLike": cft.MapOfInterfaces{
"ec2:ResourceTag/ebs.csi.aws.com/cluster": "true",
},
},
},
}
}

func serviceLinkRoleStatements() []cft.MapOfInterfaces {
return []cft.MapOfInterfaces{
{
Expand Down
Loading