Skip to content

Commit 62c693f

Browse files
authored
Merge pull request #23 from commitdev/fix-kube-admin-role
Fix the kubernetes admin role, pull some of the iam stuff out into a …
2 parents ecf2eb3 + 5d45051 commit 62c693f

File tree

5 files changed

+98
-64
lines changed

5 files changed

+98
-64
lines changed

terraform/modules/eks/main.tf

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,6 @@ provider "kubernetes" {
1414
version = "~> 1.11"
1515
}
1616

17-
# Create KubernetesAdmin role for aws-iam-authenticator
18-
resource "aws_iam_role" "kubernetes_admin_role" {
19-
name = "<% .Name %>-kubernetes-admin-${var.environment}"
20-
assume_role_policy = var.assume_role_policy
21-
description = "Kubernetes administrator role (for AWS IAM Authenticator)"
22-
}
23-
24-
# Allow kube admin to list and describe EKS clusters (through assumed role)
25-
data "aws_iam_policy_document" "eks_list_and_describe" {
26-
statement {
27-
actions = [
28-
"eks:ListUpdates",
29-
"eks:ListClusters",
30-
"eks:DescribeUpdate",
31-
"eks:DescribeCluster",
32-
]
33-
34-
resources = ["*"]
35-
}
36-
}
37-
38-
resource "aws_iam_policy" "eks_list_and_describe_policy" {
39-
name = "eks_list_and_describe"
40-
policy = data.aws_iam_policy_document.eks_list_and_describe.json
41-
}
42-
43-
resource "aws_iam_role_policy_attachment" "kube_admin_eks_access" {
44-
role = aws_iam_role.kubernetes_admin_role.id
45-
policy_arn = aws_iam_policy.eks_list_and_describe_policy.arn
46-
}
47-
48-
4917
module "eks" {
5018
source = "terraform-aws-modules/eks/aws"
5119
version = "10.0.0"

terraform/modules/eks/variables.tf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ variable "cluster_version" {
1414
description = "EKS cluster version number to use. Incrementing this will start a cluster upgrade"
1515
}
1616

17-
variable "assume_role_policy" {
18-
description = "IAM policy document for AssumeRole. Controls access to the kubernetes admin serviceaccount"
19-
}
20-
2117
variable "private_subnets" {
2218
description = "VPC subnets for the EKS cluster"
2319
# type = list(string)
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
2+
# Data sources for EKS IAM
3+
data "aws_caller_identity" "current" {}
4+
5+
# @TODO - sort out creating only a single user but multiple roles per env
6+
7+
# Create KubernetesAdmin role for aws-iam-authenticator
8+
resource "aws_iam_role" "kubernetes_admin_role" {
9+
name = "${var.project}-kubernetes-admin-${var.environment}"
10+
assume_role_policy = data.aws_iam_policy_document.assumerole_root_policy.json
11+
description = "Kubernetes administrator role (for AWS EKS auth)"
12+
}
13+
14+
# Trust relationship to limit access to the k8s admin serviceaccount
15+
data "aws_iam_policy_document" "assumerole_root_policy" {
16+
statement {
17+
actions = ["sts:AssumeRole"]
18+
19+
principals {
20+
type = "AWS"
21+
identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"]
22+
}
23+
}
24+
25+
# Allow the CI user to assume this role
26+
statement {
27+
actions = ["sts:AssumeRole"]
28+
29+
principals {
30+
type = "AWS"
31+
identifiers = [data.aws_iam_user.ci_user.arn]
32+
}
33+
}
34+
}
35+
36+
resource "aws_iam_user_policy_attachment" "circleci_ecr_access" {
37+
user = data.aws_iam_user.ci_user.user_name
38+
policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryPowerUser"
39+
}
40+
41+
42+
# Allow the CI user to list and describe clusters
43+
data "aws_iam_policy_document" "eks_list_and_describe" {
44+
statement {
45+
actions = [
46+
"eks:ListUpdates",
47+
"eks:ListClusters",
48+
"eks:DescribeUpdate",
49+
"eks:DescribeCluster",
50+
]
51+
52+
resources = ["*"]
53+
}
54+
}
55+
56+
resource "aws_iam_policy" "eks_list_and_describe_policy" {
57+
name = "${var.project}_eks_list_and_describe"
58+
policy = data.aws_iam_policy_document.eks_list_and_describe.json
59+
}
60+
61+
resource "aws_iam_user_policy_attachment" "ci_user_list_and_describe_policy" {
62+
user = data.aws_iam_user.ci_user.user_name
63+
policy_arn = aws_iam_policy.eks_list_and_describe_policy.arn
64+
}
65+
66+
# Allow the CI user read/write access to the frontend assets bucket
67+
data "aws_iam_policy_document" "read_write_s3_policy" {
68+
statement {
69+
actions = [
70+
"s3:ListBucket",
71+
]
72+
73+
resources = formatlist("arn:aws:s3:::%s", var.s3_hosting_buckets)
74+
}
75+
76+
statement {
77+
actions = [
78+
"s3:*Object",
79+
]
80+
81+
resources = formatlist("arn:aws:s3:::%s/*", var.s3_hosting_buckets)
82+
}
83+
}
84+
85+
resource "aws_iam_policy" "read_write_s3_policy" {
86+
name = "${var.project}_ci_s3_policy"
87+
policy = data.aws_iam_policy_document.read_write_s3_policy.json
88+
}
89+
90+
resource "aws_iam_user_policy_attachment" "ci_s3_policy" {
91+
user = data.aws_iam_user.ci_user.user_name
92+
policy_arn = aws_iam_policy.read_write_s3_policy.arn
93+
}

terraform/modules/environment/main.tf

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,9 @@ module "vpc" {
1717
kubernetes_cluster_name = local.kubernetes_cluster_name
1818
}
1919

20-
# Data sources for EKS IAM
20+
# To get the current account id
2121
data "aws_caller_identity" "current" {}
2222

23-
# Use this role to limit access to the k8s admin serviceaccount
24-
data "aws_iam_policy_document" "assumerole_root_policy" {
25-
statement {
26-
actions = ["sts:AssumeRole"]
27-
28-
principals {
29-
type = "AWS"
30-
identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"]
31-
}
32-
}
33-
34-
# Allow the CI user to assume this role
35-
statement {
36-
actions = ["sts:AssumeRole"]
37-
38-
principals {
39-
type = "AWS"
40-
identifiers = [data.aws_iam_user.ci_user.arn]
41-
}
42-
}
43-
}
44-
45-
resource "aws_iam_user_policy_attachment" "circleci_ecr_access" {
46-
user = data.aws_iam_user.ci_user.user_name
47-
policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryPowerUser"
48-
}
49-
5023
#
5124
# Provision the EKS cluster
5225
module "eks" {

terraform/modules/s3_hosting/main.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ resource "aws_cloudfront_distribution" "client_assets_distribution" {
126126
ssl_support_method = "sni-only"
127127
}
128128

129+
# Reference the cert validations only so it becomes a dependency
130+
tags = {
131+
"certs-validated" = join(",", var.certificate_validations)
132+
}
129133
}
130134

131135
# Subdomain to point at CF

0 commit comments

Comments
 (0)