Skip to content

Instantly share code, notes, and snippets.

@danielperna84
Created December 21, 2020 16:58
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save danielperna84/98bced96581c92772014e4acfc7bde3f to your computer and use it in GitHub Desktop.
Save danielperna84/98bced96581c92772014e4acfc7bde3f to your computer and use it in GitHub Desktop.
Unseal HashiCorp Vault using systemd

Automatically unseal HashiCorp Vault via systemd

WARNING!

Automatically unsealing Vault drastically reduces the security of the stored secrets. That being said, there might be scenarios, in which this simple approach could be useful / sufficient.

How it works / installation

This requires Vault to be started by a systemd-unit named vault.service, which typically is the case when installing from a distribution package. The script vault-unseal.sh should be placed in /root and secured with 700 permissions.
Place the required unseal-key in that script as well. This example assumes Vault can be unsealed using just one key.
When executed, it will perform the necessary POST unseal-request to the Vault instance that is running on 127.0.0.1:8200.

Store the unit-file vault-unseal.service in /etc/systemd/system, then execute:

systemctl daemon-reload
systemctl enable vault-unseal.service

Now whenever the system boots or Vault is restarted, the vault-unseal-unit will automatically be started.
It will unseal the Vault with a delay of 10 seconds.

Further thoughts about security

Obviously the vault-unseal.sh script contains the unseal-key in plaintext, which is really bad.
However, it should only be accessible by root. And if an attacker already has that level of access, he probably also will be able to spawn a malicious service that intercepts / forwards regular unseal-requests anyways.
Since he is root he can just use the same certificate / key that Vault is using and nobody would notice the keys are being leaked.

[Unit]
Description=Vault Unseal
After=vault.service
Requires=vault.service
PartOf=vault.service
[Service]
Type=oneshot
User=root
ExecStartPre=/bin/sleep 10
ExecStart=/bin/sh -c '/root/vault-unseal.sh'
RemainAfterExit=false
StandardOutput=syslog
StandardError=syslog
[Install]
WantedBy=multi-user.target vault.service
#!/bin/bash
KEY="place-unseal-key-here"
curl -s --insecure -H 'Content-Type: application/json' -X PUT -d '{"key":"'${KEY}'"}' https://127.0.0.1:8200/v1/sys/unseal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment