Skip to content

Commit

Permalink
Merge pull request #21 from serokell/rvem/retry-vault-login
Browse files Browse the repository at this point in the history
Add vault login retries
  • Loading branch information
rvem authored Mar 24, 2023
2 parents 9d8f41b + 831590f commit e4ec077
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on: push

jobs:
check:
runs-on: self-hosted
runs-on: [nix, self-hosted]
steps:
- uses: actions/checkout@v3

Expand Down
11 changes: 11 additions & 0 deletions modules/options.nix
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,17 @@ let
'';
};

loginRetries = mkOption {
type = with types; int;
default = 5;
description = ''
Number of attempts script will try to login into Vault.
This may be useful in case secrets service is restarted when internet
connection is not yet available. Sadly After=network-online.target
doesn't always guarantee that.
'';
};

__toString = mkOption {
default = _: "${cfg.outPrefix}/${name}";
readOnly = true;
Expand Down
13 changes: 11 additions & 2 deletions modules/script.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ let
inherit (scfg)
environmentKey quoteEnvironmentValues
environmentVariableNamePrefix extraScript
user group secretsKey secretsAreBase64;
user group secretsKey secretsAreBase64 loginRetries;
inherit (lib) optionalString toUpper;

secretsPath = "${cfg.outPrefix}/${name}";
Expand All @@ -18,9 +18,18 @@ in
# Make sure we start from a clean slate
rm -rf "${secretsPath}"
mkdir -p "${secretsPath}"
max_retry="${toString loginRetries}"
counter="0"
set +e
# Log into Vault using credentials from environmentFile
vaultOutput="$(vault write -format=json auth/approle/login role_id="$VAULT_ROLE_ID" secret_id=- <<< "$VAULT_SECRET_ID")"
until vaultOutput="$(vault write -format=json auth/approle/login role_id="$VAULT_ROLE_ID" secret_id=- <<< "$VAULT_SECRET_ID")"; do
echo "Failed to login into Vault, retrying"
sleep 5
[[ counter -eq $max_retry ]] && echo "Failed to login into Vault" && exit 1
((counter++))
done
set -e
jq '.auth.client_token = "redacted"' <<< "$vaultOutput"
VAULT_TOKEN="$(jq -r '.auth.client_token' <<< "$vaultOutput")"
export VAULT_TOKEN
Expand Down

0 comments on commit e4ec077

Please sign in to comment.