feat: auto-sync Antigravity OAuth auth to ephemeral Sprites
- Add server/antigravity-auth.ts to resolve Antigravity OAuth credentials from OpenBao (ANTIGRAVITY_OAUTH_TOKEN / AGY_OAUTH_TOKEN), env, local OS keyring (secret-tool / macOS security), or local token file and stream them over stdin (umask 077, mode 0600, atomic replace) into ~/.gemini/antigravity-cli/antigravity-oauth-token on Sprites - Cache full OpenBao KV secret map in OpenBaoClient and add getOptionalSecretKey for zero-extra-request lookup - Wire sync into SpritesManager.ensureSprite and restart Sprite Paseo daemon worker in ensureSpriteHost when credentials change - Add syncAntigravityAuth setting (default true) with Settings UI toggle - Add Antigravity to PROVIDER_CHOICES and prioritize it in PREFERRED_PROVIDERS
This commit is contained in:
1 parent
0b923ff019
commit
53f3e1b621
8 files changed
+404
-42
No files matched your search
+80
-37
@@ -41,14 +41,13 @@ interface PendingFlowInternal {
|
||||
abortController: AbortController;
|
||||
}
|
||||
|
||||
interface CachedSecret {
|
||||
interface CachedSecretMap {
|
||||
cacheKey: string;
|
||||
token: string;
|
||||
data: Record<string, string>;
|
||||
pulledAt: string;
|
||||
expiresAtMs: number;
|
||||
}
|
||||
|
||||
const SECRET_CACHE_TTL_MS = 5 * 60_000;
|
||||
const SECRET_CACHE_TTL_COLD_MS = 30 * 60_000;
|
||||
const DISCOVERY_CACHE_TTL_MS = 60 * 60_000;
|
||||
const REQUEST_TIMEOUT_MS = 8_000;
|
||||
@@ -138,15 +137,32 @@ function extractBaoError(body: Record<string, unknown>, status: number): string
|
||||
return `OpenBao returned HTTP ${status}`;
|
||||
}
|
||||
|
||||
function pickFirstString(data: Record<string, string>, candidateKeys: readonly string[]): string {
|
||||
for (const key of candidateKeys) {
|
||||
const value = data[key];
|
||||
if (typeof value === "string" && value.trim()) {
|
||||
return value.trim();
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
function primaryCandidateKeys(settings: FlySpritesSettings): string[] {
|
||||
const preferredKey = settings.openbaoSecretKey.trim() || "FLY_TOKEN";
|
||||
return preferredKey === "FLY_TOKEN"
|
||||
? ["FLY_TOKEN", "SPRITES_TOKEN", "FLY_API_TOKEN"]
|
||||
: [preferredKey];
|
||||
}
|
||||
|
||||
export class OpenBaoClient {
|
||||
private readonly getSettings: () => Promise<FlySpritesSettings>;
|
||||
private sessionLoaded = false;
|
||||
private session: PersistedOpenBaoSession | null = null;
|
||||
private cachedSecret: CachedSecret | null = null;
|
||||
private cachedSecret: CachedSecretMap | null = null;
|
||||
private discoveryCache = new Map<string, { expiresAtMs: number; value: OidcDiscovery }>();
|
||||
private pendingFlow: PendingFlowInternal | null = null;
|
||||
private lastError: string | null = null;
|
||||
private pullInFlight: Promise<string> | null = null;
|
||||
private pullInFlight: Promise<Record<string, string> | null> | null = null;
|
||||
|
||||
constructor(getSettings: () => Promise<FlySpritesSettings>) {
|
||||
this.getSettings = getSettings;
|
||||
@@ -353,14 +369,16 @@ export class OpenBaoClient {
|
||||
settings.openbaoAddr.trim().replace(/\/+$/, ""),
|
||||
settings.openbaoKvMount.trim().replace(/^\/+|\/+$/g, "") || "secret",
|
||||
normalizeKvPath(settings.openbaoSecretPath, settings.openbaoKvMount),
|
||||
settings.openbaoSecretKey.trim() || "FLY_TOKEN",
|
||||
].join("|");
|
||||
}
|
||||
|
||||
async getSecretToken(forceRefresh = false, settingsOverride?: FlySpritesSettings): Promise<string> {
|
||||
private async getSecretData(
|
||||
forceRefresh = false,
|
||||
settingsOverride?: FlySpritesSettings,
|
||||
): Promise<Record<string, string> | null> {
|
||||
const settings = settingsOverride ?? (await this.getSettings());
|
||||
if (!settings.openbaoEnabled) {
|
||||
return "";
|
||||
return null;
|
||||
}
|
||||
|
||||
const cacheKey = this.secretCacheKey(settings);
|
||||
@@ -370,14 +388,14 @@ export class OpenBaoClient {
|
||||
this.cachedSecret.cacheKey === cacheKey &&
|
||||
this.cachedSecret.expiresAtMs > Date.now()
|
||||
) {
|
||||
return this.cachedSecret.token;
|
||||
return this.cachedSecret.data;
|
||||
}
|
||||
|
||||
if (!forceRefresh && this.pullInFlight) {
|
||||
return await this.pullInFlight;
|
||||
}
|
||||
|
||||
const work = this.fetchSecretFromOpenBao(settings, cacheKey);
|
||||
const work = this.fetchSecretMapFromOpenBao(settings, cacheKey);
|
||||
this.pullInFlight = work;
|
||||
try {
|
||||
return await work;
|
||||
@@ -388,20 +406,52 @@ export class OpenBaoClient {
|
||||
}
|
||||
}
|
||||
|
||||
private async fetchSecretFromOpenBao(
|
||||
async getSecretToken(forceRefresh = false, settingsOverride?: FlySpritesSettings): Promise<string> {
|
||||
const settings = settingsOverride ?? (await this.getSettings());
|
||||
const data = await this.getSecretData(forceRefresh, settings);
|
||||
if (!data) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const found = pickFirstString(data, primaryCandidateKeys(settings));
|
||||
if (!found) {
|
||||
const kvMount = settings.openbaoKvMount.trim().replace(/^\/+|\/+$/g, "") || "secret";
|
||||
const secretPath = normalizeKvPath(settings.openbaoSecretPath, kvMount);
|
||||
const preferredKey = settings.openbaoSecretKey.trim() || "FLY_TOKEN";
|
||||
this.lastError = `Key "${preferredKey}" not found at ${kvMount}/${secretPath}.`;
|
||||
return "";
|
||||
}
|
||||
|
||||
this.lastError = null;
|
||||
return found;
|
||||
}
|
||||
|
||||
async getOptionalSecretKey(
|
||||
candidateKeys: readonly string[],
|
||||
forceRefresh = false,
|
||||
settingsOverride?: FlySpritesSettings,
|
||||
): Promise<string> {
|
||||
const data = await this.getSecretData(forceRefresh, settingsOverride);
|
||||
if (!data) {
|
||||
return "";
|
||||
}
|
||||
return pickFirstString(data, candidateKeys);
|
||||
}
|
||||
|
||||
private async fetchSecretMapFromOpenBao(
|
||||
settings: FlySpritesSettings,
|
||||
cacheKey: string,
|
||||
): Promise<string> {
|
||||
): Promise<Record<string, string> | null> {
|
||||
let sess = await this.ensureValidBaoToken(settings);
|
||||
if (!sess) {
|
||||
return "";
|
||||
return null;
|
||||
}
|
||||
|
||||
const kvMount = settings.openbaoKvMount.trim().replace(/^\/+|\/+$/g, "") || "secret";
|
||||
const secretPath = normalizeKvPath(settings.openbaoSecretPath, kvMount);
|
||||
if (!secretPath) {
|
||||
this.lastError = "OpenBao secret path is empty.";
|
||||
return "";
|
||||
return null;
|
||||
}
|
||||
|
||||
const encodedPath = secretPath
|
||||
@@ -431,39 +481,31 @@ export class OpenBaoClient {
|
||||
if (res.status === 401 || res.status === 403) {
|
||||
await this.saveSession(null);
|
||||
}
|
||||
return "";
|
||||
return null;
|
||||
}
|
||||
|
||||
const outerData = (res.body.data ?? {}) as Record<string, unknown>;
|
||||
const secretData = (outerData.data ?? {}) as Record<string, unknown>;
|
||||
const preferredKey = settings.openbaoSecretKey.trim() || "FLY_TOKEN";
|
||||
const candidateKeys =
|
||||
preferredKey === "FLY_TOKEN"
|
||||
? ["FLY_TOKEN", "SPRITES_TOKEN", "FLY_API_TOKEN"]
|
||||
: [preferredKey];
|
||||
|
||||
let found = "";
|
||||
for (const key of candidateKeys) {
|
||||
const value = secretData[key];
|
||||
if (typeof value === "string" && value.trim()) {
|
||||
found = value.trim();
|
||||
break;
|
||||
const rawSecretData = (outerData.data ?? {}) as Record<string, unknown>;
|
||||
const normalized: Record<string, string> = {};
|
||||
for (const [key, value] of Object.entries(rawSecretData)) {
|
||||
if (typeof value === "string") {
|
||||
normalized[key] = value;
|
||||
} else if (value && typeof value === "object") {
|
||||
try {
|
||||
normalized[key] = JSON.stringify(value);
|
||||
} catch {
|
||||
// Ignore non-serializable values.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
this.lastError = `Key "${preferredKey}" not found at ${kvMount}/${secretPath}.`;
|
||||
return "";
|
||||
}
|
||||
|
||||
this.lastError = null;
|
||||
this.cachedSecret = {
|
||||
cacheKey,
|
||||
token: found,
|
||||
data: normalized,
|
||||
pulledAt: new Date().toISOString(),
|
||||
expiresAtMs: Date.now() + SECRET_CACHE_TTL_COLD_MS,
|
||||
};
|
||||
return found;
|
||||
return normalized;
|
||||
}
|
||||
|
||||
private serializePendingFlow(): OpenBaoPendingAuth | null {
|
||||
@@ -490,7 +532,8 @@ export class OpenBaoClient {
|
||||
const hasFreshToken = Boolean(
|
||||
this.cachedSecret &&
|
||||
this.cachedSecret.cacheKey === cacheKey &&
|
||||
this.cachedSecret.expiresAtMs > Date.now(),
|
||||
this.cachedSecret.expiresAtMs > Date.now() &&
|
||||
pickFirstString(this.cachedSecret.data, primaryCandidateKeys(settings)),
|
||||
);
|
||||
|
||||
return {
|
||||
|
||||
Reference in new issue
Block a user