iam.moose-hf.tf 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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-v2" {
  21. user = aws_iam_user.moose-hf.name
  22. }
  23. resource "aws_iam_access_key" "moose-hf-v3" {
  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-v3.id
  30. "aws_secret_access_key" : aws_iam_access_key.moose-hf-v3.secret
  31. },
  32. "previous" = {
  33. "aws_access_key_id" : aws_iam_access_key.moose-hf-v2.id
  34. "aws_secret_access_key" : aws_iam_access_key.moose-hf-v2.secret
  35. }
  36. }
  37. sensitive = true
  38. }
  39. ######################
  40. # The policy is attached to both the user and the instance profile
  41. # tfsec:ignore:aws-iam-no-policy-wildcards - baseline this setting first. We use wildcards in policies
  42. resource "aws_iam_policy" "moose-hf" {
  43. name = "moose-hf"
  44. path = "/instance/"
  45. description = "Policy to allow the moose HF to assume roles"
  46. policy = <<EOF
  47. {
  48. "Version": "2012-10-17",
  49. "Statement": [
  50. {
  51. "Effect": "Allow",
  52. "Action": [
  53. "sts:AssumeRole",
  54. "logs:DescribeLogGroups",
  55. "logs:DescribeLogStreams",
  56. "logs:GetLogEvents"
  57. ],
  58. "Resource": "*"
  59. }
  60. ]
  61. }
  62. EOF
  63. }
  64. ######################
  65. # The instance profile
  66. resource "aws_iam_instance_profile" "moose-hf" {
  67. name = "moose-hf"
  68. role = aws_iam_role.moose-hf.name
  69. }
  70. resource "aws_iam_role" "moose-hf" {
  71. name = "moose-hf"
  72. path = "/instance/"
  73. assume_role_policy = <<EOF
  74. {
  75. "Version": "2012-10-17",
  76. "Statement": [
  77. {
  78. "Action": "sts:AssumeRole",
  79. "Principal": {
  80. "Service": "ec2.amazonaws.com"
  81. },
  82. "Effect": "Allow",
  83. "Sid": ""
  84. }
  85. ]
  86. }
  87. EOF
  88. }
  89. resource "aws_iam_role_policy_attachment" "moose-hf" {
  90. role = aws_iam_role.moose-hf.name
  91. policy_arn = aws_iam_policy.moose-hf.arn
  92. }
  93. ######################
  94. # the user
  95. #
  96. # Note: CIS requires that policies _NOT_ be directly attached to a user. Users must
  97. # be members of groups, and those groups can have policies.
  98. resource "aws_iam_user" "moose-hf" {
  99. name = "moose-hf"
  100. path = "/instance/"
  101. tags = merge(local.standard_tags, var.tags)
  102. }
  103. # tfsec:ignore:aws-iam-enforce-mfa
  104. resource "aws_iam_group" "moose-hf" {
  105. name = "moose-hf"
  106. path = "/instance/"
  107. }
  108. resource "aws_iam_user_group_membership" "moose-hf" {
  109. user = aws_iam_user.moose-hf.name
  110. groups = [aws_iam_group.moose-hf.name]
  111. }
  112. resource "aws_iam_group_policy_attachment" "moose-hf-group" {
  113. group = aws_iam_group.moose-hf.name
  114. policy_arn = aws_iam_policy.moose-hf.arn
  115. }