Skip to content

Commit c9c4200

Browse files
feat: Enhance database permission management and update script parameters for PostgreSQL setup
1 parent a5716fa commit c9c4200

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

infra/main.bicep

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1729,7 +1729,7 @@ module createIndex 'br/public:avm/res/resources/deployment-script:0.5.1' = if (d
17291729
retentionInterval: 'PT1H'
17301730
runOnce: true
17311731
primaryScriptUri: '${baseUrl}scripts/run_create_table_script.sh'
1732-
arguments: '${baseUrl} ${resourceGroup().name} ${postgresDBModule!.outputs.fqdn} ${managedIdentityModule.outputs.name}'
1732+
arguments: '${baseUrl} ${resourceGroup().name} ${postgresDBModule!.outputs.fqdn} ${principalId} ${managedIdentityModule.outputs.name}'
17331733
storageAccountResourceId: storage.outputs.resourceId
17341734
subnetResourceIds: enablePrivateNetworking
17351735
? [

scripts/data_scripts/create_postgres_tables.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,62 @@
11
from azure_credential_utils import get_azure_credential
22
import psycopg2
3+
from psycopg2 import sql
34

5+
principalId = "principalId"
46
user = "managedIdentityName"
57
host = "serverName"
68
dbname = "postgres"
79

810

11+
def grant_permissions(cursor, dbname, schema_name, principal_id):
12+
"""
13+
Grants database and schema-level permissions to a specified principal.
14+
15+
Parameters:
16+
- cursor: psycopg2 cursor object for database operations.
17+
- dbname: Name of the database to grant CONNECT permission.
18+
- schema_name: Name of the schema to grant table-level permissions.
19+
- principal_id: ID of the principal (role or user) to grant permissions.
20+
"""
21+
22+
# Check if the principal exists in the database
23+
cursor.execute(
24+
sql.SQL("SELECT 1 FROM pg_roles WHERE rolname = {principal}").format(
25+
principal=sql.Literal(principal_id)
26+
)
27+
)
28+
if cursor.fetchone() is None:
29+
add_principal_user_query = sql.SQL(
30+
"SELECT * FROM pgaadauth_create_principal({principal}, false, false)"
31+
)
32+
cursor.execute(
33+
add_principal_user_query.format(
34+
principal=sql.Literal(principal_id),
35+
)
36+
)
37+
38+
# Grant CONNECT on database
39+
grant_connect_query = sql.SQL("GRANT CONNECT ON DATABASE {database} TO {principal}")
40+
cursor.execute(
41+
grant_connect_query.format(
42+
database=sql.Identifier(dbname),
43+
principal=sql.Identifier(principal_id),
44+
)
45+
)
46+
print(f"Granted CONNECT on database '{dbname}' to '{principal_id}'")
47+
48+
# Grant SELECT, INSERT, UPDATE, DELETE on schema tables
49+
grant_permissions_query = sql.SQL(
50+
"GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA {schema} TO {principal}"
51+
)
52+
cursor.execute(
53+
grant_permissions_query.format(
54+
schema=sql.Identifier(schema_name),
55+
principal=sql.Identifier(principal_id),
56+
)
57+
)
58+
59+
960
# Acquire the access token
1061
cred = get_azure_credential()
1162
access_token = cred.get_token("https://ossrdbms-aad.database.windows.net/.default")
@@ -81,6 +132,9 @@
81132
)
82133
conn.commit()
83134

135+
if principalId and principalId.strip():
136+
grant_permissions(cursor, dbname, "public", principalId)
137+
conn.commit()
84138

85139
cursor.execute("ALTER TABLE public.conversations OWNER TO azure_pg_admin;")
86140
cursor.execute("ALTER TABLE public.messages OWNER TO azure_pg_admin;")

scripts/run_create_table_script.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ requirementFile="requirements.txt"
77
requirementFileUrl=${baseUrl}"scripts/data_scripts/requirements.txt"
88
resourceGroup="$2"
99
serverName="$3"
10-
managedIdentityName="$4"
10+
principalId="$4"
11+
managedIdentityName="$5"
1112

1213
echo "Script Started"
1314

@@ -27,6 +28,7 @@ curl --output "$requirementFile" "$requirementFileUrl"
2728
echo "Download completed"
2829

2930
# Replace placeholders in the python script with actual values
31+
sed -i "s/principalId/${principalId}/g" "create_postgres_tables.py"
3032
sed -i "s/managedIdentityName/${managedIdentityName}/g" "create_postgres_tables.py"
3133
sed -i "s/serverName/${serverName}/g" "create_postgres_tables.py"
3234

0 commit comments

Comments
 (0)