MCPcopy Create free account
hub / github.com/firebase/firebase-tools / greenFieldSchemaSetup

Function greenFieldSchemaSetup

src/gcp/cloudsql/permissionsSetup.ts:201–253  ·  view source on GitHub ↗
(
  instanceId: string,
  databaseId: string,
  schema: string,
  options: Options,
)

Source from the content-addressed store, hash-verified

199}
200
201export async function greenFieldSchemaSetup(
202 instanceId: string,
203 databaseId: string,
204 schema: string,
205 options: Options,
206) {
207 // Detect the minimal necessary revokes to avoid errors for users who used the old sql permissions setup.
208 const revokes = [];
209 if (
210 await checkSQLRoleIsGranted(
211 options,
212 instanceId,
213 databaseId,
214 "cloudsqlsuperuser",
215 firebaseowner(databaseId, schema),
216 )
217 ) {
218 logger.warn(
219 "Detected cloudsqlsuperuser was previously given to firebase owner, revoking to improve database security.",
220 );
221 revokes.push(`REVOKE "cloudsqlsuperuser" FROM "${firebaseowner(databaseId, schema)}"`);
222 }
223
224 const user = (await getIAMUser(options)).user;
225 const projectNumber = await needProjectNumber(options);
226 const { user: fdcP4SAUser } = toDatabaseUser(getDataConnectP4SA(projectNumber));
227
228 const sqlRoleSetupCmds = concat(
229 // For backward compatibality we sometimes need to revoke some roles.
230 revokes,
231
232 // We shoud make sure schema exists since this setup runs prior to executing the diffs.
233 [`CREATE SCHEMA IF NOT EXISTS "${schema}"`],
234
235 // Create and setup the owner role permissions.
236 ownerRolePermissions(databaseId, FIREBASE_SUPER_USER, schema),
237
238 // Create and setup writer role permissions.
239 writerRolePermissions(databaseId, FIREBASE_SUPER_USER, schema),
240
241 // Create and setup reader role permissions.
242 readerRolePermissions(databaseId, FIREBASE_SUPER_USER, schema),
243
244 // Grant firebaseowner role to the current IAM user.
245 `GRANT "${firebaseowner(databaseId, schema)}" TO "${user}"`,
246 // Grant firebaswriter to the FDC P4SA user
247 `GRANT "${firebasewriter(databaseId, schema)}" TO "${fdcP4SAUser}"`,
248
249 defaultPermissions(databaseId, schema, firebaseowner(databaseId, schema)),
250 );
251
252 return sqlRoleSetupCmds;
253}
254
255export async function getSchemaMetadata(
256 instanceId: string,

Callers 2

setupSQLPermissionsFunction · 0.85

Calls 11

firebaseownerFunction · 0.90
getIAMUserFunction · 0.90
needProjectNumberFunction · 0.90
toDatabaseUserFunction · 0.90
getDataConnectP4SAFunction · 0.90
ownerRolePermissionsFunction · 0.90
writerRolePermissionsFunction · 0.90
readerRolePermissionsFunction · 0.90
firebasewriterFunction · 0.90
defaultPermissionsFunction · 0.90
checkSQLRoleIsGrantedFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…