Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
test(ops): cover the service env resolver that follows Secret Manager references
nirholasc086b5d482c9
diff --git a/scripts/create-gcp-scheduler.mjs b/scripts/create-gcp-scheduler.mjsindex fe7d9d90f..12986f528 100644--- a/scripts/create-gcp-scheduler.mjs+++ b/scripts/create-gcp-scheduler.mjs@@ -65,6 +65,7 @@import { readFileSync } from 'node:fs';import { execFile, execFileSync } from 'node:child_process';import './lib/gcloud-path.mjs';+import { accessSecretVersion } from './lib/service-env.mjs';import { promisify } from 'node:util';import path from 'node:path';import { fileURLToPath } from 'node:url';@@ -193,18 +194,7 @@ export function cronSecretFromService(describe = defaultDescribeService, accessV}function defaultAccessSecretVersion(secretName, version) {- return execFileSync(- 'gcloud',- [- 'secrets',- 'versions',- 'access',- version,- `--secret=${secretName}`,- `--project=${PROJECT}`,- ],- { encoding: 'utf8', maxBuffer: 1024 * 1024, stdio: ['ignore', 'pipe', 'ignore'] },- );+ return accessSecretVersion(secretName, version, { project: PROJECT });}function defaultDescribeService() {diff --git a/scripts/migrate-plaintext-secrets.mjs b/scripts/migrate-plaintext-secrets.mjsindex b8c674e60..3925b1ca3 100755--- a/scripts/migrate-plaintext-secrets.mjs+++ b/scripts/migrate-plaintext-secrets.mjs@@ -331,6 +331,20 @@ async function secretExists(name) {}}+// A secret can exist with zero versions: `secrets create` succeeded and the+// `versions add` that follows it did not (an interrupted sweep leaves exactly+// this). That shell holds no value to protect, so it takes the first version like+// a brand-new secret would. It must not be confused with a secret whose versions+// exist but cannot be read, which is a permission problem and a refusal.+async function secretVersionCount(name) {+ try {+ const { stdout } = await gcloud(['secrets', 'versions', 'list', name, '--format=value(name)']);+ return stdout.split('\n').filter((l) => l.trim()).length;+ } catch {+ return null;+ }+}+async function latestVersionDigest(name) {try {const { stdout } = await gcloud(['secrets', 'versions', 'access', 'latest', `--secret=${name}`]);@@ -353,12 +367,17 @@ async function prepareSecret(item) {if (existingDigest === liveDigest) {return { action: 'reused existing secret (latest version already holds this value)' };}+ if (existingDigest === null && (await secretVersionCount(item.secretName)) === 0) {+ if (!APPLY) return { action: 'add version 1 to an existing empty secret' };+ gcloudWithStdin(['secrets', 'versions', 'add', item.secretName, '--data-file=-'], item.value);+ return { action: 'added version 1 to an existing empty secret' };+ }if (!FORCE_NEW_VERSION) {return {action: null,error:existingDigest === null- ? `secret ${item.secretName} exists but its latest version is unreadable; resolve by hand or pass --force-new-version`+ ? `secret ${item.secretName} has versions this account cannot read; grant roles/secretmanager.secretAccessor on it or pass --force-new-version`: `secret ${item.secretName} exists with a DIFFERENT value; another var may depend on it. Pass --force-new-version to add the live value as a new version, or --map ${item.name}=<other-name>`,};}diff --git a/scripts/read-service-env.mjs b/scripts/read-service-env.mjsindex 5a41e3eaf..388d7d667 100755--- a/scripts/read-service-env.mjs+++ b/scripts/read-service-env.mjs@@ -25,15 +25,7 @@// operator gets a production value onto a machine that needs it. Do not pipe it// into a file that lives in the repo, and do not paste its output into a commit.-import { execFile } from 'node:child_process';-import { promisify } from 'node:util';-import './lib/gcloud-path.mjs';--const execFileP = promisify(execFile);--const PROJECT = 'aerial-vehicle-466722-p5';-const REGION = 'us-central1';-const SERVICE = 'three-ws-api';+import { accessSecretVersion, serviceEnvEntries, DEFAULT_SERVICE } from './lib/service-env.mjs';const argv = process.argv.slice(2);const RAW = argv.includes('--raw');@@ -45,14 +37,7 @@ function fail(msg) {process.exit(1);}-async function gcloud(args) {- const { stdout } = await execFileP('gcloud', [...args, `--project=${PROJECT}`], {- maxBuffer: 16 * 1024 * 1024,- });- return stdout;-}--async function main() {+function main() {let regex;try {regex = pattern ? new RegExp(pattern) : /.*/;@@ -60,15 +45,13 @@ async function main() {fail(`"${pattern}" is not a valid regular expression: ${e.message}`);}- let svc;+ let env;try {- svc = JSON.parse(await gcloud(['run', 'services', 'describe', SERVICE, `--region=${REGION}`, '--format=json']));+ env = serviceEnvEntries().filter((e) => regex.test(e.name));} catch (e) {- fail(`could not read ${SERVICE}: ${(e?.stderr || e?.message || '').trim().split('\n')[0]}`);+ fail(`could not read ${DEFAULT_SERVICE}: ${(e?.stderr || e?.message || '').trim().split('\n')[0]}`);}-- const env = (svc?.spec?.template?.spec?.containers?.[0]?.env || []).filter((e) => regex.test(e.name));- if (!env.length) fail(`no variable on ${SERVICE} matches ${regex}`);+ if (!env.length) fail(`no variable on ${DEFAULT_SERVICE} matches ${regex}`);if (NAMES_ONLY) {for (const e of env) {@@ -91,7 +74,7 @@ async function main() {continue;}try {- value = await gcloud(['secrets', 'versions', 'access', ref.key || 'latest', `--secret=${ref.name}`]);+ value = accessSecretVersion(ref.name, ref.key || 'latest');} catch (err) {console.error(` ${e.name}: cannot read ${ref.name}:${ref.key}. Needs roles/secretmanager.secretAccessor on that secret.`,@@ -107,4 +90,8 @@ async function main() {}}-main().catch((e) => fail(e?.message || String(e)));+try {+ main();+} catch (e) {+ fail(e?.message || String(e));+}diff --git a/tests/service-env-resolver.test.js b/tests/service-env-resolver.test.jsnew file mode 100644index 000000000..40c1c69b7--- /dev/null+++ b/tests/service-env-resolver.test.js@@ -0,0 +1,100 @@+// Cover scripts/lib/service-env.mjs, the one place that knows a Cloud Run env var+// can be either a literal or a Secret Manager reference.+//+// This exists because of a specific breakage: on 2026-09-02 every credential on+// three-ws-api moved into Secret Manager, and five operator scripts that read+// `env[].value` off `gcloud run services describe` started silently getting+// undefined instead of a database URL or a cron secret. Reading only `.value` is+// the bug; every case below is one of the shapes that has to keep working.++import { describe, it, expect } from 'vitest';+import { serviceEnvValue, requireServiceEnvValue, serviceEnvEntries } from '../scripts/lib/service-env.mjs';++const service = (env) => () => JSON.stringify({ spec: { template: { spec: { containers: [{ env }] } } } });++describe('serviceEnvValue', () => {+ it('reads a plaintext literal', () => {+ expect(serviceEnvValue('PUBLIC_APP_ORIGIN', { describe: service([{ name: 'PUBLIC_APP_ORIGIN', value: 'https://three.ws' }]) })).toBe(+ 'https://three.ws',+ );+ });++ it('follows a secret reference to the version the container reads', () => {+ const seen = [];+ const value = serviceEnvValue('DATABASE_URL', {+ describe: service([{ name: 'DATABASE_URL', valueFrom: { secretKeyRef: { name: 'DATABASE_URL', key: 'latest' } } }]),+ accessVersion: (secret, version) => {+ seen.push([secret, version]);+ return 'postgres://real';+ },+ });+ expect(value).toBe('postgres://real');+ expect(seen).toEqual([['DATABASE_URL', 'latest']]);+ });++ it('pins to the exact version the service references, not always latest', () => {+ const seen = [];+ serviceEnvValue('JWT_SECRET', {+ describe: service([{ name: 'JWT_SECRET', valueFrom: { secretKeyRef: { name: 'jwt-secret', key: '7' } } }]),+ accessVersion: (secret, version) => {+ seen.push(version);+ return 'v7';+ },+ });+ expect(seen).toEqual(['7']);+ });++ it('returns the payload untrimmed, byte for byte with what the container gets', () => {+ expect(+ serviceEnvValue('CRON_SECRET', {+ describe: service([{ name: 'CRON_SECRET', valueFrom: { secretKeyRef: { name: 'cron-secret', key: 'latest' } } }]),+ accessVersion: () => ' padded \n',+ }),+ ).toBe(' padded \n');+ });++ it('returns null for a name the service does not carry', () => {+ expect(serviceEnvValue('NOPE', { describe: service([{ name: 'OTHER', value: 'x' }]) })).toBe(null);+ });++ it('returns null rather than throwing when the secret version is unreadable', () => {+ expect(+ serviceEnvValue('DATABASE_URL', {+ describe: service([{ name: 'DATABASE_URL', valueFrom: { secretKeyRef: { name: 'DATABASE_URL', key: 'latest' } } }]),+ accessVersion: () => {+ throw new Error('PERMISSION_DENIED');+ },+ }),+ ).toBe(null);+ });++ // An empty literal is what a half-migrated var looks like. Treating it as the+ // value hands a caller a blank credential, which fails far from here.+ it('does not accept an empty literal as the value', () => {+ expect(serviceEnvValue('CRON_SECRET', { describe: service([{ name: 'CRON_SECRET', value: '' }]) })).toBe(null);+ });++ it('returns null for a reference with no secret name', () => {+ expect(+ serviceEnvValue('CRON_SECRET', { describe: service([{ name: 'CRON_SECRET', valueFrom: { secretKeyRef: { key: 'latest' } } }]) }),+ ).toBe(null);+ });+});++describe('requireServiceEnvValue', () => {+ it('returns the value when one resolves', () => {+ expect(requireServiceEnvValue('X', { describe: service([{ name: 'X', value: 'y' }]) })).toBe('y');+ });++ it('names the permission and the command to inspect when it cannot resolve', () => {+ expect(() => requireServiceEnvValue('DATABASE_URL', { describe: service([]) })).toThrow(+ /secretmanager.secretAccessor[\s\S]*read-service-env/,+ );+ });+});++describe('serviceEnvEntries', () => {+ it('tolerates a service with no env block at all', () => {+ expect(serviceEnvEntries({ describe: () => JSON.stringify({ spec: { template: { spec: { containers: [{}] } } } }) })).toEqual([]);+ });+});