|
| 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