iam.moose-hf.tf 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. sensitive = true
  38. }
  39. ######################
  40. # The policy is attached to both the user and the instance profile
  41. resource "aws_iam_policy" "moose-hf" {
  42. name = "moose-hf"
  43. path = "/instance/"
  44. description = "Policy to allow the moose HF to assume roles"
  45. policy = <<EOF
  46. {
  47. "Version": "2012-10-17",
  48. "Statement": [
  49. {
  50. "Effect": "Allow",
  51. "Action": "sts:AssumeRole",
  52. "Resource": "*"
  53. }
  54. ]
  55. }
  56. EOF
  57. }
  58. ######################
  59. # The instance profile
  60. resource "aws_iam_instance_profile" "moose-hf" {
  61. name = "moose-hf"
  62. role = aws_iam_role.moose-hf.name
  63. }
  64. resource "aws_iam_role" "moose-hf" {
  65. name = "moose-hf"
  66. path = "/instance/"
  67. assume_role_policy = <<EOF
  68. {
  69. "Version": "2012-10-17",
  70. "Statement": [
  71. {
  72. "Action": "sts:AssumeRole",
  73. "Principal": {
  74. "Service": "ec2.amazonaws.com"
  75. },
  76. "Effect": "Allow",
  77. "Sid": ""
  78. }
  79. ]
  80. }
  81. EOF
  82. }
  83. resource "aws_iam_role_policy_attachment" "moose-hf" {
  84. role = aws_iam_role.moose-hf.name
  85. policy_arn = aws_iam_policy.moose-hf.arn
  86. }
  87. ######################
  88. # the user
  89. #
  90. # Note: CIS requires that policies _NOT_ be directly attached to a user. Users must
  91. # be members of groups, and those groups can have policies.
  92. resource "aws_iam_user" "moose-hf" {
  93. name = "moose-hf"
  94. path = "/instance/"
  95. tags = merge(var.standard_tags, var.tags)
  96. }
  97. resource "aws_iam_group" "moose-hf" {
  98. name = "moose-hf"
  99. path = "/instance/"
  100. }
  101. resource "aws_iam_user_group_membership" "moose-hf" {
  102. user = aws_iam_user.moose-hf.name
  103. groups = [ aws_iam_group.moose-hf.name ]
  104. }
  105. resource "aws_iam_group_policy_attachment" "moose-hf-group" {
  106. group = aws_iam_group.moose-hf.name
  107. policy_arn = aws_iam_policy.moose-hf.arn
  108. }