Skip to content

Commit

Permalink
just: credential getter as nix script
Browse files Browse the repository at this point in the history
  • Loading branch information
burgerdev committed Aug 7, 2024
1 parent 8021608 commit 5c446dd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
11 changes: 1 addition & 10 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -225,16 +225,7 @@ get-credentials-ci:
--admin
get-credentials-from-gcloud path:
#!/usr/bin/env bash
set -euo pipefail
tmpConfig=$(mktemp)
gcloud secrets versions access {{ path }} --out-file="$tmpConfig"
mergedConfig=$(mktemp)
KUBECONFIG_BAK=${KUBECONFIG:-~/.kube/config}
KUBECONFIG=$tmpConfig:${KUBECONFIG_BAK} kubectl config view --flatten > $mergedConfig
export newContext=$(yq -r '.contexts.[0].name' $tmpConfig)
yq -i '.current-context = env(newContext)' $mergedConfig
mv $mergedConfig ${KUBECONFIG_BAK%%:*}
nix run .#scripts.get-credentials {{ path }}
get-credentials-tdxbm: (get-credentials-from-gcloud "projects/796962942582/secrets/m50-ganondorf-kubeconf/versions/2")
Expand Down
18 changes: 18 additions & 0 deletions packages/scripts.nix
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,22 @@
)
'';
};

# Usage: get-credentials $gcloudSecretRef
get-credentials = writeShellApplication {
name = "extract-policies";
runtimeInputs = with pkgs; [ google-cloud-sdk ];
text = ''
set -euo pipefail
tmpConfig=$(mktemp)
gcloud secrets versions access "$1" --out-file="$tmpConfig"
mergedConfig=$(mktemp)
KUBECONFIG_BAK=''${KUBECONFIG:-~/.kube/config}
KUBECONFIG=$tmpConfig:''${KUBECONFIG_BAK} kubectl config view --flatten > "$mergedConfig"
newContext=$(yq -r '.contexts.[0].name' "$tmpConfig")
declare -x newContext
yq -i '.current-context = env(newContext)' "$mergedConfig"
mv "$mergedConfig" "''${KUBECONFIG_BAK%%:*}"
'';
};
}

0 comments on commit 5c446dd

Please sign in to comment.