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

Add revoke grant option to dataconnect:sql #7869

Draft
wants to merge 1 commit into
base: master
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
8 changes: 7 additions & 1 deletion src/commands/dataconnect-sql-grant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@
.option(
"-E, --email <email>",
"The email of the user or service account we would like to grant the role to.",
).option(

Check failure on line 20 in src/commands/dataconnect-sql-grant.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace `.option(⏎····"-D,·--revoke",⏎····"Revokes·the·granted·permission",⏎····false⏎··` with `⏎··.option("-D,·--revoke",·"Revokes·the·granted·permission",·false`

Check failure on line 20 in src/commands/dataconnect-sql-grant.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Replace `.option(⏎····"-D,·--revoke",⏎····"Revokes·the·granted·permission",⏎····false⏎··` with `⏎··.option("-D,·--revoke",·"Revokes·the·granted·permission",·false`

Check failure on line 20 in src/commands/dataconnect-sql-grant.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace `.option(⏎····"-D,·--revoke",⏎····"Revokes·the·granted·permission",⏎····false⏎··` with `⏎··.option("-D,·--revoke",·"Revokes·the·granted·permission",·false`
"-D, --revoke",
"Revokes the granted permission",
false
)
.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;
const revokeRole = options.revoke as boolean

Check failure on line 30 in src/commands/dataconnect-sql-grant.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Insert `;`

Check failure on line 30 in src/commands/dataconnect-sql-grant.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Insert `;`

Check failure on line 30 in src/commands/dataconnect-sql-grant.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Insert `;`

if (!role) {
throw new FirebaseError(
"-R, --role <role> is required. Run the command with -h for more info.",
Expand All @@ -42,6 +48,6 @@
await ensureApis(projectId);
const serviceInfo = await pickService(projectId, options.config, serviceId);

await grantRoleToUserInSchema(options, serviceInfo.schema);
await grantRoleToUserInSchema(options, serviceInfo.schema, revokeRole);
return { projectId, serviceId };
});
11 changes: 8 additions & 3 deletions src/dataconnect/schemaMigration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import * as experiments from "../experiments";
import * as errors from "./errors";

export async function diffSchema(

Check warning on line 30 in src/dataconnect/schemaMigration.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment
schema: Schema,
schemaValidation?: SchemaValidation,
): Promise<Diff[]> {
Expand Down Expand Up @@ -56,8 +56,8 @@
} else {
logLabeledSuccess("dataconnect", `Database schema is compatible.`);
}
} catch (err: any) {

Check warning on line 59 in src/dataconnect/schemaMigration.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
if (err?.status !== 400) {

Check warning on line 60 in src/dataconnect/schemaMigration.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .status on an `any` value
throw err;
}
const invalidConnectors = errors.getInvalidConnectors(err);
Expand Down Expand Up @@ -85,8 +85,8 @@
logLabeledBullet("dataconnect", `generating schema changes, including optional changes...`);
await upsertSchema(schema, /** validateOnly=*/ true);
logLabeledSuccess("dataconnect", `no additional optional changes`);
} catch (err: any) {

Check warning on line 88 in src/dataconnect/schemaMigration.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
if (err?.status !== 400) {

Check warning on line 89 in src/dataconnect/schemaMigration.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .status on an `any` value
throw err;
}
const incompatible = errors.getIncompatibleSchemaError(err);
Expand All @@ -108,7 +108,7 @@
return diffs;
}

export async function migrateSchema(args: {

Check warning on line 111 in src/dataconnect/schemaMigration.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment
options: Options;
schema: Schema;
/** true for `dataconnect:sql:migrate`, false for `deploy` */
Expand All @@ -135,8 +135,8 @@
try {
await upsertSchema(schema, validateOnly);
logger.debug(`Database schema was up to date for ${instanceId}:${databaseId}`);
} catch (err: any) {

Check warning on line 138 in src/dataconnect/schemaMigration.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
if (err?.status !== 400) {

Check warning on line 139 in src/dataconnect/schemaMigration.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .status on an `any` value
throw err;
}
// Parse and handle failed precondition errors, then retry.
Expand Down Expand Up @@ -188,8 +188,8 @@
setSchemaValidationMode(schema, validationMode);
try {
await upsertSchema(schema, validateOnly);
} catch (err: any) {

Check warning on line 191 in src/dataconnect/schemaMigration.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
if (err.status !== 400) {

Check warning on line 192 in src/dataconnect/schemaMigration.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .status on an `any` value
throw err;
}
// Parse and handle failed precondition errors, then retry.
Expand Down Expand Up @@ -224,7 +224,7 @@
return diffs;
}

export async function grantRoleToUserInSchema(options: Options, schema: Schema) {
export async function grantRoleToUserInSchema(options: Options, schema: Schema, revokeRole: boolean) {

Check failure on line 227 in src/dataconnect/schemaMigration.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace `options:·Options,·schema:·Schema,·revokeRole:·boolean` with `⏎··options:·Options,⏎··schema:·Schema,⏎··revokeRole:·boolean,⏎`

Check failure on line 227 in src/dataconnect/schemaMigration.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Replace `options:·Options,·schema:·Schema,·revokeRole:·boolean` with `⏎··options:·Options,⏎··schema:·Schema,⏎··revokeRole:·boolean,⏎`

Check failure on line 227 in src/dataconnect/schemaMigration.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace `options:·Options,·schema:·Schema,·revokeRole:·boolean` with `⏎··options:·Options,⏎··schema:·Schema,⏎··revokeRole:·boolean,⏎`
const role = options.role as string;
const email = options.email as string;

Expand All @@ -237,7 +237,7 @@
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 ${fdcSqlRole} to ${user}`,
`Only users with 'roles/cloudsql.admin' can grant/revoke SQL roles. If you do not have this role, ask your database administrator to run this command or manually grant ${fdcSqlRole} to ${user}`,
);
}

Expand All @@ -247,12 +247,17 @@
// Upsert user account into the database.
await cloudSqlAdminClient.createUser(projectId, instanceId, mode, user);

let cmd = `GRANT "${fdcSqlRole}" TO "${user}"`

Check failure on line 250 in src/dataconnect/schemaMigration.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Insert `;`

Check failure on line 250 in src/dataconnect/schemaMigration.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Insert `;`

Check failure on line 250 in src/dataconnect/schemaMigration.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Insert `;`
if (revokeRole) {
cmd = `REVOKE "${fdcSqlRole}" FROM "${user}"`

Check failure on line 252 in src/dataconnect/schemaMigration.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Insert `;`

Check failure on line 252 in src/dataconnect/schemaMigration.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Insert `;`

Check failure on line 252 in src/dataconnect/schemaMigration.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Insert `;`
}

// Grant the role to the user.
await executeSqlCmdsAsSuperUser(
options,
instanceId,
databaseId,
/** cmds= */ [`GRANT "${fdcSqlRole}" TO "${user}"`],
/** cmds= */ [cmd],
/** silent= */ false,
);
}
Expand Down
Loading