fix: Configure SSH keys for CI/CD pipeline #11

Closed
opened 2025-12-08 18:35:12 +00:00 by Damien · 0 comments
Owner

Problème

Le pipeline CI/CD Terraform échoue car :

  1. Clé SSH publique : La fonction file(var.admin_ssh_public_key_path) ne fonctionne pas dans le container CI car le chemin ~/.ssh/keys/id_rsa.pub n'existe pas.

  2. Provider bpg/proxmox SSH : Le provider est configuré avec ssh { agent = true } mais il n'y a pas d'agent SSH dans le container CI.

Error: Invalid function argument

  on 02-cloud-init.tf line 14, in resource "proxmox_virtual_environment_file" "cloud_config":
  14:       ssh_key  = file(var.admin_ssh_public_key_path)

Invalid value for "path" parameter: no file exists at "~/.ssh/keys/id_rsa.pub"

Solution

Nouveaux secrets à créer

Secret Description
SSH_PUBLIC_KEY Contenu de la clé publique SSH (pour cloud-init des VMs)
SSH_PRIVATE_KEY Contenu de la clé privée SSH (pour connexion provider → Proxmox)

Modifications Terraform

1. terraform/prod/variables.tf

Remplacer la variable path par une variable contenant directement la clé :

# Avant
variable "admin_ssh_public_key_path" {
  description = "Chemin vers le fichier de la clé SSH publique à autoriser."
  type        = string
  default     = "~/.ssh/id_rsa.pub"
}

# Après
variable "admin_ssh_public_key" {
  description = "Clé SSH publique à autoriser sur les VMs (contenu, pas le chemin)"
  type        = string
  sensitive   = true
}

2. terraform/prod/02-cloud-init.tf

Utiliser directement la variable :

# Avant
ssh_key = file(var.admin_ssh_public_key_path)

# Après  
ssh_key = var.admin_ssh_public_key

3. terraform/prod/providers.tf

Configurer SSH avec clé privée au lieu de l'agent :

provider "proxmox" {
  endpoint  = var.proxmox_url
  api_token = var.proxmox_api_token
  insecure  = true
  ssh {
    agent       = false
    username    = "root"
    private_key = var.ssh_private_key
  }
}

Et ajouter la variable dans variables.tf :

variable "ssh_private_key" {
  description = "Clé SSH privée pour la connexion du provider à Proxmox"
  type        = string
  sensitive   = true
}

Modifications Workflows

4. .gitea/workflows/terraform-deploy.yml et terraform-pr.yml

Ajouter les nouvelles variables d'environnement :

env:
  # ... existing vars ...
  TF_VAR_admin_ssh_public_key: ${{ secrets.SSH_PUBLIC_KEY }}
  TF_VAR_ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }}

Checklist

  • Créer le secret SSH_PUBLIC_KEY dans Gitea
  • Créer le secret SSH_PRIVATE_KEY dans Gitea
  • Modifier terraform/prod/variables.tf
  • Modifier terraform/prod/02-cloud-init.tf
  • Modifier terraform/prod/providers.tf
  • Modifier .gitea/workflows/terraform-deploy.yml
  • Modifier .gitea/workflows/terraform-pr.yml
  • Tester le pipeline

Notes de sécurité

  • Les clés SSH sont marquées sensitive = true dans Terraform
  • Les secrets Gitea sont masqués dans les logs
  • La clé privée ne doit jamais être commitée dans le repo

Références

## Problème Le pipeline CI/CD Terraform échoue car : 1. **Clé SSH publique** : La fonction `file(var.admin_ssh_public_key_path)` ne fonctionne pas dans le container CI car le chemin `~/.ssh/keys/id_rsa.pub` n'existe pas. 2. **Provider bpg/proxmox SSH** : Le provider est configuré avec `ssh { agent = true }` mais il n'y a pas d'agent SSH dans le container CI. ``` Error: Invalid function argument on 02-cloud-init.tf line 14, in resource "proxmox_virtual_environment_file" "cloud_config": 14: ssh_key = file(var.admin_ssh_public_key_path) Invalid value for "path" parameter: no file exists at "~/.ssh/keys/id_rsa.pub" ``` ## Solution ### Nouveaux secrets à créer | Secret | Description | |--------|-------------| | `SSH_PUBLIC_KEY` | Contenu de la clé publique SSH (pour cloud-init des VMs) | | `SSH_PRIVATE_KEY` | Contenu de la clé privée SSH (pour connexion provider → Proxmox) | ### Modifications Terraform #### 1. `terraform/prod/variables.tf` Remplacer la variable path par une variable contenant directement la clé : ```hcl # Avant variable "admin_ssh_public_key_path" { description = "Chemin vers le fichier de la clé SSH publique à autoriser." type = string default = "~/.ssh/id_rsa.pub" } # Après variable "admin_ssh_public_key" { description = "Clé SSH publique à autoriser sur les VMs (contenu, pas le chemin)" type = string sensitive = true } ``` #### 2. `terraform/prod/02-cloud-init.tf` Utiliser directement la variable : ```hcl # Avant ssh_key = file(var.admin_ssh_public_key_path) # Après ssh_key = var.admin_ssh_public_key ``` #### 3. `terraform/prod/providers.tf` Configurer SSH avec clé privée au lieu de l'agent : ```hcl provider "proxmox" { endpoint = var.proxmox_url api_token = var.proxmox_api_token insecure = true ssh { agent = false username = "root" private_key = var.ssh_private_key } } ``` Et ajouter la variable dans `variables.tf` : ```hcl variable "ssh_private_key" { description = "Clé SSH privée pour la connexion du provider à Proxmox" type = string sensitive = true } ``` ### Modifications Workflows #### 4. `.gitea/workflows/terraform-deploy.yml` et `terraform-pr.yml` Ajouter les nouvelles variables d'environnement : ```yaml env: # ... existing vars ... TF_VAR_admin_ssh_public_key: ${{ secrets.SSH_PUBLIC_KEY }} TF_VAR_ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }} ``` ## Checklist - [ ] Créer le secret `SSH_PUBLIC_KEY` dans Gitea - [ ] Créer le secret `SSH_PRIVATE_KEY` dans Gitea - [ ] Modifier `terraform/prod/variables.tf` - [ ] Modifier `terraform/prod/02-cloud-init.tf` - [ ] Modifier `terraform/prod/providers.tf` - [ ] Modifier `.gitea/workflows/terraform-deploy.yml` - [ ] Modifier `.gitea/workflows/terraform-pr.yml` - [ ] Tester le pipeline ## Notes de sécurité - Les clés SSH sont marquées `sensitive = true` dans Terraform - Les secrets Gitea sont masqués dans les logs - La clé privée ne doit jamais être commitée dans le repo ## Références - Issue parent : #3 - [Documentation bpg/proxmox SSH](https://registry.terraform.io/providers/bpg/proxmox/latest/docs#ssh-connection)
Damien added reference feat/ci-ssh-key-support 2026-05-03 17:03:36 +00:00
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Damien/iac-homelab#11