-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathdataconnect-sql-grant.ts
More file actions
57 lines (51 loc) · 2.35 KB
/
dataconnect-sql-grant.ts
File metadata and controls
57 lines (51 loc) · 2.35 KB
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
import { Command } from "../command";
import { Options } from "../options";
import { needProjectId } from "../projectUtils";
import { ensureApis } from "../dataconnect/ensureApis";
import { requirePermissions } from "../requirePermissions";
import { pickService } from "../dataconnect/load";
import { grantRoleToUserInSchema } from "../dataconnect/schemaMigration";
import { requireAuth } from "../requireAuth";
import { FirebaseError } from "../error";
import { fdcSqlRoleMap } from "../gcp/cloudsql/permissionsSetup";
import { iamUserIsCSQLAdmin } from "../gcp/cloudsql/cloudsqladmin";
import { mainSchema } from "../dataconnect/types";
const allowedRoles = Object.keys(fdcSqlRoleMap);
export const command = new Command("dataconnect:sql:grant [serviceId]")
.description("grants the SQL role <role> to the provided user or service account <email>")
.option("-R, --role <role>", "The SQL role to grant. One of: owner, writer, or reader.")
.option(
"-E, --email <email>",
"The email of the user or service account we would like to grant the role to.",
)
.before(requirePermissions, ["firebasedataconnect.services.list"])
.before(requireAuth)
.action(async (serviceId: string, options: Options) => {
const role = options.role as string;
const email = options.email as string;
if (!role) {
throw new FirebaseError(
"-R, --role <role> is required. Run the command with -h for more info.",
);
}
if (!email) {
throw new FirebaseError(
"-E, --email <email> is required. Run the command with -h for more info.",
);
}
if (!allowedRoles.includes(role.toLowerCase())) {
throw new FirebaseError(`Role should be one of ${allowedRoles.join(" | ")}.`);
}
// Make sure current user can perform this action.
const userIsCSQLAdmin = await iamUserIsCSQLAdmin(options);
if (!userIsCSQLAdmin) {
throw new FirebaseError(
`Only users with 'roles/cloudsql.admin' can grant SQL roles. If you do not have this role, ask your database administrator to run this command or manually grant ${role} to ${email}`,
);
}
const projectId = needProjectId(options);
await ensureApis(projectId);
const serviceInfo = await pickService(projectId, options.config, serviceId);
await grantRoleToUserInSchema(options, mainSchema(serviceInfo.schemas));
return { projectId, serviceId };
});