user.tf 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. ######################
  2. # Access keys
  3. #
  4. # For rotation purposes, there are two of these. Delete the oldest one,
  5. # add a new one (with a higher version number), and then update the output
  6. #
  7. # Possible futue improvement:
  8. # We could specify a pgp_key attribute, and then the secret will be encrypted
  9. # in both the state file and in the output. If we used the salt PGP key,
  10. # no user would ever have to see the secret key.
  11. locals {
  12. # The user in this file will only be created for Commercial C2
  13. is_commercial = var.aws_partition == "aws-us-gov" ? false : true
  14. is_c2 = contains(["045312110490", "816914342178" ], var.aws_account_id)
  15. user_count = local.is_commercial && local.is_c2 ? 1 : 0
  16. }
  17. resource "aws_iam_access_key" "salt-master-v0" {
  18. count = local.user_count
  19. user = aws_iam_user.salt-master[count.index].name
  20. }
  21. resource "aws_iam_access_key" "salt-master-v1" {
  22. count = local.user_count
  23. user = aws_iam_user.salt-master[count.index].name
  24. }
  25. output access_keys {
  26. # Only output the keys if there _are_ keys
  27. value = local.user_count == 0 ? null : {
  28. "current" = {
  29. "aws_access_key_id": aws_iam_access_key.salt-master-v1[0].id
  30. "aws_secret_access_key": aws_iam_access_key.salt-master-v1[0].secret
  31. },
  32. "previous" = {
  33. "aws_access_key_id": aws_iam_access_key.salt-master-v0[0].id
  34. "aws_secret_access_key": aws_iam_access_key.salt-master-v0[0].secret
  35. }
  36. }
  37. }
  38. ######################
  39. # The policy is attached to both the user and the instance profile
  40. data "aws_iam_policy_document" "salt_master_policy_doc" {
  41. statement {
  42. sid = "AllowSaltSecretsCommunication"
  43. effect = "Allow"
  44. actions = [
  45. "secretsmanager:GetResourcePolicy",
  46. "secretsmanager:GetSecretValue",
  47. "secretsmanager:DescribeSecret",
  48. "secretsmanager:ListSecretVersionIds"
  49. ]
  50. resources = [
  51. "arn:${var.aws_partition}:secretsmanager:*:*:secret:saltmaster/*"
  52. ]
  53. }
  54. statement {
  55. sid = "AllowAssumeRole"
  56. effect = "Allow"
  57. actions = [
  58. "sts:AssumeRole"
  59. ]
  60. resources = [
  61. "arn:${var.aws_partition}:iam::*:role/service/salt-master-inventory-role"
  62. ]
  63. }
  64. }
  65. resource "aws_iam_policy" "salt-master" {
  66. count = local.user_count
  67. name = "salt_master_sm"
  68. path = "/"
  69. policy = data.aws_iam_policy_document.salt_master_policy_doc.json
  70. }
  71. ######################
  72. # the user
  73. #
  74. # Note: CIS requires that policies _NOT_ be directly attached to a user. Users must
  75. # be members of groups, and those groups can have policies.
  76. resource "aws_iam_user" "salt-master" {
  77. count = local.user_count
  78. name = "salt-master"
  79. path = "/instance/"
  80. tags = merge(var.standard_tags, var.tags)
  81. }
  82. resource "aws_iam_group" "salt-master" {
  83. count = local.user_count
  84. name = "salt-master"
  85. path = "/instance/"
  86. }
  87. resource "aws_iam_user_group_membership" "salt-master" {
  88. count = local.user_count
  89. user = aws_iam_user.salt-master[count.index].name
  90. groups = [ aws_iam_group.salt-master[count.index].name ]
  91. }
  92. resource "aws_iam_group_policy_attachment" "salt-master-group" {
  93. count = local.user_count
  94. group = aws_iam_group.salt-master[count.index].name
  95. policy_arn = aws_iam_policy.salt-master[count.index].arn
  96. }