Skip to content

Commit bf37456

Browse files
committed
EES-5329 Replace role public_data_admin with public_data_read_write. Configure role privileges in Postgres init script for local environment and in database migration for non-local environment.
1 parent 94d609a commit bf37456

10 files changed

+126
-1634
lines changed

data/public-api-db/00-init.sh

+16-4
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,27 @@ psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-E
1717
1818
/*
1919
* Grant the other application user roles privileges to look up objects on the public schema.
20-
* Additional privileges are granted to these application user roles by the initial migration.
2120
*/
2221
GRANT USAGE ON SCHEMA public TO app_public_data_processor;
2322
GRANT USAGE ON SCHEMA public TO app_admin;
2423
GRANT USAGE ON SCHEMA public TO app_publisher;
2524
2625
/*
27-
* Create an admin group role which can be granted to user roles requiring privileges on public schema objects.
28-
* Privileges are granted to this admin group role by the initial migration.
26+
* Create a public_data_read_write group role which can be granted to user roles requiring read and write privileges on public schema objects.
2927
*/
30-
CREATE ROLE public_data_admin WITH NOLOGIN;
28+
CREATE ROLE public_data_read_write WITH NOLOGIN;
29+
30+
/*
31+
* Grant privileges to the public_data_read_write group role for all tables and sequences in the public schema subsequently created by app_public_data_api.
32+
*/
33+
ALTER DEFAULT PRIVILEGES FOR ROLE app_public_data_api IN SCHEMA public GRANT SELECT, INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES ON TABLES TO public_data_read_write;
34+
ALTER DEFAULT PRIVILEGES FOR ROLE app_public_data_api IN SCHEMA public GRANT SELECT, UPDATE ON SEQUENCES TO public_data_read_write;
35+
36+
/*
37+
* Grant membership of the public_data_read_write group role to the application user roles.
38+
*/
39+
GRANT public_data_read_write TO app_public_data_api;
40+
GRANT public_data_read_write TO app_public_data_processor;
41+
GRANT public_data_read_write TO app_admin;
42+
GRANT public_data_read_write TO app_publisher;
3143
EOSQL

infrastructure/templates/public-api/main.bicep

-21
Original file line numberDiff line numberDiff line change
@@ -309,27 +309,6 @@ module apiContainerAppModule 'components/containerApp.bicep' = if (deployContain
309309
name: 'DataFiles__BasePath'
310310
value: dataFilesFileShareMountPath
311311
}
312-
{
313-
// This property informs the Container App of the name of the Admin's system-assigned identity.
314-
// It uses this to grant permissions to the Admin user in order for it to be able to access
315-
// tables in the "public_data" database successfully.
316-
name: 'AdminAppServiceIdentityName'
317-
value: adminAppServiceFullName
318-
}
319-
{
320-
// This property informs the Container App of the name of the Data Processor's system-assigned identity.
321-
// It uses this to grant permissions to the Data Processor user in order for it to be able to access
322-
// tables in the "public_data" database successfully.
323-
name: 'DataProcessorFunctionAppIdentityName'
324-
value: dataProcessorFunctionAppManagedIdentity.name
325-
}
326-
{
327-
// This property informs the Container App of the name of the Publisher's system-assigned identity.
328-
// It uses this to grant permissions to the Publisher user in order for it to be able to access
329-
// tables in the "public_data" database successfully.
330-
name: 'PublisherFunctionAppIdentityName'
331-
value: publisherFunctionAppFullName
332-
}
333312
]
334313
tagValues: tagValues
335314
}

