Skip to content

Commit b030fef

Browse files
committed
Set admin for existing resources correctly
1 parent 6a7019b commit b030fef

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

src/Aspire.Hosting.Azure.Sql/AzureSqlExtensions.cs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Aspire.Hosting.Azure;
66
using Azure.Provisioning;
77
using Azure.Provisioning.Expressions;
8+
using Azure.Provisioning.Primitives;
89
using Azure.Provisioning.Sql;
910

1011
namespace Aspire.Hosting;
@@ -226,6 +227,20 @@ private static void CreateSqlServer(
226227
};
227228
});
228229

230+
// If the resource is an existing resource, we model the administrator access
231+
// for the managed identity as an "edge" between the parent SqlServer resource
232+
// and a custom SqlServerAzureADAdministrator resource.
233+
if (sqlServer.IsExistingResource)
234+
{
235+
var admin = new SqlServerAzureADAdministratorWorkaround($"{sqlServer.BicepIdentifier}_admin")
236+
{
237+
ParentOverride = sqlServer,
238+
LoginOverride = principalNameParameter,
239+
SidOverride = principalIdParameter
240+
};
241+
infrastructure.Add(admin);
242+
}
243+
229244
infrastructure.Add(new SqlFirewallRule("sqlFirewallRule_AllowAllAzureIps")
230245
{
231246
Parent = sqlServer,
@@ -268,4 +283,79 @@ private static void CreateSqlServer(
268283

269284
infrastructure.Add(new ProvisioningOutput("sqlServerFqdn", typeof(string)) { Value = sqlServer.FullyQualifiedDomainName });
270285
}
286+
287+
/// <remarks>
288+
/// Workaround for immutable properties on SqlServerAzureADAdministrator.
289+
/// </remarks>
290+
private sealed class SqlServerAzureADAdministratorWorkaround(string bicepIdentifier) : SqlServerAzureADAdministrator(bicepIdentifier)
291+
{
292+
private BicepValue<string>? _name;
293+
private BicepValue<string>? _login;
294+
private BicepValue<Guid>? _sid;
295+
private ResourceReference<SqlServer>? _parent;
296+
297+
/// <summary>
298+
/// Login name of the server administrator.
299+
/// </summary>
300+
public BicepValue<string> LoginOverride
301+
{
302+
get
303+
{
304+
Initialize();
305+
return _login!;
306+
}
307+
set
308+
{
309+
Initialize();
310+
_login!.Assign(value);
311+
}
312+
}
313+
314+
/// <summary>
315+
/// SID (object ID) of the server administrator.
316+
/// </summary>
317+
public BicepValue<Guid> SidOverride
318+
{
319+
get
320+
{
321+
Initialize();
322+
return _sid!;
323+
}
324+
set
325+
{
326+
Initialize();
327+
_sid!.Assign(value);
328+
}
329+
}
330+
331+
/// <summary>
332+
/// Parent resource of the server administrator.
333+
/// </summary>
334+
public SqlServer? ParentOverride
335+
{
336+
get
337+
{
338+
Initialize();
339+
return _parent!.Value;
340+
}
341+
set
342+
{
343+
Initialize();
344+
_parent!.Value = value;
345+
}
346+
}
347+
348+
private static BicepValue<string> GetNameDefaultValue()
349+
{
350+
return new StringLiteralExpression("ActiveDirectory");
351+
}
352+
353+
protected override void DefineProvisionableProperties()
354+
{
355+
_name = DefineProperty("Name", ["name"], defaultValue: GetNameDefaultValue());
356+
_login = DefineProperty<string>("Login", ["properties", "login"]);
357+
_sid = DefineProperty<Guid>("Sid", ["properties", "sid"]);
358+
_parent = DefineResource<SqlServer>("Parent", ["parent"], isOutput: false, isRequired: true);
359+
}
360+
}
271361
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,15 @@ param existingResourceName string
11241124
name: existingResourceName
11251125
}
11261126
1127+
resource sqlServer_admin 'Microsoft.Sql/servers/administrators@2021-11-01' = {
1128+
name: 'ActiveDirectory'
1129+
properties: {
1130+
login: principalName
1131+
sid: principalId
1132+
}
1133+
parent: sqlServer
1134+
}
1135+
11271136
resource sqlFirewallRule_AllowAllAzureIps 'Microsoft.Sql/servers/firewallRules@2021-11-01' = {
11281137
name: 'AllowAllAzureIps'
11291138
properties: {
@@ -1183,6 +1192,15 @@ param principalType string
11831192
name: existingResourceName
11841193
}
11851194
1195+
resource sqlServer_admin 'Microsoft.Sql/servers/administrators@2021-11-01' = {
1196+
name: 'ActiveDirectory'
1197+
properties: {
1198+
login: principalName
1199+
sid: principalId
1200+
}
1201+
parent: sqlServer
1202+
}
1203+
11861204
resource sqlFirewallRule_AllowAllAzureIps 'Microsoft.Sql/servers/firewallRules@2021-11-01' = {
11871205
name: 'AllowAllAzureIps'
11881206
properties: {

0 commit comments

Comments
 (0)