|
| 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 | +} |
0 commit comments