main.tf 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //----------------------Подготовка тестовой инфраструктуры-----------------------------------
  2. //Генерация random-string для имени bucket---------------------------------------------------------
  3. resource "random_string" "random" {
  4. length = 8
  5. special = false
  6. upper = false
  7. }
  8. //Создание сети
  9. resource "yandex_vpc_network" "vpc-enc" {
  10. name = "vpc-enc"
  11. }
  12. //Создание подсетей
  13. resource "yandex_vpc_subnet" "enc-subnet" {
  14. name = "enc-subnet"
  15. zone = "ru-central1-a"
  16. network_id = yandex_vpc_network.vpc-enc.id
  17. v4_cidr_blocks = ["192.168.20.0/24"]
  18. }
  19. //Создание sa storage admin
  20. resource "yandex_iam_service_account" "sa-bucket-creator" {
  21. name = "sa-bucket-creator-${random_string.random.result}"
  22. folder_id = var.folder_id
  23. }
  24. //Создание стат ключа
  25. resource "yandex_iam_service_account_static_access_key" "sa-bucket-creator-sk" {
  26. service_account_id = yandex_iam_service_account.sa-bucket-creator.id
  27. }
  28. //Назначение прав для создания бакета
  29. resource "yandex_resourcemanager_folder_iam_binding" "storage_admin" {
  30. folder_id = var.folder_id
  31. role = "storage.admin"
  32. members = [
  33. "serviceAccount:${yandex_iam_service_account.sa-bucket-creator.id}",
  34. ]
  35. }
  36. //Создание S3 bucket для
  37. resource "yandex_storage_bucket" "enc-bucket" {
  38. bucket = "bucket-for-encryption-${random_string.random.result}"
  39. access_key = yandex_iam_service_account_static_access_key.sa-bucket-creator-sk.access_key
  40. secret_key = yandex_iam_service_account_static_access_key.sa-bucket-creator-sk.secret_key
  41. }
  42. //Создание sa storage editor для работы от VM с Bucket
  43. resource "yandex_iam_service_account" "sa-bucket-editor" {
  44. name = "sa-bucket-editor-${random_string.random.result}"
  45. folder_id = var.folder_id
  46. }
  47. //Назначение прав для изменения бакета
  48. resource "yandex_resourcemanager_folder_iam_binding" "storage_editor" {
  49. folder_id = var.folder_id
  50. role = "storage.editor"
  51. members = [
  52. "serviceAccount:${yandex_iam_service_account.sa-bucket-editor.id}",
  53. ]
  54. }
  55. //Создание стат ключа editor
  56. resource "yandex_iam_service_account_static_access_key" "sa-bucket-editor_stat" {
  57. service_account_id = yandex_iam_service_account.sa-bucket-editor.id
  58. }
  59. //Работа с ssh ключем
  60. resource "tls_private_key" "ssh" {
  61. algorithm = "RSA"
  62. rsa_bits = "4096"
  63. }
  64. resource "local_file" "private_key" {
  65. content = tls_private_key.ssh.private_key_pem
  66. filename = "pt_key.pem"
  67. file_permission = "0600"
  68. }
  69. data "template_file" "cloud_init_lin" {
  70. template = file("./cloud-init_lin.tpl.yaml")
  71. vars = {
  72. ssh_key = "${chomp(tls_private_key.ssh.public_key_openssh)}"
  73. aws_key = "${yandex_iam_service_account_static_access_key.sa-bucket-editor_stat.access_key}"
  74. aws_sec = "${yandex_iam_service_account_static_access_key.sa-bucket-editor_stat.secret_key}"
  75. DEVICE = "${var.device}"
  76. MAPPED_DEVICE = "${var.mapped_device}"
  77. KMS_KEY_ID = "${yandex_kms_symmetric_key.key-enc.id}"
  78. ENCRYPTED_DEK_FILE= "${var.encrypted_dek_file}"
  79. PLAINTEXT_DEK_FILE="${var.plaintext_dek_file}"
  80. MOUNT="${var.mount}"
  81. BUCKET_NAME="${yandex_storage_bucket.enc-bucket.bucket}"
  82. }
  83. }
  84. //Создание диска
  85. resource "yandex_compute_disk" "disk" {
  86. name = "disk-for-enc"
  87. type = "network-ssd"
  88. zone = "ru-central1-a"
  89. size = 20
  90. }
  91. //Развертывание ВМ
  92. data "yandex_compute_image" "vm-image" {
  93. family = "ubuntu-1804-lts"
  94. }
  95. resource "yandex_compute_instance" "vm" {
  96. name = "vm-for-enc"
  97. hostname = "vm-for-enc"
  98. zone = "ru-central1-a"
  99. service_account_id = yandex_iam_service_account.sa-bucket-editor.id
  100. boot_disk {
  101. initialize_params {
  102. image_id = data.yandex_compute_image.vm-image.id
  103. type = "network-ssd"
  104. size = 100
  105. }
  106. }
  107. secondary_disk {
  108. disk_id = yandex_compute_disk.disk.id
  109. }
  110. network_interface {
  111. subnet_id = yandex_vpc_subnet.enc-subnet.id
  112. nat = true
  113. }
  114. resources {
  115. cores = 4
  116. memory = 4
  117. }
  118. metadata = {
  119. user-data = "${data.template_file.cloud_init_lin.rendered}"
  120. }
  121. }
  122. //Создание KMS ключа
  123. resource "yandex_kms_symmetric_key" "key-enc" {
  124. name = "key-enc"
  125. description = "description for key"
  126. default_algorithm = "AES_128"
  127. }
  128. //Назначение роли на sa на расшифровку ключа
  129. resource "yandex_resourcemanager_folder_iam_binding" "binding" {
  130. folder_id = var.folder_id
  131. role = "kms.keys.encrypterDecrypter"
  132. members = [
  133. "serviceAccount:${yandex_iam_service_account.sa-bucket-editor.id}",
  134. ]
  135. }