Skip to content

Commit 541fb40

Browse files
committed
feat: enhance Ansible and Terraform configurations for improved deployment security and management
- Updated .gitignore to exclude Terraform state files for better repository cleanliness. - Modified site.yml to run synchronization tasks as the root user for elevated permissions. - Enhanced vars.example.yml to include ansible_become_flags for sudo command customization. - Updated hosts.ini with the specific IP address for the Compose host. - Improved cloud-init.yaml to include password hash management for the deploy user. - Added deploy_password_hash variable in variables.tf to securely manage the deploy user's password hash. - Updated README.md to reflect changes in deployment user management and password handling.
1 parent 045935f commit 541fb40

File tree

8 files changed

+29
-6
lines changed

8 files changed

+29
-6
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,8 @@ infra/ansible/group_vars/constructa/vars.yml
101101
.vault-pass.txt
102102
# END Ruler Generated Files
103103
.ex0
104+
105+
# Terraform state files anywhere in the repo
106+
*.tfstate
107+
*.tfstate.*
108+
*.tfstate.backup

infra/ansible/group_vars/constructa/vars.example.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ constructa_deploy_sudo_passwort: 'change-me-deploy-password'
1515
ansible_become: true
1616
ansible_become_method: sudo
1717
ansible_become_password: "{{ constructa_deploy_sudo_passwort }}"
18+
ansible_become_flags: "-SE"
1819

1920
# Optional: login to image registry before pulling
2021
constructa_enable_registry_login: false

infra/ansible/inventory/hosts.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[constructa]
22
# Replace with the reachable hostname or IP of your Compose host
33
# example: ex0-dev ansible_user=deploy
4-
ex0-dev ansible_host=<IP> ansible_user=deploy
4+
ex0-dev ansible_host=5.75.251.186 ansible_user=deploy

infra/ansible/site.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@
109109

110110
tasks:
111111
- name: Sync compose bundle (excluding .env; rendered from Vault below)
112+
become: true
113+
become_user: root
112114
ansible.posix.synchronize:
113115
src: "{{ playbook_dir }}/../deploy/"
114116
dest: "{{ constructa_deploy_dir }}/"

infra/hetzner/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export TF_VAR_hcloud_token=your-token
1313
# export TF_VAR_ssh_public_key_path=~/.ssh/id_ed25519.pub
1414
# required: restrict SSH to your IP/CIDR
1515
export TF_VAR_allowed_ssh_cidr="203.0.113.42/32"
16+
# required: set the deploy sudo password hash (SHA-512)
17+
export TF_VAR_deploy_password_hash="$(python3 -c "import crypt, getpass; pwd=getpass.getpass('deploy password: '); print(crypt.crypt(pwd, crypt.mksalt(crypt.METHOD_SHA512)))")"
1618

1719
terraform init
1820
terraform apply
@@ -21,7 +23,7 @@ terraform apply
2123
This provisions a Debian 13 server with:
2224

2325
* **Docker Engine** (incl. buildx + compose plugin)
24-
* Non-root `${deploy_username}` user (limited sudo; not in `docker` group)
26+
* Non-root `${deploy_username}` user with a primed sudo password (not in `docker` group)
2527
* UFW hardened (SSH from your CIDR only; **80/443** open)
2628
* Fail2ban enabled with an sshd jail
2729

@@ -43,4 +45,4 @@ MinIO remains bound to `127.0.0.1:9000/9001` on the host to support SSH tunnels
4345
For deployment updates, build & push your image, then instruct the server to `docker compose pull && up -d`.
4446
Use the project CLI (`pnpm run ex0`) for `release`, `deploy`, `deploy-branch`, `logs`, `restart`, and SSH tunnels.
4547

46-
```
48+
```

infra/hetzner/cloud-init.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ users:
99
ssh_authorized_keys:
1010
- ${deploy_ssh_pubkey}
1111

12+
chpasswd:
13+
expire: false
14+
encrypted: true
15+
list: |
16+
${deploy_username}:${deploy_password_hash}
17+
1218
package_update: true
1319
package_upgrade: true
1420
packages:

infra/hetzner/main.tf

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ resource "hcloud_server" "app" {
5151
ssh_keys = [hcloud_ssh_key.me.id]
5252
firewall_ids = [hcloud_firewall.app.id]
5353
user_data = templatefile("${path.module}/cloud-init.yaml", {
54-
deploy_username = var.deploy_username
55-
deploy_ssh_pubkey = local.ssh_pub_key
56-
allowed_ssh_cidr = var.allowed_ssh_cidr
54+
deploy_username = var.deploy_username
55+
deploy_ssh_pubkey = local.ssh_pub_key
56+
allowed_ssh_cidr = var.allowed_ssh_cidr
57+
deploy_password_hash = var.deploy_password_hash
5758
})
5859
}

infra/hetzner/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,9 @@ variable "deploy_username" {
5252
type = string
5353
default = "deploy"
5454
}
55+
56+
variable "deploy_password_hash" {
57+
description = "SHA-512 password hash for the deploy user (used to prime sudo password)."
58+
type = string
59+
sensitive = true
60+
}

0 commit comments

Comments
 (0)