Skip to content

handle null service account on CF deploy #8381

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 6 commits into from
Mar 28, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- Fixed a bug where the Admin SDK would not automatically connect to the Data Connect emulator when run in the Functions emulator. (#8379)
- Fix Cloud Functions deployment failure when service account is null. (#8381)
18 changes: 18 additions & 0 deletions src/gcp/cloudfunctions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,24 @@ describe("cloudfunctions", () => {
serviceAccountEmail: `robot@${ENDPOINT.project}.iam.gserviceaccount.com`,
});
});

it("should handle null service account", () => {
expect(
cloudfunctions.functionFromEndpoint(
{
...ENDPOINT,
httpsTrigger: {},
serviceAccount: null,
},
UPLOAD_URL,
),
).to.deep.equal({
...CLOUD_FUNCTION,
sourceUploadUrl: UPLOAD_URL,
httpsTrigger: {},
serviceAccountEmail: null,
});
});
});

describe("endpointFromFunction", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/gcp/cloudfunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@
* @param type Type of deployment - create, update, or delete.
* @param err The error returned from the operation.
*/
function functionsOpLogReject(funcName: string, type: string, err: any): void {

Check warning on line 154 in src/gcp/cloudfunctions.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
if (err?.context?.response?.statusCode === 429) {

Check warning on line 155 in src/gcp/cloudfunctions.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .context on an `any` value
utils.logWarning(
`${clc.bold(
clc.yellow("functions:"),
Expand All @@ -164,8 +164,8 @@
);
}
throw new FirebaseError(`Failed to ${type} function ${funcName}`, {
original: err,

Check warning on line 167 in src/gcp/cloudfunctions.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
status: err?.context?.response?.statusCode,

Check warning on line 168 in src/gcp/cloudfunctions.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value

Check warning on line 168 in src/gcp/cloudfunctions.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .context on an `any` value
context: { function: funcName },
});
}
Expand All @@ -187,7 +187,7 @@
{ retryCodes: [503] },
);
return res.body.uploadUrl;
} catch (err: any) {

Check warning on line 190 in src/gcp/cloudfunctions.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
logger.info(
"\n\nThere was an issue deploying your functions. Verify that your project has a Google App Engine instance setup at https://console.cloud.google.com/appengine and try again. If this issue persists, please contact support.",
);
Expand Down Expand Up @@ -222,7 +222,7 @@
type: "create",
done: false,
};
} catch (err: any) {

Check warning on line 225 in src/gcp/cloudfunctions.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
throw functionsOpLogReject(cloudFunction.name, "create", err);
}
}
Expand All @@ -248,10 +248,10 @@
policy: options.policy,
updateMask: Object.keys(options.policy).join(","),
});
} catch (err: any) {

Check warning on line 251 in src/gcp/cloudfunctions.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
throw new FirebaseError(`Failed to set the IAM Policy on the function ${options.name}`, {
original: err,

Check warning on line 253 in src/gcp/cloudfunctions.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
status: err?.context?.response?.statusCode,

Check warning on line 254 in src/gcp/cloudfunctions.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
});
}
}
Expand Down Expand Up @@ -681,7 +681,7 @@
);

proto.convertIfPresent(gcfFunction, endpoint, "serviceAccountEmail", "serviceAccount", (from) =>
proto.formatServiceAccount(from!, endpoint.project, true /* removeTypePrefix */),
!from ? null : proto.formatServiceAccount(from, endpoint.project, true /* removeTypePrefix */),
);
proto.convertIfPresent(
gcfFunction,
Expand Down
18 changes: 17 additions & 1 deletion src/gcp/cloudfunctionsv2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ describe("cloudfunctionsv2", () => {
});
});

it("should expand shorthand SA to full email", () => {
it("should expand shorthand service account to full email", () => {
expect(
cloudfunctionsv2.functionFromEndpoint({
...ENDPOINT,
Expand All @@ -436,6 +436,22 @@ describe("cloudfunctionsv2", () => {
},
});
});

it("should handle null service account", () => {
expect(
cloudfunctionsv2.functionFromEndpoint({
...ENDPOINT,
serviceAccount: null,
httpsTrigger: {},
}),
).to.deep.equal({
...CLOUD_FUNCTION_V2,
serviceConfig: {
...CLOUD_FUNCTION_V2.serviceConfig,
serviceAccountEmail: null,
},
});
});
});

describe("endpointFromFunction", () => {
Expand Down
5 changes: 4 additions & 1 deletion src/gcp/cloudfunctionsv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,10 @@ export function functionFromEndpoint(endpoint: backend.Endpoint): InputCloudFunc
endpoint,
"serviceAccountEmail",
"serviceAccount",
(from) => proto.formatServiceAccount(from!, endpoint.project, true /* removeTypePrefix */),
(from) =>
!from
? null
: proto.formatServiceAccount(from, endpoint.project, true /* removeTypePrefix */),
);
// Memory must be set because the default value of GCF gen 2 is Megabytes and
// we use mebibytes
Expand Down
Loading