Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- Improved Auth emulator startup time by compiling validation schemas lazily. (#11176)
- Updated the Firebase SQL Connect local toolkit to v3.4.26, which includes the following changes:
- Internal improvements and bug fixes.
- Fixed the emulators startup message showing 127.0.0.1 for the Emulator UI and Hub when they listen on 0.0.0.0; it now also prints the address they listen on. (#6222)
Expand Down
13 changes: 13 additions & 0 deletions src/emulator/auth/rest.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { expect } from "chai";
import { expectStatusCode, registerTenant, registerUser } from "./testing/helpers";
import { describeAuthEmulator, PROJECT_ID } from "./testing/setup";
import { createApp } from "./server";
import { SingleProjectMode } from "./index";

describeAuthEmulator("REST API mapping", ({ authApi }) => {
it("should respond to status checks", async () => {
Expand All @@ -8,7 +10,7 @@
.get("/")
.then((res) => {
expectStatusCode(200, res);
expect(res.body.authEmulator).to.be.an("object");

Check warning on line 13 in src/emulator/auth/rest.spec.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Unsafe member access .authEmulator on an `any` value
});
});

Expand All @@ -24,8 +26,8 @@
// Some clients (including older browsers and jsdom) won't accept '*' as a
// wildcard, so we need to reflect Origin and Access-Control-Request-Headers.
// https://github.com/firebase/firebase-tools/issues/3200
expect(res.header["access-control-allow-origin"]).to.eql("example.com");

Check warning on line 29 in src/emulator/auth/rest.spec.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Unsafe member access ["access-control-allow-origin"] on an `any` value
expect((res.header["access-control-allow-headers"] as string).split(",")).to.have.members([

Check warning on line 30 in src/emulator/auth/rest.spec.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Unsafe member access ["access-control-allow-headers"] on an `any` value
"Authorization",
"X-Client-Version",
"X-Whatever-Header",
Expand All @@ -33,7 +35,7 @@

// Check that access-control-allow-private-network = true
// Enables accessing locahost when site is exposed via tunnel see https://github.com/firebase/firebase-tools/issues/4227
expect(res.header["access-control-allow-private-network"]).to.eql("true");

Check warning on line 38 in src/emulator/auth/rest.spec.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Unsafe member access ["access-control-allow-private-network"] on an `any` value
});
});

Expand All @@ -46,7 +48,7 @@
.send({ email: "bob@example.com", requestType, returnOobLink: true })
.then((res) => {
expectStatusCode(200, res);
expect(res.body.oobLink).to.include("mode=signIn");

Check warning on line 51 in src/emulator/auth/rest.spec.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Unsafe member access .oobLink on an `any` value
});
});

Expand All @@ -59,7 +61,7 @@
.send({ email: "bob@example.com", requestType, returnOobLink: true })
.then((res) => {
expectStatusCode(200, res);
expect(res.body.oobLink).to.include("mode=signIn");

Check warning on line 64 in src/emulator/auth/rest.spec.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Unsafe member access .oobLink on an `any` value
});
});

Expand All @@ -74,7 +76,7 @@
.then((res) => {
expectStatusCode(400, res);
// It should pass JSON schema validation and get into handler logic.
expect(res.body.error.message).to.equal("USER_NOT_FOUND");

Check warning on line 79 in src/emulator/auth/rest.spec.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Unsafe member access .error on an `any` value
});
});
});
Expand All @@ -89,7 +91,7 @@
.send({ returnSecureToken: true })
.then((res) => {
expectStatusCode(403, res);
expect(res.body.error).to.have.property("status").equal("PERMISSION_DENIED");

Check warning on line 94 in src/emulator/auth/rest.spec.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Unsafe member access .error on an `any` value
});
});

Expand Down Expand Up @@ -126,7 +128,7 @@
.send({ returnSecureToken: true })
.then((res) => {
expectStatusCode(403, res);
expect(res.body.error).to.have.property("status").equal("PERMISSION_DENIED");

Check warning on line 131 in src/emulator/auth/rest.spec.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Unsafe member access .error on an `any` value
});
});

Expand Down Expand Up @@ -190,7 +192,7 @@
})
.then((res) => {
expectStatusCode(400, res);
expect(res.body.error)

Check warning on line 195 in src/emulator/auth/rest.spec.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Unsafe member access .error on an `any` value
.to.have.property("message")
.equals(
"INSUFFICIENT_PERMISSION : Only authenticated requests can specify target_project_id.",
Expand Down Expand Up @@ -255,3 +257,14 @@
});
});
});

describe("Auth Emulator OpenAPI schema validation", function (this: Mocha.Suite) {
this.timeout(60000);

it("should compile all OpenAPI validation schemas eagerly without error", async () => {
const app = await createApp("test-project", SingleProjectMode.NO_WARNING, undefined, {
lazyCompileValidationSchemas: false,
});
expect(app).to.be.a("function");
});
});
11 changes: 10 additions & 1 deletion src/emulator/auth/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const API_SPEC_PATH = "/emulator/openapi.json";
const AUTH_HEADER_PREFIX = "bearer ";
const SERVICE_ACCOUNT_TOKEN_PREFIX = "ya29.";

function specForRouter(): OpenAPIObject {
export function specForRouter(): OpenAPIObject {
const paths: PathsObject = {};
Object.entries(apiSpec.paths).forEach(([path, pathObj]) => {
const servers = (pathObj as { servers?: { url: string }[] }).servers ?? apiSpec.servers;
Expand Down Expand Up @@ -105,6 +105,10 @@ function specWithEmulatorServer(protocol: string, host: string | undefined): Ope
}
}

export interface CreateAppOptions {
lazyCompileValidationSchemas?: boolean;
}

/**
* Create an Express app that serves Auth Emulator APIs.
*
Expand All @@ -114,11 +118,13 @@ function specWithEmulatorServer(protocol: string, host: string | undefined): Ope
* accepted by the target endpoint), it assumes ALL keys / tokens map to this
* default project ID.
* @param projectStateForId a map from projectId to state, injectable for tests
* @param options configuration options for creating the app, e.g. schema compilation
*/
export async function createApp(
defaultProjectId: string,
singleProjectMode = SingleProjectMode.NO_WARNING,
projectStateForId = new Map<string, AgentProjectState>(),
options: CreateAppOptions = {},
): Promise<express.Express> {
const app = express();
app.set("json spaces", 2);
Expand Down Expand Up @@ -227,6 +233,9 @@ export async function createApp(
);
};
const apis = await exegesisExpress.middleware(specForRouter(), {
// Eager ajv compilation of the entire 386 KB Identity Toolkit spec takes ~10-20s.
// Compiling validation schemas lazily on first use cuts startup time and speeds up unit tests.
lazyCompileValidationSchemas: options.lazyCompileValidationSchemas ?? true,
controllers: { auth: toExegesisController(authOperations, getProjectStateById) },
authenticators: {
apiKeyQuery: apiKeyAuthenticator,
Expand Down
Loading