/
githubConnections.ts
445 lines (410 loc) · 14.4 KB
/
githubConnections.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import * as clc from "colorette";
import * as devConnect from "../gcp/devConnect";
import * as rm from "../gcp/resourceManager";
import * as poller from "../operation-poller";
import * as utils from "../utils";
import { FirebaseError } from "../error";
import { promptOnce } from "../prompt";
import { getProjectNumber } from "../getProjectNumber";
import { developerConnectOrigin } from "../api";
import * as fuzzy from "fuzzy";
import * as inquirer from "inquirer";
interface ConnectionNameParts {
projectId: string;
location: string;
id: string;
}
// Note: This does not match the sentinel oauth connection
const APPHOSTING_CONN_PATTERN = /.+\/apphosting-github-conn-.+$/;
const APPHOSTING_OAUTH_CONN_NAME = "apphosting-github-oauth";
const CONNECTION_NAME_REGEX =
/^projects\/(?<projectId>[^\/]+)\/locations\/(?<location>[^\/]+)\/connections\/(?<id>[^\/]+)$/;
/**
* Exported for unit testing.
*
* Example: /projects/my-project/locations/us-central1/connections/my-connection-id => {
* projectId: "my-project",
* location: "us-central1",
* id: "my-connection-id",
* }
*/
export function parseConnectionName(name: string): ConnectionNameParts | undefined {
const match = CONNECTION_NAME_REGEX.exec(name);
if (!match || typeof match.groups === undefined) {
return;
}
const { projectId, location, id } = match.groups as unknown as ConnectionNameParts;
return {
projectId,
location,
id,
};
}
const devConnectPollerOptions: Omit<poller.OperationPollerOptions, "operationResourceName"> = {
apiOrigin: developerConnectOrigin(),
apiVersion: "v1",
masterTimeout: 25 * 60 * 1_000,
maxBackoff: 10_000,
};
/**
* Exported for unit testing.
*
* Example usage:
* extractRepoSlugFromURI("https://github.com/user/repo.git") => "user/repo"
*/
export function extractRepoSlugFromUri(cloneUri: string): string | undefined {
const match = /github.com\/(.+).git/.exec(cloneUri);
if (!match) {
return undefined;
}
return match[1];
}
/**
* Exported for unit testing.
*
* Generates a repository ID.
* The relation is 1:* between Developer Connect Connection and GitHub Repositories.
*/
export function generateRepositoryId(remoteUri: string): string | undefined {
return extractRepoSlugFromUri(remoteUri)?.replaceAll("/", "-");
}
/**
* Generates connection id that matches specific id format recognized by all Firebase clients.
*/
function generateConnectionId(): string {
const randomHash = Math.random().toString(36).slice(6);
return `apphosting-github-conn-${randomHash}`;
}
const ADD_CONN_CHOICE = "@ADD_CONN";
/**
* Prompts the user to link their backend to a GitHub repository.
*/
export async function linkGitHubRepository(
projectId: string,
location: string,
): Promise<devConnect.GitRepositoryLink> {
utils.logBullet(clc.bold(`${clc.yellow("===")} Set up a GitHub connection`));
// Fetch the sentinel Oauth connection first which is needed to create further GitHub connections.
const oauthConn = await getOrCreateOauthConnection(projectId, location);
const existingConns = await listAppHostingConnections(projectId);
if (existingConns.length === 0) {
existingConns.push(
await createFullyInstalledConnection(projectId, location, generateConnectionId(), oauthConn),
);
}
let repoCloneUri: string | undefined;
let connection: devConnect.Connection;
do {
if (repoCloneUri === ADD_CONN_CHOICE) {
existingConns.push(
await createFullyInstalledConnection(
projectId,
location,
generateConnectionId(),
oauthConn,
/** withNewInstallation= */ true,
),
);
}
const selection = await promptCloneUri(projectId, existingConns);
repoCloneUri = selection.cloneUri;
connection = selection.connection;
} while (repoCloneUri === ADD_CONN_CHOICE);
// Ensure that the selected connection exists in the same region as the backend
const { id: connectionId } = parseConnectionName(connection.name)!;
await getOrCreateConnection(projectId, location, connectionId, {
authorizerCredential: connection.githubConfig?.authorizerCredential,
appInstallationId: connection.githubConfig?.appInstallationId,
});
const repo = await getOrCreateRepository(projectId, location, connectionId, repoCloneUri);
utils.logSuccess(`Successfully linked GitHub repository at remote URI`);
utils.logSuccess(`\t${repo.cloneUri}\n`);
return repo;
}
/**
* Creates a new DevConnect GitHub connection resource and ensures that it is fully configured on the GitHub
* side (ie associated with an account/org and some subset of repos within that scope).
* Copies over Oauth creds from the sentinel Oauth connection to save the user from having to
* reauthenticate with GitHub.
* @param projectId user's Firebase projectID
* @param location region where backend is being created
* @param connectionId id of connection to be created
* @param oauthConn user's oauth connection
* @param withNewInstallation Defaults to false if not set, and the Oauth connection's
* Installation Id is re-used when creating a new connection.
* If true the Oauth connection's installation Id is not re-used.
*/
async function createFullyInstalledConnection(
projectId: string,
location: string,
connectionId: string,
oauthConn: devConnect.Connection,
withNewInstallation = false,
): Promise<devConnect.Connection> {
let conn = await createConnection(projectId, location, connectionId, {
appInstallationId: withNewInstallation ? undefined : oauthConn.githubConfig?.appInstallationId,
authorizerCredential: oauthConn.githubConfig?.authorizerCredential,
});
while (conn.installationState.stage !== "COMPLETE") {
utils.logBullet("Install the Firebase GitHub app to enable access to GitHub repositories");
const targetUri = conn.installationState.actionUri;
utils.logBullet(targetUri);
await utils.openInBrowser(targetUri);
await promptOnce({
type: "input",
message:
"Press Enter once you have installed or configured the Firebase GitHub app to access your GitHub repo.",
});
conn = await devConnect.getConnection(projectId, location, connectionId);
}
return conn;
}
/**
* Gets or creates the sentinel GitHub connection resource that contains our Firebase-wide GitHub Oauth token.
* This Oauth token can be used to create other connections without reprompting the user to grant access.
*/
export async function getOrCreateOauthConnection(
projectId: string,
location: string,
): Promise<devConnect.Connection> {
let conn: devConnect.Connection;
try {
conn = await devConnect.getConnection(projectId, location, APPHOSTING_OAUTH_CONN_NAME);
} catch (err: unknown) {
if ((err as any).status === 404) {
// Cloud build P4SA requires the secret manager admin role.
// This is required when creating an initial connection which is the Oauth connection in our case.
await ensureSecretManagerAdminGrant(projectId);
conn = await createConnection(projectId, location, APPHOSTING_OAUTH_CONN_NAME);
} else {
throw err;
}
}
while (conn.installationState.stage === "PENDING_USER_OAUTH") {
utils.logBullet("You must authorize the Firebase GitHub app.");
utils.logBullet("Sign in to GitHub and authorize Firebase GitHub app:");
const { url, cleanup } = await utils.openInBrowserPopup(
conn.installationState.actionUri,
"Authorize the GitHub app",
);
utils.logBullet(`\t${url}`);
await promptOnce({
type: "input",
message: "Press Enter once you have authorized the app",
});
cleanup();
const { projectId, location, id } = parseConnectionName(conn.name)!;
conn = await devConnect.getConnection(projectId, location, id);
}
return conn;
}
async function promptCloneUri(
projectId: string,
connections: devConnect.Connection[],
): Promise<{ cloneUri: string; connection: devConnect.Connection }> {
const { cloneUris, cloneUriToConnection } = await fetchAllRepositories(projectId, connections);
const cloneUri = await promptOnce({
type: "autocomplete",
name: "cloneUri",
message: "Which repository would you like to deploy?",
source: (_: any, input = ""): Promise<(inquirer.DistinctChoice | inquirer.Separator)[]> => {
return new Promise((resolve) =>
resolve([
new inquirer.Separator(),
{
name: "Missing a repo? Select this option to configure your GitHub connection settings",
value: ADD_CONN_CHOICE,
},
new inquirer.Separator(),
...fuzzy
.filter(input, cloneUris, {
extract: (uri) => extractRepoSlugFromUri(uri) || "",
})
.map((result) => {
return {
name: extractRepoSlugFromUri(result.original) || "",
value: result.original,
};
}),
]),
);
},
});
return { cloneUri, connection: cloneUriToConnection[cloneUri] };
}
/**
* Exported for unit testing
*/
export async function ensureSecretManagerAdminGrant(projectId: string): Promise<void> {
const projectNumber = await getProjectNumber({ projectId });
const dcsaEmail = devConnect.serviceAgentEmail(projectNumber);
// will return false even if the service account does not exist in the project
const alreadyGranted = await rm.serviceAccountHasRoles(
projectId,
dcsaEmail,
["roles/secretmanager.admin"],
true,
);
if (alreadyGranted) {
utils.logBullet("secret manager admin role already granted");
return;
}
utils.logBullet(
"To create a new GitHub connection, Secret Manager Admin role (roles/secretmanager.admin) is required on the Developer Connect Service Agent.",
);
const grant = await promptOnce({
type: "confirm",
message: "Grant the required role to the Developer Connect Service Agent?",
});
if (!grant) {
utils.logBullet(
"You, or your project administrator, should run the following command to grant the required role:\n\n" +
"You, or your project adminstrator, can run the following command to grant the required role manually:\n\n" +
`\tgcloud projects add-iam-policy-binding ${projectId} \\\n` +
`\t --member="serviceAccount:${dcsaEmail} \\\n` +
`\t --role="roles/secretmanager.admin\n`,
);
throw new FirebaseError("Insufficient IAM permissions to create a new connection to GitHub");
}
try {
await rm.addServiceAccountToRoles(
projectId,
dcsaEmail,
["roles/secretmanager.admin"],
/* skipAccountLookup= */ true,
);
} catch (e: any) {
// if the dev connect P4SA doesn't exist in the project, generate one
if (e?.code === 400 || e?.status === 400) {
await devConnect.generateP4SA(projectNumber);
await rm.addServiceAccountToRoles(
projectId,
dcsaEmail,
["roles/secretmanager.admin"],
/* skipAccountLookup= */ true,
);
} else {
throw e;
}
}
utils.logSuccess(
"Successfully granted the required role to the Developer Connect Service Agent!",
);
}
/**
* Creates a new Developer Connect Connection resource. Will typically need some initialization
* or configuration after being created.
*/
export async function createConnection(
projectId: string,
location: string,
connectionId: string,
githubConfig?: devConnect.GitHubConfig,
): Promise<devConnect.Connection> {
const op = await devConnect.createConnection(projectId, location, connectionId, githubConfig);
const conn = await poller.pollOperation<devConnect.Connection>({
...devConnectPollerOptions,
pollerName: `create-${location}-${connectionId}`,
operationResourceName: op.name,
});
return conn;
}
/**
* Exported for unit testing.
*/
export async function getOrCreateConnection(
projectId: string,
location: string,
connectionId: string,
githubConfig?: devConnect.GitHubConfig,
): Promise<devConnect.Connection> {
let conn: devConnect.Connection;
try {
conn = await devConnect.getConnection(projectId, location, connectionId);
} catch (err: unknown) {
if ((err as any).status === 404) {
utils.logBullet("creating connection");
conn = await createConnection(projectId, location, connectionId, githubConfig);
} else {
throw err;
}
}
return conn;
}
/**
* Exported for unit testing.
*/
export async function getOrCreateRepository(
projectId: string,
location: string,
connectionId: string,
cloneUri: string,
): Promise<devConnect.GitRepositoryLink> {
const repositoryId = generateRepositoryId(cloneUri);
if (!repositoryId) {
throw new FirebaseError(`Failed to generate repositoryId for URI "${cloneUri}".`);
}
let repo: devConnect.GitRepositoryLink;
try {
repo = await devConnect.getGitRepositoryLink(projectId, location, connectionId, repositoryId);
} catch (err: unknown) {
if ((err as FirebaseError).status === 404) {
const op = await devConnect.createGitRepositoryLink(
projectId,
location,
connectionId,
repositoryId,
cloneUri,
);
repo = await poller.pollOperation<devConnect.GitRepositoryLink>({
...devConnectPollerOptions,
pollerName: `create-${location}-${connectionId}-${repositoryId}`,
operationResourceName: op.name,
});
} else {
throw err;
}
}
return repo;
}
/**
* Exported for unit testing.
*
* Lists all App Hosting Developer Connect Connections
* not including the OAuth Connection
*/
export async function listAppHostingConnections(
projectId: string,
): Promise<devConnect.Connection[]> {
const conns = await devConnect.listAllConnections(projectId, "-");
return conns.filter(
(conn) =>
APPHOSTING_CONN_PATTERN.test(conn.name) &&
conn.installationState.stage === "COMPLETE" &&
!conn.disabled,
);
}
/**
* Exported for unit testing.
*/
export async function fetchAllRepositories(
projectId: string,
connections: devConnect.Connection[],
): Promise<{
cloneUris: string[];
cloneUriToConnection: Record<string, devConnect.Connection>;
}> {
const cloneUriToConnection: Record<string, devConnect.Connection> = {};
for (const conn of connections) {
const { location, id } = parseConnectionName(conn.name)!;
const connectionRepos = await devConnect.listAllLinkableGitRepositories(
projectId,
location,
id,
);
connectionRepos.forEach((repo) => {
cloneUriToConnection[repo.cloneUri] = conn;
});
}
return { cloneUris: Object.keys(cloneUriToConnection), cloneUriToConnection };
}