Skip to content

Commit 90f982e

Browse files
committed
Add user_groups_assignment module
1 parent 6e46b12 commit 90f982e

File tree

9 files changed

+191
-0
lines changed

9 files changed

+191
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Terraform AWS SSO User and Groups Assignment Module
2+
This Terraform module manages the assignment of AWS SSO groups to users within the organization.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Terraform AWS SSO User and Groups Assignment Module
2+
This Terraform module manages the assignment of AWS SSO groups to users within the organization.
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_local"></a> [local](#requirement\_local) | ~> 2.5.1 |
11+
12+
## Providers
13+
14+
| Name | Version |
15+
|------|---------|
16+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.65.0 |
17+
| <a name="provider_local"></a> [local](#provider\_local) | ~> 2.5.1 |
18+
19+
## Modules
20+
21+
No modules.
22+
23+
## Resources
24+
25+
| Name | Type |
26+
|------|------|
27+
| [aws_identitystore_group_membership.sso_membership](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/identitystore_group_membership) | resource |
28+
| [aws_identitystore_group.existing_sso_group](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/identitystore_group) | data source |
29+
| [aws_identitystore_user.existing_sso_user](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/identitystore_user) | data source |
30+
| [aws_ssoadmin_instances.sso_instance](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ssoadmin_instances) | data source |
31+
| [local_file.sso_user](https://registry.terraform.io/providers/hashicorp/local/latest/docs/data-sources/file) | data source |
32+
33+
## Inputs
34+
35+
| Name | Description | Type | Default | Required |
36+
|------|-------------|------|---------|:--------:|
37+
| <a name="input_region"></a> [region](#input\_region) | n/a | `string` | `"us-east-1"` | no |
38+
| <a name="input_user_groups_mapping"></a> [user\_groups\_mapping](#input\_user\_groups\_mapping) | Mapping of users to their respective sso groups within the Organisation. For example map of `user@infraspec.dev=[sso_groups]` | `map(list(string))` | `{}` | no |
39+
40+
## Outputs
41+
42+
No outputs.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Fetch existing SSO Instance
2+
data "aws_ssoadmin_instances" "sso_instance" {}
3+
4+
# - Fetch of SSO Users to be used for group membership assignment -
5+
data "aws_identitystore_user" "existing_sso_user" {
6+
for_each = local.users_and_their_groups
7+
8+
identity_store_id = local.sso_instance_id
9+
10+
alternate_identifier {
11+
# Filter users by user_name (nuzumaki, suchiha, dovis, etc.)
12+
unique_attribute {
13+
attribute_path = "UserName"
14+
attribute_value = each.value.user_name
15+
}
16+
}
17+
}
18+
19+
# - Fetch of SSO Groups to be used for group membership assignment -
20+
data "aws_identitystore_group" "existing_sso_group" {
21+
for_each = local.users_and_their_groups
22+
23+
identity_store_id = local.sso_instance_id
24+
25+
alternate_identifier {
26+
unique_attribute {
27+
attribute_path = "DisplayName"
28+
attribute_value = each.value.group_name
29+
}
30+
}
31+
}
32+
33+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# - Users and Groups
2+
locals {
3+
# Create a new local variable by flattening the complex type given in the variable "sso_users"
4+
flatten_user_data = flatten([
5+
for this_user in keys(var.user_groups_mapping) : [
6+
for group in var.user_groups_mapping[this_user] : {
7+
user_name = this_user
8+
group_name = group
9+
}
10+
]
11+
])
12+
13+
users_and_their_groups = {
14+
for s in local.flatten_user_data : format("%s_%s", s.user_name, s.group_name) => s
15+
}
16+
17+
# - Fetch SSO Instance ARN and SSO Instance ID -
18+
sso_instance_id = tolist(data.aws_ssoadmin_instances.sso_instance.identity_store_ids)[0]
19+
}
20+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# - Identity Store Group Membership -
2+
resource "aws_identitystore_group_membership" "sso_membership" {
3+
for_each = local.users_and_their_groups
4+
5+
identity_store_id = local.sso_instance_id
6+
group_id = data.aws_identitystore_group.existing_sso_group[each.key].group_id
7+
member_id = data.aws_identitystore_user.existing_sso_user[each.key].user_id
8+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
variable "user_groups_mapping" {
2+
type = map(list(string))
3+
description = "Mapping of users to their respective sso groups within the Organisation. For example map of `user@infraspec.dev=[sso_groups]"
4+
default = {}
5+
validation {
6+
condition = alltrue([
7+
for user, groups in var.user_groups_mapping : (
8+
can(regex("^[a-zA-Z0-9+]+@infraspec\\.dev$", user)) &&
9+
length(groups) > 0 &&
10+
alltrue([for group in groups : can(regex("^[A-Za-z0-9_]+$", group))])
11+
)
12+
])
13+
error_message = "Each key must be a valid email address with the domain 'infraspec.dev', and each value must be a non-empty list of SSO group names."
14+
}
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = ">= 4.65.0"
6+
}
7+
local = {
8+
source = "hashicorp/local"
9+
version = "~> 2.5.1"
10+
}
11+
}
12+
13+
required_version = ">= 1.4.6"
14+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#Argument validation for user-group-assignment module
2+
run "check_for_invalid_user" {
3+
module {
4+
source = "./modules/user_groups_assignment"
5+
}
6+
7+
variables {
8+
user_groups_mapping = {
9+
"dummyUser@gmail.com" = [
10+
"Group1"
11+
]
12+
}
13+
}
14+
15+
command = plan
16+
17+
expect_failures = [
18+
var.user_groups_mapping
19+
]
20+
}
21+
22+
run "check_for_valid_user_with_no_groups" {
23+
module {
24+
source = "./modules/user_groups_assignment"
25+
}
26+
27+
variables {
28+
user_groups_mapping = {
29+
"dummyUser@gmail.com" = []
30+
}
31+
}
32+
33+
command = plan
34+
35+
expect_failures = [
36+
var.user_groups_mapping
37+
]
38+
}
39+
40+
run "check_for_invalid_group" {
41+
module {
42+
source = "./modules/user_groups_assignment"
43+
}
44+
45+
variables {
46+
user_groups_mapping = {
47+
"dummyUser@gmail.com" = [""]
48+
}
49+
}
50+
51+
command = plan
52+
53+
expect_failures = [
54+
var.user_groups_mapping
55+
]
56+
}

0 commit comments

Comments
 (0)