Skip to content

Commit

Permalink
Allow users manage own access keys (#10)
Browse files Browse the repository at this point in the history
* Allow users manage own access keys
  • Loading branch information
goruha authored Dec 26, 2018
1 parent 1e6a99d commit c810a48
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,48 @@ data "aws_iam_policy_document" "allow_change_password" {
count = "${local.enabled ? 1 : 0}"

statement {
actions = ["iam:ChangePassword"]
actions = ["iam:ChangePassword"]

resources = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:user/&{aws:username}"]
}

statement {
actions = ["iam:GetAccountPasswordPolicy"]
resources = ["*"]
}

statement {
actions = ["iam:GetLoginProfile"]

resources = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:user/&{aws:username}"]

condition {
test = "Bool"
variable = "aws:MultiFactorAuthPresent"
values = ["true"]
}
}
}

data "aws_iam_policy_document" "allow_key_management" {
statement {
actions = [
"iam:DeleteAccessKey",
"iam:GetAccessKeyLastUsed",
"iam:UpdateAccessKey",
"iam:GetUser",
"iam:CreateAccessKey",
"iam:ListAccessKeys",
]

resources = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:user/&{aws:username}"]

condition {
test = "Bool"
variable = "aws:MultiFactorAuthPresent"
values = ["true"]
}
}
}

# Admin config
Expand All @@ -128,6 +162,12 @@ resource "aws_iam_policy" "allow_change_password_admin" {
policy = "${join("", data.aws_iam_policy_document.allow_change_password.*.json)}"
}

resource "aws_iam_policy" "allow_key_management_admin" {
name = "${module.admin_label.id}-allow-key-management"
description = "Allow admin users to manage their own access keys"
policy = "${data.aws_iam_policy_document.allow_key_management.json}"
}

data "aws_iam_policy_document" "assume_role_admin" {
count = "${local.enabled ? 1 : 0}"

Expand Down Expand Up @@ -173,6 +213,11 @@ resource "aws_iam_group_policy_attachment" "allow_chage_password_admin" {
policy_arn = "${join("", aws_iam_policy.allow_change_password_admin.*.arn)}"
}

resource "aws_iam_group_policy_attachment" "key_management_admin" {
group = "${aws_iam_group.admin.name}"
policy_arn = "${aws_iam_policy.allow_key_management_admin.arn}"
}

resource "aws_iam_role_policy_attachment" "admin" {
count = "${local.enabled ? 1 : 0}"
role = "${join("", aws_iam_role.admin.*.name)}"
Expand Down Expand Up @@ -202,6 +247,12 @@ resource "aws_iam_policy" "allow_change_password_readonly" {
policy = "${join("", data.aws_iam_policy_document.allow_change_password.*.json)}"
}

resource "aws_iam_policy" "allow_key_management_readonly" {
name = "${module.readonly_label.id}-permit-manage-keys"
description = "Allow readonly users to manage their own access keys"
policy = "${data.aws_iam_policy_document.allow_key_management.json}"
}

data "aws_iam_policy_document" "assume_role_readonly" {
statement {
actions = ["sts:AssumeRole"]
Expand Down Expand Up @@ -245,6 +296,11 @@ resource "aws_iam_group_policy_attachment" "allow_change_password_readonly" {
policy_arn = "${join("", aws_iam_policy.allow_change_password_readonly.*.arn)}"
}

resource "aws_iam_group_policy_attachment" "key_management_readonly" {
group = "${aws_iam_group.readonly.name}"
policy_arn = "${aws_iam_policy.allow_key_management_readonly.arn}"
}

resource "aws_iam_role_policy_attachment" "readonly" {
count = "${local.enabled ? 1 : 0}"
role = "${join("", aws_iam_role.readonly.*.name)}"
Expand Down

0 comments on commit c810a48

Please sign in to comment.