#!/bin/bash # Import existing Kubernetes Secret to HSM via REST API # Usage: ./import-from-k8s.sh [secret-name] [namespace] [target-label] [target-id] set -e API_BASE_URL=${API_BASE_URL:-"http://localhost:8090"} SOURCE_SECRET=${1:-""} SOURCE_NAMESPACE=${2:-"default"} TARGET_LABEL=${3:-""} TARGET_ID=${4:-"$(date +%s)"} if [ -z "$SOURCE_SECRET" ]; then echo "Usage: $0 [namespace] [target-label] [target-id]" echo "" echo "Available secrets in namespace '$SOURCE_NAMESPACE':" kubectl get secrets -n "$SOURCE_NAMESPACE" --field-selector type=Opaque -o name | sed 's/secret\///' exit 1 fi if [ -z "$TARGET_LABEL" ]; then TARGET_LABEL="$SOURCE_SECRET-hsm" fi echo "📦 Importing Kubernetes Secret to HSM..." echo "Source Secret: $SOURCE_SECRET" echo "Source Namespace: $SOURCE_NAMESPACE" echo "Target Label: $TARGET_LABEL" echo "Target ID: $TARGET_ID" echo "API Base URL: $API_BASE_URL" echo "" # Check if source secret exists echo "🔍 Checking source secret..." if ! kubectl get secret "$SOURCE_SECRET" -n "$SOURCE_NAMESPACE" >/dev/null 2>&1; then echo "❌ Source secret '$SOURCE_SECRET' not found in namespace '$SOURCE_NAMESPACE'" exit 1 fi # Show source secret info echo "📋 Source secret info:" kubectl describe secret "$SOURCE_SECRET" -n "$SOURCE_NAMESPACE" echo "" # Create the import request payload payload=$(cat </dev/null; then echo " Error Details:" echo "$response" | jq '.error.details' fi exit 1 fi