Skip to content

Commit

Permalink
ref: make use of systemd LoadCredential
Browse files Browse the repository at this point in the history
- consistently use LoadCredential in order to pass credentials to
  a process
  • Loading branch information
blaggacao committed Jan 9, 2022
1 parent b038cc0 commit 089e60d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 22 deletions.
5 changes: 4 additions & 1 deletion modules/ingress-config.nix
Expand Up @@ -181,6 +181,9 @@
TimeoutStopSec = "30s";
RestartSec = "10s";
Restart = "on-failure";
LoadCredential = [
"${builtins.baseNameOf hashiTokens.vault}:${hashiTokens.vault}"
];
};

unitConfig = {
Expand All @@ -199,7 +202,7 @@
script = ''
set -euo pipefail
export VAULT_TOKEN="$(< ${hashiTokens.vault})"
export VAULT_TOKEN="$(< $CREDENTIALS_DIRECTORY/${builtins.baseNameOf hashiTokens.vault})"
CONSUL_HTTP_TOKEN="$(vault read -field token consul/creds/ingress)"
export CONSUL_HTTP_TOKEN
Expand Down
12 changes: 5 additions & 7 deletions modules/nomad-autoscaler.nix
Expand Up @@ -609,20 +609,18 @@ in {
serviceConfig = {
StateDirectory = "nomad-autoscaler";
RuntimeDirectory = "nomad-autoscaler";
LoadCredential = [
"${builtins.baseNameOf hashiTokens.nomad-autoscaler}:${hashiTokens.nomad-autoscaler}"
];


# DynamicUser = true;
# User = "nomad-autoscaler";
# Group = "nomad-autoscaler";

ExecStartPre = pkgs.writeBashChecked "nomad-autoscaler-pre" ''
set -exuo pipefail
cp ${hashiTokens.nomad-autoscaler} .
'';

ExecStart = pkgs.writeBashChecked "nomad-autsocaler" ''
set -euo pipefail
NOMAD_TOKEN="$(< ${builtins.baseNameOf hashiTokens.nomad-autoscaler})"
NOMAD_TOKEN="$(< $CREDENTIALS_DIRECTORY/${builtins.baseNameOf hashiTokens.nomad-autoscaler})"
export NOMAD_TOKEN
unset AWS_DEFAULT_REGION
Expand Down
17 changes: 7 additions & 10 deletions modules/nomad-snapshots.nix
Expand Up @@ -147,6 +147,9 @@ let
Type = "oneshot";
Restart = "on-failure";
RestartSec = "30s";
LoadCredential = [
"${builtins.baseNameOf hashiTokens.nomad-snapshot}:${hashiTokens.nomad-snapshot}"
];
ExecStart = pkgs.writeBashChecked "nomad-snapshot-${job}-script" ''
set -exuo pipefail
Expand All @@ -168,16 +171,10 @@ let
}
exportToken () {
if [ ! -f ${hashiTokens.nomad-snapshot} ]; then
echo "Suitable nomad token for snapshotting not found."
echo "Ensure the appropriate token for snapshotting is available.";
exit 0;
else
set +x
NOMAD_TOKEN="$(< ${hashiTokens.nomad-snapshot})"
export NOMAD_TOKEN
set -x
fi
set +x
NOMAD_TOKEN=$(< "$CREDENTIALS_DIRECTORY/${builtins.baseNameOf hashiTokens.nomad-snapshot}")
export NOMAD_TOKEN
set -x
}
isNotLeader () {
Expand Down
9 changes: 6 additions & 3 deletions modules/nomad.nix
Expand Up @@ -1161,10 +1161,13 @@ in {
set -exuo pipefail
# ${bittelib.ensureDependencies pkgs [ "consul" "vault" ]}
cp /etc/ssl/certs/cert-key.pem .
cp ${hashiTokens.vault} .
chown --reference . ./*.pem
'';
in {
LoadCredential = [
"${builtins.baseNameOf hashiTokens.vault}:${hashiTokens.vault}"
"${builtins.baseNameOf hashiTokens.consul-nomad}:${hashiTokens.consul-nomad}"
];
ExecStartPre = "!${start-pre}";
ExecStart = let
args = [ "${cfg.package}/bin/nomad" "agent" ]
Expand All @@ -1180,7 +1183,7 @@ in {
set -euo pipefail
${lib.optionalString cfg.server.enabled ''
VAULT_TOKEN="$(< ${builtins.baseNameOf hashiTokens.vault})"
VAULT_TOKEN="$(< $CREDENTIALS_DIRECTORY/${builtins.baseNameOf hashiTokens.vault})"
export VAULT_TOKEN
token="$(vault token create -policy ${cfg.tokenPolicy} -period 72h -orphan -field token)"
Expand All @@ -1189,7 +1192,7 @@ in {
exec ${
lib.concatStringsSep " " args
} -consul-token "$(< ${hashiTokens.consul-nomad})"
} -consul-token "$(< $CREDENTIALS_DIRECTORY/${builtins.baseNameOf hashiTokens.consul-nomad})"
'';

KillMode = "process";
Expand Down
5 changes: 4 additions & 1 deletion modules/vault-snapshots.nix
Expand Up @@ -148,6 +148,9 @@ let
Type = "oneshot";
Restart = "on-failure";
RestartSec = "30s";
LoadCredential = [
"${builtins.baseNameOf hashiTokens.vault}:${hashiTokens.vault}"
];
ExecStart = pkgs.writeBashChecked "vault-snapshot-${job}-script" ''
set -exuo pipefail
Expand All @@ -170,7 +173,7 @@ let
exportToken () {
set +x
VAULT_TOKEN="$(< ${hashiTokens.vault})"
VAULT_TOKEN="$(< "$CREDENTIALS_DIRECTORY"/${builtins.baseNameOf hashiTokens.vault})"
export VAULT_TOKEN
set -x
}
Expand Down

0 comments on commit 089e60d

Please sign in to comment.