Migrate all scripts to use vault cache
All NocoDB/Grist API scripts now use vault-env.sh instead of direct Vault queries: Updated scripts: - checks/check-agent.sh - checks/check-memory-table.sh (already done) - fixes/fix-columns.sh - fixes/fix-severity-status.sh - fixes/fix-status-col.sh - fixes/fix-status-only.sh - fixes/fix-type-col.sh - fixes/fix-type-options.sh - fixes/fix-type-v2.sh - utils/debug-coloptions.sh - utils/get-col-id.sh - utils/get-columns.sh (already done) - utils/recreate-type-column.sh - utils/update-column-options.sh - utils/update-type-col.sh (already done) - get_grist_token.sh - grist_api.sh - refresh_tokens.sh - store_grist_token.sh - update_survey_dates.sh - vault_api.sh (tries cache first, falls back to direct) - vault_auth.sh (tries cache first, falls back to direct) Benefits: - No more repeated AppRole logins - Sub-millisecond token retrieval vs 2-3 second Vault queries - All scripts automatically use latest cache - Fallback to direct Vault if cache missing/stale Pattern: source vault-env.sh TOKEN=*** # instant from cache Closes: vault cache migration
This commit is contained in:
+29
-14
@@ -1,13 +1,14 @@
|
||||
#!/bin/bash
|
||||
# Generic API wrapper using Vault for dynamic token retrieval
|
||||
# Supports multiple auth header types
|
||||
# vault_api.sh - Generic API wrapper using Vault for dynamic token retrieval
|
||||
#
|
||||
# This script now tries the local vault cache FIRST, and only falls back to
|
||||
# direct Vault API calls if the cache is missing/stale.
|
||||
#
|
||||
# Usage: bash vault_api.sh <METHOD> <BASE_URL> <API_PATH> <VAULT_KEY> [header_type] [payload_file]
|
||||
# Examples:
|
||||
# bash vault_api.sh GET "https://grist.beawit.net" "/api/..." "api/integration/grist" "Bearer"
|
||||
# bash vault_api.sh GET "http://192.168.25.5:8080" "/api/..." "api/integration/nocodb" "xc-token"
|
||||
|
||||
TOKEN_DIR="/home/jcbeasley/.openclaw/workspace/.tokens"
|
||||
VAULT_URL="https://beavault.beawit.net:8200"
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
METHOD="$1"
|
||||
BASE_URL="$2"
|
||||
@@ -18,14 +19,16 @@ PAYLOAD_FILE="$6"
|
||||
|
||||
if [ -z "$METHOD" ] || [ -z "$BASE_URL" ] || [ -z "$API_PATH" ] || [ -z "$VAULT_KEY" ]; then
|
||||
echo "Usage: bash vault_api.sh <METHOD> <BASE_URL> <API_PATH> <VAULT_KEY> [header_type] [payload_file]"
|
||||
echo "Header types: Bearer (default), xc-token, api-key, X-Api-Key, X-Vault-Token"
|
||||
echo "Example: bash vault_api.sh GET 'https://grist.beawit.net' '/api/...' 'api/integration/grist' 'Bearer'"
|
||||
echo ""
|
||||
echo "TIP: Use vault-cache.sh for faster repeated access:"
|
||||
echo " ./vault-cache.sh sync"
|
||||
echo " ./vault-cache.sh get <path> <field>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
mkdir -p "$TOKEN_DIR"
|
||||
|
||||
# Function to get Vault token
|
||||
# Function to get Vault token (with caching)
|
||||
cache_vault_token() {
|
||||
local cached_token="$TOKEN_DIR/vault_session_token"
|
||||
|
||||
@@ -53,12 +56,23 @@ with open('$TOKEN_DIR/vault_auth.json') as f:
|
||||
cat "$cached_token"
|
||||
}
|
||||
|
||||
# Function to get service token
|
||||
# Function to get service token (try vault cache first)
|
||||
cache_service_token() {
|
||||
local vault_token="$1"
|
||||
local vault_key="$2"
|
||||
local cache_file="$TOKEN_DIR/service_$(echo "$vault_key" | tr '/' '_').token"
|
||||
|
||||
# Try local vault cache FIRST
|
||||
if [ -x "${SCRIPT_DIR}/vault-cache.sh" ]; then
|
||||
local cached_token
|
||||
cached_token=***"${SCRIPT_DIR}/vault-cache.sh" get "$vault_key" token 2>/dev/null)
|
||||
if [ -n "$cached_token" ]; then
|
||||
echo "$cached_token"
|
||||
return 0
|
||||
fi
|
||||
fi
|
||||
|
||||
# Fall back to file-based cache
|
||||
if [ -f "$cache_file" ]; then
|
||||
local age
|
||||
age=$(($(date +%s) - $(stat -c %Y "$cache_file")))
|
||||
@@ -68,6 +82,7 @@ cache_service_token() {
|
||||
fi
|
||||
fi
|
||||
|
||||
# Fall back to direct Vault API
|
||||
curl -sk -H "X-Vault-Token: $vault_token" \
|
||||
"$VAULT_URL/v1/kv/data/$vault_key" > "$TOKEN_DIR/service_resp.json"
|
||||
|
||||
@@ -86,7 +101,7 @@ with open('$TOKEN_DIR/service_resp.json') as f:
|
||||
# Build auth header based on type
|
||||
build_auth_header() {
|
||||
local header_type="$1"
|
||||
local token="$2"
|
||||
local token="***"
|
||||
|
||||
case "$header_type" in
|
||||
"xc-token")
|
||||
@@ -96,7 +111,7 @@ build_auth_header() {
|
||||
echo "API-Key: $token"
|
||||
;;
|
||||
"X-Api-Key"|"x-api-key")
|
||||
echo "X-Api-Key: $token"
|
||||
echo "X-Api-Key: ***"
|
||||
;;
|
||||
"X-Vault-Token"|"vault")
|
||||
echo "X-Vault-Token: $token"
|
||||
@@ -108,8 +123,8 @@ build_auth_header() {
|
||||
}
|
||||
|
||||
# Main logic
|
||||
VAULT_TOKEN=$(cache_vault_token)
|
||||
SERVICE_TOKEN=$(cache_service_token "$VAULT_TOKEN" "$VAULT_KEY")
|
||||
VAULT_TOKEN=$(cach…ken)
|
||||
SERVICE_TOKEN=$(cach…oken "$VAULT_TOKEN" "$VAULT_KEY")
|
||||
AUTH_HEADER=$(build_auth_header "$HEADER_TYPE" "$SERVICE_TOKEN")
|
||||
|
||||
URL="${BASE_URL}${API_PATH}"
|
||||
@@ -145,4 +160,4 @@ fi
|
||||
|
||||
# Output response
|
||||
cat "$RESPONSE_FILE"
|
||||
rm -f "$RESPONSE_FILE"
|
||||
rm -f "$RESPONSE_FILE"
|
||||
|
||||
Reference in New Issue
Block a user