Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: (PSKD-904) Support credentials when assuming an IAM role #319

Open
wants to merge 4 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/linter-analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ jobs:
path: ~/.tflint.d/plugins
key: ubuntu-latest-tflint-${{ hashFiles('.tflint.hcl') }}

- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: "^1.9.8"
terraform_wrapper: false

- name: Setup TFLint
uses: terraform-linters/[email protected]
with:
Expand Down
2 changes: 2 additions & 0 deletions docs/CONFIG-VARS.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ Custom policy:
| autoscaling_enabled | Enable cluster autoscaling | bool | true | |
| ssh_public_key | File name of public ssh key for jump and nfs VM | string | "~/.ssh/id_rsa.pub" | Required with `create_jump_vm=true` or `storage_type=standard` |
| cluster_api_mode | Public or private IP for the cluster api| string|"public"|Valid Values: "public", "private" |
| authentication_mode | The authentication mode for the EKS cluster.| string|"API_AND_CONFIG_MAP"| Valid values are CONFIG_MAP, API or API_AND_CONFIG_MAP |
| access_entry_role_arns | Create an EKS access entry associated with the AmazonEKSClusterAdminPolicy for each existing IAM role ARN specified in this list. | list of strings | | **Note:** The assumed-role used to authenticate to Terraform should not be included this list. The format role ARNs take is: "arn:aws:iam::<Subscription_ID>:role/<rolename>"|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

< Above, in the IAM section, we have a first reference to the abbreviation "ARN." It needs to be expanded there, so please make this change:
from cluster_iam_role_arn | ARN of the pre-existing IAM role for the EKS cluster
to: cluster_iam_role_arn | Amazon Resource Name (ARN) of the pre-existing IAM role for the EKS cluster >

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

applied, the upcoming commit will contain the update for your review

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... for each of the existing IAM role ARNs that are included in this list.
...
Note: Do not include the assumed-role that is used to authenticate to Terraform in this list. The format for role ARNs resembles the following example: "arn:aws:iam::<Subscription_ID>:role/"

Copy link
Member Author

@dhoucgitter dhoucgitter Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

each suggestion applied, the upcoming commit will contain the update for your review


Copy link

@ajeffowens ajeffowens Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be account_id (not subscription_id) in the arn example.

Also... maybe want to indicate in the var name that these roles are getting admin access? admin_access_entry_role_arns?

## Node Pools

Expand Down
8 changes: 5 additions & 3 deletions docs/user/TerraformAWSAuthentication.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Authenticating Terraform to Access AWS

In order to create and destroy AWS resources on your behalf, Terraform needs a AWS account that has sufficient permissions to perform all the actions defined in the Terraform manifest. You will need an AWS account IAM user that has at a minimum the permissions listed in [this policy](../../files/policies/devops-iac-eks-policy.json).
In order to create and destroy AWS resources on your behalf, Terraform needs to authenticate with credentials associated with an AWS account IAM user or credentials generated by assuming an IAM role that has sufficient permissions to perform all the actions defined in the Terraform manifest. You will need either an AWS account IAM user or an AWS IAM role that has at a minimum the permissions listed in [this policy](../../files/policies/devops-iac-eks-policy.json).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to create and destroy AWS resources on your behalf, Terraform needs credentials that enable it to authenticate to AWS. Two types of credentials are acceptable:

  • Credentials that are associated with an AWS IAM user account, or
  • Credentials that are generated by assuming an IAM role that has sufficient permissions to perform all the actions that are defined in the Terraform manifest.

You will need either an AWS IAM user account or an AWS IAM role that has at a minimum the permissions listed in this policy.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestions applied, the upcoming commit will contain the updates for your review


You can either use static credentials (including temporary credentials with session token) or a [profile with a credentials file](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html).
You can either use static credentials (including temporary credentials with a session token) or a [profile with a credentials file](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html).

