main.tf 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Генерация random-string для имени bucket---------------------------------------------------------
  2. resource "random_string" "random" {
  3. length = 8
  4. special = false
  5. upper = false
  6. }
  7. // ---------------------------------------------------------------------------------------------
  8. // Создание sa storage admin для создания bucket
  9. resource "yandex_iam_service_account" "sa-creator" {
  10. name = "sa-creator-${random_string.random.result}"
  11. description = "service account to create bucket for audit-logs"
  12. folder_id = var.folder_id
  13. }
  14. // Создание стат ключа
  15. resource "yandex_iam_service_account_static_access_key" "tr-sa-static-key" {
  16. service_account_id = yandex_iam_service_account.sa-creator.id
  17. description = "static access key for object storage"
  18. }
  19. // Назначение прав
  20. resource "yandex_resourcemanager_folder_iam_binding" "storage_admin" {
  21. folder_id = var.folder_id
  22. role = "storage.admin"
  23. members = [
  24. "serviceAccount:${yandex_iam_service_account.sa-creator.id}",
  25. ]
  26. }
  27. // Назначение прав на KMS ключи для работы с шифрованным бакетом для sa-creator
  28. resource "yandex_resourcemanager_folder_iam_binding" "binding-for-sa-creator" {
  29. folder_id = var.folder_id
  30. role = "kms.keys.encrypterDecrypter"
  31. members = ["serviceAccount:${yandex_iam_service_account.sa-creator.id}"]
  32. }
  33. /*
  34. // Назначение прав на KMS ключи для работы с шифрованным бакетом для группы all-access
  35. resource "yandex_resourcemanager_folder_iam_binding" "binding-for-all-access" {
  36. count = length(var.all-access-users)
  37. folder_id = var.folder_id
  38. role = "kms.keys.encrypterDecrypter"
  39. members = [element(var.all-access-users, count.index)]
  40. }
  41. */
  42. // Назначение прав на KMS ключи для работы с шифрованным бакетом для группы read-only-sa
  43. resource "yandex_resourcemanager_folder_iam_binding" "binding-for-read-only-sa" {
  44. count = length(var.read-only-sa)
  45. folder_id = var.folder_id
  46. role = "kms.keys.encrypterDecrypter"
  47. members = [element(var.read-only-sa, count.index)]
  48. }
  49. // Назначение прав на KMS ключи для работы с шифрованным бакетом для группы write-only-sa
  50. resource "yandex_resourcemanager_folder_iam_binding" "binding-for-write-only-sa" {
  51. count = length(var.write-only-sa)
  52. folder_id = var.folder_id
  53. role = "kms.keys.encrypterDecrypter"
  54. members = [element(var.write-only-sa, count.index)]
  55. }
  56. // -------------------------------------------------------------------------------------------------
  57. // Назначение прав группам УЗ
  58. resource "yandex_resourcemanager_folder_iam_binding" "binding-for-all-access2" {
  59. count = length(var.all-access-users)
  60. folder_id = var.folder_id
  61. role = "storage.admin"
  62. members = [element(var.all-access-users, count.index)]
  63. }
  64. resource "yandex_resourcemanager_folder_iam_binding" "binding-for-read-only-sa2" {
  65. count = length(var.read-only-sa)
  66. folder_id = var.folder_id
  67. role = "storage.viewer"
  68. members = [element(var.read-only-sa, count.index)]
  69. }
  70. resource "yandex_resourcemanager_folder_iam_binding" "binding-for-write-only-sa2" {
  71. count = length(var.write-only-sa)
  72. folder_id = var.folder_id
  73. role = "storage.uploader"
  74. members = [element(var.write-only-sa, count.index)]
  75. }
  76. // -------------------------------------------------------------------------------------------------
  77. // Операции с S3:
  78. // Создание KMS ключа для server-side encryption
  79. resource "yandex_kms_symmetric_key" "key-a" {
  80. name = "key-for-bucket-k8s-logs"
  81. description = "description for key"
  82. default_algorithm = "AES_128"
  83. rotation_period = "8760h" // equal to 1 year
  84. }
  85. // Cоздание отдельного S3 bucket для логирования действий
  86. resource "yandex_storage_bucket" "log_bucket" {
  87. bucket = "action-log-${random_string.random.result}"
  88. access_key = yandex_iam_service_account_static_access_key.tr-sa-static-key.access_key
  89. secret_key = yandex_iam_service_account_static_access_key.tr-sa-static-key.secret_key
  90. }
  91. // -------------------------------------------------
  92. // Создание основного S3 bucket
  93. resource "yandex_storage_bucket" "bucket-main" {
  94. bucket = "bucket-main-${random_string.random.result}"
  95. access_key = yandex_iam_service_account_static_access_key.tr-sa-static-key.access_key
  96. secret_key = yandex_iam_service_account_static_access_key.tr-sa-static-key.secret_key
  97. // Создание BucketPolicy:
  98. policy = <<POLICY
  99. {"Version":"2012-10-17","Id":"myid","Statement":[{"Sid":"rule-admin-for-terr-admin","Effect":"Allow","Principal":{"CanonicalUser":["${yandex_iam_service_account.sa-creator.id}"]},"Action":"*","Resource":["arn:aws:s3:::bucket-main-${random_string.random.result}/*","arn:aws:s3:::bucket-main-${random_string.random.result}"]}, {"Sid":"rule-all-access-users","Effect":"Allow","Principal":{"CanonicalUser":[${replace("${join(", ", [for s in var.all-access-users : format("%q", s)])}", "federatedUser:", "")}]},"Action":"*","Resource":["arn:aws:s3:::bucket-main-${random_string.random.result}/*","arn:aws:s3:::bucket-main-${random_string.random.result}"]}, {"Sid":"rule-admin-web","Effect":"Allow","Principal":{"CanonicalUser":[${replace("${join(", ", [for s in var.all-access-users : format("%q", s)])}", "federatedUser:", "")}]},"Action":"*","Resource":["arn:aws:s3:::bucket-main-${random_string.random.result}/*","arn:aws:s3:::bucket-main-${random_string.random.result}"], "Condition": {"StringLike": {"aws:referer": "https://console.cloud.yandex.*/folders/*/storage/buckets/bucket-main-${random_string.random.result}*"}}}, {"Sid":"rule-write-only-sa","Effect":"Allow","Principal":{"CanonicalUser":[${replace("${join(", ", [for s in var.write-only-sa : format("%q", s)])}", "serviceAccount:", "")}]},"Action":"s3:PutObject" ,"Resource":["arn:aws:s3:::bucket-main-${random_string.random.result}/*","arn:aws:s3:::bucket-main-${random_string.random.result}"]}, {"Sid":"rule-read-only-sa","Effect":"Allow","Principal":{"CanonicalUser":[${replace("${join(", ", [for s in var.read-only-sa : format("%q", s)])}", "serviceAccount:", "")}]},"Action":["s3:ListBucket", "s3:GetObject"],"Resource":["arn:aws:s3:::bucket-main-${random_string.random.result}/*","arn:aws:s3:::bucket-main-${random_string.random.result}"]}]}
  100. POLICY
  101. // Включение версионирования
  102. versioning {
  103. enabled = true
  104. }
  105. // Настройка жизненного цикла: удаление НЕтекущих версий и текущих версий
  106. lifecycle_rule {
  107. id = "cleanupoldlogs"
  108. enabled = true
  109. expiration {
  110. days = 365
  111. }
  112. }
  113. lifecycle_rule {
  114. id = "cleanupoldversions"
  115. enabled = true
  116. noncurrent_version_transition {
  117. days = 60
  118. storage_class = "COLD"
  119. }
  120. noncurrent_version_expiration {
  121. days = 150
  122. }
  123. }
  124. // Включение логирования действий над бакетом
  125. logging {
  126. target_bucket = yandex_storage_bucket.log_bucket.id
  127. target_prefix = "logs/"
  128. }
  129. // Включение шифрования
  130. server_side_encryption_configuration {
  131. rule {
  132. apply_server_side_encryption_by_default {
  133. kms_master_key_id = yandex_kms_symmetric_key.key-a.id
  134. sse_algorithm = "aws:kms"
  135. }
  136. }
  137. }
  138. }