Skip to content

Update function deploy to add necessary Genkit monitoring permissions when deploying Genkit function #8636

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 9 commits into from
Jun 3, 2025

Conversation

bryanatkinson
Copy link
Contributor

Description

Genkit Monitoring requires that the service account running the Genkit code has permission to write metrics, traces and logs. This change adds those permissions to the default service account when deploying as a Firebase Function.

Scenarios Tested

  • Tested with newly added Genkit function and ensure the permissions are set on the first firebase deploy
  • Added unit tests

@bryanatkinson bryanatkinson force-pushed the ba-genkit-function-permissions branch 2 times, most recently from cd747d4 to df10513 Compare May 28, 2025 18:14
Copy link
Member

@inlined inlined left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. We only do IAM stuff on new function creation because it requires an OWNER permission rather than an editor permission and we want large teams to be able to have finer grained ACLs

  2. I'm concerned that this is granting the binding to the default compute service account, rather than the accounts that the actual functions run as (which defaults to the compute SA but is not guaranteed to be so).

For 2, it's not clear that this is technically correct. @taeold do you know why obtainDefaultComputeServiceAgentBindings is here? Is this part of the bug/feature request that we don't allow configuring Eventarc to use custom service accounts yet?

I would consider a new dedicated function that

  1. Finds all new endpoints that are Genkit callable endpoints
  2. Finds the set of service accounts they run as (defaulting to the default compute SA)
  3. Adds those role bindings for the SA to the project.

@github-project-automation github-project-automation bot moved this from Approved [PR] to Changes Requested [PR] in [Cloud] Extensions + Functions May 28, 2025
@bryanatkinson
Copy link
Contributor Author

  1. We only do IAM stuff on new function creation because it requires an OWNER permission rather than an editor permission and we want large teams to be able to have finer grained ACLs
  2. I'm concerned that this is granting the binding to the default compute service account, rather than the accounts that the actual functions run as (which defaults to the compute SA but is not guaranteed to be so).

For 2, it's not clear that this is technically correct. @taeold do you know why obtainDefaultComputeServiceAgentBindings is here? Is this part of the bug/feature request that we don't allow configuring Eventarc to use custom service accounts yet?

I would consider a new dedicated function that

  1. Finds all new endpoints that are Genkit callable endpoints
  2. Finds the set of service accounts they run as (defaulting to the default compute SA)
  3. Adds those role bindings for the SA to the project.

I have updated to move this into a separate function called in the prepare step, and to find all service accounts for new Genkit endpoints only. Let me know if this doesn't match what you were thinking.

expect(getIamStub).to.have.been.calledWith(projectNumber);
expect(setIamStub).to.have.been.calledOnce;
});
it("should not update policy if it already has necessary bindings", async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: missing newline

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

const wantEndpoints = backend.allEndpoints(want).filter(isGenkitEndpoint);
const haveEndpoints = backend.allEndpoints(have).filter(isGenkitEndpoint);
const newEndpoints = wantEndpoints.filter(
(wantE) => !haveEndpoints.find((haveE) => haveE.id === wantE.id),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically you should compare the region too. There's a helper here:

wantEndopints.filter(backend.missingEndpoint(haveEndpoints))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Updated.

return;
}

const defaultComputeServiceAgent = await gce.getDefaultServiceAccount(projectNumber);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be ideal to only calculate this if necessary. I'm not sure what this code would do on a project that has disabled the default SA.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to only make this call if the default is needed.

@@ -215,8 +288,8 @@ export async function ensureServiceAgentRoles(
iam.printManualIamConfig(requiredBindings, projectId, "functions");
utils.logLabeledBullet(
"functions",
"Could not verify the necessary IAM configuration for the following newly-integrated services: " +
`${newServices.map((service) => service.api).join(", ")}` +
"Could not verify the necessary IAM configuration for the following newly-integrated services or endpoints: " +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

endpoints is an internal term because we were experimenting with code that would eventually become App Hosting. It is meaningless to users.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted this back to just say "services"

@github-project-automation github-project-automation bot moved this from Changes Requested [PR] to Approved [PR] in [Cloud] Extensions + Functions May 30, 2025
@bryanatkinson bryanatkinson force-pushed the ba-genkit-function-permissions branch from 27729cb to ebe05b7 Compare June 3, 2025 15:18
@bryanatkinson bryanatkinson force-pushed the ba-genkit-function-permissions branch from ebe05b7 to 5f567a9 Compare June 3, 2025 18:07
@bryanatkinson bryanatkinson merged commit b103ff1 into master Jun 3, 2025
48 of 50 checks passed
@bryanatkinson bryanatkinson deleted the ba-genkit-function-permissions branch June 3, 2025 18:39
@github-project-automation github-project-automation bot moved this from Approved [PR] to Done in [Cloud] Extensions + Functions Jun 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants