Skip to content
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

Introduce default and privileged service accounts #184

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
6 changes: 3 additions & 3 deletions functions/src/functions/blocking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import {
beforeUserCreated,
beforeUserSignedIn,
} from 'firebase-functions/v2/identity'
import { serviceAccount } from './helpers.js'
import { privilegedServiceAccount } from './helpers.js'
import { getServiceFactory } from '../services/factory/getServiceFactory.js'

export const beforeUserCreatedFunction = beforeUserCreated(
{ serviceAccount: serviceAccount },
{ serviceAccount: privilegedServiceAccount },
async (event) => {
const userId = event.data.uid

Expand Down Expand Up @@ -70,7 +70,7 @@ export const beforeUserCreatedFunction = beforeUserCreated(
)

export const beforeUserSignedInFunction = beforeUserSignedIn(
{ serviceAccount: serviceAccount },
{ serviceAccount: privilegedServiceAccount },
async (event) => {
try {
const userService = getServiceFactory().user()
Expand Down
5 changes: 4 additions & 1 deletion functions/src/functions/enrollUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import { enrollUserInputSchema } from '@stanfordbdhg/engagehf-models'
import { https, logger } from 'firebase-functions'
import { validatedOnCall } from './helpers.js'
import { privilegedServiceAccount, validatedOnCall } from './helpers.js'
import { getServiceFactory } from '../services/factory/getServiceFactory.js'

export const enrollUser = validatedOnCall(
Expand Down Expand Up @@ -39,4 +39,7 @@ export const enrollUser = validatedOnCall(

logger.debug(`setupUser: User '${userId}' enrollment triggers finished`)
},
{
serviceAccount: privilegedServiceAccount,
},
)
7 changes: 4 additions & 3 deletions functions/src/functions/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ import {
} from 'firebase-functions/v2/https'
import { z } from 'zod'

export const serviceAccount = `cloudfunctionsserviceaccount@${process.env.GCLOUD_PROJECT}.iam.gserviceaccount.com`
export const privilegedServiceAccount = `cloudfunctionsserviceaccount@${process.env.GCLOUD_PROJECT}.iam.gserviceaccount.com`
export const defaultServiceAccount = `limited-cloudfunctions-sa@${process.env.GCLOUD_PROJECT}.iam.gserviceaccount.com`

export function validatedOnCall<Schema extends z.ZodTypeAny, Return>(
name: string,
schema: Schema,
handler: (request: CallableRequest<z.output<Schema>>) => Promise<Return>,
options: CallableOptions = {
invoker: 'public',
serviceAccount: serviceAccount,
serviceAccount: defaultServiceAccount,
},
): CallableFunction<z.input<Schema>, Promise<Return>> {
return onCall(options, async (request) => {
Expand Down Expand Up @@ -62,7 +63,7 @@ export function validatedOnRequest<Schema extends z.ZodTypeAny>(
) => void | Promise<void>,
options: https.HttpsOptions = {
invoker: 'public',
serviceAccount: serviceAccount,
serviceAccount: defaultServiceAccount,
},
): https.HttpsFunction {
return onRequest(options, async (request, response) => {
Expand Down
6 changes: 3 additions & 3 deletions functions/src/functions/onSchedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
//

import { onSchedule } from 'firebase-functions/v2/scheduler'
import { serviceAccount } from './helpers.js'
import { defaultServiceAccount } from './helpers.js'
import { getServiceFactory } from '../services/factory/getServiceFactory.js'

export const onScheduleEveryMorning = onSchedule(
{
schedule: '0 8 * * *',
timeZone: 'America/Los_Angeles',
serviceAccount: serviceAccount,
serviceAccount: defaultServiceAccount,
},
async () => getServiceFactory().trigger().everyMorning(),
)
Expand All @@ -23,7 +23,7 @@ export const onScheduleUpdateMedicationRecommendations = onSchedule(
{
schedule: '0 0 * * *',
timeZone: 'America/Los_Angeles',
serviceAccount: serviceAccount,
serviceAccount: defaultServiceAccount,
},
async () =>
getServiceFactory().trigger().updateRecommendationsForAllPatients(),
Expand Down
Loading