Skip to content

Commit

Permalink
Merge pull request #2 from terraform-modules/add-attached-policies
Browse files Browse the repository at this point in the history
add attached_policies variable
  • Loading branch information
badra001 committed May 12, 2021
2 parents 38cc37d + fe00fb2 commit 2525e1b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md → CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@

* v2.4.0 -- 20210405
- use `ldap-get-attribute` to get email address

* v2.5.0 -- 20210512
- add `attached_policies` to enable adding policies for an IAM account (generally, just services)
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ version.
`roles`, `saml_role` and `create_access_keys` do not at this point do anything,
though some effort is in progress to make access keys operational.

You can only attach a policy if the `service_account` is true. This example below will not fail,
but it will not attach a policy to the user account.

# Usage

```hcl
Expand All @@ -27,6 +30,7 @@ module "admin_user_bond0007" {
pgp_key = file("init/tf-gpg-key.b64")
service_account = false
enable_sending_mail = false
attached_policies = [ aws_iam_policy.mypolicy.arn ]
# roles = [ ]
# saml_role = [ "role-name" ]
Expand Down Expand Up @@ -54,6 +58,7 @@ No requirements.

| Name | Type |
|------|------|
| [aws_iam_policy_attachment.user_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy_attachment) | resource |
| [aws_iam_user.user](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_user) | resource |
| [aws_iam_user_group_membership.user_groups](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_user_group_membership) | resource |
| [aws_iam_user_login_profile.user](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_user_login_profile) | resource |
Expand All @@ -66,6 +71,7 @@ No requirements.

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_attached_policies"></a> [attached\_policies](#input\_attached\_policies) | List of IAM Policy ARNs to attach to this user (only for service accounts) | `list(string)` | `[]` | no |
| <a name="input_create_access_keys"></a> [create\_access\_keys](#input\_create\_access\_keys) | Set to 1 or true to create access keys (not implemented) | `bool` | `false` | no |
| <a name="input_email_address"></a> [email\_address](#input\_email\_address) | Email address for the user | `string` | n/a | yes |
| <a name="input_enable_sending_mail"></a> [enable\_sending\_mail](#input\_enable\_sending\_mail) | Enable using email address for the user from SES | `bool` | `false` | no |
Expand Down
19 changes: 18 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
* `roles`, `saml_role` and `create_access_keys` do not at this point do anything,
* though some effort is in progress to make access keys operational.
*
* You can only attach a policy if the `service_account` is true. This example below will not fail,
* but it will not attach a policy to the user account.
*
* # Usage
*
*
* ```hcl
* module "admin_user_bond0007" {
Expand All @@ -29,6 +31,7 @@
* pgp_key = file("init/tf-gpg-key.b64")
* service_account = false
* enable_sending_mail = false
* attached_policies = [ aws_iam_policy.mypolicy.arn ]
*
* # roles = [ ]
* # saml_role = [ "role-name" ]
Expand All @@ -44,6 +47,12 @@ locals {
email_address = var.email_address != "" ? lower(var.email_address) : module.user_email.search_result["attribute_value"][0]
pgp_key_exists = var.pgp_key != "" ? 1 : 0
generate_password = var.generate_password ? local.pgp_key_exists : 0
ap1 = var.service_account ? var.attached_policies : []
ap2 = [for arn in local.ap1 : {
"policy" = arn
"parts" = split("/", arn)
}]
attached_policies = { for v in local.ap2 : element(v.parts, length(v.parts) - 1) => v.policy }
tags_username = var.service_account ? map("boc:id:contact", local.username) : map("boc:id:username", local.username)
tags_email = {
"exists" = {
Expand Down Expand Up @@ -120,3 +129,11 @@ module "user_email" {
filter = "cn=${local.username}"
attribute = "mail"
}

resource "aws_iam_policy_attachment" "user_policy" {
for_each = local.attached_policies
name = format("%v-attachment", each.key)
users = aws_iam_user.user.user_name
policy_arn = each.value
}

6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,9 @@ variable "service_account" {
type = bool
default = false
}

variable "attached_policies" {
description = "List of IAM Policy ARNs to attach to this user (only for service accounts)"
type = list(string)
default = []
}
2 changes: 1 addition & 1 deletion version.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
locals {
_module_version = "2.4.0"
_module_version = "2.5.0"
}

0 comments on commit 2525e1b

Please sign in to comment.