( instanceId: string, databaseId: string, schema: string, options: Options, )
| 199 | } |
| 200 | |
| 201 | export 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 | |
| 255 | export async function getSchemaMetadata( |
| 256 | instanceId: string, |
no test coverage detected
searching dependent graphs…