From 639651921d3721d4cec07d05c91fac0e65518a13 Mon Sep 17 00:00:00 2001 From: Tom Moguljak Date: Fri, 6 Dec 2024 15:16:29 +0100 Subject: [PATCH] Ajout du module IAM --- terraform/environments/dev/main.tf | 22 ++++++++++++++++++++++ terraform/environments/dev/variables.tf | 6 ++++++ terraform/modules/iam/main.tf | 24 ++++++++++++++++++++++++ terraform/modules/iam/outputs.tf | 10 ++++++++++ terraform/modules/iam/variables.tf | 4 ++++ 5 files changed, 66 insertions(+) diff --git a/terraform/environments/dev/main.tf b/terraform/environments/dev/main.tf index 9a9ed5d..81b90b0 100644 --- a/terraform/environments/dev/main.tf +++ b/terraform/environments/dev/main.tf @@ -35,4 +35,26 @@ module "compute" { frontend_subnet = module.network.subnet_id["frontend"] backend_subnet = module.network.subnet_id["backend"] database_subnet = module.network.subnet_id["database"] +} + +module "iam" { + source = "../../modules/iam" + project_id = var.project_id +} + +data "google_client_openid_userinfo" "me" { +} + +resource "local_file" "ansible_config" { + content = templatefile("${path.module}/../../templates/ansible.cfg.tpl", + { + remote_user = data.google_client_openid_userinfo.me.email + } + ) + filename = "../../../ansible/ansible.cfg" +} + +resource "local_file" "service_account" { + content = base64decode(module.iam.service_account_key) + filename = "../../../ansible/service_account.json" } \ No newline at end of file diff --git a/terraform/environments/dev/variables.tf b/terraform/environments/dev/variables.tf index b2b6955..6964838 100644 --- a/terraform/environments/dev/variables.tf +++ b/terraform/environments/dev/variables.tf @@ -44,4 +44,10 @@ variable "zone" { description = "zone" type = string default = "europe-west4-b" +} + +variable "project_id" { + description = "ID du projet GCP" + type = string + default = "projet-vertu" } \ No newline at end of file diff --git a/terraform/modules/iam/main.tf b/terraform/modules/iam/main.tf index e69de29..4068059 100644 --- a/terraform/modules/iam/main.tf +++ b/terraform/modules/iam/main.tf @@ -0,0 +1,24 @@ +resource "google_service_account" "service_account" { + account_id = "terraform" + display_name = "terraform" +} + +resource "google_service_account_key" "service_account" { + service_account_id = google_service_account.service_account.name + public_key_type = "TYPE_X509_PEM_FILE" +} + +resource "google_project_iam_binding" "service_account_roles" { + project = var.project_id + role = "roles/viewer" + members = ["serviceAccount:${google_service_account.service_account.email}"] +} + +data "google_client_openid_userinfo" "me" { +} + +resource "google_os_login_ssh_public_key" "add_my_key" { + project = var.project_id + user = data.google_client_openid_userinfo.me.email + key = file("~/.ssh/id_ed25519.pub") +} diff --git a/terraform/modules/iam/outputs.tf b/terraform/modules/iam/outputs.tf index e69de29..9e9916e 100644 --- a/terraform/modules/iam/outputs.tf +++ b/terraform/modules/iam/outputs.tf @@ -0,0 +1,10 @@ +output "service_account_email" { + description = "Email du compte de service" + value = google_service_account.service_account.email +} + +output "service_account_key" { + description = "Clé du compte de service" + value = google_service_account_key.service_account.private_key + sensitive = true +} \ No newline at end of file diff --git a/terraform/modules/iam/variables.tf b/terraform/modules/iam/variables.tf index e69de29..3af7121 100644 --- a/terraform/modules/iam/variables.tf +++ b/terraform/modules/iam/variables.tf @@ -0,0 +1,4 @@ +variable "project_id" { + description = "ID du projet GCP" + type = string +} \ No newline at end of file