Skip to content

Commit

Permalink
[fcmv1] Manage FCM V1 Google Service Account Key in CLI
Browse files Browse the repository at this point in the history
# Why
NOTE: DO NOT LAND THIS UNTIL GRAPHQL CHANGES LAND IN WWW

We're adding support for FCM V1 credentials, as Google is shutting down
the FCM Legacy API for sending Android notifications in June.

# How
Add new prompts and refactor existing prompts to match this schematic:

# Test Plan
Verified all functionality e2e:
  • Loading branch information
christopherwalter committed Feb 7, 2024
1 parent 0b71812 commit fc1461b
Show file tree
Hide file tree
Showing 20 changed files with 358 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {
CommonAndroidAppCredentialsFragment,
GoogleServiceAccountKeyFragment,
} from '../../../graphql/generated';
import Log from '../../../log';
import { CredentialsContext } from '../../context';
import { AppLookupParams } from '../api/GraphqlClient';

export class AssignGoogleServiceAccountKeyForFcmV1 {
constructor(private app: AppLookupParams) {}

public async runAsync(
ctx: CredentialsContext,
googleServiceAccountKey: GoogleServiceAccountKeyFragment
): Promise<CommonAndroidAppCredentialsFragment> {
const appCredentials =
await ctx.android.createOrGetExistingAndroidAppCredentialsWithBuildCredentialsAsync(
ctx.graphqlClient,
this.app
);
const updatedAppCredentials = await ctx.android.updateAndroidAppCredentialsAsync(
ctx.graphqlClient,
appCredentials,
{
googleServiceAccountKeyForFcmV1Id: googleServiceAccountKey.id,
}
);
Log.succeed(
`Google Service Account Key assigned to ${this.app.androidApplicationIdentifier} for FCM V1`
);
return updatedAppCredentials;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Log from '../../../log';
import { CredentialsContext } from '../../context';
import { AppLookupParams } from '../api/GraphqlClient';

export class AssignGoogleServiceAccountKey {
export class AssignGoogleServiceAccountKeyForSubmissions {
constructor(private app: AppLookupParams) {}

public async runAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class CreateGoogleServiceAccountKey {

Log.log(
`${chalk.bold(
'A Google Service Account JSON key is required to upload your app to Google Play Store'
'A Google Service Account JSON key is required for uploading your app to Google Play Store, and for sending Android Notifications via FCM V1.'
)}.\n` +
`If you're not sure what this is or how to create one, ${learnMore(
'https://expo.fyi/creating-google-service-account',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import nullthrows from 'nullthrows';

import { AssignGoogleServiceAccountKeyForFcmV1 } from './AssignGoogleServiceAccountKeyForFcmV1';
import { CreateGoogleServiceAccountKey } from './CreateGoogleServiceAccountKey';
import { UseExistingGoogleServiceAccountKey } from './UseExistingGoogleServiceAccountKey';
import {
CommonAndroidAppCredentialsFragment,
GoogleServiceAccountKeyFragment,
} from '../../../graphql/generated';
import Log from '../../../log';
import { promptAsync } from '../../../prompts';
import { CredentialsContext } from '../../context';
import { MissingCredentialsNonInteractiveError } from '../../errors';
import { AppLookupParams } from '../api/GraphqlClient';

export class SetUpGoogleServiceAccountKeyForFcmV1 {
constructor(private app: AppLookupParams) {}

public async runAsync(ctx: CredentialsContext): Promise<CommonAndroidAppCredentialsFragment> {
const isKeySetup = await this.isGoogleServiceAccountKeySetupAsync(ctx);
if (isKeySetup) {
Log.succeed('Google Service Account Key for FCM V1 already set up.');
return nullthrows(
await ctx.android.getAndroidAppCredentialsWithCommonFieldsAsync(
ctx.graphqlClient,
this.app
),
'androidAppCredentials cannot be null if google service account key is already set up'
);
}
if (ctx.nonInteractive) {
throw new MissingCredentialsNonInteractiveError(
'Google Service Account Keys cannot be set up in --non-interactive mode.'
);
}

const keysForAccount = await ctx.android.getGoogleServiceAccountKeysForAccountAsync(
ctx.graphqlClient,
this.app.account
);
let googleServiceAccountKey = null;
if (keysForAccount.length === 0) {
googleServiceAccountKey = await new CreateGoogleServiceAccountKey(this.app.account).runAsync(
ctx
);
} else {
googleServiceAccountKey = await this.createOrUseExistingKeyAsync(ctx);
}
return await new AssignGoogleServiceAccountKeyForFcmV1(this.app).runAsync(
ctx,
googleServiceAccountKey
);
}

private async isGoogleServiceAccountKeySetupAsync(ctx: CredentialsContext): Promise<boolean> {
const appCredentials = await ctx.android.getAndroidAppCredentialsWithCommonFieldsAsync(
ctx.graphqlClient,
this.app
);
return !!appCredentials?.googleServiceAccountKeyForFcmV1;
}

private async createOrUseExistingKeyAsync(
ctx: CredentialsContext
): Promise<GoogleServiceAccountKeyFragment> {
const { action } = await promptAsync({
type: 'select',
name: 'action',
message: 'Select the Google Service Account Key to use for FCM V1:',
choices: [
{
title: '[Choose an existing key]',
value: 'CHOOSE_EXISTING',
},
{ title: '[Upload a new service account key]', value: 'GENERATE' },
],
});

if (action === 'GENERATE') {
return await new CreateGoogleServiceAccountKey(this.app.account).runAsync(ctx);
}
return (
(await new UseExistingGoogleServiceAccountKey(this.app.account).runAsync(ctx)) ??
(await this.createOrUseExistingKeyAsync(ctx))
);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import nullthrows from 'nullthrows';

import { AssignGoogleServiceAccountKey } from './AssignGoogleServiceAccountKey';
import { AssignGoogleServiceAccountKeyForSubmissions } from './AssignGoogleServiceAccountKeyForSubmissions';
import { CreateGoogleServiceAccountKey } from './CreateGoogleServiceAccountKey';
import { UseExistingGoogleServiceAccountKey } from './UseExistingGoogleServiceAccountKey';
import {
Expand All @@ -13,7 +13,7 @@ import { CredentialsContext } from '../../context';
import { MissingCredentialsNonInteractiveError } from '../../errors';
import { AppLookupParams } from '../api/GraphqlClient';

export class SetUpGoogleServiceAccountKey {
export class SetUpGoogleServiceAccountKeyForSubmissions {
constructor(private app: AppLookupParams) {}

public async runAsync(ctx: CredentialsContext): Promise<CommonAndroidAppCredentialsFragment> {
Expand Down Expand Up @@ -46,7 +46,10 @@ export class SetUpGoogleServiceAccountKey {
} else {
googleServiceAccountKey = await this.createOrUseExistingKeyAsync(ctx);
}
return await new AssignGoogleServiceAccountKey(this.app).runAsync(ctx, googleServiceAccountKey);
return await new AssignGoogleServiceAccountKeyForSubmissions(this.app).runAsync(
ctx,
googleServiceAccountKey
);
}

private async isGoogleServiceAccountKeySetupAsync(ctx: CredentialsContext): Promise<boolean> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { AppQuery } from '../../../../graphql/queries/AppQuery';
import { testGoogleServiceAccountKeyFragment } from '../../../__tests__/fixtures-android';
import { testAppQueryByIdResponse } from '../../../__tests__/fixtures-constants';
import { createCtxMock } from '../../../__tests__/fixtures-context';
import { AssignGoogleServiceAccountKey } from '../AssignGoogleServiceAccountKey';
import { AssignGoogleServiceAccountKeyForSubmissions } from '../AssignGoogleServiceAccountKeyForSubmissions';
import { getAppLookupParamsFromContextAsync } from '../BuildCredentialsUtils';

jest.mock('../../../../graphql/queries/AppQuery');

describe(AssignGoogleServiceAccountKey, () => {
describe(AssignGoogleServiceAccountKeyForSubmissions, () => {
beforeEach(() => {
jest.mocked(AppQuery.byIdAsync).mockResolvedValue(testAppQueryByIdResponse);
});
Expand All @@ -16,7 +16,9 @@ describe(AssignGoogleServiceAccountKey, () => {
nonInteractive: false,
});
const appLookupParams = await getAppLookupParamsFromContextAsync(ctx);
const assignGoogleServiceAccountKeyAction = new AssignGoogleServiceAccountKey(appLookupParams);
const assignGoogleServiceAccountKeyAction = new AssignGoogleServiceAccountKeyForSubmissions(
appLookupParams
);
await assignGoogleServiceAccountKeyAction.runAsync(ctx, testGoogleServiceAccountKeyFragment);

// expect app credentials to be fetched/created, then updated
Expand All @@ -30,7 +32,9 @@ describe(AssignGoogleServiceAccountKey, () => {
nonInteractive: true,
});
const appLookupParams = await getAppLookupParamsFromContextAsync(ctx);
const assignGoogleServiceAccountKeyAction = new AssignGoogleServiceAccountKey(appLookupParams);
const assignGoogleServiceAccountKeyAction = new AssignGoogleServiceAccountKeyForSubmissions(
appLookupParams
);

// dont fail if users are running in non-interactive mode
await expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { testAppQueryByIdResponse } from '../../../__tests__/fixtures-constants'
import { createCtxMock } from '../../../__tests__/fixtures-context';
import { MissingCredentialsNonInteractiveError } from '../../../errors';
import { getAppLookupParamsFromContextAsync } from '../BuildCredentialsUtils';
import { SetUpGoogleServiceAccountKey } from '../SetUpGoogleServiceAccountKey';
import { SetUpGoogleServiceAccountKeyForSubmissions } from '../SetUpGoogleServiceAccountKeyForSubmissions';

jest.mock('../../../../prompts');
jest.mock('fs');
Expand All @@ -24,7 +24,7 @@ beforeEach(() => {
vol.reset();
});

describe(SetUpGoogleServiceAccountKey, () => {
describe(SetUpGoogleServiceAccountKeyForSubmissions, () => {
beforeEach(() => {
jest.mocked(AppQuery.byIdAsync).mockResolvedValue(testAppQueryByIdResponse);
});
Expand All @@ -39,7 +39,9 @@ describe(SetUpGoogleServiceAccountKey, () => {
},
});
const appLookupParams = await getAppLookupParamsFromContextAsync(ctx);
const setupGoogleServiceAccountKeyAction = new SetUpGoogleServiceAccountKey(appLookupParams);
const setupGoogleServiceAccountKeyAction = new SetUpGoogleServiceAccountKeyForSubmissions(
appLookupParams
);
await setupGoogleServiceAccountKeyAction.runAsync(ctx);

expect(ctx.android.createGoogleServiceAccountKeyAsync).not.toHaveBeenCalled();
Expand All @@ -62,7 +64,9 @@ describe(SetUpGoogleServiceAccountKey, () => {
},
});
const appLookupParams = await getAppLookupParamsFromContextAsync(ctx);
const setupGoogleServiceAccountKeyAction = new SetUpGoogleServiceAccountKey(appLookupParams);
const setupGoogleServiceAccountKeyAction = new SetUpGoogleServiceAccountKeyForSubmissions(
appLookupParams
);
await setupGoogleServiceAccountKeyAction.runAsync(ctx);

expect(ctx.android.createGoogleServiceAccountKeyAsync).toHaveBeenCalledTimes(1);
Expand All @@ -73,7 +77,9 @@ describe(SetUpGoogleServiceAccountKey, () => {
nonInteractive: true,
});
const appLookupParams = await getAppLookupParamsFromContextAsync(ctx);
const setupGoogleServiceAccountKeyAction = new SetUpGoogleServiceAccountKey(appLookupParams);
const setupGoogleServiceAccountKeyAction = new SetUpGoogleServiceAccountKeyForSubmissions(
appLookupParams
);
await expect(setupGoogleServiceAccountKeyAction.runAsync(ctx)).rejects.toThrowError(
MissingCredentialsNonInteractiveError
);
Expand Down
10 changes: 10 additions & 0 deletions packages/eas-cli/src/credentials/android/api/GraphqlClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,11 @@ export async function updateAndroidAppCredentialsAsync(
{
androidFcmId,
googleServiceAccountKeyForSubmissionsId,
googleServiceAccountKeyForFcmV1Id,
}: {
androidFcmId?: string;
googleServiceAccountKeyForSubmissionsId?: string;
googleServiceAccountKeyForFcmV1Id?: string;
}
): Promise<CommonAndroidAppCredentialsFragment> {
let updatedAppCredentials = appCredentials;
Expand All @@ -127,6 +129,14 @@ export async function updateAndroidAppCredentialsAsync(
googleServiceAccountKeyForSubmissionsId
);
}
if (googleServiceAccountKeyForFcmV1Id) {
updatedAppCredentials =
await AndroidAppCredentialsMutation.setGoogleServiceAccountKeyForFcmV1Async(
graphqlClient,
appCredentials.id,
googleServiceAccountKeyForFcmV1Id
);
}
return updatedAppCredentials;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
CommonAndroidAppCredentialsFragment,
CreateAndroidAppCredentialsMutation,
SetFcmMutation,
SetGoogleServiceAccountKeyForFcmV1Mutation,
SetGoogleServiceAccountKeyForSubmissionsMutation,
} from '../../../../../graphql/generated';
import { CommonAndroidAppCredentialsFragmentNode } from '../../../../../graphql/types/credentials/AndroidAppCredentials';
Expand Down Expand Up @@ -124,4 +125,42 @@ export const AndroidAppCredentialsMutation = {
);
return data.androidAppCredentials.setGoogleServiceAccountKeyForSubmissions;
},
async setGoogleServiceAccountKeyForFcmV1Async(
graphqlClient: ExpoGraphqlClient,
androidAppCredentialsId: string,
googleServiceAccountKeyId: string
): Promise<CommonAndroidAppCredentialsFragment> {
const data = await withErrorHandlingAsync(
graphqlClient
.mutation<SetGoogleServiceAccountKeyForFcmV1Mutation>(
gql`
mutation SetGoogleServiceAccountKeyForFcmV1Mutation(
$androidAppCredentialsId: ID!
$googleServiceAccountKeyId: ID!
) {
androidAppCredentials {
setGoogleServiceAccountKeyForFcmV1(
id: $androidAppCredentialsId
googleServiceAccountKeyId: $googleServiceAccountKeyId
) {
id
...CommonAndroidAppCredentialsFragment
}
}
}
${print(CommonAndroidAppCredentialsFragmentNode)}
`,
{
androidAppCredentialsId,
googleServiceAccountKeyId,
}
)
.toPromise()
);
assert(
data.androidAppCredentials.setGoogleServiceAccountKeyForFcmV1,
'GraphQL: `setGoogleServiceAccountKeyForFcmV1` not defined in server response'
);
return data.androidAppCredentials.setGoogleServiceAccountKeyForFcmV1;
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
exports[`print credentials prints the AndroidAppCredentials fragment 1`] = `
"Android Credentials
Project testApp
Application Identifier test.com.appPush Notifications (FCM) Key abcd...efgh
Updated 0 second agoGoogle Service Account Key For Submissions Project ID sdf.sdf.sdf
Application Identifier test.com.appPush Notifications (FCM Legacy) Key abcd...efgh
Updated 0 second agoPush Notifications (FCM V1): Google Service Account Key For FCM V1 None assigned yetSubmissions: Google Service Account Key for Play Store Submissions Project ID sdf.sdf.sdf
Client Email [email protected]
Client ID test-client-identifier
Private Key ID test-private-key-identifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import { getAppLookupParamsFromContextAsync } from '../../actions/BuildCredentia
import { displayAndroidAppCredentials } from '../printCredentials';

jest.mock('../../../../log');
jest.mock('chalk', () => ({ bold: jest.fn(log => log), cyan: { bold: jest.fn(log => log) } }));
jest.mock('chalk', () => ({
bold: jest.fn(log => log),
cyan: { bold: jest.fn(log => log) },
dim: jest.fn(log => log),
}));
jest.mock('../../../../graphql/queries/AppQuery');

mockdate.set(testLegacyAndroidFcmFragment.updatedAt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export async function detectGoogleServiceAccountKeyPathAsync(
): Promise<string | null> {
const foundFilePaths = await glob('**/*.json', {
cwd: projectDir,
ignore: ['app.json', 'package*.json', 'tsconfig.json', 'node_modules'],
ignore: ['app.json', 'package*.json', 'tsconfig.json', 'node_modules', 'google-services.json'],
});

const googleServiceFiles = foundFilePaths
Expand Down
Loading

0 comments on commit fc1461b

Please sign in to comment.