src/GovUk.Education.ExploreEducationStatistics.Common/Extensions/MigrationBuilderExtensions.cs

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#nullable enable
2+
using System;
23
using System.IO;
34
using System.Reflection;
45
using Microsoft.EntityFrameworkCore.Migrations;
@@ -7,7 +8,13 @@ namespace GovUk.Education.ExploreEducationStatistics.Common.Extensions
78
{
89
public static class MigrationBuilderExtensions
910
{
10-
public static void SqlFromFile(this MigrationBuilder migrationBuilder,
11+
public static bool IsEnvironment(
12+
this MigrationBuilder _,
13+
string environmentName) =>
14+
Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == environmentName;
15+
16+
public static void SqlFromFile(
17+
this MigrationBuilder migrationBuilder,
1118
string migrationsPath,
1219
string filename,
1320
bool suppressTransaction = false)
@@ -19,7 +26,8 @@ public static void SqlFromFile(this MigrationBuilder migrationBuilder,
1926
migrationBuilder.Sql(File.ReadAllText(file), suppressTransaction);
2027
}
2128

22-
public static void SqlFromFileByLine(this MigrationBuilder migrationBuilder,
29+
public static void SqlFromFileByLine(
30+
this MigrationBuilder migrationBuilder,
2331
string migrationsPath,
2432
string filename)
2533
{

src/GovUk.Education.ExploreEducationStatistics.Public.Data.Model/Database/PublicDataDbContext.cs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace GovUk.Education.ExploreEducationStatistics.Public.Data.Model.Database;
77

88
public class PublicDataDbContext : DbContext
99
{
10+
public const string PublicDataReadWriteRole = "public_data_read_write";
1011
public const string FilterOptionMetaLinkSequence = "FilterOptionMetaLink_seq";
1112
public const string LocationOptionMetasIdSequence = "LocationOptionMetas_Id_seq";
1213

src/GovUk.Education.ExploreEducationStatistics.Public.Data.Model/Migrations/20240501113006_InitialMigration.cs

+13-62
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3+
using GovUk.Education.ExploreEducationStatistics.Common.Extensions;
4+
using GovUk.Education.ExploreEducationStatistics.Public.Data.Model.Database;
35
using Microsoft.EntityFrameworkCore.Migrations;
46
using Microsoft.Extensions.Hosting;
57
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
@@ -14,6 +16,17 @@ public partial class InitialMigration : Migration
1416
/// <inheritdoc />
1517
protected override void Up(MigrationBuilder migrationBuilder)
1618
{
19+
if (migrationBuilder.IsEnvironment(Environments.Production))
20+
{
21+
// Grant privileges to the 'public_data_read_write' group role for objects in the public schema
22+
// subsequently created by this applications user role. Membership of the role will be granted to other
23+
// application and indvidual user roles who require read and write privileges on public schema objects.
24+
migrationBuilder.Sql(
25+
$"ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT, INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES ON TABLES TO {PublicDataDbContext.PublicDataReadWriteRole}");
26+
migrationBuilder.Sql(
27+
$"ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT, UPDATE ON SEQUENCES TO {PublicDataDbContext.PublicDataReadWriteRole}");
28+
}
29+
1730
migrationBuilder.CreateTable(
1831
name: "FilterOptionMetas",
1932
columns: table => new
@@ -579,68 +592,6 @@ protected override void Up(MigrationBuilder migrationBuilder)
579592
column: "LatestLiveVersionId",
580593
principalTable: "DataSetVersions",
581594
principalColumn: "Id");
582-
583-
var isLocalEnvironment =
584-
Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == Environments.Development;
585-
586-
// Grant privileges on objects created by this resource's database user to the Admin App Service user.
587-
var adminAppServiceRoleName = isLocalEnvironment
588-
? "app_admin"
589-
: Environment.GetEnvironmentVariable("AdminAppServiceIdentityName");
590-
if (adminAppServiceRoleName != null)
591-
{
592-
migrationBuilder.Sql(
593-
$"""GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO {adminAppServiceRoleName}""");
594-
migrationBuilder.Sql(
595-
$"""ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO {adminAppServiceRoleName}""");
596-
}
597-
598-
// Grant privileges on objects created by this resource's database user to the Public API Data Processor Function App user.
599-
var dataProcessorFunctionAppRoleName = isLocalEnvironment
600-
? "app_public_data_processor"
601-
: Environment.GetEnvironmentVariable("DataProcessorFunctionAppIdentityName");
602-
if (dataProcessorFunctionAppRoleName != null)
603-
{
604-
migrationBuilder.Sql(
605-
$"""GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO {dataProcessorFunctionAppRoleName}""");
606-
migrationBuilder.Sql(
607-
$"""ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO {dataProcessorFunctionAppRoleName}""");
608-
609-
migrationBuilder.Sql(
610-
$"""GRANT SELECT, UPDATE ON ALL SEQUENCES IN SCHEMA public TO {dataProcessorFunctionAppRoleName}""");
611-
migrationBuilder.Sql(
612-
$"""ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT, UPDATE ON SEQUENCES TO {dataProcessorFunctionAppRoleName}""");
613-
}
614-
615-
// Grant privileges on objects created by this resource's database user to the Publisher Function App user.
616-
var publisherFunctionAppRoleName = isLocalEnvironment
617-
? "app_publisher"
618-
: Environment.GetEnvironmentVariable("PublisherFunctionAppIdentityName");
619-
if (publisherFunctionAppRoleName != null)
620-
{
621-
migrationBuilder.Sql(
622-
$"""GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO {publisherFunctionAppRoleName}""");
623-
migrationBuilder.Sql(
624-
$"""ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO {publisherFunctionAppRoleName}""");
625-
}
626-
627-
// Grant privileges on objects created by this resource's database user to the 'public_data_admin' role.
628-
// The 'public_data_admin' role represents a group of admin users. Membership of it will be granted to
629-
// indvidual user roles who require write privileges on public schema objects.
630-
// Locally this is created in the initialisation script run by the Docker entrypoint.
631-
// In Azure this role needs to be created manually after the database is created.
632-
migrationBuilder.Sql(
633-
"""
634-
DO $$
635-
BEGIN
636-
IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'public_data_admin') THEN
637-
GRANT SELECT, INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES ON ALL TABLES IN SCHEMA public TO public_data_admin;
638-
GRANT SELECT, UPDATE ON ALL SEQUENCES IN SCHEMA public TO public_data_admin;
639-
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT, INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES ON TABLES TO public_data_admin;
640-
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT, UPDATE ON SEQUENCES TO public_data_admin;
641-
END IF;
642-
END $$;
643-
""");
644595
}
645596

646597
/// <inheritdoc />

0 commit comments

Comments
 (0)