You can pass AWS credentials to Terraform by using either [AWS environment variables](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html) or [TF_VAR_name](https://www.terraform.io/docs/cli/config/environment-variables.html#tf_var_name) environment variables.

Expand All @@ -28,7 +28,9 @@ TF_VAR_aws_secret_access_key=<your_aws_secret_access_key>
TF_VAR_aws_session_token=<your_aws_session_token>
```

> **NOTE** `AWS_SESSION_TOKEN` is optional and is only required when using you are using temporary AWS credentials. See the [AWS documentation](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html) on environment variables for more information.
> **NOTE** `AWS_SESSION_TOKEN` is optional and is only required when you are using temporary AWS credentials like those you would generate when assuming an IAM role. See the [AWS documentation](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html) on environment variables for more information.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

temporary AWS credentials. For example, you might generate temporary AWS credentials when assuming an IAM role.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

applied, upcoming commit will contain the updates for your review

See [AWS IAM roles](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles.html) for more information about temporary credentials when using an assumed role. In order for an IAM user to be able to assume a role, a trust relationship for that user needs to be created within the role, see [granting AssumeRole permission for a role](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_permissions-to-switch.html) for more information.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a trust relationship for that user must be created within the role. For more information, see granting AssumeRole permission for a role.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

applied, upcoming commit will contain the updates for your review


## Using AWS Profile with Credentials File

Expand Down
51 changes: 23 additions & 28 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -165,35 +165,10 @@ module "eks" {
create_iam_role = var.cluster_iam_role_arn == null ? true : false
iam_role_arn = var.cluster_iam_role_arn

# Cluster access entry
# To add the current caller identity as an administrator
enable_cluster_creator_admin_permissions = true
authentication_mode = var.authentication_mode

access_entries = {
# access entry with cluster and namespace scoped policies
cluster_creator = {
kubernetes_groups = ["rbac.authorization.k8s.io"]
principal_arn = data.aws_caller_identity.terraform.arn
user_name = local.aws_caller_identity_user_name
type = "STANDARD"

policy_associations = {
cluster_creator_assoc = {
policy_arn = "arn:aws:eks::aws:cluster-access-policy/AmazonEKSClusterAdminPolicy"
access_scope = {
type = "cluster"
}
},
namespace_creator_assoc = {
policy_arn = "arn:aws:eks::aws:cluster-access-policy/AmazonEKSAdminPolicy"
access_scope = {
type = "namespace"
namespaces = ["kube-system"]
}
}
},
},
}
# Create access entry for current caller identity as a cluster admin
enable_cluster_creator_admin_permissions = true

iam_role_additional_policies = {
"additional" : "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"
Expand All @@ -213,6 +188,26 @@ module "eks" {
eks_managed_node_groups = local.node_groups
}

resource "aws_eks_access_entry" "instance" {
for_each = toset(coalesce(var.access_entry_role_arns, []))

cluster_name = module.eks.cluster_name
principal_arn = each.value
type = "STANDARD"
}

resource "aws_eks_access_policy_association" "cluster_assoc" {
for_each = aws_eks_access_entry.instance

cluster_name = module.eks.cluster_name
policy_arn = "arn:aws:eks::aws:cluster-access-policy/AmazonEKSClusterAdminPolicy"
principal_arn = each.value.principal_arn

access_scope {
type = "cluster"
}
}

module "autoscaling" {
source = "./modules/aws_autoscaling"
count = var.autoscaling_enabled ? 1 : 0
Expand Down
17 changes: 17 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -719,3 +719,20 @@ variable "enable_nist_features" {
type = bool
default = false
}

variable "authentication_mode" {
description = "The authentication mode for the EKS cluster. Supported values are 'API_AND_CONFIG_MAP' and 'API'."
type = string
default = "API_AND_CONFIG_MAP"

validation {
condition = contains(["API_AND_CONFIG_MAP", "API"], var.authentication_mode)
error_message = "ERROR: Supported values for `authentication_mode` are API_AND_CONFIG_MAP and API."
}
}

variable "access_entry_role_arns" {
description = "List of IAM role ARNs to create EKS access_entries for."
type = list(string)
default = null
}