Skip to content

Commit b78ef60

Browse files
Fix Azure PostgreSQL AsExisting (#7702)
In bicep, it is not supported to set properties on "existing" resources. When using AsExisting on an Azure PostgreSQL resource, we are setting authConfig properties to enable/disable password and activeDirectory auth. This is causing errors when trying to deploy because the bicep is invalid. Fix this by not setting these properties on existing resources. Instead for activeDirectory/Entra ID auth, we will add the principle as an admin on the server. For password auth on an existing resource, we expect the user to pass the correct username/password parameters to connect to the database server. Fix #7694 Co-authored-by: Eric Erhardt <[email protected]>
1 parent 5bab8b4 commit b78ef60

File tree

2 files changed

+96
-16
lines changed

2 files changed

+96
-16
lines changed

src/Aspire.Hosting.Azure.PostgreSQL/AzurePostgresExtensions.cs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -394,14 +394,19 @@ private static void ConfigurePostgreSqlInfrastructure(AzureResourceInfrastructur
394394
keyVault.Name = kvNameParam;
395395
infrastructure.Add(keyVault);
396396

397-
postgres.AuthConfig = new PostgreSqlFlexibleServerAuthConfig()
397+
// bicep doesn't allow for setting properties on existing resources. So we don't set auth properties here.
398+
// The administratorLogin and administratorLoginPassword are expected to match what is already configured on the server
399+
if (!postgres.IsExistingResource)
398400
{
399-
ActiveDirectoryAuth = PostgreSqlFlexibleServerActiveDirectoryAuthEnum.Disabled,
400-
PasswordAuth = PostgreSqlFlexibleServerPasswordAuthEnum.Enabled
401-
};
401+
postgres.AuthConfig = new PostgreSqlFlexibleServerAuthConfig()
402+
{
403+
ActiveDirectoryAuth = PostgreSqlFlexibleServerActiveDirectoryAuthEnum.Disabled,
404+
PasswordAuth = PostgreSqlFlexibleServerPasswordAuthEnum.Enabled
405+
};
402406

403-
postgres.AdministratorLogin = administratorLogin;
404-
postgres.AdministratorLoginPassword = administratorLoginPassword;
407+
postgres.AdministratorLogin = administratorLogin;
408+
postgres.AdministratorLoginPassword = administratorLoginPassword;
409+
}
405410

406411
var secret = new KeyVaultSecret("connectionString")
407412
{
@@ -430,11 +435,14 @@ private static void ConfigurePostgreSqlInfrastructure(AzureResourceInfrastructur
430435
}
431436
else
432437
{
433-
postgres.AuthConfig = new PostgreSqlFlexibleServerAuthConfig()
438+
if (!postgres.IsExistingResource)
434439
{
435-
ActiveDirectoryAuth = PostgreSqlFlexibleServerActiveDirectoryAuthEnum.Enabled,
436-
PasswordAuth = PostgreSqlFlexibleServerPasswordAuthEnum.Disabled
437-
};
440+
postgres.AuthConfig = new PostgreSqlFlexibleServerAuthConfig()
441+
{
442+
ActiveDirectoryAuth = PostgreSqlFlexibleServerActiveDirectoryAuthEnum.Enabled,
443+
PasswordAuth = PostgreSqlFlexibleServerPasswordAuthEnum.Disabled
444+
};
445+
}
438446

439447
var principalIdParameter = new ProvisioningParameter(AzureBicepResource.KnownParameters.PrincipalId, typeof(string));
440448
infrastructure.Add(principalIdParameter);

tests/Aspire.Hosting.Azure.Tests/ExistingAzureResourceTests.cs

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -778,12 +778,6 @@ param principalName string
778778
779779
resource postgresSql 'Microsoft.DBforPostgreSQL/flexibleServers@2024-08-01' existing = {
780780
name: existingResourceName
781-
properties: {
782-
authConfig: {
783-
activeDirectoryAuth: 'Enabled'
784-
passwordAuth: 'Disabled'
785-
}
786-
}
787781
}
788782
789783
resource postgreSqlFirewallRule_AllowAllAzureIps 'Microsoft.DBforPostgreSQL/flexibleServers/firewallRules@2024-08-01' = {
@@ -815,6 +809,84 @@ param principalName string
815809
Assert.Equal(expectedBicep, BicepText);
816810
}
817811

812+
[Fact]
813+
public async Task SupportsExistingPostgresSqlWithResourceGroupWithPasswordAuth()
814+
{
815+
using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish);
816+
817+
var existingResourceName = builder.AddParameter("existingResourceName");
818+
var existingResourceGroupName = builder.AddParameter("existingResourceGroupName");
819+
var existingUserName = builder.AddParameter("existingUserName");
820+
var existingPassword = builder.AddParameter("existingPassword");
821+
822+
var postgresSql = builder.AddAzurePostgresFlexibleServer("postgresSql")
823+
.PublishAsExisting(existingResourceName, existingResourceGroupName)
824+
.WithPasswordAuthentication(existingUserName, existingPassword);
825+
826+
var (ManifestNode, BicepText) = await ManifestUtils.GetManifestWithBicep(postgresSql.Resource);
827+
828+
var expectedManifest = """
829+
{
830+
"type": "azure.bicep.v1",
831+
"connectionString": "{postgresSql.secretOutputs.connectionString}",
832+
"path": "postgresSql.module.bicep",
833+
"params": {
834+
"administratorLogin": "{existingUserName.value}",
835+
"administratorLoginPassword": "{existingPassword.value}",
836+
"existingResourceName": "{existingResourceName.value}",
837+
"keyVaultName": ""
838+
},
839+
"scope": {
840+
"resourceGroup": "{existingResourceGroupName.value}"
841+
}
842+
}
843+
""";
844+
845+
Assert.Equal(expectedManifest, ManifestNode.ToString());
846+
847+
var expectedBicep = """
848+
@description('The location for the resource(s) to be deployed.')
849+
param location string = resourceGroup().location
850+
851+
param existingResourceName string
852+
853+
param administratorLogin string
854+
855+
@secure()
856+
param administratorLoginPassword string
857+
858+
param keyVaultName string
859+
860+
resource postgresSql 'Microsoft.DBforPostgreSQL/flexibleServers@2024-08-01' existing = {
861+
name: existingResourceName
862+
}
863+
864+
resource postgreSqlFirewallRule_AllowAllAzureIps 'Microsoft.DBforPostgreSQL/flexibleServers/firewallRules@2024-08-01' = {
865+
name: 'AllowAllAzureIps'
866+
properties: {
867+
endIpAddress: '0.0.0.0'
868+
startIpAddress: '0.0.0.0'
869+
}
870+
parent: postgresSql
871+
}
872+
873+
resource keyVault 'Microsoft.KeyVault/vaults@2023-07-01' existing = {
874+
name: keyVaultName
875+
}
876+
877+
resource connectionString 'Microsoft.KeyVault/vaults/secrets@2023-07-01' = {
878+
name: 'connectionString'
879+
properties: {
880+
value: 'Host=${postgresSql.properties.fullyQualifiedDomainName};Username=${administratorLogin};Password=${administratorLoginPassword}'
881+
}
882+
parent: keyVault
883+
}
884+
""";
885+
886+
output.WriteLine(BicepText);
887+
Assert.Equal(expectedBicep, BicepText);
888+
}
889+
818890
[Fact]
819891
public async Task SupportsExistingAzureSearchWithResourceGroup()
820892
{

0 commit comments

Comments
 (0)