Skip to content

Commit 247bf00

Browse files
authored
Merge pull request #11 from cloudposse/feature-expose-policies
Feature expose policies
2 parents a186042 + 04cd451 commit 247bf00

File tree

5 files changed

+156
-25
lines changed

5 files changed

+156
-25
lines changed

.gitignore

100644100755
Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
# Compiled files
1+
# Local .terraform directories
2+
**/.terraform/*
3+
4+
# .tfstate files
25
*.tfstate
3-
*.tfstate.backup
6+
*.tfstate.*
7+
8+
# .tfvars files
9+
*.tfvars
10+
11+
**/.idea
12+
**/*.iml
413

5-
# Module directory
6-
.terraform
7-
.idea
8-
*.iml
14+
**/.build-harness
15+
**/build-harness

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,35 @@ module "ecr" {
3131
}
3232
```
3333

34+
Example of attaching policies to a user for CI/CD
35+
36+
```hcl
37+
module "cicd_user" {
38+
source = "git::https://github.com/cloudposse/terraform-aws-iam-system-user.git?ref=tags/0.3.0"
39+
namespace = "${var.namespace}"
40+
stage = "${var.stage}"
41+
name = "codefresh"
42+
}
43+
44+
resource "aws_iam_policy_attachment" "login" {
45+
name = "${module.cicd_user.user_name}-login"
46+
users = ["${module.cicd_user.user_name}"]
47+
policy_arn = "${module.kops_ecr.policy_login_arn}"
48+
}
49+
50+
resource "aws_iam_policy_attachment" "read" {
51+
name = "${module.cicd_user.user_name}-read"
52+
users = ["${module.cicd_user.user_name}"]
53+
policy_arn = "${module.kops_ecr.policy_read_arn}"
54+
}
55+
56+
resource "aws_iam_policy_attachment" "write" {
57+
name = "${module.cicd_user.user_name}-write"
58+
users = ["${module.cicd_user.user_name}"]
59+
policy_arn = "${module.kops_ecr.policy_write_arn}"
60+
}
61+
```
62+
3463

3564
## Variables
3665

main.tf

100644100755
Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ data "aws_iam_policy_document" "assume_role" {
1616
}
1717
}
1818

19-
data "aws_iam_policy_document" "token" {
19+
data "aws_iam_policy_document" "login" {
2020
statement {
2121
sid = "ECRGetAuthorizationToken"
2222
effect = "Allow"
@@ -26,6 +26,41 @@ data "aws_iam_policy_document" "token" {
2626
}
2727
}
2828

29+
data "aws_iam_policy_document" "write" {
30+
statement {
31+
sid = "ECRGetAuthorizationToken"
32+
effect = "Allow"
33+
34+
actions = [
35+
"ecr:InitiateLayerUpload",
36+
"ecr:UploadLayerPart",
37+
"ecr:CompleteLayerUpload",
38+
"ecr:PutImage",
39+
]
40+
41+
resources = ["${aws_ecr_repository.default.arn}"]
42+
}
43+
}
44+
45+
data "aws_iam_policy_document" "read" {
46+
statement {
47+
sid = "ECRGetAuthorizationToken"
48+
effect = "Allow"
49+
50+
actions = [
51+
"ecr:BatchCheckLayerAvailability",
52+
"ecr:GetDownloadUrlForLayer",
53+
"ecr:GetRepositoryPolicy",
54+
"ecr:DescribeRepositories",
55+
"ecr:ListImages",
56+
"ecr:DescribeImages",
57+
"ecr:BatchGetImage",
58+
]
59+
60+
resources = ["${aws_ecr_repository.default.arn}"]
61+
}
62+
}
63+
2964
data "aws_iam_policy_document" "default_ecr" {
3065
count = "${signum(length(var.roles)) == 1 ? 0 : 1}"
3166

@@ -114,10 +149,22 @@ resource "aws_ecr_repository_policy" "default_ecr" {
114149
policy = "${data.aws_iam_policy_document.default_ecr.json}"
115150
}
116151

117-
resource "aws_iam_policy" "default" {
118-
name = "${module.label.id}"
152+
resource "aws_iam_policy" "login" {
153+
name = "${module.label.id}${var.delimiter}login"
119154
description = "Allow IAM Users to call ecr:GetAuthorizationToken"
120-
policy = "${data.aws_iam_policy_document.token.json}"
155+
policy = "${data.aws_iam_policy_document.login.json}"
156+
}
157+
158+
resource "aws_iam_policy" "read" {
159+
name = "${module.label.id}${var.delimiter}read"
160+
description = "Allow IAM Users to push into ECR"
161+
policy = "${data.aws_iam_policy_document.read.json}"
162+
}
163+
164+
resource "aws_iam_policy" "write" {
165+
name = "${module.label.id}${var.delimiter}write"
166+
description = "Allow IAM Users to pull from ECR"
167+
policy = "${data.aws_iam_policy_document.write.json}"
121168
}
122169

123170
resource "aws_iam_role" "default" {
@@ -129,13 +176,13 @@ resource "aws_iam_role" "default" {
129176
resource "aws_iam_role_policy_attachment" "default_ecr" {
130177
count = "${signum(length(var.roles)) == 1 ? 0 : 1}"
131178
role = "${aws_iam_role.default.name}"
132-
policy_arn = "${aws_iam_policy.default.arn}"
179+
policy_arn = "${aws_iam_policy.login.arn}"
133180
}
134181

135182
resource "aws_iam_role_policy_attachment" "default" {
136183
count = "${signum(length(var.roles)) == 1 ? length(var.roles) : 0}"
137184
role = "${element(var.roles, count.index)}"
138-
policy_arn = "${aws_iam_policy.default.arn}"
185+
policy_arn = "${aws_iam_policy.login.arn}"
139186
}
140187

141188
resource "aws_iam_instance_profile" "default" {

output.tf

100644100755
Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,54 @@
11
output "registry_id" {
2-
value = "${aws_ecr_repository.default.registry_id}"
2+
value = "${aws_ecr_repository.default.registry_id}"
3+
description = "Registry ID"
34
}
45

56
output "registry_url" {
6-
value = "${aws_ecr_repository.default.repository_url}"
7+
value = "${aws_ecr_repository.default.repository_url}"
8+
description = "Registry URL"
79
}
810

911
output "repository_name" {
10-
value = "${aws_ecr_repository.default.name}"
12+
value = "${aws_ecr_repository.default.name}"
13+
description = "Registry name"
1114
}
1215

1316
output "role_name" {
14-
value = "${join("", aws_iam_role.default.*.name)}"
17+
value = "${join("", aws_iam_role.default.*.name)}"
18+
description = "Assume Role name to get registry access"
19+
}
20+
21+
output "role_arn" {
22+
value = "${join("", aws_iam_role.default.*.arn)}"
23+
description = "Assume Role ARN to get registry access"
24+
}
25+
26+
output "policy_login_name" {
27+
value = "${aws_iam_policy.login.name}"
28+
description = "The IAM Policy name to be given access to login in ECR"
29+
}
30+
31+
output "policy_login_arn" {
32+
value = "${aws_iam_policy.login.arn}"
33+
description = "The IAM Policy ARN to be given access to login in ECR"
34+
}
35+
36+
output "policy_read_name" {
37+
value = "${aws_iam_policy.read.name}"
38+
description = "The IAM Policy name to be given access to pull images from ECR"
39+
}
40+
41+
output "policy_read_arn" {
42+
value = "${aws_iam_policy.read.arn}"
43+
description = "The IAM Policy ARN to be given access to pull images from ECR"
44+
}
45+
46+
output "policy_write_name" {
47+
value = "${aws_iam_policy.write.name}"
48+
description = "The IAM Policy name to be given access to push images to ECR"
49+
}
50+
51+
output "policy_write_arn" {
52+
value = "${aws_iam_policy.write.arn}"
53+
description = "The IAM Policy ARN to be given access to push images to ECR"
1554
}

variables.tf

100644100755
Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
variable "name" {}
1+
variable "name" {
2+
description = "The Name of the application or solution (e.g. `bastion` or `portal`)"
3+
}
24

3-
variable "namespace" {}
5+
variable "namespace" {
6+
description = "Namespace (e.g. `cp` or `cloudposse`)"
7+
}
48

5-
variable "stage" {}
9+
variable "stage" {
10+
description = "Stage (e.g. `prod`, `dev`, `staging`)"
11+
}
612

713
variable "roles" {
814
type = "list"
@@ -11,18 +17,21 @@ variable "roles" {
1117
}
1218

1319
variable "delimiter" {
14-
type = "string"
15-
default = "-"
20+
type = "string"
21+
default = "-"
22+
description = "Delimiter to be used between `name`, `namespace`, `stage`, etc."
1623
}
1724

1825
variable "attributes" {
19-
type = "list"
20-
default = []
26+
type = "list"
27+
default = []
28+
description = "Additional attributes (e.g. `policy` or `role`)"
2129
}
2230

2331
variable "tags" {
24-
type = "map"
25-
default = {}
32+
type = "map"
33+
default = {}
34+
description = "Additional tags (e.g. `map('BusinessUnit','XYZ')`)"
2635
}
2736

2837
variable "max_image_count" {

0 commit comments

Comments
 (0)