Skip to content

Commit

Permalink
Limit the IAM EC2 policy for the master nodes, wrapped in 'Spec.IAM.L…
Browse files Browse the repository at this point in the history
…egacyIAM' API flag.
  • Loading branch information
KashifSaadat committed Aug 25, 2017
1 parent d661e27 commit 289f294
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 24 deletions.
69 changes: 52 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"`
}

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,51 @@ 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:ModifyInstanceAttribute",
"ec2:CreateRoute",
}),
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
117 changes: 117 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,120 @@ 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:ModifyInstanceAttribute",
"ec2:CreateRoute",
}),
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

0 comments on commit 289f294

Please sign in to comment.