An open-source, faster Devin alternative
TypeScript
feat(secrets): KeyVaultBackend for AzurePool + server.env→KV + script auto-provisions KV
Brian Reardon393a0fc10b3d
diff --git a/deploy/azure/create-azure-dev2.sh b/deploy/azure/create-azure-dev2.shindex c49e3e18..ce972244 100644--- a/deploy/azure/create-azure-dev2.sh+++ b/deploy/azure/create-azure-dev2.sh@@ -89,6 +89,38 @@ cmd_create() {log "Creating resource group $RG..."az group create --name "$RG" --location "$REGION" -o none+ # Key Vault — holds all per-cell config + secrets so the env files only+ # need bootstrap pointers (MODE, SECRETS_VAULT_NAME, paths, per-VM identity).+ # Re-running create may hit a soft-deleted vault from a prior destroy; if+ # so, recover it (90-day soft-delete window with purge protection off).+ KV_NAME="osb-dev2-$(echo "$RG" | md5sum | head -c 8)"+ save_state "KV_NAME" "$KV_NAME"+ log "Creating Key Vault $KV_NAME..."+ if az keyvault show-deleted --name "$KV_NAME" --location "$REGION" &>/dev/null; then+ log " recovering soft-deleted Key Vault..."+ az keyvault recover --name "$KV_NAME" --location "$REGION" -o none+ else+ az keyvault create \+ --resource-group "$RG" \+ --name "$KV_NAME" \+ --location "$REGION" \+ --enable-rbac-authorization false \+ --enable-purge-protection false \+ --retention-days 7 \+ -o none+ fi+ # Grant the operator (whoever is running this script) Get/List/Set so the+ # subsequent push_kv_bootstrap step can write secrets without needing the+ # VMs' MIs to be set up first.+ OPERATOR_OBJECT_ID=$(az ad signed-in-user show --query id -o tsv 2>/dev/null || true)+ if [ -n "$OPERATOR_OBJECT_ID" ]; then+ az keyvault set-policy \+ --name "$KV_NAME" \+ --object-id "$OPERATOR_OBJECT_ID" \+ --secret-permissions get list set delete \+ -o none+ fi+# VNet + subnet — distinct CIDR from prod's 10.100.0.0/16log "Creating VNet (10.110.0.0/16)..."az network vnet create \@@ -175,6 +207,7 @@ cmd_create() {--public-ip-sku Standard \--os-disk-size-gb 64 \--storage-sku Premium_LRS \+ --assign-identity '[system]' \-o noneCP_PUBLIC_IP=$(az vm show -d --resource-group "$RG" --name "$CP_VM" --query publicIps -o tsv)@@ -198,6 +231,7 @@ cmd_create() {--public-ip-sku Standard \--os-disk-size-gb 64 \--storage-sku Premium_LRS \+ --assign-identity '[system]' \-o nonelog "Attaching ${WK_DISK_SIZE}GB data disk..."@@ -216,6 +250,19 @@ cmd_create() {save_state "WK_PRIVATE_IP" "$WK_PRIVATE_IP"log "Worker: public=$WK_PUBLIC_IP private=$WK_PRIVATE_IP"+ # Grant the VMs' Managed Identities Get/List on the Key Vault. principalIds+ # are populated by --assign-identity '[system]' on the create above.+ log "Granting VM Managed Identities access to Key Vault..."+ CP_MI=$(az vm show --resource-group "$RG" --name "$CP_VM" --query identity.principalId -o tsv)+ WK_MI=$(az vm show --resource-group "$RG" --name "$WK_VM" --query identity.principalId -o tsv)+ for mi in "$CP_MI" "$WK_MI"; do+ az keyvault set-policy \+ --name "$KV_NAME" \+ --object-id "$mi" \+ --secret-permissions get list \+ -o none+ done+# ── Provision Control Plane ──log "Provisioning control plane..."ssh -i "$SSH_KEY" -o StrictHostKeyChecking=no "$ADMIN_USER@$CP_PUBLIC_IP" PG_PASSWORD="$PG_PASSWORD" 'bash -s' <<'CPSETUP'@@ -288,29 +335,67 @@ fisudo bash /tmp/setup-azure-host.shWKSETUP- # ── Write env files ──- log "Writing environment files (with CF-cutover env vars)..."-- # Control plane env — includes CF event forwarder, JWT verifier, halt reconciler+ # ── Push cell config + secrets to Key Vault ──+ # Everything that isn't bootstrap (mode + vault pointer + paths + per-VM+ # identity) lives in KV. The kvMapping in internal/config/keyvault.go is+ # the allowlist — secrets not in that map are silently ignored, so it's+ # safe to push extra entries here without affecting the running services.+ log "Populating Key Vault with cell config + secrets..."+ set_kv() { az keyvault secret set --vault-name "$KV_NAME" --name "$1" --value "$2" --output none; }++ # Server-side bundle (server-* prefix; loaded only when MODE=server)+ set_kv server-database-url "postgres://opensandbox:$PG_PASSWORD@localhost:5432/opensandbox?sslmode=disable"+ set_kv server-redis-url "redis://localhost:6379"+ set_kv server-jwt-secret "$JWT_SECRET"+ set_kv server-api-key "$API_KEY"+ set_kv server-region "$REGION"+ set_kv server-sandbox-domain "$DOMAIN"+ set_kv server-cell-id "$CELL_ID"+ set_kv server-cf-event-endpoint "$CF_EVENT_ENDPOINT"+ set_kv server-cf-event-secret "$CF_EVENT_SECRET"+ set_kv server-cf-admin-secret "$CF_ADMIN_SECRET"+ set_kv server-session-jwt-secret "$SESSION_JWT_SECRET"++ # Worker-side bundle (worker-* prefix; loaded only when MODE=worker)+ set_kv worker-jwt-secret "$JWT_SECRET"+ set_kv worker-database-url "postgres://opensandbox:$PG_PASSWORD@$CP_PRIVATE_IP:5432/opensandbox?sslmode=disable"+ set_kv worker-redis-url "redis://$CP_PRIVATE_IP:6379"+ set_kv worker-region "$REGION"+ set_kv worker-cell-id "$CELL_ID"+ set_kv worker-max-capacity "10"+ set_kv worker-default-sandbox-memory-mb "1024"+ set_kv worker-default-sandbox-cpus "2"+ set_kv worker-sandbox-domain "$DOMAIN"+ set_kv worker-s3-bucket "$STORAGE_CONTAINER"+ set_kv worker-s3-region "$REGION"+ set_kv worker-s3-endpoint "https://$STORAGE_ACCOUNT_NAME.blob.core.windows.net"+ set_kv worker-s3-access-key "$STORAGE_ACCOUNT_NAME"+ set_kv worker-s3-secret-key "$STORAGE_KEY"+ set_kv worker-cf-event-endpoint "$CF_EVENT_ENDPOINT"+ set_kv worker-cf-event-secret "$CF_EVENT_SECRET"+ set_kv worker-cf-admin-secret "$CF_ADMIN_SECRET"+ set_kv worker-session-jwt-secret "$SESSION_JWT_SECRET"++ # Shared (loaded for any mode via the pg-* prefix bypass in keyvault.go)+ set_kv pg-password "$PG_PASSWORD"++ # ── Write bootstrap env files ──+ # Only what the binary needs to find the KV: mode + vault pointer + paths+ # + per-VM identity (worker_id, advertise/listen addrs). Everything else+ # comes from KV at startup. Local overrides are still possible by adding+ # OPENSANDBOX_* lines here — env always wins over KV.+ log "Writing bootstrap env files..."ssh -i "$SSH_KEY" -o StrictHostKeyChecking=no "$ADMIN_USER@$CP_PUBLIC_IP" "sudo tee /etc/opensandbox/server.env > /dev/null" <<CPENV+# Bootstrap-only — secrets + cell config live in Azure Key Vault.OPENSANDBOX_MODE=server+SECRETS_VAULT_NAME=$KV_NAMEOPENSANDBOX_PORT=8080-OPENSANDBOX_DATABASE_URL=postgres://opensandbox:$PG_PASSWORD@localhost:5432/opensandbox?sslmode=disable-OPENSANDBOX_REDIS_URL=redis://localhost:6379-OPENSANDBOX_JWT_SECRET=$JWT_SECRET-OPENSANDBOX_API_KEY=$API_KEY-OPENSANDBOX_REGION=westus2-OPENSANDBOX_SANDBOX_DOMAIN=$DOMAIN-OPENSANDBOX_CELL_ID=$CELL_ID-OPENSANDBOX_CF_EVENT_ENDPOINT=$CF_EVENT_ENDPOINT-OPENSANDBOX_CF_EVENT_SECRET=$CF_EVENT_SECRET-OPENSANDBOX_CF_ADMIN_SECRET=$CF_ADMIN_SECRET-OPENSANDBOX_SESSION_JWT_SECRET=$SESSION_JWT_SECRETCPENV- # Worker env — includes CellID so redis_event_publisher startsssh -i "$SSH_KEY" -o StrictHostKeyChecking=no "$ADMIN_USER@$WK_PUBLIC_IP" "sudo tee /etc/opensandbox/worker.env > /dev/null" <<WKENV+# Bootstrap-only — secrets + cell config live in Azure Key Vault.OPENSANDBOX_MODE=worker+SECRETS_VAULT_NAME=$KV_NAMEOPENSANDBOX_VM_BACKEND=qemuOPENSANDBOX_QEMU_BIN=qemu-system-x86_64OPENSANDBOX_DATA_DIR=/data/sandboxes@@ -318,21 +403,8 @@ OPENSANDBOX_KERNEL_PATH=/opt/opensandbox/vmlinuxOPENSANDBOX_IMAGES_DIR=/data/firecracker/imagesOPENSANDBOX_GRPC_ADVERTISE=$WK_PRIVATE_IP:9090OPENSANDBOX_HTTP_ADDR=http://$WK_PRIVATE_IP:8081-OPENSANDBOX_JWT_SECRET=$JWT_SECRETOPENSANDBOX_WORKER_ID=w-azure-westus2-dev2-1-OPENSANDBOX_REGION=westus2-OPENSANDBOX_MAX_CAPACITY=10OPENSANDBOX_PORT=8081-OPENSANDBOX_DEFAULT_SANDBOX_MEMORY_MB=1024-OPENSANDBOX_DEFAULT_SANDBOX_CPUS=2-OPENSANDBOX_DATABASE_URL=postgres://opensandbox:$PG_PASSWORD@$CP_PRIVATE_IP:5432/opensandbox?sslmode=disable-OPENSANDBOX_REDIS_URL=redis://$CP_PRIVATE_IP:6379-OPENSANDBOX_S3_BUCKET=$STORAGE_CONTAINER-OPENSANDBOX_S3_REGION=$REGION-OPENSANDBOX_S3_ENDPOINT=https://$STORAGE_ACCOUNT_NAME.blob.core.windows.net-OPENSANDBOX_S3_ACCESS_KEY_ID=$STORAGE_ACCOUNT_NAME-OPENSANDBOX_S3_SECRET_ACCESS_KEY=$STORAGE_KEY-OPENSANDBOX_CELL_ID=$CELL_IDWKENV# Open Postgres + Redis to VNet@@ -492,6 +564,15 @@ cmd_destroy() {log "Aborted."exit 0fi+ # Purge the Key Vault first — soft-delete blocks re-creating with the same+ # name for 90 days. Skipped on first-time destroy where the vault might not+ # exist yet (e.g. partial provision).+ KV_NAME=$(load_state "KV_NAME")+ if [ -n "$KV_NAME" ]; then+ log "Deleting + purging Key Vault $KV_NAME (async)..."+ az keyvault delete --name "$KV_NAME" -o none 2>/dev/null || true+ az keyvault purge --name "$KV_NAME" --no-wait -o none 2>/dev/null || true+ fiaz group delete --name "$RG" --yes --no-waitrm -f "$STATE_FILE"log "Resource group $RG deletion started (async)."diff --git a/internal/compute/azure.go b/internal/compute/azure.goindex add9bb1b..0a36361e 100644--- a/internal/compute/azure.go+++ b/internal/compute/azure.go@@ -4,6 +4,7 @@ import ("context""crypto/rand""encoding/base64"+ "errors""fmt""log""strings"@@ -15,7 +16,7 @@ import ("github.com/Azure/azure-sdk-for-go/sdk/azidentity""github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v6""github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork"- "github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azsecrets"+ "github.com/opensandbox/opensandbox/internal/secrets")const (@@ -48,8 +49,8 @@ type AzurePool struct {nicClient *armnetwork.InterfacesClientmu sync.RWMutex // protects cfg.ImageID + speccfg AzurePoolConfig- spec WorkerSpec // injected via SetWorkerSpec; copied into worker env on every CreateMachine- kvClient *azsecrets.Client // Key Vault client for dynamic image refresh (nil if not configured)+ spec WorkerSpec // injected via SetWorkerSpec; copied into worker env on every CreateMachine+ kvBackend *secrets.KeyVaultBackend // Key Vault for dynamic image refresh (nil if not configured)}// SetWorkerSpec injects the cloud-neutral worker config the CP wants every@@ -97,14 +98,16 @@ func NewAzurePool(cfg AzurePoolConfig) (*AzurePool, error) {cfg: cfg,}- // Initialize Key Vault client for dynamic image refresh+ // Initialize Key Vault backend for dynamic image refresh. Empty name map ++ // empty mode prefix means we only use the Get() path; no bulk-load happens+ // from this backend (server/worker bootstrap their own backend separately+ // via internal/config/keyvault.go).if cfg.KeyVaultName != "" {- vaultURL := fmt.Sprintf("https://%s.vault.azure.net/", cfg.KeyVaultName)- kvClient, err := azsecrets.NewClient(vaultURL, cred, nil)+ be, err := secrets.NewKeyVaultBackend(cfg.KeyVaultName, nil, "")if err != nil {- log.Printf("azure: Key Vault client failed (image refresh disabled): %v", err)- } else {- pool.kvClient = kvClient+ log.Printf("azure: Key Vault backend failed (image refresh disabled): %v", err)+ } else if be != nil {+ pool.kvBackend = belog.Printf("azure: Key Vault image refresh enabled (vault=%s)", cfg.KeyVaultName)}}@@ -423,25 +426,27 @@ func (p *AzurePool) CleanupOrphanedResources(_ context.Context) (int, error) {// Satisfies the controlplane.AMIRefresher interface.// If Key Vault is not configured, returns the static image ID with no error.func (p *AzurePool) RefreshAMI(ctx context.Context) (imageID string, version string, err error) {- if p.kvClient == nil {+ if p.kvBackend == nil {p.mu.RLock()defer p.mu.RUnlock()return p.cfg.ImageID, "", nil}- // Fetch image ID from Key Vault- resp, err := p.kvClient.GetSecret(ctx, "worker-image-id", "", nil)+ newImageID, err := p.kvBackend.Get(ctx, "worker-image-id")if err != nil {return "", "", fmt.Errorf("azure: Key Vault get worker-image-id: %w", err)}- if resp.Value == nil || *resp.Value == "" {+ if newImageID == "" {return "", "", fmt.Errorf("azure: Key Vault worker-image-id is empty")}- newImageID := *resp.Value- // Fetch version- if vResp, vErr := p.kvClient.GetSecret(ctx, "worker-image-version", "", nil); vErr == nil && vResp.Value != nil {- version = *vResp.Value+ // Version is optional — a missing key here just means "version unknown",+ // not a failure. Any other error (network, perm) is also non-fatal because+ // the image-id is what actually drives the refresh.+ if v, vErr := p.kvBackend.Get(ctx, "worker-image-version"); vErr == nil {+ version = v+ } else if !errors.Is(vErr, secrets.ErrNotFound) {+ log.Printf("azure: Key Vault worker-image-version fetch failed: %v (continuing)", vErr)}p.mu.Lock()diff --git a/internal/config/keyvault.go b/internal/config/keyvault.goindex 9cd0789a..6e5316fa 100644--- a/internal/config/keyvault.go+++ b/internal/config/keyvault.go@@ -49,6 +49,18 @@ var kvMapping = map[string]string{"server-stripe-webhook-secret": "STRIPE_WEBHOOK_SECRET","server-sentry-dsn": "OPENSANDBOX_SENTRY_DSN",+ // Server-side cell config + shared secrets. These mirror the worker-* keys+ // of the same name — both sides need them, and the prefix filter loads only+ // secrets for the current mode. When dev1/prod consolidate to cell-* we can+ // drop the duplicates; for now this keeps the layout symmetric and explicit.+ "server-cell-id": "OPENSANDBOX_CELL_ID",+ "server-region": "OPENSANDBOX_REGION",+ "server-sandbox-domain": "OPENSANDBOX_SANDBOX_DOMAIN",+ "server-cf-event-endpoint": "OPENSANDBOX_CF_EVENT_ENDPOINT",+ "server-cf-event-secret": "OPENSANDBOX_CF_EVENT_SECRET",+ "server-cf-admin-secret": "OPENSANDBOX_CF_ADMIN_SECRET",+ "server-session-jwt-secret": "OPENSANDBOX_SESSION_JWT_SECRET",+// Worker secrets"worker-jwt-secret": "OPENSANDBOX_JWT_SECRET","worker-database-url": "OPENSANDBOX_DATABASE_URL",