Skip to content

Commit 729027d

Browse files
committed
fix(server): verify the configured Agent Platform metadata host
1 parent 70b6c60 commit 729027d

4 files changed

Lines changed: 25 additions & 12 deletions

File tree

‎apps/server/src/provider/Drivers/AntigravityDriver.test.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ const makeHarness = Effect.fn("makeAntigravityDriverHarness")(function* (
5858
readonly enabled?: boolean;
5959
readonly environment?: ReadonlyArray<{ readonly name: string; readonly value: string }>;
6060
readonly metadataServer?: boolean;
61+
readonly metadataHost?: string;
6162
} = {},
6263
) {
6364
const fs = yield* FileSystem.FileSystem;
@@ -162,7 +163,7 @@ const makeHarness = Effect.fn("makeAntigravityDriverHarness")(function* (
162163
});
163164
const metadataRequests: Array<string> = [];
164165
const httpClient = HttpClient.make((request) => {
165-
if (!request.url.startsWith("http://169.254.169.254")) {
166+
if (!request.url.startsWith(`http://${options.metadataHost ?? "169.254.169.254"}`)) {
166167
return Effect.die("Unexpected quota request");
167168
}
168169
metadataRequests.push(request.url);
@@ -421,13 +422,17 @@ it.layer(layerTest)("AntigravityDriver", (it) => {
421422
const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-antigravity-home-" });
422423
const h = yield* makeHarness({
423424
config: { authMethod: "agent-platform", gcpProject: "proj", gcpLocation: "us-central1" },
424-
environment: [{ name: "HOME", value: home }],
425+
environment: [
426+
{ name: "HOME", value: home },
427+
{ name: "GCE_METADATA_HOST", value: "metadata.internal:8080" },
428+
],
429+
metadataHost: "metadata.internal:8080",
425430
metadataServer: true,
426431
});
427432
yield* h.refresh();
428433
yield* h.refresh();
429434
expect((yield* h.instance.snapshot.getSnapshot).auth.status).toBe("authenticated");
430-
expect(h.metadataRequests).toHaveLength(1);
435+
expect(h.metadataRequests).toEqual(["http://metadata.internal:8080"]);
431436
yield* h.assertClosed;
432437
}).pipe(Effect.scoped),
433438
);

‎apps/server/src/provider/Drivers/AntigravityDriver.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ export const AntigravityDriver: ProviderDriver<
132132
);
133133
// Probe the metadata server at most once per instance, as the Go client does per process.
134134
const onGoogleCloud = yield* Effect.cached(
135-
probeGoogleMetadataServer.pipe(Effect.provideService(HttpClient.HttpClient, httpClient)),
135+
probeGoogleMetadataServer(processEnvironment.GCE_METADATA_HOST || undefined).pipe(
136+
Effect.provideService(HttpClient.HttpClient, httpClient),
137+
),
136138
);
137139
// Recheck on every use so credentials created after startup take effect.
138140
const authIssue = antigravityAuthIssue(auth, {

‎apps/server/src/provider/antigravityAuthSupport.test.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import {
3737
parseAntigravityAuthorizationUrl,
3838
prepareAntigravityProfile,
3939
probeGoogleMetadataServer,
40-
probeMetadataServer,
4140
resolveAntigravityInstanceDirectories,
4241
} from "./antigravityAuthSupport.ts";
4342

@@ -711,6 +710,16 @@ it.layer(NodeServices.layer)("Antigravity Agent Platform credentials", (it) => {
711710
CLOUDSDK_CONFIG: path.join(userHome, "missing"),
712711
GCE_METADATA_HOST: "metadata.google.internal",
713712
}),
713+
).toContain("gcloud auth application-default login");
714+
expect(
715+
yield* issueWith(
716+
{
717+
CLOUDSDK_CONFIG: path.join(userHome, "missing"),
718+
GCE_METADATA_HOST: "metadata.google.internal",
719+
},
720+
"linux",
721+
true,
722+
),
714723
).toBeNull();
715724

716725
// Windows reads %APPDATA%\gcloud and never the POSIX path.
@@ -737,7 +746,7 @@ it.layer(NodeServices.layer)("Antigravity Agent Platform credentials", (it) => {
737746
it.effect("detects the metadata server the way the Go client does", () =>
738747
Effect.gen(function* () {
739748
const probeWith = (respond: Parameters<typeof HttpClient.make>[0]) =>
740-
probeGoogleMetadataServer.pipe(
749+
probeGoogleMetadataServer().pipe(
741750
Effect.provideService(HttpClient.HttpClient, HttpClient.make(respond)),
742751
);
743752
const respondWith = (headers: Record<string, string>) =>
@@ -801,7 +810,7 @@ describe("Google metadata server probe", () => {
801810
}),
802811
);
803812

804-
const onGoogleCloud = yield* probeMetadataServer(metadataServer).pipe(
813+
const onGoogleCloud = yield* probeGoogleMetadataServer(new URL(metadataServer).host).pipe(
805814
Effect.provide(FetchHttpClient.layer),
806815
);
807816
expect({ onGoogleCloud, proxied }).toEqual({ onGoogleCloud: true, proxied: [] });

‎apps/server/src/provider/antigravityAuthSupport.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ export const antigravityAuthIssue = Effect.fn("antigravityAuthIssue")(function*
155155
if (configIssue !== null || auth.authMethod !== "agent-platform" || auth.apiKey) {
156156
return configIssue;
157157
}
158-
if (host.environment.GCE_METADATA_HOST) return null;
159158
const fs = yield* FileSystem.FileSystem;
160159
const path = yield* Path.Path;
161160
const gcloudDirectory =
@@ -178,11 +177,11 @@ export const antigravityAuthIssue = Effect.fn("antigravityAuthIssue")(function*
178177

179178
// The Go client's metadata server check. Like Go's transport it never uses a proxy, which cannot
180179
// reach this host's link-local address. Unreachable link-local addresses can hang, so bound it.
181-
export const probeMetadataServer = (url: string) =>
180+
export const probeGoogleMetadataServer = (host = "169.254.169.254") =>
182181
Effect.acquireUseRelease(
183182
Effect.try(makeDirectFetch),
184183
(direct) =>
185-
HttpClient.get(url).pipe(
184+
HttpClient.get(`http://${host}`).pipe(
186185
Effect.map((response) => response.headers["metadata-flavor"] === "Google"),
187186
Effect.provideService(FetchHttpClient.Fetch, direct.fetch),
188187
),
@@ -192,8 +191,6 @@ export const probeMetadataServer = (url: string) =>
192191
Effect.orElseSucceed(() => false),
193192
);
194193

195-
export const probeGoogleMetadataServer = probeMetadataServer("http://169.254.169.254");
196-
197194
/**
198195
* `auth.type` names the selected method so a native logout clears only that method's credentials
199196
* instead of every stored token. Never holds a credential.

0 commit comments

Comments
 (0)