Skip to content

Commit 757286d

Browse files
authored
Merge pull request #103 from answerdigital/sso-inline-policy
SSO Permission Set support for inline IAM policies
2 parents a73bba3 + 5e5f474 commit 757286d

File tree

4 files changed

+60
-11
lines changed

4 files changed

+60
-11
lines changed

modules/aws/sso_account_assignment/README.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,17 @@ to be used with AWS IAM Identity Center.
2525
| [aws_ssoadmin_account_assignment.to_group](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ssoadmin_account_assignment) | resource |
2626
| [aws_ssoadmin_managed_policy_attachment.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ssoadmin_managed_policy_attachment) | resource |
2727
| [aws_ssoadmin_permission_set.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ssoadmin_permission_set) | resource |
28+
| [aws_ssoadmin_permission_set_inline_policy.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ssoadmin_permission_set_inline_policy) | resource |
2829
| [aws_identitystore_group.by_display_name](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/identitystore_group) | data source |
2930
| [aws_ssoadmin_instances.identity_center](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ssoadmin_instances) | data source |
3031

3132
## Inputs
3233

3334
| Name | Description | Type | Default | Required |
3435
|------|-------------|------|---------|:--------:|
35-
| <a name="input_assignments"></a> [assignments](#input\_assignments) | List of assignments between group, account and permission set. The key of each object is the group<br> name that will be assigned the permissions. Ideally the organisation will use an external identity<br> provider and this group should be created via SCIM. To also create the groups, enable `create_groups`.<br><br> • `account_ids` - (Required) The AWS account IDs to apply the assignment.<br> • `permission_sets` - (Required) The Permission Sets to be assigned to the group. These should<br> be a subset of the Permission Sets created above. | <pre>map(list(object({<br> account_ids = list(string)<br> permission_sets = list(string)<br> })))</pre> | n/a | yes |
36+
| <a name="input_assignments"></a> [assignments](#input\_assignments) | List of assignments between group, account and Permission Set. The key of each object is the group<br> name that will be assigned the permissions. Ideally the organisation will use an external identity<br> provider and this group should be created via SCIM. To also create the groups, enable `create_groups`.<br><br> • `account_ids` - (Required) The AWS account IDs to apply the assignment.<br> • `permission_sets` - (Required) The Permission Sets to be assigned to the group. These should<br> be a subset of the Permission Sets created above. | <pre>map(list(object({<br> account_ids = list(string)<br> permission_sets = list(string)<br> })))</pre> | n/a | yes |
3637
| <a name="input_create_groups"></a> [create\_groups](#input\_create\_groups) | Whether the module should also create the groups. | `bool` | `false` | no |
37-
| <a name="input_permission_sets"></a> [permission\_sets](#input\_permission\_sets) | List of permission sets for the organization.<br><br> • `name` - (Optional) The name of the Permission Set. The key will be used by default.<br> • `description` - (Optional) The description of the Permission Set.<br> • `managed_policies` - (Required) A list of managed policy names. The prefix `arn:aws:iam::aws:policy/`<br> will be prepended to create the full ARN. | <pre>map(object({<br> name = optional(string)<br> description = optional(string)<br> managed_policies = list(string)<br> }))</pre> | n/a | yes |
38+
| <a name="input_permission_sets"></a> [permission\_sets](#input\_permission\_sets) | List of Permission Sets for the organization. Each Permission Set must include AWS managed<br> policies and/or an IAM inline policy.<br><br>`name` - (Optional) The name of the Permission Set. The key will be used by default.<br> • `description` - (Optional) The description of the Permission Set.<br> • `managed_policies` - (Optional) A list of AWS-managed policy names. The prefix `arn:aws:iam::aws:policy/`<br> will be prepended to create the full ARN.<br> • `inline_policy` - (Optional) An IAM inline policy to attach to the Permission Set. | <pre>map(object({<br> name = optional(string)<br> description = optional(string)<br> managed_policies = optional(list(string), [])<br> inline_policy = optional(string, "")<br> }))</pre> | n/a | yes |
3839
<!-- END_TF_DOCS -->
3940

4041
# Example Usage
@@ -67,3 +68,28 @@ module "iam_example" {
6768
}
6869
}
6970
```
71+
72+
You can also provide inline IAM policies:
73+
74+
```hcl
75+
data "aws_iam_policy_document" "example" {
76+
statement {
77+
actions = [
78+
"s3:ListAllMyBuckets",
79+
"s3:GetBucketLocation",
80+
]
81+
82+
resources = ["arn:aws:s3:::*"]
83+
}
84+
}
85+
86+
module "iam_example" {
87+
# ...
88+
89+
permission_sets = {
90+
S3BucketAccess = {
91+
inline_policy = data.aws_iam_policy_document.example.json
92+
}
93+
}
94+
}
95+
```

modules/aws/sso_account_assignment/locals.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ locals {
1212
]
1313
])
1414

15+
inline_policies = [
16+
for permission_set, options in var.permission_sets : {
17+
permission_set = permission_set
18+
inline_policy = options.inline_policy
19+
} if options.inline_policy != ""
20+
]
21+
1522
account_assignments = flatten(flatten(flatten([
1623
for group, assignments in var.assignments : [
1724
for assignment in assignments : [

modules/aws/sso_account_assignment/main.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ resource "aws_ssoadmin_managed_policy_attachment" "this" {
2222
permission_set_arn = aws_ssoadmin_permission_set.this[each.value.permission_set].arn
2323
}
2424

25+
resource "aws_ssoadmin_permission_set_inline_policy" "this" {
26+
for_each = { for p in local.inline_policies : p.permission_set => p }
27+
28+
inline_policy = each.value.inline_policy
29+
instance_arn = local.instance_arn
30+
permission_set_arn = aws_ssoadmin_permission_set.this[each.key].arn
31+
}
32+
2533
resource "aws_ssoadmin_account_assignment" "to_group" {
2634
for_each = { for a in local.account_assignments : "${a.account_id}_${a.group}_${a.permission_set}" => a }
2735

modules/aws/sso_account_assignment/variables.tf

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
11
variable "permission_sets" {
22
description = <<EOT
3-
List of permission sets for the organization.
4-
5-
• `name` - (Optional) The name of the Permission Set. The key will be used by default.
6-
• `description` - (Optional) The description of the Permission Set.
7-
• `managed_policies` - (Required) A list of managed policy names. The prefix `arn:aws:iam::aws:policy/`
8-
will be prepended to create the full ARN.
3+
List of Permission Sets for the organization. Each Permission Set must include AWS managed
4+
policies and/or an IAM inline policy.
5+
6+
• `name` - (Optional) The name of the Permission Set. The key will be used by default.
7+
• `description` - (Optional) The description of the Permission Set.
8+
• `managed_policies` - (Optional) A list of AWS-managed policy names. The prefix `arn:aws:iam::aws:policy/`
9+
will be prepended to create the full ARN.
10+
• `inline_policy` - (Optional) An IAM inline policy to attach to the Permission Set.
911
EOT
1012
type = map(object({
1113
name = optional(string)
1214
description = optional(string)
13-
managed_policies = list(string)
15+
managed_policies = optional(list(string), [])
16+
inline_policy = optional(string, "")
1417
}))
18+
19+
validation {
20+
condition = alltrue([for p, opts in var.permission_sets : (length(opts.managed_policies) > 0 || opts.inline_policy != "")])
21+
error_message = "Permission Sets must specify at least one of managed_policies or inline_policy."
22+
}
1523
}
1624

1725
variable "assignments" {
1826
description = <<EOT
19-
List of assignments between group, account and permission set. The key of each object is the group
27+
List of assignments between group, account and Permission Set. The key of each object is the group
2028
name that will be assigned the permissions. Ideally the organisation will use an external identity
2129
provider and this group should be created via SCIM. To also create the groups, enable `create_groups`.
22-
30+
2331
• `account_ids` - (Required) The AWS account IDs to apply the assignment.
2432
• `permission_sets` - (Required) The Permission Sets to be assigned to the group. These should
2533
be a subset of the Permission Sets created above.

0 commit comments

Comments
 (0)