Skip to content

Commit

Permalink
Expand TestPolicyGeneration to cover gossip/no-gossip cases
Browse files Browse the repository at this point in the history
  • Loading branch information
johngmyers committed Jan 12, 2023
1 parent c1c75ca commit f06f3c6
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 35 deletions.
43 changes: 42 additions & 1 deletion pkg/model/iam/iam_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func TestRoundTrip(t *testing.T) {

func TestPolicyGeneration(t *testing.T) {
grid := []struct {
Gossip bool
Role Subject
AllowContainerRegistry bool
Policy string
Expand All @@ -111,6 +112,18 @@ func TestPolicyGeneration(t *testing.T) {
AllowContainerRegistry: true,
Policy: "tests/iam_builder_master_strict_ecr.json",
},
{
Gossip: true,
Role: &NodeRoleMaster{},
AllowContainerRegistry: false,
Policy: "tests/iam_builder_master_strict.json",
},
{
Gossip: true,
Role: &NodeRoleMaster{},
AllowContainerRegistry: true,
Policy: "tests/iam_builder_master_strict_ecr.json",
},
{
Role: &NodeRoleNode{},
AllowContainerRegistry: false,
Expand All @@ -121,12 +134,36 @@ func TestPolicyGeneration(t *testing.T) {
AllowContainerRegistry: true,
Policy: "tests/iam_builder_node_strict_ecr.json",
},
{
Gossip: true,
Role: &NodeRoleNode{},
AllowContainerRegistry: false,
Policy: "tests/iam_builder_node_gossip.json",
},
{
Gossip: true,
Role: &NodeRoleNode{},
AllowContainerRegistry: true,
Policy: "tests/iam_builder_node_gossip_ecr.json",
},
{
Role: &NodeRoleBastion{},
AllowContainerRegistry: false,
Policy: "tests/iam_builder_bastion.json",
},
{
Role: &NodeRoleBastion{},
AllowContainerRegistry: true,
Policy: "tests/iam_builder_bastion.json",
},
{
Gossip: true,
Role: &NodeRoleBastion{},
AllowContainerRegistry: false,
Policy: "tests/iam_builder_bastion.json",
},
{
Gossip: true,
Role: &NodeRoleBastion{},
AllowContainerRegistry: true,
Policy: "tests/iam_builder_bastion.json",
Expand Down Expand Up @@ -178,7 +215,11 @@ func TestPolicyGeneration(t *testing.T) {
Role: x.Role,
Partition: "aws-test",
}
b.Cluster.SetName("iam-builder-test.k8s.local")
if x.Gossip {
b.Cluster.SetName("iam-builder-test.k8s.local")
} else {
b.Cluster.SetName("iam-builder-test.nonexistant")
}

p, err := b.BuildAWSPolicy()
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions pkg/model/iam/tests/iam_builder_master_strict.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"Action": "ec2:AttachVolume",
"Condition": {
"StringEquals": {
"aws:ResourceTag/KubernetesCluster": "iam-builder-test.k8s.local",
"aws:ResourceTag/KubernetesCluster": "iam-builder-test.nonexistant",
"aws:ResourceTag/k8s.io/role/master": "1"
}
},
Expand Down Expand Up @@ -36,7 +36,7 @@
"Action": "ec2:CreateTags",
"Condition": {
"StringEquals": {
"aws:RequestTag/KubernetesCluster": "iam-builder-test.k8s.local",
"aws:RequestTag/KubernetesCluster": "iam-builder-test.nonexistant",
"ec2:CreateAction": [
"CreateSecurityGroup"
]
Expand All @@ -57,7 +57,7 @@
"aws:RequestTag/KubernetesCluster": "true"
},
"StringEquals": {
"aws:ResourceTag/KubernetesCluster": "iam-builder-test.k8s.local"
"aws:ResourceTag/KubernetesCluster": "iam-builder-test.nonexistant"
}
},
"Effect": "Allow",
Expand All @@ -69,7 +69,7 @@
"Action": "ec2:CreateTags",
"Condition": {
"StringEquals": {
"aws:RequestTag/KubernetesCluster": "iam-builder-test.k8s.local",
"aws:RequestTag/KubernetesCluster": "iam-builder-test.nonexistant",
"ec2:CreateAction": [
"CreateVolume",
"CreateSnapshot"
Expand All @@ -92,7 +92,7 @@
"aws:RequestTag/KubernetesCluster": "true"
},
"StringEquals": {
"aws:ResourceTag/KubernetesCluster": "iam-builder-test.k8s.local"
"aws:ResourceTag/KubernetesCluster": "iam-builder-test.nonexistant"
}
},
"Effect": "Allow",
Expand Down Expand Up @@ -193,7 +193,7 @@
],
"Condition": {
"StringEquals": {
"aws:ResourceTag/KubernetesCluster": "iam-builder-test.k8s.local"
"aws:ResourceTag/KubernetesCluster": "iam-builder-test.nonexistant"
}
},
"Effect": "Allow",
Expand All @@ -210,7 +210,7 @@
],
"Condition": {
"StringEquals": {
"aws:RequestTag/KubernetesCluster": "iam-builder-test.k8s.local"
"aws:RequestTag/KubernetesCluster": "iam-builder-test.nonexistant"
}
},
"Effect": "Allow",
Expand Down
14 changes: 7 additions & 7 deletions pkg/model/iam/tests/iam_builder_master_strict_ecr.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"Action": "ec2:AttachVolume",
"Condition": {
"StringEquals": {
"aws:ResourceTag/KubernetesCluster": "iam-builder-test.k8s.local",
"aws:ResourceTag/KubernetesCluster": "iam-builder-test.nonexistant",
"aws:ResourceTag/k8s.io/role/master": "1"
}
},
Expand Down Expand Up @@ -36,7 +36,7 @@
"Action": "ec2:CreateTags",
"Condition": {
"StringEquals": {
"aws:RequestTag/KubernetesCluster": "iam-builder-test.k8s.local",
"aws:RequestTag/KubernetesCluster": "iam-builder-test.nonexistant",
"ec2:CreateAction": [
"CreateSecurityGroup"
]
Expand All @@ -57,7 +57,7 @@
"aws:RequestTag/KubernetesCluster": "true"
},
"StringEquals": {
"aws:ResourceTag/KubernetesCluster": "iam-builder-test.k8s.local"
"aws:ResourceTag/KubernetesCluster": "iam-builder-test.nonexistant"
}
},
"Effect": "Allow",
Expand All @@ -69,7 +69,7 @@
"Action": "ec2:CreateTags",
"Condition": {
"StringEquals": {
"aws:RequestTag/KubernetesCluster": "iam-builder-test.k8s.local",
"aws:RequestTag/KubernetesCluster": "iam-builder-test.nonexistant",
"ec2:CreateAction": [
"CreateVolume",
"CreateSnapshot"
Expand All @@ -92,7 +92,7 @@
"aws:RequestTag/KubernetesCluster": "true"
},
"StringEquals": {
"aws:ResourceTag/KubernetesCluster": "iam-builder-test.k8s.local"
"aws:ResourceTag/KubernetesCluster": "iam-builder-test.nonexistant"
}
},
"Effect": "Allow",
Expand Down Expand Up @@ -200,7 +200,7 @@
],
"Condition": {
"StringEquals": {
"aws:ResourceTag/KubernetesCluster": "iam-builder-test.k8s.local"
"aws:ResourceTag/KubernetesCluster": "iam-builder-test.nonexistant"
}
},
"Effect": "Allow",
Expand All @@ -217,7 +217,7 @@
],
"Condition": {
"StringEquals": {
"aws:RequestTag/KubernetesCluster": "iam-builder-test.k8s.local"
"aws:RequestTag/KubernetesCluster": "iam-builder-test.nonexistant"
}
},
"Effect": "Allow",
Expand Down
40 changes: 40 additions & 0 deletions pkg/model/iam/tests/iam_builder_node_gossip.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"Statement": [
{
"Action": [
"s3:Get*"
],
"Effect": "Allow",
"Resource": [
"arn:aws-test:s3:::kops-tests/iam-builder-test.k8s.local/cluster-completed.spec",
"arn:aws-test:s3:::kops-tests/iam-builder-test.k8s.local/igconfig/node/*"
]
},
{
"Action": [
"s3:GetBucketLocation",
"s3:GetEncryptionConfiguration",
"s3:ListBucket",
"s3:ListBucketVersions"
],
"Effect": "Allow",
"Resource": [
"arn:aws-test:s3:::kops-tests"
]
},
{
"Action": [
"autoscaling:DescribeAutoScalingInstances",
"ec2:DescribeInstanceTypes",
"ec2:DescribeInstances",
"ec2:DescribeRegions",
"iam:GetServerCertificate",
"iam:ListServerCertificates",
"kms:GenerateRandom"
],
"Effect": "Allow",
"Resource": "*"
}
],
"Version": "2012-10-17"
}
47 changes: 47 additions & 0 deletions pkg/model/iam/tests/iam_builder_node_gossip_ecr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"Statement": [
{
"Action": [
"s3:Get*"
],
"Effect": "Allow",
"Resource": [
"arn:aws-test:s3:::kops-tests/iam-builder-test.k8s.local/cluster-completed.spec",
"arn:aws-test:s3:::kops-tests/iam-builder-test.k8s.local/igconfig/node/*"
]
},
{
"Action": [
"s3:GetBucketLocation",
"s3:GetEncryptionConfiguration",
"s3:ListBucket",
"s3:ListBucketVersions"
],
"Effect": "Allow",
"Resource": [
"arn:aws-test:s3:::kops-tests"
]
},
{
"Action": [
"autoscaling:DescribeAutoScalingInstances",
"ec2:DescribeInstanceTypes",
"ec2:DescribeInstances",
"ec2:DescribeRegions",
"ecr:BatchCheckLayerAvailability",
"ecr:BatchGetImage",
"ecr:DescribeRepositories",
"ecr:GetAuthorizationToken",
"ecr:GetDownloadUrlForLayer",
"ecr:GetRepositoryPolicy",
"ecr:ListImages",
"iam:GetServerCertificate",
"iam:ListServerCertificates",
"kms:GenerateRandom"
],
"Effect": "Allow",
"Resource": "*"
}
],
"Version": "2012-10-17"
}
10 changes: 0 additions & 10 deletions pkg/model/iam/tests/iam_builder_node_strict.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
{
"Statement": [
{
"Action": [
"s3:Get*"
],
"Effect": "Allow",
"Resource": [
"arn:aws-test:s3:::kops-tests/iam-builder-test.k8s.local/cluster-completed.spec",
"arn:aws-test:s3:::kops-tests/iam-builder-test.k8s.local/igconfig/node/*"
]
},
{
"Action": [
"s3:GetBucketLocation",
Expand Down
10 changes: 0 additions & 10 deletions pkg/model/iam/tests/iam_builder_node_strict_ecr.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
{
"Statement": [
{
"Action": [
"s3:Get*"
],
"Effect": "Allow",
"Resource": [
"arn:aws-test:s3:::kops-tests/iam-builder-test.k8s.local/cluster-completed.spec",
"arn:aws-test:s3:::kops-tests/iam-builder-test.k8s.local/igconfig/node/*"
]
},
{
"Action": [
"s3:GetBucketLocation",
Expand Down

0 comments on commit f06f3c6

Please sign in to comment.