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

Limit the IAM EC2 policy for the master nodes #3186

Merged
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
72 changes: 55 additions & 17 deletions pkg/model/iam/iam_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@ type IAMStatementEffect string
const IAMStatementEffectAllow IAMStatementEffect = "Allow"
const IAMStatementEffectDeny IAMStatementEffect = "Deny"

type Condition map[string]interface{}

type IAMStatement struct {
Effect IAMStatementEffect
Action stringorslice.StringOrSlice
Resource stringorslice.StringOrSlice
Effect IAMStatementEffect
Action stringorslice.StringOrSlice
Resource stringorslice.StringOrSlice
Condition Condition `json:",omitempty"`
Copy link
Member

Choose a reason for hiding this comment

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

Why, this makes writing IAM statements almost pleasurable ;-)

}

func (l *IAMStatement) Equal(r *IAMStatement) bool {
Expand Down Expand Up @@ -106,14 +109,7 @@ func (b *IAMPolicyBuilder) BuildAWSIAMPolicy() (*IAMPolicy, error) {
return p, nil
}

if b.Role == api.InstanceGroupRoleNode {
p.Statement = append(p.Statement, &IAMStatement{
Effect: IAMStatementEffectAllow,
Action: stringorslice.Slice([]string{"ec2:Describe*"}),
Resource: wildcard,
})

}
addEC2Permissions(p, iamPrefix, b, wildcard, legacyIAM)

{
// We provide ECR access on the nodes (naturally), but we also provide access on the master.
Expand All @@ -135,12 +131,6 @@ func (b *IAMPolicyBuilder) BuildAWSIAMPolicy() (*IAMPolicy, error) {
}

if b.Role == api.InstanceGroupRoleMaster {
p.Statement = append(p.Statement, &IAMStatement{
Effect: IAMStatementEffectAllow,
Action: stringorslice.Slice([]string{"ec2:*"}),
Resource: wildcard,
})

p.Statement = append(p.Statement, &IAMStatement{
Effect: IAMStatementEffectAllow,
Action: stringorslice.Slice([]string{"elasticloadbalancing:*"}),
Expand Down Expand Up @@ -251,6 +241,54 @@ func (b *IAMPolicyBuilder) BuildAWSIAMPolicy() (*IAMPolicy, error) {
return p, nil
}

// addEC2Permissions updates the IAM Policy with statements granting tailored
// access to EC2 resources, depending on the instance role
func addEC2Permissions(p *IAMPolicy, iamPrefix string, b *IAMPolicyBuilder, wildcard stringorslice.StringOrSlice, legacyIAM bool) {
if (b.Role == api.InstanceGroupRoleNode) || (b.Role == api.InstanceGroupRoleMaster) {
p.Statement = append(p.Statement, &IAMStatement{
Effect: IAMStatementEffectAllow,
Action: stringorslice.Slice([]string{"ec2:Describe*"}),
Resource: wildcard,
})
}

if b.Role == api.InstanceGroupRoleMaster {
if legacyIAM {
p.Statement = append(p.Statement,
&IAMStatement{
Effect: IAMStatementEffectAllow,
Action: stringorslice.Slice([]string{"ec2:*"}),
Resource: wildcard,
},
)
} else {
p.Statement = append(p.Statement,
&IAMStatement{
Effect: IAMStatementEffectAllow,
Action: stringorslice.Slice([]string{
"ec2:CreateRoute",
"ec2:CreateTags",
"ec2:CreateVolume",
"ec2:DeleteVolume",
"ec2:ModifyInstanceAttribute",
}),
Resource: wildcard,
},
&IAMStatement{
Effect: IAMStatementEffectAllow,
Action: stringorslice.Slice([]string{"ec2:*"}),
Resource: wildcard,
Condition: Condition{
"StringEquals": map[string]string{
"ec2:ResourceTag/KubernetesCluster": b.Cluster.GetName(),
},
},
},
)
}
}
}

func addRoute53Permissions(p *IAMPolicy, hostedZoneID string) {
// Remove /hostedzone/ prefix (if present)
hostedZoneID = strings.TrimPrefix(hostedZoneID, "/")
Expand Down
120 changes: 120 additions & 0 deletions pkg/model/iam/iam_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,123 @@ func TestS3PolicyGeneration(t *testing.T) {
}
}
}

func TestEC2PolicyGeneration(t *testing.T) {
wildcard := stringorslice.Slice([]string{"*"})
clusterName := "my-cluster.k8s.local"
defaultEC2Statements := []*IAMStatement{
{
Effect: IAMStatementEffectAllow,
Action: stringorslice.Slice([]string{"ec2:Describe*"}),
Resource: wildcard,
},
}

grid := []struct {
Role kops.InstanceGroupRole
LegacyIAM bool
IAMPolicy IAMPolicy
}{
{
Role: "Node",
LegacyIAM: false,
IAMPolicy: IAMPolicy{
Statement: defaultEC2Statements,
},
},
{
Role: "Node",
LegacyIAM: true,
IAMPolicy: IAMPolicy{
Statement: defaultEC2Statements,
},
},
{
Role: "Master",
LegacyIAM: false,
IAMPolicy: IAMPolicy{
Statement: append(
defaultEC2Statements,
&IAMStatement{
Effect: IAMStatementEffectAllow,
Action: stringorslice.Slice([]string{
"ec2:CreateRoute",
"ec2:CreateTags",
"ec2:CreateVolume",
"ec2:DeleteVolume",
"ec2:ModifyInstanceAttribute",
}),
Resource: wildcard,
},
&IAMStatement{
Effect: IAMStatementEffectAllow,
Action: stringorslice.Slice([]string{"ec2:*"}),
Resource: wildcard,
Condition: Condition{
"StringEquals": map[string]string{
"ec2:ResourceTag/KubernetesCluster": clusterName,
},
},
},
),
},
},
{
Role: "Master",
LegacyIAM: true,
IAMPolicy: IAMPolicy{
Statement: append(
defaultEC2Statements,
&IAMStatement{
Effect: IAMStatementEffectAllow,
Action: stringorslice.Slice([]string{"ec2:*"}),
Resource: wildcard,
},
),
},
},
{
Role: "Bastion",
LegacyIAM: false,
IAMPolicy: IAMPolicy{
Statement: nil,
},
},
{
Role: "Bastion",
LegacyIAM: true,
IAMPolicy: IAMPolicy{
Statement: nil,
},
},
}

for i, x := range grid {
ip := &IAMPolicy{}
b := IAMPolicyBuilder{
Role: x.Role,
Cluster: &kops.Cluster{},
}
b.Cluster.SetName(clusterName)

addEC2Permissions(ip, "arn:aws", &b, wildcard, x.LegacyIAM)

expectedPolicy, err := x.IAMPolicy.AsJSON()
if err != nil {
t.Errorf("case %d failed to convert expected IAM Policy to JSON. Error: %q", i, err)
continue
}
actualPolicy, err := ip.AsJSON()
if err != nil {
t.Errorf("case %d failed to convert generated IAM Policy to JSON. Error: %q", i, err)
continue
}

if expectedPolicy != actualPolicy {
diffString := diff.FormatDiff(expectedPolicy, actualPolicy)
t.Logf("diff:\n%s\n", diffString)
t.Errorf("case %d failed, policy output differed from expected.", i)
continue
}
}
}
23 changes: 16 additions & 7 deletions tests/integration/minimal/cloudformation.json
Original file line number Diff line number Diff line change
Expand Up @@ -536,13 +536,7 @@
"Statement": [
{
"Action": [
"ecr:GetAuthorizationToken",
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:GetRepositoryPolicy",
"ecr:DescribeRepositories",
"ecr:ListImages",
"ecr:BatchGetImage"
"ec2:Describe*"
],
"Effect": "Allow",
"Resource": [
Expand All @@ -558,6 +552,21 @@
"*"
]
},
{
"Action": [
"ecr:GetAuthorizationToken",
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:GetRepositoryPolicy",
"ecr:DescribeRepositories",
"ecr:ListImages",
"ecr:BatchGetImage"
],
"Effect": "Allow",
"Resource": [
"*"
]
},
{
"Action": [
"elasticloadbalancing:*"
Expand Down