Skip to content

fix(functions): Use emulated credentials when connecting to the emulator #2857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 25, 2025
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
15 changes: 14 additions & 1 deletion src/functions/functions-api-client-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class FunctionsApiClient {
'invalid-argument',
'First argument passed to getFunctions() must be a valid Firebase app instance.');
}
this.httpClient = new AuthorizedHttpClient(app as FirebaseApp);
this.httpClient = new FunctionsHttpClient(app as FirebaseApp);
}
/**
* Deletes a task from a queue.
Expand Down Expand Up @@ -362,6 +362,19 @@ export class FunctionsApiClient {
}
}

/**
* Functions-specific HTTP client which uses the special "owner" token
* when communicating with the Emulator.
*/
class FunctionsHttpClient extends AuthorizedHttpClient {
protected getToken(): Promise<string> {
if (process.env.CLOUD_TASKS_EMULATOR_HOST) {
return Promise.resolve('owner');
}
return super.getToken();
}
}

interface ErrorResponse {
error?: Error;
}
Expand Down
14 changes: 10 additions & 4 deletions test/unit/functions/functions-api-client-internal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
'X-Goog-Api-Client': getMetricsHeader(),
};

const EXPECTED_HEADERS_EMULATOR = {
'X-Firebase-Client': `fire-admin-node/${getSdkVersion()}`,
'Authorization': 'Bearer owner',
'X-Goog-Api-Client': getMetricsHeader(),
};

const noProjectId = 'Failed to determine project ID. Initialize the SDK with service '
+ 'account credentials or set project ID as an app option. Alternatively, set the '
+ 'GOOGLE_CLOUD_PROJECT environment variable.';
Expand Down Expand Up @@ -498,7 +504,7 @@
expect(stub).to.have.been.calledOnce.and.calledWith({
method: 'POST',
url: CLOUD_TASKS_URL_EMULATOR,
headers: EXPECTED_HEADERS,
headers: EXPECTED_HEADERS_EMULATOR,
data: {
task: expectedPayload
}
Expand All @@ -519,7 +525,7 @@
expect(stub).to.have.been.calledOnce.and.calledWith({
method: 'POST',
url: CLOUD_TASKS_URL_EMULATOR,
headers: EXPECTED_HEADERS,
headers: EXPECTED_HEADERS_EMULATOR,
data: {
task: expectedPayload
}
Expand Down Expand Up @@ -547,7 +553,7 @@
expect(stub).to.have.been.calledOnce.and.calledWith({
method: 'POST',
url: CLOUD_TASKS_URL_EMULATOR,
headers: EXPECTED_HEADERS,
headers: EXPECTED_HEADERS_EMULATOR,
data: {
task: expectedPayload
}
Expand Down Expand Up @@ -603,7 +609,7 @@
expect(stub).to.have.been.calledWith({
method: 'DELETE',
url: CLOUD_TASKS_URL_EMULATOR.concat('/', 'mock-task'),
headers: EXPECTED_HEADERS,
headers: EXPECTED_HEADERS_EMULATOR,
});
});

Expand Down