-
Notifications
You must be signed in to change notification settings - Fork 694
Don't set properties on existing Azure SQL server resources #7705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6a7019b
Don't set properties on existing Azure SQL server resources
captainsafia 8d73431
Set admin for existing resources correctly
captainsafia 67ce9e4
Add workaround reference for Azure SDK issue
captainsafia 57ded95
PR feedback.
eerhardt b58fca3
Remove `principalType` parameter from test assertion
captainsafia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
using Aspire.Hosting.Azure; | ||
using Azure.Provisioning; | ||
using Azure.Provisioning.Expressions; | ||
using Azure.Provisioning.Primitives; | ||
using Azure.Provisioning.Sql; | ||
|
||
namespace Aspire.Hosting; | ||
|
@@ -205,14 +206,6 @@ private static void CreateSqlServer( | |
{ | ||
var resource = SqlServer.FromExisting(identifier); | ||
resource.Name = name; | ||
resource.Administrators = new ServerExternalAdministrator() | ||
{ | ||
AdministratorType = SqlAdministratorType.ActiveDirectory, | ||
IsAzureADOnlyAuthenticationEnabled = true, | ||
Sid = principalIdParameter, | ||
Login = principalNameParameter, | ||
TenantId = BicepFunction.GetSubscription().TenantId | ||
}; | ||
return resource; | ||
}, | ||
(infrastructure) => | ||
|
@@ -234,6 +227,20 @@ private static void CreateSqlServer( | |
}; | ||
}); | ||
|
||
// If the resource is an existing resource, we model the administrator access | ||
// for the managed identity as an "edge" between the parent SqlServer resource | ||
// and a custom SqlServerAzureADAdministrator resource. | ||
if (sqlServer.IsExistingResource) | ||
{ | ||
var admin = new SqlServerAzureADAdministratorWorkaround($"{sqlServer.BicepIdentifier}_admin") | ||
{ | ||
ParentOverride = sqlServer, | ||
LoginOverride = principalNameParameter, | ||
SidOverride = principalIdParameter | ||
}; | ||
infrastructure.Add(admin); | ||
} | ||
|
||
infrastructure.Add(new SqlFirewallRule("sqlFirewallRule_AllowAllAzureIps") | ||
{ | ||
Parent = sqlServer, | ||
|
@@ -244,11 +251,15 @@ private static void CreateSqlServer( | |
|
||
if (distributedApplicationBuilder.ExecutionContext.IsRunMode) | ||
{ | ||
// When in run mode we inject the users identity and we need to specify | ||
// the principalType. | ||
var principalTypeParameter = new ProvisioningParameter(AzureBicepResource.KnownParameters.PrincipalType, typeof(string)); | ||
infrastructure.Add(principalTypeParameter); | ||
sqlServer.Administrators.PrincipalType = principalTypeParameter; | ||
// Avoid mutating properties on existing resources. | ||
if (!sqlServer.IsExistingResource) | ||
{ | ||
// When in run mode we inject the users identity and we need to specify | ||
// the principalType. | ||
var principalTypeParameter = new ProvisioningParameter(AzureBicepResource.KnownParameters.PrincipalType, typeof(string)); | ||
infrastructure.Add(principalTypeParameter); | ||
sqlServer.Administrators.PrincipalType = principalTypeParameter; | ||
} | ||
|
||
infrastructure.Add(new SqlFirewallRule("sqlFirewallRule_AllowAllIps") | ||
{ | ||
|
@@ -273,4 +284,80 @@ private static void CreateSqlServer( | |
|
||
infrastructure.Add(new ProvisioningOutput("sqlServerFqdn", typeof(string)) { Value = sqlServer.FullyQualifiedDomainName }); | ||
} | ||
|
||
/// <remarks> | ||
/// Workaround for issue using SqlServerAzureADAdministrator. | ||
/// See https://github.com/Azure/azure-sdk-for-net/issues/48364 for more information. | ||
/// </remarks> | ||
private sealed class SqlServerAzureADAdministratorWorkaround(string bicepIdentifier) : SqlServerAzureADAdministrator(bicepIdentifier) | ||
{ | ||
private BicepValue<string>? _name; | ||
private BicepValue<string>? _login; | ||
private BicepValue<Guid>? _sid; | ||
private ResourceReference<SqlServer>? _parent; | ||
|
||
/// <summary> | ||
/// Login name of the server administrator. | ||
/// </summary> | ||
public BicepValue<string> LoginOverride | ||
{ | ||
get | ||
{ | ||
Initialize(); | ||
return _login!; | ||
} | ||
set | ||
{ | ||
Initialize(); | ||
_login!.Assign(value); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// SID (object ID) of the server administrator. | ||
/// </summary> | ||
public BicepValue<Guid> SidOverride | ||
{ | ||
get | ||
{ | ||
Initialize(); | ||
return _sid!; | ||
} | ||
set | ||
{ | ||
Initialize(); | ||
_sid!.Assign(value); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Parent resource of the server administrator. | ||
/// </summary> | ||
public SqlServer? ParentOverride | ||
{ | ||
get | ||
{ | ||
Initialize(); | ||
return _parent!.Value; | ||
} | ||
set | ||
{ | ||
Initialize(); | ||
_parent!.Value = value; | ||
} | ||
} | ||
|
||
private static BicepValue<string> GetNameDefaultValue() | ||
{ | ||
return new StringLiteralExpression("ActiveDirectory"); | ||
} | ||
|
||
protected override void DefineProvisionableProperties() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tg-msft this is the workaround, notice we don't call |
||
{ | ||
_name = DefineProperty("Name", ["name"], defaultValue: GetNameDefaultValue()); | ||
_login = DefineProperty<string>("Login", ["properties", "login"]); | ||
_sid = DefineProperty<Guid>("Sid", ["properties", "sid"]); | ||
_parent = DefineResource<SqlServer>("Parent", ["parent"], isOutput: false, isRequired: true); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI @tg-msft - this is how we figured to work around this issue.