Skip to content

Commit cbfe01c

Browse files
committed
test: add unit tests
1 parent d267203 commit cbfe01c

File tree

13 files changed

+187
-42
lines changed

13 files changed

+187
-42
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_permissions_assignment"
3+
4+
account_assignments = var.account_assignments
5+
}

examples/assign-permissions-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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
}
16+
variable "identitystore_group_depends_on" {
17+
description = "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"
18+
type = list(string)
19+
default = []
20+
}
21+
22+
variable "identitystore_user_depends_on" {
23+
description = "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"
24+
type = list(string)
25+
default = []
26+
}
27+
28+
variable "identitystore_permission_set_depends_on" {
29+
description = "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"
30+
type = list(string)
31+
default = []
32+
}
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+
}

modules/account_permissions_assignment/README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Terraform AWS Organization Account Permissions Assignment Module
2+
A Terraform module for associating permissions to AWS accounts.
3+
14
## Requirements
25

36
| Name | Version |
@@ -10,7 +13,7 @@
1013

1114
| Name | Version |
1215
|------|---------|
13-
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.57.0 |
16+
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.58.0 |
1417
| <a name="provider_null"></a> [null](#provider\_null) | 3.2.2 |
1518

1619
## Modules
@@ -24,17 +27,20 @@ No modules.
2427
| [aws_ssoadmin_account_assignment.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ssoadmin_account_assignment) | resource |
2528
| [null_resource.sso_group_dependency](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
2629
| [null_resource.sso_permission_set_dependency](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
27-
| [aws_identitystore_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/identitystore_group) | data source |
28-
| [aws_ssoadmin_instances.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ssoadmin_instances) | data source |
29-
| [aws_ssoadmin_permission_set.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ssoadmin_permission_set) | data source |
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 |
3035

3136
## Inputs
3237

3338
| Name | Description | Type | Default | Required |
3439
|------|-------------|------|---------|:--------:|
35-
| <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 names to be assigned.<br> - principal\_name: An identifier for an object in AWS SSO, such as the name of an SSO group. | <pre>list(object({<br> account_id = string<br> permission_sets = list(string)<br> principal_name = string<br> }))</pre> | `[]` | no |
36-
| <a name="input_identitystore_group_depends_on"></a> [identitystore\_group\_depends\_on](#input\_identitystore\_group\_depends\_on) | A list of parameters 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 |
37-
| <a name="input_identitystore_permission_set_depends_on"></a> [identitystore\_permission\_set\_depends\_on](#input\_identitystore\_permission\_set\_depends\_on) | A list of parameters 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 |
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 |
3844

3945
## Outputs
4046

modules/account_permissions_assignment/data.tf

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ resource "null_resource" "sso_group_dependency" {
44
}
55
}
66

7-
data "aws_identitystore_group" "this" {
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" {
814
for_each = local.group_list
915

1016
identity_store_id = local.identity_store_id
@@ -19,7 +25,22 @@ data "aws_identitystore_group" "this" {
1925
depends_on = [null_resource.sso_group_dependency]
2026
}
2127

22-
data "aws_ssoadmin_instances" "this" {
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" {
2344

2445
}
2546

@@ -29,7 +50,7 @@ resource "null_resource" "sso_permission_set_dependency" {
2950
}
3051
}
3152

32-
data "aws_ssoadmin_permission_set" "this" {
53+
data "aws_ssoadmin_permission_set" "default" {
3354
for_each = local.permission_set_list
3455

3556
instance_arn = local.sso_instance_arn

modules/account_permissions_assignment/locals.tf

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,40 @@
22
locals {
33
flatten_account_group_permission = flatten([
44
for acc_assignment in var.account_assignments : [
5-
for ps_name in acc_assignment.permission_sets : {
6-
acc_id = acc_assignment.account_id
7-
principal_name = acc_assignment.principal_name
8-
ps_name = ps_name
9-
}
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+
]
1013
]
1114
])
1215
assignment_map = {
1316
for a in local.flatten_account_group_permission :
14-
format("%v-%v-%v", a.acc_id, a.principal_name, a.ps_name) => a
17+
format("%v-%v-%v-%v", a.acc_id, substr(a.principal_type, 0, 1), a.principal_name, a.ps_name) => a
1518
}
1619

17-
identity_store_id = tolist(data.aws_ssoadmin_instances.this.identity_store_ids)[0]
18-
sso_instance_arn = tolist(data.aws_ssoadmin_instances.this.arns)[0]
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]
1922

20-
group_list = toset([for mapping in var.account_assignments : mapping.principal_name])
21-
all_permission_sets = flatten([for mapping in var.account_assignments : [
22-
[for ps_name in mapping.permission_sets : ps_name
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"
2326
]
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"
2432
]
25-
])
26-
permission_set_list = toset(local.all_permission_sets)
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)
2741
}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
locals {
2-
target_type = "AWS_ACCOUNT"
3-
principal_type = "GROUP"
2+
target_type = "AWS_ACCOUNT"
43
}
54

65
resource "aws_ssoadmin_account_assignment" "this" {
76
for_each = local.assignment_map
87

98
instance_arn = local.sso_instance_arn
10-
permission_set_arn = data.aws_ssoadmin_permission_set.this[each.value.ps_name].arn
11-
principal_id = data.aws_identitystore_group.this[each.value.principal_name].group_id
12-
principal_type = local.principal_type
9+
permission_set_arn = data.aws_ssoadmin_permission_set.default[each.value.ps_name].arn
10+
principal_id = each.value.principal_type == "GROUP" ? data.aws_identitystore_group.default[each.value.principal_name].group_id : data.aws_identitystore_user.default[each.value.principal_name].user_id
11+
principal_type = each.value.principal_type
1312
target_id = each.value.acc_id
1413
target_type = local.target_type
1514
}

0 commit comments

Comments
 (0)