-
Notifications
You must be signed in to change notification settings - Fork 13
feat: initial commit for module iam_users #6
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
30e537a
initial commit for module iam_users
sshi100 55f73d3
pre-commit applied
sshi100 690d08f
fix with review
sshi100 c73f997
enhancement with new defition
sshi100 b7aed10
enhancement with user group
sshi100 b534abc
doc formalized
sshi100 bb7171d
add environment for group and set name of dependent resource
sshi100 18ea0ea
Merge remote-tracking branch 'origin/main' into 223-security-add-new-…
sshi100 333ff0b
enhancement with assume role and k8s rules, etc.
sshi100 a6da572
added assume role to user group
sshi100 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # user_access | ||
|
|
||
| Create IAM Roles/Groups and Kubernetes Cluster Roles for user access | ||
|
|
||
| ## Notes | ||
|
|
||
| <!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK --> | ||
| ## Requirements | ||
|
|
||
| | Name | Version | | ||
| |------|---------| | ||
| | terraform | >= 0.13 | | ||
|
|
||
| ## Providers | ||
|
|
||
| | Name | Version | | ||
| |------|---------| | ||
| | aws | n/a | | ||
| | kubernetes | n/a | | ||
|
|
||
| ## Inputs | ||
|
|
||
| | Name | Description | Type | Default | Required | | ||
| |------|-------------|------|---------|:--------:| | ||
| | environment | The environment (development/staging/production) | `any` | n/a | yes | | ||
| | project | Name of the project | `any` | n/a | yes | | ||
| | roles | Role list with policies | <pre>list(object({<br> name = string<br> aws_policy = string<br> k8s_policies = list(map(list(string)))<br> }))</pre> | n/a | yes | | ||
| | users | User list with roles | <pre>list(object({<br> name = string<br> roles = list(string)<br> }))</pre> | n/a | yes | | ||
|
|
||
| ## Outputs | ||
|
|
||
| | Name | Description | | ||
| |------|-------------| | ||
| | eks\_iam\_role\_mapping | List of mappings of users to roles | | ||
|
|
||
| <!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK --> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| # User and Roles | ||
|
|
||
| locals { | ||
| account_id = data.aws_caller_identity.current.account_id | ||
| } | ||
|
|
||
| # Create group with polcy for AWS resource access | ||
| resource "aws_iam_group" "access_group" { | ||
| count = length(var.roles) | ||
| name = "${var.project}-${var.roles[count.index].name}-${var.environment}" | ||
| path = "/users/" | ||
| } | ||
|
|
||
| data "aws_iam_policy_document" "access_group" { | ||
| count = length(var.roles) | ||
| source_json = var.roles[count.index].aws_policy | ||
|
|
||
| statement { | ||
| sid = "AssumeRolePolicy" | ||
| effect = "Allow" | ||
| actions = [ | ||
| "iam:ListRoles", | ||
| "sts:AssumeRole" | ||
| ] | ||
| resources = [aws_iam_role.access_assumerole[count.index].arn] | ||
| } | ||
| } | ||
|
|
||
| resource "aws_iam_policy" "access_group" { | ||
| count = length(var.roles) | ||
| name = aws_iam_group.access_group[count.index].name | ||
| description = "Group policy" | ||
| policy = data.aws_iam_policy_document.access_group[count.index].json | ||
| } | ||
|
|
||
| resource "aws_iam_group_policy_attachment" "access_group" { | ||
| count = length(var.roles) | ||
| group = aws_iam_group.access_group[count.index].name | ||
| policy_arn = aws_iam_policy.access_group[count.index].arn | ||
| } | ||
|
|
||
| resource "aws_iam_user_group_membership" "access_user_group" { | ||
| count = length(var.users) | ||
| user = "${var.project}-${var.users[count.index].name}" | ||
| groups = [for r in var.users[count.index].roles : "${var.project}-${r}-${var.environment}"] | ||
| } | ||
|
|
||
| # Create assumeroles with policy for Kubernetes aws-auth | ||
| resource "aws_iam_role" "access_assumerole" { | ||
| count = length(var.roles) | ||
| name = "${var.project}-kubernetes-${var.roles[count.index].name}-${var.environment}" | ||
| assume_role_policy = data.aws_iam_policy_document.access_assumerole_root_policy.json | ||
| description = "Assume role for Kubernetes aws-auth" | ||
| } | ||
|
|
||
| data "aws_caller_identity" "current" {} | ||
|
|
||
| data "aws_iam_policy_document" "access_assumerole_root_policy" { | ||
| statement { | ||
| actions = ["sts:AssumeRole"] | ||
|
|
||
| principals { | ||
| type = "AWS" | ||
| identifiers = [local.account_id] | ||
| } | ||
| } | ||
| } | ||
|
|
||
| # Create Kubernetes cluster role and group binding for API access | ||
| resource "kubernetes_cluster_role" "access_role" { | ||
| count = length(var.roles) | ||
| metadata { | ||
| name = aws_iam_role.access_assumerole[count.index].name | ||
| } | ||
|
|
||
| dynamic "rule" { | ||
| for_each = var.roles[count.index].k8s_policies | ||
| content { | ||
| verbs = rule.value.verbs | ||
| api_groups = rule.value.api_groups | ||
| resources = rule.value.resources | ||
| } | ||
| } | ||
| } | ||
|
|
||
| resource "kubernetes_cluster_role_binding" "access_role" { | ||
| count = length(var.roles) | ||
| metadata { | ||
| name = aws_iam_role.access_assumerole[count.index].name | ||
| } | ||
| subject { | ||
| kind = "Group" | ||
| name = kubernetes_cluster_role.access_role[count.index].metadata.0.name | ||
| } | ||
| role_ref { | ||
| api_group = "rbac.authorization.k8s.io" | ||
| kind = "ClusterRole" | ||
| name = kubernetes_cluster_role.access_role[count.index].metadata.0.name | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # output for EKS module | ||
| output "eks_iam_role_mapping" { | ||
| value = [ | ||
| for r in aws_iam_role.access_assumerole : { | ||
| arn = r.arn | ||
| name = r.name | ||
| } | ||
| ] | ||
| description = "List of mappings of users to roles" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| variable "project" { | ||
| description = "Name of the project" | ||
| } | ||
|
|
||
| variable "environment" { | ||
| description = "The environment (development/staging/production)" | ||
| } | ||
|
|
||
| variable "roles" { | ||
| type = list(object({ | ||
| name = string | ||
| aws_policy = string | ||
| k8s_policies = list(map(list(string))) | ||
| })) | ||
| description = "Role list with policies" | ||
| } | ||
|
|
||
| variable "users" { | ||
| type = list(object({ | ||
| name = string | ||
| roles = list(string) | ||
| })) | ||
| description = "User list with roles" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
|
|
||
| terraform { | ||
| required_version = ">= 0.13" | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of having the assumerole policy included in the policy that is passed in, we should be creating it here. (this code from zero-aws-eks-stack:)
It shouldn't be up to the user to have to include that policy block, we can create it dynamically here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done automatically in module side