Skip to content

Commit 3df107b

Browse files
authored
Merge pull request #9 from infraspecdev/feat/account_permissions_assignment
feat: Account Users and Groups Assignment
2 parents 6fd47e4 + b8a2891 commit 3df107b

File tree

15 files changed

+380
-0
lines changed

15 files changed

+380
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
locals {
2+
aws_region = "us-east-1"
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module "account_perimissions_assignment" {
2+
source = "../../modules/account_users_and_groups_assignments"
3+
4+
account_assignments = var.account_assignments
5+
}

examples/assign-users-and-groups-to-account/outputs.tf

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
provider "aws" {
2+
region = local.aws_region
3+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
variable "account_assignments" {
2+
description = <<EOF
3+
A list of objects representing permission assignments for AWS SSO. Each object contains the following attributes:
4+
- account_id: The AWS account ID where the permissions will be applied.
5+
- permission_sets: List of permission-set to be assigned to the specified principals.
6+
- principal_names: An identifier for an object in AWS SSO, such as the names of groups or users .
7+
-principal_type: The entity type for which the assignment will be created. Valid values: USER, GROUP.
8+
EOF
9+
type = list(object({
10+
account_id = string
11+
permission_sets = list(string)
12+
principal_names = list(string)
13+
principal_type = string
14+
}))
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_version = ">= 1.4.6"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = ">= 4.65.0"
8+
}
9+
}
10+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Terraform AWS Account Users and Groups Assignments Module
2+
A Terraform module for assigning users and groups to AWS accounts.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Terraform AWS Organization Account Permissions Assignment Module
2+
A Terraform module for associating permissions to AWS accounts.
3+
4+
## Requirements
5+
6+
| Name | Version |
7+
|------|---------|
8+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.4.6 |
9+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.65.0 |
10+
| <a name="requirement_null"></a> [null](#requirement\_null) | ~> 3.2.2 |
11+
12+
## Providers
13+
14+
| Name | Version |
15+
|------|---------|
16+
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.58.0 |
17+
| <a name="provider_null"></a> [null](#provider\_null) | 3.2.2 |
18+
19+
## Modules
20+
21+
No modules.
22+
23+
## Resources
24+
25+
| Name | Type |
26+
|------|------|
27+
| [aws_ssoadmin_account_assignment.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ssoadmin_account_assignment) | resource |
28+
| [null_resource.sso_group_dependency](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
29+
| [null_resource.sso_permission_set_dependency](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
30+
| [null_resource.sso_user_dependency](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
31+
| [aws_identitystore_group.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/identitystore_group) | data source |
32+
| [aws_identitystore_user.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/identitystore_user) | data source |
33+
| [aws_ssoadmin_instances.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ssoadmin_instances) | data source |
34+
| [aws_ssoadmin_permission_set.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ssoadmin_permission_set) | data source |
35+
36+
## Inputs
37+
38+
| Name | Description | Type | Default | Required |
39+
|------|-------------|------|---------|:--------:|
40+
| <a name="input_account_assignments"></a> [account\_assignments](#input\_account\_assignments) | A list of objects representing permission assignments for AWS SSO. Each object contains the following attributes:<br> - account\_id: The AWS account ID where the permissions will be applied.<br> - permission\_sets: List of permission-set to be assigned to the specified principals.<br> - principal\_names: An identifier for an object in AWS SSO, such as the names of groups or users .<br> - principal\_type:The entity type for which the assignment will be created. Valid values: USER, GROUP. | <pre>list(object({<br> account_id = string<br> permission_sets = list(string)<br> principal_names = list(string)<br> principal_type = string<br> }))</pre> | n/a | yes |
41+
| <a name="input_identitystore_group_depends_on"></a> [identitystore\_group\_depends\_on](#input\_identitystore\_group\_depends\_on) | A list of parameters (For example group IDs)to use for data resources to depend on. This is to avoid module depends\_on as that will unnecessarily create the module resources | `list(string)` | `[]` | no |
42+
| <a name="input_identitystore_permission_set_depends_on"></a> [identitystore\_permission\_set\_depends\_on](#input\_identitystore\_permission\_set\_depends\_on) | A list of parameters (For example permission set ARNs)to use for data resources to depend on. This is to avoid module depends\_on as that will unnecessarily create the module resources | `list(string)` | `[]` | no |
43+
| <a name="input_identitystore_user_depends_on"></a> [identitystore\_user\_depends\_on](#input\_identitystore\_user\_depends\_on) | A list of parameters (For example user IDs)to use for data resources to depend on. This is to avoid module depends\_on as that will unnecessarily create the module resources | `list(string)` | `[]` | no |
44+
45+
## Outputs
46+
47+
| Name | Description |
48+
|------|-------------|
49+
| <a name="output_assignments"></a> [assignments](#output\_assignments) | The account assignment resources created for AWS SSO. Each resource includes details about the account, permission set, principal, and the status of the assignments. |
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
resource "null_resource" "sso_group_dependency" {
2+
triggers = {
3+
dependency_id = join(",", var.identitystore_group_depends_on)
4+
}
5+
}
6+
7+
resource "null_resource" "sso_user_dependency" {
8+
triggers = {
9+
dependency_id = join(",", var.identitystore_user_depends_on)
10+
}
11+
}
12+
13+
data "aws_identitystore_group" "default" {
14+
for_each = local.group_list
15+
16+
identity_store_id = local.identity_store_id
17+
18+
alternate_identifier {
19+
unique_attribute {
20+
attribute_path = "DisplayName"
21+
attribute_value = each.key
22+
}
23+
}
24+
25+
depends_on = [null_resource.sso_group_dependency]
26+
}
27+
28+
data "aws_identitystore_user" "default" {
29+
for_each = local.user_list
30+
31+
identity_store_id = local.identity_store_id
32+
33+
alternate_identifier {
34+
unique_attribute {
35+
attribute_path = "UserName"
36+
attribute_value = each.key
37+
}
38+
}
39+
40+
depends_on = [null_resource.sso_user_dependency]
41+
}
42+
43+
data "aws_ssoadmin_instances" "default" {
44+
45+
}
46+
47+
resource "null_resource" "sso_permission_set_dependency" {
48+
triggers = {
49+
dependency_id = join(",", var.identitystore_permission_set_depends_on)
50+
}
51+
}
52+
53+
data "aws_ssoadmin_permission_set" "default" {
54+
for_each = local.permission_set_list
55+
56+
instance_arn = local.sso_instance_arn
57+
name = each.value
58+
depends_on = [null_resource.sso_permission_set_dependency]
59+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
locals {
3+
flatten_account_group_permission = flatten([
4+
for acc_assignment in var.account_assignments : [
5+
for ps_name in acc_assignment.permission_sets : [
6+
for pr_name in acc_assignment.principal_names : {
7+
acc_id = acc_assignment.account_id
8+
principal_name = pr_name
9+
ps_name = ps_name
10+
principal_type = acc_assignment.principal_type
11+
}
12+
]
13+
]
14+
])
15+
assignment_map = {
16+
for a in local.flatten_account_group_permission :
17+
format("%v-%v-%v-%v", a.acc_id, substr(a.principal_type, 0, 1), a.principal_name, a.ps_name) => a
18+
}
19+
20+
identity_store_id = tolist(data.aws_ssoadmin_instances.default.identity_store_ids)[0]
21+
sso_instance_arn = tolist(data.aws_ssoadmin_instances.default.arns)[0]
22+
23+
group_list = toset(flatten([for a in var.account_assignments : [
24+
for pr_name in a.principal_names : [
25+
pr_name] if a.principal_type == "GROUP"
26+
]
27+
]))
28+
29+
user_list = toset(flatten([for a in var.account_assignments : [
30+
for pr_name in a.principal_names : [
31+
pr_name] if a.principal_type == "USER"
32+
]
33+
]))
34+
35+
permission_set_list = toset(flatten([for a in var.account_assignments : [
36+
[for ps_name in a.permission_sets : ps_name
37+
]
38+
]
39+
]))
40+
# permission_set_list = toset(local.all_permission_sets)
41+
}

0 commit comments

Comments
 (0)