inventory_role.tf 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Contains roles used for gathering inventory across AWS accounts
  2. # These roles are assumed into from the salt-master instance in order to
  3. # gather data about the instances.
  4. #
  5. # This is a low risk policy that provides view only access to select
  6. # services.
  7. locals {
  8. # Trust these ARNs:
  9. #
  10. # Commercial - Trust the user in C2
  11. # GovCloud - Trust the role in C2
  12. #
  13. # Test - Trust both prod and test C2 arns
  14. # Prod - Trust only the prod C2 arns
  15. #
  16. # Note: No support for the legacy salt master is included. The
  17. # new govcloud salt masters will be 100% repsonsible for
  18. # the inventory.
  19. trusted_arns_map = {
  20. "test" = {
  21. "aws" = [
  22. "arn:aws:iam::045312110490:user/instance/salt-master", # mdr-prod-c2
  23. "arn:aws:iam::816914342178:user/instance/salt-master", # mdr-test-c2
  24. ],
  25. "aws-us-gov" = [
  26. "arn:aws-us-gov:iam::721817724804:role/salt-master-instance-role", # mdr-prod-c2-gov
  27. "arn:aws-us-gov:iam::738800754746:role/salt-master-instance-role", # mdr-test-c2-gov
  28. ]
  29. },
  30. "prod" = {
  31. "aws" = [
  32. "arn:aws:iam::045312110490:user/instance/salt-master", # mdr-prod-c2
  33. ],
  34. "aws-us-gov" = [
  35. "arn:aws-us-gov:iam::721817724804:role/salt-master-instance-role", # mdr-prod-c2-gov
  36. ]
  37. },
  38. "common" = {
  39. "aws" = [
  40. "arn:aws:iam::045312110490:user/instance/salt-master", # mdr-prod-c2
  41. ],
  42. "aws-us-gov" = [
  43. "arn:aws-us-gov:iam::721817724804:role/salt-master-instance-role", # mdr-prod-c2-gov
  44. ]
  45. }
  46. }
  47. trusted_arns = local.trusted_arns_map[var.environment][var.aws_partition]
  48. }
  49. resource "aws_iam_role" "salt_master_inventory_role" {
  50. depends_on = [aws_iam_user.salt-master]
  51. name = "salt-master-inventory-role"
  52. path = "/service/"
  53. force_detach_policies = true # causes "DeleteConflict" if not present
  54. # the extra_trusted_salt variable allows the addition of additional
  55. # trusted sources, such as the dev salt master (for dev environments)
  56. # and developer users.
  57. assume_role_policy = <<EOF
  58. {
  59. "Version": "2012-10-17",
  60. "Statement": [
  61. {
  62. "Effect": "Allow",
  63. "Principal": {
  64. "AWS": ${jsonencode(local.trusted_arns)}
  65. },
  66. "Action": "sts:AssumeRole"
  67. }
  68. ]
  69. }
  70. EOF
  71. }
  72. resource "aws_iam_role_policy_attachment" "salt_master_inventory_policy_attach" {
  73. role = aws_iam_role.salt_master_inventory_role.name
  74. policy_arn = aws_iam_policy.salt_master_inventory_policy.arn
  75. }
  76. resource "aws_iam_policy" "salt_master_inventory_policy" {
  77. name = "salt-master-inventory-policy"
  78. path = "/service/"
  79. description = "Policy which allows the salt master to perform inventory."
  80. policy = data.aws_iam_policy_document.salt_master_inventory_policy_doc.json
  81. }
  82. data "aws_iam_policy_document" "salt_master_inventory_policy_doc" {
  83. statement {
  84. sid = "DescribeAllAssets"
  85. effect = "Allow"
  86. actions = [
  87. "ec2:DescribeInstances",
  88. "ec2:DescribeRegions",
  89. "rds:DescribeDBInstances",
  90. "rds:ListTagsForResource"
  91. ]
  92. # tfsec:ignore:aws-iam-no-policy-wildcards This is read-only access
  93. resources = ["*"]
  94. }
  95. }