Skip to content

Commit

Permalink
add create-profile.USERNAME.sh generation
Browse files Browse the repository at this point in the history
  • Loading branch information
badra001 committed Aug 1, 2022
1 parent a2bbff9 commit 07799cb
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,6 @@

* 2.7.4 -- 2022-06-27
- fix pgp key detection

* 2.8.0 -- 2022-08-01
- add create-profile.USERNAME.sh generation
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ No requirements.
| [aws_ses_email_identity.user_send_mail](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ses_email_identity) | resource |
| [local_file.rotate_keys_tfvars](https://registry.terraform.io/providers/hashicorp/local/latest/docs/resources/file) | resource |
| [local_sensitive_file.access_key_file](https://registry.terraform.io/providers/hashicorp/local/latest/docs/resources/sensitive_file) | resource |
| [local_sensitive_file.user_create_profile](https://registry.terraform.io/providers/hashicorp/local/latest/docs/resources/sensitive_file) | resource |
| [null_resource.access_key_file_decrypt_key](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [null_resource.rotate_keys_tfvars](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [time_sleep.wait_iam_access_key_v1](https://registry.terraform.io/providers/hashicorp/time/latest/docs/resources/sleep) | resource |
Expand Down
10 changes: 10 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,13 @@ resource "aws_iam_user_policy" "user_policy" {
user = aws_iam_user.user.name
policy = each.value
}

resource "local_sensitive_file" "user_create_profile" {
filename = format("%v/access_keys/%v/create-profile.%v.sh", path.root, aws_iam_user.user.name)
file_permission = "0755"
content = templatefile("${path.root}/templates/create-profile.sh.tpl", {
username = aws_iam_user.user.name
source_profile = var.profile
source_region = local.region
})
}
30 changes: 30 additions & 0 deletions templates/create-assumerole-profile.sh.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

VERSION="1.0.2"
SOURCE_PROFILE=$1
if [ -z "$SOURCE_PROFILE" ]
then
SOURCE_PROFILE="${source_profile}"
fi

if [ -z $USER ]
then
USER="${username}"
fi

%{ for group,arn in groups ~}
NEW_PROFILE="${profile_prefix}-${group}"
if [ $(aws configure list-profiles | grep -c ^$NEW_PROFILE$) == 0 ]
then
echo "echo \"# v$VERSION: creating assume-role profile $NEW_PROFILE\""
echo "aws configure set profile.$NEW_PROFILE.source_profile $SOURCE_PROFILE"
echo "aws configure set profile.$NEW_PROFILE.region ${source_region}"
echo "aws configure set profile.$NEW_PROFILE.role_arn ${arn}"
echo "aws configure set profile.$NEW_PROFILE.role_session_name $USER"
else
echo "echo \"# profile $NEW_PROFILE already created with source_profile $SOURCE_PROFILE\""
fi
echo "echo \"# TEST: aws --profile $NEW_PROFILE sts get-caller-identity\""
echo "echo \"\""

%{ endfor ~}
50 changes: 50 additions & 0 deletions templates/create-profile.sh.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash

VERSION="1.0.1"
SOURCE_PROFILE=$1
if [ -z "$SOURCE_PROFILE" ]
then
SOURCE_PROFILE="${source_profile}"
fi

if [ -z $USER ]
then
USER="${username}"
fi

if [ -z $FILE ]
then
FILE="access_key.yml"
fi

if [ ! -r $FILE ]
then
echo "# unable to find $FILE, exiting"
exit 1
fi

aws_access_key_id=$( grep ^access_key_id: $FILE | sed -e 's/^.*: *//')
aws_secret_access_key=$( grep ^access_secret_key: $FILE | sed -e 's/^.*: *//')

if [ -z $aws_access_key_id ]
then
echo "# access_key_id missing from $FILE"
exit 1
fi
if [ -z $aws_secret_access_key ]
then
echo "# acess_secret_key missing from $FILE"
exit 1
fi

if [ $(aws configure list-profiles | grep -c ^$SOURCE_PROFILE$) == 0 ]
then
echo "echo \"# v$VERSION: creating source profile $SOURCE_PROFILE\""
echo "aws configure set profile.$SOURCE_PROFILE.aws_access_key_id $aws_access_key_id"
echo "aws configure set profile.$SOURCE_PROFILE.aws_secret_access_key $aws_secret_access_key"
echo "aws configure set profile.$SOURCE_PROFILE.region ${source_region}"
echo "aws configure set profile.$SOURCE_PROFILE.output json"
else
echo "echo \"# profile $SOURCE_PROFILE already created\""
fi
echo "echo \"# TEST: aws --profile $SOURCE_PROFILE sts get-caller-identity\""
2 changes: 1 addition & 1 deletion version.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
locals {
_module_name = "aws-iam-user"
_module_version = "2.7.4"
_module_version = "2.8.0"
}

0 comments on commit 07799cb

Please sign in to comment.