Skip to main content

Command Palette

Search for a command to run...

Vault for Secret Management

Home Lab 2.0 - Part 05

Updated
โ€ข5 min read
Vault for Secret Management
J

Welcome to my blog where I share a few more words! No Artificial Intelligence (AI) form is harmed when writing my posts ;-) All mistakes are due to the simple and non-artificial nature of my own human mind.

Hello, Nice to see you again! ๐Ÿ‘‹๐Ÿ˜Š

๐Ÿงต This is the part 05 of the Home Lab 2.0 series.

Secret Management ๐Ÿ” is always a challenging exercise and like many of you, I have seen my fair share of credentials on plain text config files. Leaked and forgotten long living credentials in many shapes and forms are not a rare sight in todays IT system labyrinths. All the leading cloud providers have their own secret management solutions that tightly integrate with the rest of their eco-system. However they fall short in a multi-cloud environments.

Hashicorp Vault shines as an independent centralised highly capable secret management platform that can be integrated with many cloud providers as well as other existing solutions. Though I have used Vault in a few of my previous work places (including the current work setup) I didnโ€™t have my own environment to tinker with as I wish. Therefore I decided to deploy my own instance of Hashicorp Vault to manage secrets in my Home Lab setup. Of course, I will get a few more gray hair in the process but thats a small price to pay ๐Ÿคฉ

Host Selection ๐Ÿ›๏ธ๐Ÿ 

My initial plan was to use a VM to deploy Vault as a service or use a bunch of containers with Docker Compose. However during this Home Lab 2.0 journey, it was apparent that LXC Containers are perfect choice for most of my applications. They are super efficient (easy on my poor mans hardware ๐Ÿซ ) and first class citizens in a Proxmox setup. Though LXC containers donโ€™t offer a similar level of isolation as a virtual machine, running them as unprivileged with a minimal up-to-date Linux destro template offers a balanced outcome for a Home Lab setup. Therefore I opted for an Ubuntu based LXC Container as my Vault host.

Vault LXC Container

I could easily reuse my Terraform templates that I used for Keycloak and Traefik deployments to get the host up and running. I have a separate VLAN for the Home Lab and that makes my life easier when it comes to allocate a static IP to the hosts.

Install and Configure Vault ๐Ÿ› ๏ธ๐Ÿง‘โ€๐Ÿ’ป

This is the first time that I ever tried to setup my own Vault. Just running a Vault docker container in my Mac to poke around doesnโ€™t really count, right?๐Ÿ˜Š. Therefore, I took some time and did a bit of a digging to figure out the best way to set this up. Usual AI overlords (Gemini, Claude, ChatGPT) provided me some useful configuration help (most were riddled with errors) but I went through a lot of iterations to get things working with Ansible.

I decided to install Vault using apt as that was the most straightforward way to install Vault in Ubuntu. Since Hashicorp recommends using Vaultโ€™s Integrated Storage (Raft) for most use cases, I didnโ€™t bother to mess with anything else. Also it was the simplest for my need. As you may have noticed, I separated host setup (Terraform) and application installation and configuration (Ansible, Bash) as two distinct actions for my setup. This reduced the complexity of the Home Lab 2.0 code. The seal/unseal process of Vault after each restart was a separate piece of challenge. After looking into the available options I ended up with file based auto unseal (duct tape-ish) solution as an acceptable workaround ยฏ\_(ใƒ„)_/ยฏ for my Home Lab 2.0 setup. This approach stores the single shard unseal key in a file but if anyone/anything is able to get to that far into my network, Iโ€™m cooked without a doubt and will have a lot more things to worry about ๐Ÿ˜‚.

Maybe I will think about auto-unseal Vault using AWS Secrets Manager in future, as an improvement to the current setup ๐Ÿคž.

Integration with Traefik and Keycloak ๐Ÿ”—๐Ÿค

The main Ansible role uses two separate roles for the Vault init and Keycloak setup. Once this got executed, I dropped in a new config file into dynamic folder of the Traefik, added a vault-headers section into middlewares config and re-run the Traefik playbook. Also I had to update my DNS records in AdGuard and pfSense. My initial setup seems to paid off as the integration effort with Traefik and Keycloak became a quick and easy process than I expected ๐Ÿ˜.

Keycloak client configuration

# traefik/ansible/roles/traefik/files/dynamic/vault.yml
# make sure to add the middlewares as well.
http:
  routers:
    to-vault:
      entryPoints:
        - websecure
      rule: "Host(`vault.jayforweb.com`)"
      service: vault
      tls:
        certResolver: dnsresolver
      middlewares:
        - vault-headers

  services:
    vault:
      loadBalancer:
        servers:
          - url: "http://192.168.193.80:8200"
        healthCheck:
          path: "/v1/sys/health"
          interval: "30s"
          timeout: "5s"
# traefik/ansible/roles/traefik/files/dynamic/middlewares.yml
http:
  middlewares:
    redirect-to-https:
      redirectScheme:
        scheme: https
        permanent: true

    vault-headers:
      headers:
        customRequestHeaders:
          X-Forwarded-Proto: "https"
          X-Forwarded-Port: "443"
        customResponseHeaders:
          X-Frame-Options: "DENY"
          X-Content-Type-Options: "nosniff"
          Referrer-Policy: "strict-origin-when-cross-origin"

Vault is Alive ๐ŸŽ‰๐Ÿพ

As I mentioned above, it took me many iterations to get everything working and I think there will be other challenges when I try to use the Vault with GitLab and the Kubernetes cluster. So far, Iโ€™m happy about how everything is coming into place. The Home Lab 2.0 is already serving me well as it gives me the opportunity to experiment with all these amazing tools, in my own terms.

I sincerely hope that my effort in writing these things and sharing the not-so-perfect code will help at least one more person to figure out a thing or two.

Next, onwards to setup GitLab!

Happy tinkering! ๐Ÿ˜Š

๐“‡ข๐“†ธ๐Ÿง˜๐Ÿปโ€โ™‚๏ธ๐“Ÿ๐“ฎ๐“ช๐“ฌ๐“ฎ

Reference ๐Ÿ“š๐Ÿ“–

Vault Installation - Official documentation

Vault Seal/Unseal process

Vault unseal options comparison

Home Lab 2.0

Part 1 of 5

In this series, I will write about the process of setting up my Home Lab, again!. As usual I will automate things as much as possible, with Terraform, Ansible, Bash and other beautiful things. Hopefully there will be something useful for you as well!

Up next

Keycloak for AuthN and AuthZ

Home Lab 2.0 - Part 04