Skip to content

Commit

Permalink
use the devops MSI to manage postgres
Browse files Browse the repository at this point in the history
... instead of introducing new MSIs

Signed-off-by: Gerd Oberlechner <[email protected]>
  • Loading branch information
geoberle committed Nov 28, 2024
1 parent a50d8dc commit 1e00fd9
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 36 deletions.
19 changes: 6 additions & 13 deletions dev-infrastructure/modules/cluster-service.bicep
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
@description('The location for the PostGres DB')
param location string

@description('The managed identity name CS will use to interact with Azure resources')
param clusterServiceManagedIdentityName string

Expand Down Expand Up @@ -43,25 +40,21 @@ param regionalResourceGroup string
@description('The names of the ACR resource groups / will be refactored soon into dedicated ACR Resource IDs')
param acrResourceGroupNames array = []

@description('The resource ID of the managed identity used to manage the Postgres server')
param postgresAdministrationManagedIdentityId string

//
// P O S T G R E S
//

resource postgresAdminManagedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' = {
name: '${postgresServerName}-db-admin-msi'
location: location
}

module postgres 'postgres/postgres.bicep' = if (deployPostgres) {
name: '${deployment().name}-postgres'
params: {
name: postgresServerName
databaseAdministrators: [
// add the dedicated admin managed identity as administrator
// this one is going to be used to manage DB access
{
principalId: postgresAdminManagedIdentity.properties.principalId
principalName: postgresAdminManagedIdentity.name
principalId: reference(postgresAdministrationManagedIdentityId, '2023-01-31').principalId
principalName: reference(postgresAdministrationManagedIdentityId, '2023-01-31').name
principalType: 'ServicePrincipal'
}
]
Expand Down Expand Up @@ -108,7 +101,7 @@ module csManagedIdentityDatabaseAccess 'postgres/postgres-access.bicep' = if (de
name: '${deployment().name}-cs-db-access'
params: {
postgresServerName: postgresServerName
postgresAdminManagedIdentityName: postgresAdminManagedIdentity.name
postgresAdministrationManagedIdentityId: postgresAdministrationManagedIdentityId
databaseName: csDatabaseName
newUserName: clusterServiceManagedIdentityName
newUserPrincipalId: clusterServiceManagedIdentityPrincipalId
Expand Down
14 changes: 5 additions & 9 deletions dev-infrastructure/modules/maestro/maestro-server.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,13 @@ param maestroServerManagedIdentityName string
@description('The principal ID of the Managed Identity for the Maestro cluster service')
param maestroServerManagedIdentityPrincipalId string

param location string
@description('The resource ID of the managed identity used to manage the Postgres server')
param postgresAdministrationManagedIdentityId string

//
// P O S T G R E S
//

resource postgresAdminManagedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' = {
name: '${postgresServerName}-db-admin-msi'
location: location
}

module postgres '../postgres/postgres.bicep' = if (deployPostgres) {
name: '${deployment().name}-postgres'
params: {
Expand All @@ -80,8 +76,8 @@ module postgres '../postgres/postgres.bicep' = if (deployPostgres) {
// add the dedicated admin managed identity as administrator
// this one is going to be used to manage DB access
{
principalId: postgresAdminManagedIdentity.properties.principalId
principalName: postgresAdminManagedIdentity.name
principalId: reference(postgresAdministrationManagedIdentityId, '2023-01-31').principalId
principalName: reference(postgresAdministrationManagedIdentityId, '2023-01-31').name
principalType: 'ServicePrincipal'
}
]
Expand Down Expand Up @@ -121,7 +117,7 @@ module csManagedIdentityDatabaseAccess '../postgres/postgres-access.bicep' = if
name: '${deployment().name}-maestro-db-access'
params: {
postgresServerName: postgresServerName
postgresAdminManagedIdentityName: postgresAdminManagedIdentity.name
postgresAdministrationManagedIdentityId: postgresAdministrationManagedIdentityId
databaseName: maestroDatabaseName
newUserName: maestroServerManagedIdentityName
newUserPrincipalId: maestroServerManagedIdentityPrincipalId
Expand Down
6 changes: 3 additions & 3 deletions dev-infrastructure/modules/postgres/postgres-access.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ The user will also be enabled for entra authentication.
@description('The name of the postgres server that will be managed')
param postgresServerName string

@description('The name of the managed identity that will be used to manage access in the database')
param postgresAdminManagedIdentityName string
@description('The resource ID of the managed identity that will be used to manage access in the database')
param postgresAdministrationManagedIdentityId string

@description('The principal ID / object ID of the managed identity that will be granted access to')
param newUserPrincipalId string
Expand Down Expand Up @@ -42,7 +42,7 @@ module csManagedIdentityDatabaseAccess 'postgres-sql.bicep' = {
params: {
postgresServerName: postgres.properties.fullyQualifiedDomainName
databaseName: 'postgres' // access configuration is managed in the postgres DB
postgresAdminManagedIdentityName: postgresAdminManagedIdentityName
postgresAdministrationManagedIdentityId: postgresAdministrationManagedIdentityId
sqlScript: string(join(sqlScriptLines, '\n'))
}
}
14 changes: 5 additions & 9 deletions dev-infrastructure/modules/postgres/postgres-sql.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@ param postgresServerName string
@description('The database name where an SQL script will be executed')
param databaseName string

@description('The name of the user-assigned managed identity that will be used to execute the SQL script')
param postgresAdminManagedIdentityName string
@description('The resource ID of the user-assigned managed identity that will be used to execute the SQL script')
param postgresAdministrationManagedIdentityId string

@description('The SQL script to execute on the PostgreSQL server')
param sqlScript string

param forceUpdateTag string = guid('${sqlScript}/${postgresServerName}/${databaseName}+${postgresAdminManagedIdentityName}')

resource postgresAdminManagedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' existing = {
name: postgresAdminManagedIdentityName
}
param forceUpdateTag string = guid('${sqlScript}/${postgresServerName}/${databaseName}/${postgresAdministrationManagedIdentityId}')

resource deploymentScript 'Microsoft.Resources/deploymentScripts@2023-08-01' = {
name: deployment().name
Expand All @@ -27,7 +23,7 @@ resource deploymentScript 'Microsoft.Resources/deploymentScripts@2023-08-01' = {
identity: {
type: 'UserAssigned'
userAssignedIdentities: {
'${postgresAdminManagedIdentity.id}': {}
'${postgresAdministrationManagedIdentityId}': {}
}
}

Expand Down Expand Up @@ -58,7 +54,7 @@ resource deploymentScript 'Microsoft.Resources/deploymentScripts@2023-08-01' = {
}
{
name: 'PGUSER'
value: postgresAdminManagedIdentity.name
value: reference(postgresAdministrationManagedIdentityId, '2023-01-31').name
}
]
timeout: 'PT30M'
Expand Down
4 changes: 2 additions & 2 deletions dev-infrastructure/templates/svc-cluster.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ module maestroServer '../modules/maestro/maestro-server.bicep' = {
privateEndpointSubnetId: svcCluster.outputs.aksNodeSubnetId
privateEndpointVnetId: svcCluster.outputs.aksVnetId
postgresServerPrivate: maestroPostgresPrivate
postgresAdministrationManagedIdentityId: aroDevopsMsiId
maestroServerManagedIdentityPrincipalId: filter(
svcCluster.outputs.userAssignedIdentities,
id => id.uamiName == 'maestro-server'
Expand All @@ -258,7 +259,6 @@ module maestroServer '../modules/maestro/maestro-server.bicep' = {
svcCluster.outputs.userAssignedIdentities,
id => id.uamiName == 'maestro-server'
)[0].uamiName
location: location
}
dependsOn: [
serviceKeyVault
Expand Down Expand Up @@ -307,7 +307,6 @@ var csManagedIdentityPrincipalId = filter(
module cs '../modules/cluster-service.bicep' = {
name: 'cluster-service'
params: {
location: location
postgresServerName: csPostgresServerName
postgresServerMinTLSVersion: csPostgresServerMinTLSVersion
privateEndpointSubnetId: svcCluster.outputs.aksNodeSubnetId
Expand All @@ -321,6 +320,7 @@ module cs '../modules/cluster-service.bicep' = {
regionalDNSZoneName: regionalDNSZoneName
regionalResourceGroup: regionalResourceGroup
acrResourceGroupNames: clustersServiceAcrResourceGroupNames
postgresAdministrationManagedIdentityId: aroDevopsMsiId
}
dependsOn: [
maestroServer
Expand Down

0 comments on commit 1e00fd9

Please sign in to comment.