iam.moose-hf.tf 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # the 'splunk-addon-for-aws' role is created in all accounts via
  2. # the base/account_standards module.
  3. #
  4. # Then, there is an instance profile (for use in the partition holding moose)
  5. # and a user account (for use in the partion _not_ holding moose) that
  6. # with keys for moose.
  7. #
  8. # That instance profile/user is allowed to assumerole into the
  9. # 'splunk-addon-for-aws' role in the other accounts.
  10. ######################
  11. # Access keys
  12. #
  13. # For rotation purposes, there are two of these. Delete the oldest one,
  14. # add a new one (with a higher version number), and then update the output
  15. #
  16. # Possible futue improvement:
  17. # We could specify a pgp_key attribute, and then the secret will be encrypted
  18. # in both the state file and in the output. If we used the salt PGP key,
  19. # no user would ever have to see the secret key.
  20. resource "aws_iam_access_key" "moose-hf-v0" {
  21. user = aws_iam_user.moose-hf.name
  22. }
  23. resource "aws_iam_access_key" "moose-hf-v1" {
  24. user = aws_iam_user.moose-hf.name
  25. }
  26. output access_keys {
  27. value = {
  28. "current" = {
  29. "aws_access_key_id": aws_iam_access_key.moose-hf-v1.id
  30. "aws_secret_access_key": aws_iam_access_key.moose-hf-v1.secret
  31. },
  32. "previous" = {
  33. "aws_access_key_id": aws_iam_access_key.moose-hf-v0.id
  34. "aws_secret_access_key": aws_iam_access_key.moose-hf-v0.secret
  35. }
  36. }
  37. }
  38. ######################
  39. # The policy is attached to both the user and the instance profile
  40. resource "aws_iam_policy" "moose-hf" {
  41. name = "moose-hf"
  42. path = "/instance/"
  43. description = "Policy to allow the moose HF to assume roles"
  44. policy = <<EOF
  45. {
  46. "Version": "2012-10-17",
  47. "Statement": [
  48. {
  49. "Effect": "Allow",
  50. "Action": "sts:AssumeRole",
  51. "Resource": "*"
  52. }
  53. ]
  54. }
  55. EOF
  56. }
  57. ######################
  58. # The instance profile
  59. resource "aws_iam_instance_profile" "moose-hf" {
  60. name = "moose-hf"
  61. role = aws_iam_role.moose-hf.name
  62. }
  63. resource "aws_iam_role" "moose-hf" {
  64. name = "moose-hf"
  65. path = "/instance/"
  66. assume_role_policy = <<EOF
  67. {
  68. "Version": "2012-10-17",
  69. "Statement": [
  70. {
  71. "Action": "sts:AssumeRole",
  72. "Principal": {
  73. "Service": "ec2.amazonaws.com"
  74. },
  75. "Effect": "Allow",
  76. "Sid": ""
  77. }
  78. ]
  79. }
  80. EOF
  81. }
  82. resource "aws_iam_role_policy_attachment" "moose-hf" {
  83. role = aws_iam_role.moose-hf.name
  84. policy_arn = aws_iam_policy.moose-hf.arn
  85. }
  86. ######################
  87. # the user
  88. #
  89. # Note: CIS requires that policies _NOT_ be directly attached to a user. Users must
  90. # be members of groups, and those groups can have policies.
  91. resource "aws_iam_user" "moose-hf" {
  92. name = "moose-hf"
  93. path = "/instance/"
  94. tags = merge(var.standard_tags, var.tags)
  95. }
  96. resource "aws_iam_group" "moose-hf" {
  97. name = "moose-hf"
  98. path = "/instance/"
  99. }
  100. resource "aws_iam_user_group_membership" "moose-hf" {
  101. user = aws_iam_user.moose-hf.name
  102. groups = [ aws_iam_group.moose-hf.name ]
  103. }
  104. resource "aws_iam_group_policy_attachment" "moose-hf-group" {
  105. group = aws_iam_group.moose-hf.name
  106. policy_arn = aws_iam_policy.moose-hf.arn
  107. }