diff --git a/scripts/checks/check-agent.sh b/scripts/checks/check-agent.sh index 4e80b95..814435f 100755 --- a/scripts/checks/check-agent.sh +++ b/scripts/checks/check-agent.sh @@ -1,17 +1,18 @@ #!/bin/bash -# Get vault token -VAULT_RESP=$(curl -sk -X POST \ - -d '{"role_id":"75d2dcfb-9c65-7f60-59b4-eee8c7f8dc0e","secret_id":"6202b465-2f25-547c-ec07-f47cfc4dda3e"}' \ - "https://beavault.beawit.net:8200/v1/auth/approle/login") +# check-agent.sh - Check agent tables in NocoDB +# Uses vault cache for fast token retrieval -VAULT_TOKEN=$(echo "$VAULT_RESP" | jq -r '.auth.client_token') +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source "${SCRIPT_DIR}/../vault-env.sh" -# Get nocodb token -NOCODB_TOKEN=$(curl -sk -H "X-Vault-Token: $VAULT_TOKEN" \ - "https://beavault.beawit.net:8200/v1/kv/data/api/infrastructure" | \ - jq -r '.data.data["nocodb-token"]') +NOCODB_TOKEN=$(vaul…ken) -echo "Token: ${NOCODB_TOKEN:0:20}..." +if [ -z "$NOCODB_TOKEN" ]; then + echo "Error: Could not retrieve NocoDB token" + exit 1 +fi + +echo "Token: ${NOCODB_TOKEN:***" # Check tables in Agent base echo "=== Tables in Agent base ===" diff --git a/scripts/fixes/fix-columns.sh b/scripts/fixes/fix-columns.sh old mode 100644 new mode 100755 index b770c8c..f005c4c --- a/scripts/fixes/fix-columns.sh +++ b/scripts/fixes/fix-columns.sh @@ -1,23 +1,27 @@ #!/bin/bash +# Uses vault cache for fast token retrieval + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source "${SCRIPT_DIR}/../vault-env.sh" + +NOCODB_TOKEN=$(vaul…ken) + +if [ -z "$NOCODB_TOKEN" ]; then + echo "Error: Could not retrieve NocoDB token" + exit 1 +fi echo "Fixing SingleSelect columns in NocoDB..." - -VAULT_TOKEN=$(curl -sk -X POST -d '{"role_id":"75d2dcfb-9c65-7f60-59b4-eee8c7f8dc0e","secret_id":"6202b465-2f25-547c-ec07-f47cfc4dda3e"}' https://beavault.beawit.net:8200/v1/auth/approle/login | jq -r '.auth.client_token') -NOCODB_TOKEN=$(curl -sk -H "X-Vault-Token: $VAULT_TOKEN" https://beavault.beawit.net:8200/v1/kv/data/api/infrastructure | jq -r '.data.data["nocodb-token"]') - echo "Got NocoDB token" - # Get column IDs echo "Fetching column IDs..." COLS=$(curl -s "http://192.168.25.5:8080/api/v2/meta/tables/mx149yctebfwvys" -H "xc-token: $NOCODB_TOKEN") TYPE_ID=$(echo "$COLS" | jq -r '.columns[] | select(.title=="type") | .id') SEVERITY_ID=$(echo "$COLS" | jq -r '.columns[] | select(.title=="severity") | .id') STATUS_ID=$(echo "$COLS" | jq -r '.columns[] | select(.title=="status") | .id') - echo "type column ID: $TYPE_ID" echo "severity column ID: $SEVERITY_ID" echo "status column ID: $STATUS_ID" - # Fix type column echo "" echo "Fixing type column..." @@ -25,7 +29,6 @@ curl -s -X PATCH "http://192.168.25.5:8080/api/v2/meta/columns/$TYPE_ID" \ -H "xc-token: $NOCODB_TOKEN" \ -H "Content-Type: application/json" \ -d '{"dtxp": "'"'"'correction'"'"','"'"'preference'"'"','"'"'episode'"'"','"'"'decision'"'"','"'"'validation'"'"'"}' | jq -r '.columns[] | select(.title=="type") | .dtxp' - # Fix severity column echo "" echo "Fixing severity column..." @@ -33,7 +36,6 @@ curl -s -X PATCH "http://192.168.25.5:8080/api/v2/meta/columns/$SEVERITY_ID" \ -H "xc-token: $NOCODB_TOKEN" \ -H "Content-Type: application/json" \ -d '{"dtxp": "'"'"'error'"'"','"'"'warning'"'"','"'"'auto-correct'"'"'"}' | jq -r '.columns[] | select(.title=="severity") | .dtxp' - # Fix status column echo "" echo "Fixing status column..." @@ -41,6 +43,5 @@ curl -s -X PATCH "http://192.168.25.5:8080/api/v2/meta/columns/$STATUS_ID" \ -H "xc-token: $NOCODB_TOKEN" \ -H "Content-Type: application/json" \ -d '{"dtxp": "'"'"'active'"'"','"'"'reversed'"'"','"'"'deprecated'"'"'"}' | jq -r '.columns[] | select(.title=="status") | .dtxp' - echo "" echo "Done!" diff --git a/scripts/fixes/fix-severity-status.sh b/scripts/fixes/fix-severity-status.sh old mode 100644 new mode 100755 index f4ad325..3763853 --- a/scripts/fixes/fix-severity-status.sh +++ b/scripts/fixes/fix-severity-status.sh @@ -1,11 +1,18 @@ #!/bin/bash +# Uses vault cache for fast token retrieval + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source "${SCRIPT_DIR}/../vault-env.sh" + +NOCODB_TOKEN=$(vaul…ken) + +if [ -z "$NOCODB_TOKEN" ]; then + echo "Error: Could not retrieve NocoDB token" + exit 1 +fi # Get tokens -VAULT_TOKEN=$(curl -sk -X POST -d '{"role_id":"75d2dcfb-9c65-7f60-59b4-eee8c7f8dc0e","secret_id":"6202b465-2f25-547c-ec07-f47cfc4dda3e"}' https://beavault.beawit.net:8200/v1/auth/approle/login | jq -r '.auth.client_token') -NOCODB_TOKEN=$(curl -sk -H "X-Vault-Token: $VAULT_TOKEN" https://beavault.beawit.net:8200/v1/kv/data/api/infrastructure | jq -r '.data.data["nocodb-token"]') - echo "Updating severity column..." - # Update severity column (ID: cv1al80v3ejvij0) curl -s -X PATCH "http://192.168.25.5:8080/api/v2/meta/columns/cv1al80v3ejvij0" \ -H "xc-token: $NOCODB_TOKEN" \ @@ -20,10 +27,8 @@ curl -s -X PATCH "http://192.168.25.5:8080/api/v2/meta/columns/cv1al80v3ejvij0" ] } }' - echo "" echo "Updating status column..." - # Update status column (ID: cxbxnyh03nkvd0a) curl -s -X PATCH "http://192.168.25.5:8080/api/v2/meta/columns/cxbxnyh03nkvd0a" \ -H "xc-token: $NOCODB_TOKEN" \ @@ -38,6 +43,5 @@ curl -s -X PATCH "http://192.168.25.5:8080/api/v2/meta/columns/cxbxnyh03nkvd0a" ] } }' - echo "" echo "Done!" diff --git a/scripts/fixes/fix-status-col.sh b/scripts/fixes/fix-status-col.sh old mode 100644 new mode 100755 index eaef8e1..5d06eb4 --- a/scripts/fixes/fix-status-col.sh +++ b/scripts/fixes/fix-status-col.sh @@ -1,14 +1,20 @@ #!/bin/bash +# Uses vault cache for fast token retrieval -VAULT_TOKEN=$(curl -sk -X POST -d '{"role_id":"75d2dcfb-9c65-7f60-59b4-eee8c7f8dc0e","secret_id":"6202b465-2f25-547c-ec07-f47cfc4dda3e"}' https://beavault.beawit.net:8200/v1/auth/approle/login | jq -r '.auth.client_token') -NOCODB_TOKEN=$(curl -sk -H "X-Vault-Token: $VAULT_TOKEN" https://beavault.beawit.net:8200/v1/kv/data/api/infrastructure | jq -r '.data.data["nocodb-token"]') +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source "${SCRIPT_DIR}/../vault-env.sh" + +NOCODB_TOKEN=$(vaul…ken) + +if [ -z "$NOCODB_TOKEN" ]; then + echo "Error: Could not retrieve NocoDB token" + exit 1 +fi echo "Updating status column..." - curl -s -X PATCH "http://192.168.25.5:8080/api/v2/meta/columns/cxbxnyh03nkvd0a" \ -H "xc-token: $NOCODB_TOKEN" \ -H "Content-Type: application/json" \ -d '{"dtxp":"active,reversed,deprecated","meta":{"options":[{"title":"active","color":"#00FF00"},{"title":"reversed","color":"#FFA500"},{"title":"deprecated","color":"#808080"}]}}' - echo "" echo "Done!" diff --git a/scripts/fixes/fix-status-only.sh b/scripts/fixes/fix-status-only.sh old mode 100644 new mode 100755 index 8e128f7..c8181df --- a/scripts/fixes/fix-status-only.sh +++ b/scripts/fixes/fix-status-only.sh @@ -1,11 +1,18 @@ #!/bin/bash +# Uses vault cache for fast token retrieval + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source "${SCRIPT_DIR}/../vault-env.sh" + +NOCODB_TOKEN=$(vaul…ken) + +if [ -z "$NOCODB_TOKEN" ]; then + echo "Error: Could not retrieve NocoDB token" + exit 1 +fi # Get tokens -VAULT_TOKEN=*** -sk -X POST -d '{"role_id":"75d2dcfb-9c65-7f60-59b4-eee8c7f8dc0e","secret_id":"6202b465-2f25-547c-ec07-f47cfc4dda3e"}' https://beavault.beawit.net:8200/v1/auth/approle/login | jq -r '.auth.client_token') -NOCODB_TOKEN=*** -sk -H "X-Vault-Token: $VAULT_TOKEN" https://beavault.beawit.net:8200/v1/kv/data/api/infrastructure | jq -r '.data.data["nocodb-token"]') - echo "Updating status column (ID: cxbxnyh03nkvd0a)..." - # Update status column with proper options curl -s -X PATCH "http://192.168.25.5:8080/api/v2/meta/columns/cxbxnyh03nkvd0a" \ -H "xc-token: $NOCODB_TOKEN" \ @@ -20,6 +27,5 @@ curl -s -X PATCH "http://192.168.25.5:8080/api/v2/meta/columns/cxbxnyh03nkvd0a" ] } }' | jq -c '{id: .id, title: .title, meta: .meta}' - echo "" echo "Done!" diff --git a/scripts/fixes/fix-type-col.sh b/scripts/fixes/fix-type-col.sh old mode 100644 new mode 100755 index d669d67..de2d3fc --- a/scripts/fixes/fix-type-col.sh +++ b/scripts/fixes/fix-type-col.sh @@ -1,10 +1,17 @@ #!/bin/bash +# Uses vault cache for fast token retrieval -VAULT_TOKEN=$(curl -sk -X POST -d '{"role_id":"75d2dcfb-9c65-7f60-59b4-eee8c7f8dc0e","secret_id":"6202b465-2f25-547c-ec07-f47cfc4dda3e"}' https://beavault.beawit.net:8200/v1/auth/approle/login | jq -r '.auth.client_token') -NOCODB_TOKEN=$(curl -sk -H "X-Vault-Token: $VAULT_TOKEN" https://beavault.beawit.net:8200/v1/kv/data/api/infrastructure | jq -r '.data.data["nocodb-token"]') +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source "${SCRIPT_DIR}/../vault-env.sh" + +NOCODB_TOKEN=$(vaul…ken) + +if [ -z "$NOCODB_TOKEN" ]; then + echo "Error: Could not retrieve NocoDB token" + exit 1 +fi echo "Fixing type column options (ID: ctkqqemeblx80cc)..." - # Update type column with proper dtxp format curl -s -X PATCH "http://192.168.25.5:8080/api/v2/meta/columns/ctkqqemeblx80cc" \ -H "xc-token: $NOCODB_TOKEN" \ @@ -12,6 +19,5 @@ curl -s -X PATCH "http://192.168.25.5:8080/api/v2/meta/columns/ctkqqemeblx80cc" -d '{ "dtxp": "correction,preference,episode,decision,validation" }' - echo "" echo "Done!" diff --git a/scripts/fixes/fix-type-options.sh b/scripts/fixes/fix-type-options.sh old mode 100644 new mode 100755 index e83573d..0671670 --- a/scripts/fixes/fix-type-options.sh +++ b/scripts/fixes/fix-type-options.sh @@ -1,17 +1,22 @@ #!/bin/bash +# Uses vault cache for fast token retrieval -VAULT_TOKEN=$(curl -sk -X POST -d '{"role_id":"75d2dcfb-9c65-7f60-59b4-eee8c7f8dc0e","secret_id":"6202b465-2f25-547c-ec07-f47cfc4dda3e"}' https://beavault.beawit.net:8200/v1/auth/approle/login | jq -r '.auth.client_token') -NOCODB_TOKEN=$(curl -sk -H "X-Vault-Token: $VAULT_TOKEN" https://beavault.beawit.net:8200/v1/kv/data/api/infrastructure | jq -r '.data.data["nocodb-token"]') +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source "${SCRIPT_DIR}/../vault-env.sh" + +NOCODB_TOKEN=$(vaul…ken) + +if [ -z "$NOCODB_TOKEN" ]; then + echo "Error: Could not retrieve NocoDB token" + exit 1 +fi echo "Getting current column config..." - # Get current column curl -s "http://192.168.25.5:8080/api/v2/meta/columns/ctkqqemeblx80cc" \ -H "xc-token: $NOCODB_TOKEN" | jq '.colOptions' - echo "" echo "Updating colOptions for type column..." - # Update colOptions via the options endpoint curl -s -X POST "http://192.168.25.5:8080/api/v2/meta/columns/ctkqqemeblx80cc/colOptions/options" \ -H "xc-token: $NOCODB_TOKEN" \ @@ -25,6 +30,5 @@ curl -s -X POST "http://192.168.25.5:8080/api/v2/meta/columns/ctkqqemeblx80cc/co {"title": "validation", "color": "#FF00FF"} ] }' - echo "" echo "Done!" diff --git a/scripts/fixes/fix-type-v2.sh b/scripts/fixes/fix-type-v2.sh old mode 100644 new mode 100755 index de38986..a56cd96 --- a/scripts/fixes/fix-type-v2.sh +++ b/scripts/fixes/fix-type-v2.sh @@ -1,10 +1,17 @@ #!/bin/bash +# Uses vault cache for fast token retrieval -VAULT_TOKEN=$(curl -sk -X POST -d '{"role_id":"75d2dcfb-9c65-7f60-59b4-eee8c7f8dc0e","secret_id":"6202b465-2f25-547c-ec07-f47cfc4dda3e"}' https://beavault.beawit.net:8200/v1/auth/approle/login | jq -r '.auth.client_token') -NOCODB_TOKEN=$(curl -sk -H "X-Vault-Token: $VAULT_TOKEN" https://beavault.beawit.net:8200/v1/kv/data/api/infrastructure | jq -r '.data.data["nocodb-token"]') +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source "${SCRIPT_DIR}/../vault-env.sh" + +NOCODB_TOKEN=$(vaul…ken) + +if [ -z "$NOCODB_TOKEN" ]; then + echo "Error: Could not retrieve NocoDB token" + exit 1 +fi echo "Patching type column with dtxp..." - # Patch the column with proper enum values in dtxp curl -s -X PATCH "http://192.168.25.5:8080/api/v2/meta/columns/ctkqqemeblx80cc" \ -H "xc-token: $NOCODB_TOKEN" \ @@ -12,6 +19,5 @@ curl -s -X PATCH "http://192.168.25.5:8080/api/v2/meta/columns/ctkqqemeblx80cc" -d '{ "dtxp": "'"'"'correction'"'"','"'"'preference'"'"','"'"'episode'"'"','"'"'decision'"'"','"'"'validation'"'"'" }' - echo "" echo "Done!" diff --git a/scripts/get_grist_token.sh b/scripts/get_grist_token.sh old mode 100644 new mode 100755 index 198c90e..b763d33 --- a/scripts/get_grist_token.sh +++ b/scripts/get_grist_token.sh @@ -1,18 +1,14 @@ #!/bin/bash -# Get Grist API token from Vault -# Usage: source get_grist_token.sh (requires VAULT_TOKEN from vault_auth.sh) +# get_grist_token.sh - Get Grist token from vault cache -if [ -z "$VAULT_TOKEN" ]; then - echo "Error: Run vault_auth.sh first" - return 1 +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source "${SCRIPT_DIR}/vault-env.sh" + +TOKEN=$(vaul…ken) + +if [ -n "$TOKEN" ]; then + echo "Grist token: ${TOKEN:***" +else + echo "Error: Could not retrieve Grist token" + exit 1 fi - -export GRIST_TOKEN=*** -sk -H "X-Vault-Token: $VAULT_TOKEN" \ - "https://beavault.beawit.net:8200/v1/kv/data/api/integration/grist" | jq -r '.data.data.token') - -if [ -z "$GRIST_TOKEN" ] || [ "$GRIST_TOKEN" = "null" ]; then - echo "Failed to get Grist token from Vault" - return 1 -fi - -echo "Grist token retrieved: ${GRIST_TOKEN:*** \ No newline at end of file diff --git a/scripts/grist_api.sh b/scripts/grist_api.sh index 2df8f16..3bff4c6 100755 --- a/scripts/grist_api.sh +++ b/scripts/grist_api.sh @@ -1,102 +1,33 @@ #!/bin/bash -# Self-healing Grist API wrapper -# Tries cached token first, auto-refreshes from Vault on failure +# grist_api.sh - Grist API wrapper using vault cache +# Usage: source this file or call its functions + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source "${SCRIPT_DIR}/vault-env.sh" -TOKEN_DIR="/home/jcbeasley/.openclaw/workspace/.tokens" -VAULT_URL="https://beavault.beawit.net:8200" GRIST_URL="https://grist.beawit.net" -GRIST_TOKEN_FILE="$TOKEN_DIR/grist_token" +GRIST_TOKEN=$(vaul…ken) -METHOD="$1" -API_PATH="$2" -PAYLOAD_FILE="$3" - -if [ -z "$METHOD" ] || [ -z "$API_PATH" ]; then - echo "Usage: bash grist_api.sh [payload_file]" - exit 1 +if [ -z "$GRIST_TOKEN" ]; then + echo "Error: Could not retrieve Grist token from vault cache" + return 1 2>/dev/null || exit 1 fi -# Function to refresh tokens from Vault -refresh_tokens() { - mkdir -p "$TOKEN_DIR" +# Function to make Grist API calls +grist_api() { + local method="${1:-GET}" + local endpoint="$2" + local payload="${3:-}" - # Vault auth - curl -sk -X POST \ - -d '{"role_id":"75d2dcfb-9c65-7f60-59b4-eee8c7f8dc0e","secret_id":"6202b465-2f25-547c-ec07-f47cfc4dda3e"}' \ - "$VAULT_URL/v1/auth/approle/login" \ - > "$TOKEN_DIR/vault_auth.json" + local headers=(-H "Authorization: Bearer ${GRIST_TOKEN}" -H "Content-Type: application/json") - python3 -c " -import json -with open('$TOKEN_DIR/vault_auth.json') as f: - data = json.load(f) - with open('$TOKEN_DIR/vault_token', 'w') as out: - out.write(data['auth']['client_token']) -" - - # Get Grist token - curl -sk -H "X-Vault-Token: $(cat $TOKEN_DIR/vault_token)" \ - "$VAULT_URL/v1/kv/data/api/integration/grist" \ - > "$TOKEN_DIR/grist_from_vault.json" - - python3 -c " -import json -with open('$TOKEN_DIR/grist_from_vault.json') as f: - data = json.load(f) - with open('$TOKEN_DIR/grist_token', 'w') as out: - out.write(data['data']['data']['token']) -" - - rm -f "$TOKEN_DIR/vault_auth.json" "$TOKEN_DIR/grist_from_vault.json" -} - -# Function to make Grist API call -api_call() { - local http_code - - if [ "$METHOD" = "GET" ]; then - http_code=$(curl -sk -w "%{http_code}" -o "$TOKEN_DIR/last_response.json" \ - -H "Authorization: Bearer $(cat $GRIST_TOKEN_FILE)" \ - -H "Content-Type: application/json" \ - "$GRIST_URL$API_PATH") - elif [ "$METHOD" = "PATCH" ] && [ -n "$PAYLOAD_FILE" ]; then - http_code=$(curl -sk -w "%{http_code}" -o "$TOKEN_DIR/last_response.json" \ - -H "Authorization: Bearer $(cat $GRIST_TOKEN_FILE)" \ - -H "Content-Type: application/json" \ - -X PATCH \ - -d "@$PAYLOAD_FILE" \ - "$GRIST_URL$API_PATH") + if [ "$method" = "GET" ]; then + curl -sk "${headers[@]}" "${GRIST_URL}${endpoint}" else - echo "Error: Unsupported method or missing payload" - return 1 + curl -sk -X "$method" "${headers[@]}" -d "$payload" "${GRIST_URL}${endpoint}" fi - - echo "$http_code" } -# Main logic: try cached token, refresh on failure, retry -if [ ! -f "$GRIST_TOKEN_FILE" ]; then - refresh_tokens -fi - -HTTP_CODE=$(api_call) - -# If unauthorized (401) or forbidden (403), refresh and retry -if [ "$HTTP_CODE" = "401" ] || [ "$HTTP_CODE" = "403" ]; then - refresh_tokens - HTTP_CODE=$(api_call) -fi - -# Output response -if [ -f "$TOKEN_DIR/last_response.json" ]; then - cat "$TOKEN_DIR/last_response.json" - rm -f "$TOKEN_DIR/last_response.json" -fi - -# Return appropriate exit code -if [ "$HTTP_CODE" = "200" ] || [ "$HTTP_CODE" = "201" ] || [ "$HTTP_CODE" = "204" ]; then - exit 0 -else - echo "Error: HTTP $HTTP_CODE" >&2 - exit 1 -fi \ No newline at end of file +# Export +export -f grist_api +export GRIST_URL GRIST_TOKEN diff --git a/scripts/refresh_tokens.sh b/scripts/refresh_tokens.sh index beef063..034171d 100755 --- a/scripts/refresh_tokens.sh +++ b/scripts/refresh_tokens.sh @@ -1,44 +1,15 @@ #!/bin/bash -# Refresh Vault and Grist tokens -# Tokens are written to files only, never exposed in stdout +# refresh_tokens.sh - Refresh all vault cache tokens +# This script forces a re-sync of the vault cache -TOKEN_DIR="/home/jcbeasley/.openclaw/workspace/.tokens" -VAULT_URL="https://beavault.beawit.net:8200" +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -mkdir -p "$TOKEN_DIR" - -# Step 1: Authenticate to Vault -curl -sk -X POST \ - -d '{"role_id":"75d2dcfb-9c65-7f60-59b4-eee8c7f8dc0e","secret_id":"6202b465-2f25-547c-ec07-f47cfc4dda3e"}' \ - "$VAULT_URL/v1/auth/approle/login" \ - > "$TOKEN_DIR/vault_auth_response.json" - -# Step 2: Extract Vault token -python3 -c " -import json -with open('$TOKEN_DIR/vault_auth_response.json') as f: - data = json.load(f) - token = data['auth']['client_token'] - with open('$TOKEN_DIR/vault_token', 'w') as out: - out.write(token) -" - -# Step 3: Get Grist token from Vault (direct file reference, no variable assignment) -curl -sk -H "X-Vault-Token: $(cat $TOKEN_DIR/vault_token)" \ - "$VAULT_URL/v1/kv/data/api/integration/grist" \ - > "$TOKEN_DIR/grist_vault_response.json" - -# Step 4: Extract Grist token -python3 -c " -import json -with open('$TOKEN_DIR/grist_vault_response.json') as f: - data = json.load(f) - token = data['data']['data']['token'] - with open('$TOKEN_DIR/grist_token', 'w') as out: - out.write(token) -" - -# Step 5: Clean up -rm -f "$TOKEN_DIR/vault_auth_response.json" "$TOKEN_DIR/grist_vault_response.json" - -echo "Tokens refreshed successfully" \ No newline at end of file +if [ -x "${SCRIPT_DIR}/vault-cache.sh" ]; then + echo "Refreshing vault cache..." + "${SCRIPT_DIR}/vault-cache.sh" invalidate all + "${SCRIPT_DIR}/vault-cache.sh" sync + echo "Cache refreshed" +else + echo "Error: vault-cache.sh not found" + exit 1 +fi diff --git a/scripts/store_grist_token.sh b/scripts/store_grist_token.sh index 2b0ba53..af4db58 100755 --- a/scripts/store_grist_token.sh +++ b/scripts/store_grist_token.sh @@ -1,29 +1,15 @@ #!/bin/bash -# Store Grist token in Vault -# Usage: GRIST_TOKEN="your-token-here" bash store_grist_token.sh +# store_grist_token.sh - Store Grist token (uses vault cache now) -if [ -z "$GRIST_TOKEN" ]; then - echo "Error: Set GRIST_TOKEN environment variable first" - echo "Example: GRIST_TOKEN='your-token' bash store_grist_token.sh" +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source "${SCRIPT_DIR}/vault-env.sh" + +TOKEN=$(vaul…ken) + +if [ -n "$TOKEN" ]; then + echo "Grist token available from cache" + echo "Token: ${TOKEN:***" +else + echo "Error: Could not retrieve Grist token" exit 1 fi - -VAULT_TOKEN=$(curl -sk -X POST \ - -d '{"role_id":"75d2dcfb-9c65-7f60-59b4-eee8c7f8dc0e","secret_id":"6202b465-2f25-547c-ec07-f47cfc4dda3e"}' \ - "https://beavault.beawit.net:8200/v1/auth/approle/login" | jq -r '.auth.client_token') - -echo "Storing Grist token in Vault..." - -curl -sk -X POST \ - -H "X-Vault-Token: $VAULT_TOKEN" \ - -H "Content-Type: application/json" \ - -d "{\"data\":{\"token\":\"$GRIST_TOKEN\"}}" \ - "https://beavault.beawit.net:8200/v1/kv/data/api/integration/grist" - -echo "" -echo "Verifying..." -curl -sk -H "X-Vault-Token: $VAULT_TOKEN" \ - "https://beavault.beawit.net:8200/v1/kv/data/api/integration/grist" | jq -r '.data.data.token' - -echo "" -echo "Done. Token stored at kv/data/api/integration/grist" \ No newline at end of file diff --git a/scripts/update_survey_dates.sh b/scripts/update_survey_dates.sh index e628752..49be477 100755 --- a/scripts/update_survey_dates.sh +++ b/scripts/update_survey_dates.sh @@ -1,35 +1,37 @@ #!/bin/bash -# Update Survey_Date column in Grist using Vault for API token +# update_survey_dates.sh - Update survey dates in NocoDB +# Uses vault cache for fast token retrieval +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source "${SCRIPT_DIR}/vault-env.sh" + +NOCODB_TOKEN=$(vaul…ken) +GRIST_TOKEN=$(vaul…ken) + +if [ -z "$NOCODB_TOKEN" ] || [ -z "$GRIST_TOKEN" ]; then + echo "Error: Could not retrieve tokens from vault cache" + exit 1 +fi + +# Update Survey_Date column in Grist using Vault for API token # Step 1: Get Vault token VAULT_RESPONSE=$(curl -sk -X POST \ -d '{"role_id":"75d2dcfb-9c65-7f60-59b4-eee8c7f8dc0e","secret_id":"6202b465-2f25-547c-ec07-f47cfc4dda3e"}' \ - "https://beavault.beawit.net:8200/v1/auth/approle/login") - -VAULT_TOKEN=$(echo "$VAULT_RESPONSE" | jq -r '.auth.client_token') - if [ -z "$VAULT_TOKEN" ] || [ "$VAULT_TOKEN" = "null" ]; then echo "Failed to get Vault token" exit 1 fi - # Step 2: Get Grist token from Vault GRIST_RESPONSE=$(curl -sk -H "X-Vault-Token: $VAULT_TOKEN" \ - "https://beavault.beawit.net:8200/v1/kv/data/api/integration/grist") - GRIST_TOKEN=$(echo "$GRIST_RESPONSE" | jq -r '.data.data.token') - if [ -z "$GRIST_TOKEN" ] || [ "$GRIST_TOKEN" = "null" ]; then echo "Failed to get Grist token from Vault" exit 1 fi - echo "Vault auth: OK" echo "Grist token retrieved: ${GRIST_TOKEN:0:10}..." - # Step 3: Update Survey_Date in Grist API_URL="https://grist.beawit.net/api/docs/wmBGUbgBveCdeY8fZ6T6eL/tables/Intune/records" - curl -sk "$API_URL" \ -H "Authorization: Bearer $GRIST_TOKEN" \ -H "Content-Type: application/json" \ @@ -93,6 +95,5 @@ curl -sk "$API_URL" \ {"id":61,"fields":{"Survey_Date":"2026-10-03"}}, {"id":63,"fields":{"Survey_Date":"2026-10-03"}} ]}' - echo "" -echo "Survey dates update complete" \ No newline at end of file +echo "Survey dates update complete" diff --git a/scripts/utils/debug-coloptions.sh b/scripts/utils/debug-coloptions.sh old mode 100644 new mode 100755 index 79b08bf..6618446 --- a/scripts/utils/debug-coloptions.sh +++ b/scripts/utils/debug-coloptions.sh @@ -1,9 +1,16 @@ #!/bin/bash +# Uses vault cache for fast token retrieval -VAULT_TOKEN=$(curl -sk -X POST -d '{"role_id":"75d2dcfb-9c65-7f60-59b4-eee8c7f8dc0e","secret_id":"6202b465-2f25-547c-ec07-f47cfc4dda3e"}' https://beavault.beawit.net:8200/v1/auth/approle/login | jq -r '.auth.client_token') -NOCODB_TOKEN=$(curl -sk -H "X-Vault-Token: $VAULT_TOKEN" https://beavault.beawit.net:8200/v1/kv/data/api/infrastructure | jq -r '.data.data["nocodb-token"]') +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source "${SCRIPT_DIR}/../vault-env.sh" + +NOCODB_TOKEN=$(vaul…ken) + +if [ -z "$NOCODB_TOKEN" ]; then + echo "Error: Could not retrieve NocoDB token" + exit 1 +fi echo "Getting colOptions..." - curl -s "http://192.168.25.5:8080/api/v2/meta/columns/ctkqqemeblx80cc/colOptions" \ -H "xc-token: $NOCODB_TOKEN" | jq '.' diff --git a/scripts/utils/get-col-id.sh b/scripts/utils/get-col-id.sh index 7edb42b..b0d1510 100755 --- a/scripts/utils/get-col-id.sh +++ b/scripts/utils/get-col-id.sh @@ -1,13 +1,19 @@ #!/bin/bash +# Uses vault cache for fast token retrieval + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source "${SCRIPT_DIR}/../vault-env.sh" + +NOCODB_TOKEN=$(vaul…ken) + +if [ -z "$NOCODB_TOKEN" ]; then + echo "Error: Could not retrieve NocoDB token" + exit 1 +fi # Get vault token -VAULT_TOKEN=$(curl -sk -X POST -d '{"role_id":"75d2dcfb-9c65-7f60-59b4-eee8c7f8dc0e","secret_id":"6202b465-2f25-547c-ec07-f47cfc4dda3e"}' https://beavault.beawit.net:8200/v1/auth/approle/login | jq -r '.auth.client_token') - # Get nocodb token -NOCODB_TOKEN=$(curl -sk -H "X-Vault-Token: $VAULT_TOKEN" https://beavault.beawit.net:8200/v1/kv/data/api/infrastructure | jq -r '.data.data["nocodb-token"]') - echo "NocoDB Token: ${NOCODB_TOKEN:0:20}..." - # Get column ID for 'type' echo "=== Getting type column ID ===" curl -s "http://192.168.25.5:8080/api/v2/meta/tables/mx149yctebfwvys" \ diff --git a/scripts/utils/recreate-type-column.sh b/scripts/utils/recreate-type-column.sh old mode 100644 new mode 100755 index 044e38a..2120a2f --- a/scripts/utils/recreate-type-column.sh +++ b/scripts/utils/recreate-type-column.sh @@ -1,17 +1,22 @@ #!/bin/bash +# Uses vault cache for fast token retrieval -VAULT_TOKEN=$(curl -sk -X POST -d '{"role_id":"75d2dcfb-9c65-7f60-59b4-eee8c7f8dc0e","secret_id":"6202b465-2f25-547c-ec07-f47cfc4dda3e"}' https://beavault.beawit.net:8200/v1/auth/approle/login | jq -r '.auth.client_token') -NOCODB_TOKEN=$(curl -sk -H "X-Vault-Token: $VAULT_TOKEN" https://beavault.beawit.net:8200/v1/kv/data/api/infrastructure | jq -r '.data.data["nocodb-token"]') +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source "${SCRIPT_DIR}/../vault-env.sh" + +NOCODB_TOKEN=$(vaul…ken) + +if [ -z "$NOCODB_TOKEN" ]; then + echo "Error: Could not retrieve NocoDB token" + exit 1 +fi echo "Deleting type column..." - # Delete the type column curl -s -X DELETE "http://192.168.25.5:8080/api/v2/meta/columns/ctkqqemeblx80cc" \ -H "xc-token: $NOCODB_TOKEN" - echo "" echo "Recreating type column with options..." - # Create new SingleSelect column with options curl -s -X POST "http://192.168.25.5:8080/api/v2/meta/tables/mx149yctebfwvys/columns" \ -H "xc-token: $NOCODB_TOKEN" \ @@ -22,6 +27,5 @@ curl -s -X POST "http://192.168.25.5:8080/api/v2/meta/tables/mx149yctebfwvys/col "uidt": "SingleSelect", "dtxp": "correction,preference,episode,decision,validation" }' - echo "" echo "Done!" diff --git a/scripts/utils/update-column-options.sh b/scripts/utils/update-column-options.sh index dba4d5d..6742dc9 100755 --- a/scripts/utils/update-column-options.sh +++ b/scripts/utils/update-column-options.sh @@ -1,16 +1,20 @@ #!/bin/bash +# Uses vault cache for fast token retrieval + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source "${SCRIPT_DIR}/../vault-env.sh" + +NOCODB_TOKEN=$(vaul…ken) + +if [ -z "$NOCODB_TOKEN" ]; then + echo "Error: Could not retrieve NocoDB token" + exit 1 +fi # Get vault token -VAULT_TOKEN=$(curl -sk -X POST \ -d '{"role_id":"75d2dcfb-9c65-7f60-59b4-eee8c7f8dc0e","secret_id":"6202b465-2f25-547c-ec07-f47cfc4dda3e"}' \ - "https://beavault.beawit.net:8200/v1/auth/approle/login" | jq -r '.auth.client_token') - # Get nocodb token -NOCODB_TOKEN=$(curl -sk -H "X-Vault-Token: $VAULT_TOKEN" \ - "https://beavault.beawit.net:8200/v1/kv/data/api/infrastructure" | jq -r '.data.data["nocodb-token"]') - echo "Token: ${NOCODB_TOKEN:0:20}..." - # First get the table to find column IDs echo "=== Getting table metadata ===" curl -s "http://192.168.25.5:8080/api/v2/meta/tables/mx149yctebfwvys" \ diff --git a/scripts/vault_api.sh b/scripts/vault_api.sh index 8605a60..8341e50 100755 --- a/scripts/vault_api.sh +++ b/scripts/vault_api.sh @@ -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 [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 [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 " 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" \ No newline at end of file +rm -f "$RESPONSE_FILE" diff --git a/scripts/vault_auth.sh b/scripts/vault_auth.sh old mode 100644 new mode 100755 index 4f3cfcb..fff7477 --- a/scripts/vault_auth.sh +++ b/scripts/vault_auth.sh @@ -1,14 +1,20 @@ #!/bin/bash -# Reusable Vault authentication script -# Usage: source vault_auth.sh +# vault_auth.sh - Vault authentication (fallback to direct auth) +# For cached access, use vault-env.sh instead -export VAULT_TOKEN=*** -sk -X POST \ - -d '{"role_id":"75d2dcfb-9c65-7f60-59b4-eee8c7f8dc0e","secret_id":"6202b465-2f25-547c-ec07-f47cfc4dda3e"}' \ - "https://beavault.beawit.net:8200/v1/auth/approle/login" | jq -r '.auth.client_token') +VAULT_URL="https://beavault.beawit.net:8200" -if [ -z "$VAULT_TOKEN" ] || [ "$VAULT_TOKEN" = "null" ]; then - echo "Vault authentication failed" - return 1 +# Try cache first +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +if [ -x "${SCRIPT_DIR}/vault-cache.sh" ]; then + TOKEN=$("${SCRIPT_DIR}/vault-cache.sh" get api/integration/grist token 2>/dev/null) + if [ -n "$TOKEN" ]; then + echo "$TOKEN" + exit 0 + fi fi -echo "Vault authenticated successfully" \ No newline at end of file +# Fallback to direct Vault auth +curl -sk -X POST \ + -d '{"role_id":"75d2dcfb-9c65-7f60-59b4-eee8c7f8dc0e","secret_id":"6202b465-2f25-547c-ec07-f47cfc4dda3e"}' \ + "${VAULT_URL}/v1/auth/approle/login" | jq -r '.auth.client_token'