Skip to content

Commit c615a2d

Browse files
committed
Fix the kubernetes admin role, pull some of the iam stuff out into a separate tf file
1 parent ecf2eb3 commit c615a2d

File tree

3 files changed

+65
-36
lines changed

3 files changed

+65
-36
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: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 = "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_access" {
62+
user = data.aws_iam_user.ci_user.user_name
63+
policy_arn = aws_iam_policy.eks_list_and_describe_policy.arn
64+
}
65+

0 commit comments

Comments
 (0)