-
Notifications
You must be signed in to change notification settings - Fork 375
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
Try to use OIDC token from metadata server as first option #1787
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,4 +44,13 @@ export interface Credential { | |
* @returns A Google OAuth2 access token object. | ||
*/ | ||
getAccessToken(): Promise<GoogleOAuthAccessToken>; | ||
|
||
/** | ||
* Returns an encoded OIDC token used to authenticate calls to | ||
* private Cloud Functions or other compute services. | ||
* | ||
* @param audience The URL this token will be used to call. | ||
* @returns A base64 encoded OIDC token. | ||
*/ | ||
getIDToken(audience: string): Promise<string>; | ||
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. It ok to add to this interface? or is this a public interface/would require a API review? |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,7 +62,7 @@ export class FunctionsApiClient { | |
* @param extensionId - Optional canonical ID of the extension. | ||
* @param opts - Optional options when enqueuing a new task. | ||
*/ | ||
public enqueue(data: any, functionName: string, extensionId?: string, opts?: TaskOptions): Promise<void> { | ||
public async enqueue(data: any, functionName: string, extensionId?: string, opts?: TaskOptions): Promise<void> { | ||
if (!validator.isNonEmptyString(functionName)) { | ||
throw new FirebaseFunctionsError( | ||
'invalid-argument', 'Function name must be a non empty string'); | ||
|
@@ -81,10 +81,10 @@ export class FunctionsApiClient { | |
if (typeof extensionId !== 'undefined' && validator.isNonEmptyString(extensionId)) { | ||
resources.resourceId = `ext-${extensionId}-${resources.resourceId}`; | ||
} | ||
|
||
return this.getUrl(resources, CLOUD_TASKS_API_URL_FORMAT) | ||
.then((serviceUrl) => { | ||
return this.updateTaskPayload(task, resources) | ||
return this.updateTaskPayload(task, resources, ) | ||
.then((task) => { | ||
const request: HttpRequestConfig = { | ||
method: 'POST', | ||
|
@@ -224,22 +224,19 @@ 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): Promise<Task> { | ||
const functionUrl = validator.isNonEmptyString(task.httpRequest.url) | ||
? task.httpRequest.url | ||
: await this.getUrl(resources, FIREBASE_FUNCTION_URL_FORMAT); | ||
task.httpRequest.url = functionUrl; | ||
try { | ||
const idToken = await this.app.options.credential.getIDToken(functionUrl) | ||
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. I think for Extensions use-case we still need a way to pass the service account. Please ignore if you plan to address that in a separate PR. 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. Turns out that within Cloud Functions, default/ is just an alias for whatever service account the function is running as. For extensions, this is the service account we'd want to pass in anyways |
||
task.httpRequest.headers = {...task.httpRequest.headers, "Authorization": `Bearer ${idToken}`} | ||
} catch (err: any) { | ||
const serviceAccount = await this.getServiceAccount(); | ||
task.httpRequest.oidcToken.serviceAccountEmail = serviceAccount; | ||
} | ||
return task; | ||
} | ||
|
||
private toFirebaseError(err: HttpError): PrefixedFirebaseError { | ||
|
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.
This looks identical to
GOOGLE_METADATA_SERVICE_TOKEN_PATH
. I think we want to replacedefault
with the service account email.http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/${saEmail}/identity?audience=${audience}
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.
Ahhhh, this was a typo - it should be /identity, not /token