-
Notifications
You must be signed in to change notification settings - Fork 389
Use ID token from metadata server when sending tasks for extensions #1812
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ import { PrefixedFirebaseError } from '../utils/error'; | |
import * as utils from '../utils/index'; | ||
import * as validator from '../utils/validator'; | ||
import { TaskOptions } from './functions-api'; | ||
import { ComputeEngineCredential } from '../app/credential-internal'; | ||
|
||
const CLOUD_TASKS_API_URL_FORMAT = 'https://cloudtasks.googleapis.com/v2/projects/{projectId}/locations/{locationId}/queues/{resourceId}/tasks'; | ||
const FIREBASE_FUNCTION_URL_FORMAT = 'https://{locationId}-{projectId}.cloudfunctions.net/{resourceId}'; | ||
|
@@ -84,7 +85,7 @@ export class FunctionsApiClient { | |
|
||
return this.getUrl(resources, CLOUD_TASKS_API_URL_FORMAT) | ||
.then((serviceUrl) => { | ||
return this.updateTaskPayload(task, resources) | ||
return this.updateTaskPayload(task, resources, extensionId) | ||
.then((task) => { | ||
const request: HttpRequestConfig = { | ||
method: 'POST', | ||
|
@@ -224,22 +225,22 @@ export class FunctionsApiClient { | |
return task; | ||
} | ||
|
||
private updateTaskPayload(task: Task, resources: utils.ParsedResource): Promise<Task> { | ||
return Promise.resolve() | ||
.then(() => { | ||
if (validator.isNonEmptyString(task.httpRequest.url)) { | ||
return task.httpRequest.url; | ||
} | ||
return this.getUrl(resources, FIREBASE_FUNCTION_URL_FORMAT); | ||
}) | ||
.then((functionUrl) => { | ||
return this.getServiceAccount() | ||
.then((account) => { | ||
task.httpRequest.oidcToken.serviceAccountEmail = account; | ||
task.httpRequest.url = functionUrl; | ||
return task; | ||
}) | ||
}); | ||
private async updateTaskPayload(task: Task, resources: utils.ParsedResource, extensionId?: string): Promise<Task> { | ||
const functionUrl = validator.isNonEmptyString(task.httpRequest.url) | ||
? task.httpRequest.url | ||
: await this.getUrl(resources, FIREBASE_FUNCTION_URL_FORMAT); | ||
task.httpRequest.url = functionUrl; | ||
// When run from a deployed extension, we should be using ComputeEngineCredentials | ||
if (extensionId && this.app.options.credential instanceof ComputeEngineCredential) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use |
||
const idToken = await this.app.options.credential.getIDToken(functionUrl); | ||
task.httpRequest.headers = { ...task.httpRequest.headers, 'Authorization': `Bearer ${idToken}` }; | ||
// Don't send httpRequest.oidcToken if we set Authorization header, or Cloud Tasks will overwrite it. | ||
delete task.httpRequest.oidcToken; | ||
} else { | ||
const account = await this.getServiceAccount(); | ||
task.httpRequest.oidcToken = { serviceAccountEmail: account }; | ||
} | ||
return task; | ||
} | ||
|
||
private toFirebaseError(err: HttpError): PrefixedFirebaseError { | ||
|
@@ -274,15 +275,19 @@ interface Error { | |
status?: string; | ||
} | ||
|
||
interface Task { | ||
/** | ||
* Task is a limited subset of https://cloud.google.com/tasks/docs/reference/rest/v2/projects.locations.queues.tasks#resource:-task | ||
* containing the relevant fields for enqueueing tasks that tirgger Cloud Functions. | ||
*/ | ||
export interface Task { | ||
// A timestamp in RFC3339 UTC "Zulu" format, with nanosecond resolution and up to nine fractional | ||
// digits. Examples: "2014-10-02T15:01:23Z" and "2014-10-02T15:01:23.045123456Z". | ||
scheduleTime?: string; | ||
// A duration in seconds with up to nine fractional digits, terminated by 's'. Example: "3.5s". | ||
dispatchDeadline?: string; | ||
httpRequest: { | ||
url: string; | ||
oidcToken: { | ||
oidcToken?: { | ||
serviceAccountEmail: string; | ||
}; | ||
// A base64-encoded string. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,7 @@ import * as mocks from '../../resources/mocks'; | |
import { getSdkVersion } from '../../../src/utils'; | ||
|
||
import { FirebaseApp } from '../../../src/app/firebase-app'; | ||
import { FirebaseFunctionsError, FunctionsApiClient } from '../../../src/functions/functions-api-client-internal'; | ||
import { FirebaseFunctionsError, FunctionsApiClient, Task } from '../../../src/functions/functions-api-client-internal'; | ||
import { HttpClient } from '../../../src/utils/api-request'; | ||
import { FirebaseAppError } from '../../../src/utils/error'; | ||
import { deepCopy } from '../../../src/utils/deep-copy'; | ||
|
@@ -65,7 +65,13 @@ describe('FunctionsApiClient', () => { | |
serviceAccountId: '[email protected]' | ||
}; | ||
|
||
const TEST_TASK_PAYLOAD = { | ||
const mockExtensionOptions = { | ||
credential: new mocks.MockComputeEngineCredential(), | ||
projectId: 'test-project', | ||
serviceAccountId: '[email protected]' | ||
}; | ||
|
||
const TEST_TASK_PAYLOAD: Task = { | ||
httpRequest: { | ||
url: `https://${DEFAULT_REGION}-${mockOptions.projectId}.cloudfunctions.net/${FUNCTION_NAME}`, | ||
oidcToken: { | ||
|
@@ -291,10 +297,15 @@ describe('FunctionsApiClient', () => { | |
}); | ||
}); | ||
|
||
it('should update the function name when the extension-id is provided', () => { | ||
it('should update the function name and set headers when the extension-id is provided', () => { | ||
app = mocks.appWithOptions(mockExtensionOptions); | ||
apiClient = new FunctionsApiClient(app); | ||
|
||
const expectedPayload = deepCopy(TEST_TASK_PAYLOAD); | ||
expectedPayload.httpRequest.url = | ||
`https://${DEFAULT_REGION}-${mockOptions.projectId}.cloudfunctions.net/ext-${EXTENSION_ID}-${FUNCTION_NAME}`; | ||
expectedPayload.httpRequest.headers['Authorization'] = 'Bearer mockIdToken'; | ||
delete expectedPayload.httpRequest.oidcToken; | ||
const stub = sinon | ||
.stub(HttpClient.prototype, 'send') | ||
.resolves(utils.responseFrom({}, 200)); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we explicitly handle the cases where
resp.text
is undefined, or is it safe to just return an empty token here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call - we probably should throw in this case (since if were requesting an id token & get an empty string, we'll almost certainly get an error later).