#!/bin/bash

# Generate new secrets
NEW_MINIO_ACCESS_KEY=$(openssl rand -base64 32 | tr -dc 'a-zA-Z0-9' | fold -w 20 | head -n 1)
NEW_MINIO_SECRET_KEY=$(openssl rand -base64 32 | tr -dc 'a-zA-Z0-9' | fold -w 40 | head -n 1)
NEW_RANCHER_PASSWORD=$(openssl rand -base64 32 | tr -dc 'a-zA-Z0-9' | fold -w 20 | head -n 1)

# Update secrets in Vault
vault kv put kvv2/minio access_key="$NEW_MINIO_ACCESS_KEY" secret_key="$NEW_MINIO_SECRET_KEY"
vault kv put kvv2/rancher admin_password="$NEW_RANCHER_PASSWORD"

# Apply Terraform configuration
terraform apply -var="minio_access_key=$NEW_MINIO_ACCESS_KEY" -var="minio_secret_key=$NEW_MINIO_SECRET_KEY" -var="rancher_admin_password=$NEW_RANCHER_PASSWORD"
