iam.github-actions.tf 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #
  2. # Create an IAM user (and group) to use with GitHub Actions
  3. #
  4. ######################
  5. # Access keys
  6. #
  7. # For rotation purposes, there are two of these. Delete the oldest one,
  8. # add a new one (with a higher version number), and then update the output
  9. #
  10. # Possible futue improvement:
  11. # We could specify a pgp_key attribute, and then the secret will be encrypted
  12. # in both the state file and in the output. If we used the salt PGP key,
  13. # no user would ever have to see the secret key.
  14. resource "aws_iam_access_key" "github-actions-v1" {
  15. user = aws_iam_user.github-actions.name
  16. }
  17. resource "aws_iam_access_key" "github-actions-v2" {
  18. user = aws_iam_user.github-actions.name
  19. }
  20. output "access_keys" {
  21. value = {
  22. "current" = {
  23. "aws_access_key_id" : aws_iam_access_key.github-actions-v2.id
  24. "aws_secret_access_key" : aws_iam_access_key.github-actions-v2.secret
  25. },
  26. "previous" = {
  27. "aws_access_key_id" : aws_iam_access_key.github-actions-v1.id
  28. "aws_secret_access_key" : aws_iam_access_key.github-actions-v1.secret
  29. }
  30. }
  31. sensitive = true
  32. }
  33. ######################
  34. # The policy is attached to both the user and the instance profile
  35. data "aws_iam_policy_document" "github-actions" {
  36. statement {
  37. sid = "1"
  38. actions = [
  39. "s3:PutObject",
  40. "s3:GetObject",
  41. "s3:ListBucketMultipartUploads",
  42. "kms:Decrypt",
  43. "s3:AbortMultipartUpload",
  44. "kms:GenerateDataKey",
  45. "s3:ListBucket",
  46. "s3:DeleteObject",
  47. "s3:ListMultipartUploadParts"
  48. ]
  49. resources = [
  50. aws_kms_key.bucketkey.arn,
  51. aws_s3_bucket.bucket.arn,
  52. "${aws_s3_bucket.bucket.arn}/*"
  53. ]
  54. }
  55. }
  56. resource "aws_iam_policy" "github-actions" {
  57. name = "github-actions"
  58. path = "/"
  59. description = "Policy to allow the github-actions to use the GH Actions S3 bucket and KMS"
  60. policy = data.aws_iam_policy_document.github-actions.json
  61. }
  62. ######################
  63. # the user
  64. #
  65. # Note: CIS requires that policies _NOT_ be directly attached to a user. Users must
  66. # be members of groups, and those groups can have policies.
  67. resource "aws_iam_user" "github-actions" {
  68. name = "github-actions"
  69. path = "/instance/"
  70. tags = merge(local.standard_tags, var.tags)
  71. }
  72. # tfsec:ignore:aws-iam-enforce-mfa
  73. resource "aws_iam_group" "github-actions" {
  74. name = "github-actions"
  75. path = "/instance/"
  76. }
  77. resource "aws_iam_user_group_membership" "github-actions" {
  78. user = aws_iam_user.github-actions.name
  79. groups = [aws_iam_group.github-actions.name]
  80. }
  81. resource "aws_iam_group_policy_attachment" "github-actions-group" {
  82. group = aws_iam_group.github-actions.name
  83. policy_arn = aws_iam_policy.github-actions.arn
  84. }