Skip to content

Instantly share code, notes, and snippets.

@ycyr
Forked from danielperna84/README.md
Created February 11, 2022 21:23
Show Gist options
  • Save ycyr/b7604040a89e476faadbb4db667c04d2 to your computer and use it in GitHub Desktop.
Save ycyr/b7604040a89e476faadbb4db667c04d2 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