Skip to content

Improve managed identity for sql server #9041

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 44 commits into from
May 9, 2025
Merged

Improve managed identity for sql server #9041

merged 44 commits into from
May 9, 2025

Conversation

sebastienros
Copy link
Member

@sebastienros sebastienros commented Apr 30, 2025

Description

Please include a summary of the changes and the related issue. Please also include relevant motivation and context. List any dependencies that are required for this change.

Fixes #8389

Remaining tasks:

  • Document changes (de-breaking)

Checklist

  • Is this feature complete?
    • Yes. Ready to ship.
    • No. Follow-up changes expected.
  • Are you including unit tests for the changes and scenario tests if relevant?
    • Yes
    • No
  • Did you add public API?
    • Yes
      • If yes, did you have an API Review for it?
        • Yes
        • No
      • Did you add <remarks /> and <code /> elements on your triple slash comments?
        • Yes
        • No
    • No
  • Does the change make any security assumptions or guarantees?
    • Yes
      • If yes, have you done a threat model and had a security review?
        • Yes
        • No
    • No
  • Does the change require an update in our Aspire docs?

@github-actions github-actions bot added the area-integrations Issues pertaining to Aspire Integrations packages label Apr 30, 2025
@sebastienros sebastienros changed the title Sebros/bicep Improve managed identity for container apps and sql server Apr 30, 2025
@sebastienros sebastienros requested a review from eerhardt April 30, 2025 23:10
# Conflicts:
#	src/Aspire.Hosting.Azure/AzurePublisher.cs
@eerhardt
Copy link
Member

eerhardt commented May 8, 2025

This code can now be deleted.

// 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;
}

}
kind: 'AzurePowerShell'
properties: {
scriptContent: '\$sqlServerFqdn = "\$env:DBSERVER"\r\n\$sqlDatabaseName = "\$env:DBNAME"\r\n\$principalName = "\$env:PRINCIPALNAME"\r\n\$id = "\$env:ID"\r\n\r\n# Install SqlServer module\r\nInstall-Module -Name SqlServer -Force -AllowClobber -Scope CurrentUser\r\nImport-Module SqlServer\r\n\r\n\$sqlCmd = @"\r\nDECLARE @name SYSNAME = \'\$principalName\';\r\nDECLARE @id UNIQUEIDENTIFIER = \'\$id\';\r\n\r\n-- Convert the guid to the right type\r\nDECLARE @castId NVARCHAR(MAX) = CONVERT(VARCHAR(MAX), CONVERT (VARBINARY(16), @id), 1);\r\n\r\n-- Construct command: CREATE USER [@name] WITH SID = @castId, TYPE = E;\r\nDECLARE @cmd NVARCHAR(MAX) = N\'CREATE USER [\' + @name + \'] WITH SID = \' + @castId + \', TYPE = E;\'\r\nEXEC (@cmd);\r\n\r\n-- Assign roles to the new user\r\nDECLARE @role1 NVARCHAR(MAX) = N\'ALTER ROLE db_owner ADD MEMBER [\' + @name + \']\';\r\nEXEC (@role1);\r\n\r\n"@\r\n# Note: the string terminator must not have whitespace before it, therefore it is not indented.\r\n\r\nWrite-Host \$sqlCmd\r\n\r\n\$connectionString = "Server=tcp:\${sqlServerFqdn},1433;Initial Catalog=\${sqlDatabaseName};Authentication=Active Directory Default;"\r\n\r\nInvoke-Sqlcmd -ConnectionString \$connectionString -Query \$sqlCmd'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to make this scriptContent readable? e.g. have newlines in it. I assume not, but needed to ask.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a CDK limitation, we can set a bicep string value to the property, which will be encoded in bicep, but I don't know if we can set the literal that has the multi-line string (line in c#) fragment. @tg-msft

@sebastienros sebastienros marked this pull request as ready for review May 8, 2025 22:50
@sebastienros
Copy link
Member Author

@eerhardt things for closing:

  • Rename AppHost1 project? Remove this project? (2 app containers with 1 sql)
  • Remove "Free sql tier" changes and wait for the other PR to get in?
  • File doc issues
  • anything else?

@@ -1,12 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- https://learn.microsoft.com/dotnet/fundamentals/package-validation/diagnostic-ids -->
<Suppressions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!-- This API shipped as experimental in 9.2 so it is okay to remove. -->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this comment should stay

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also added a comment for the new suppression

@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you revert this? git checkout <sha-before-your-changes> -- src/Aspire.Hosting.Azure.Sql/Aspire.Hosting.Azure.Sql.csproj

_sid = DefineProperty<Guid>("Sid", ["properties", "sid"]);
_parent = DefineResource<SqlServer>("Parent", ["parent"], isOutput: false, isRequired: true);
}
infrastructure.Add(new ProvisioningOutput("sqlServerAdminName", typeof(string)) { Value = sqlServer.Administrators.Login });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this output for an existing SQL Server that has a username/password administrator? The username?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, the username, but I believe this case is fine since in that case we don't try to update the resource.

{
Name = BicepFunction.Take(BicepFunction.Interpolate($"script-{BicepFunction.GetUniqueString(this.GetBicepIdentifier(), roleAssignmentContext.PrincipalName, new StringLiteralExpression(database), BicepFunction.GetResourceGroup().Id)}"), 24),
Kind = "AzurePowerShell",
AZPowerShellVersion = "7.4"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do we keep this up-to-date?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated it to the latest right now and added a comment with the official list. I assume we'll be able to remove it once the resource is supported in the CDK.

# Conflicts:
#	src/Aspire.Hosting.Azure.Sql/AzureSqlExtensions.cs
#	tests/Aspire.Hosting.Azure.Tests/AzureSqlExtensionsTests.cs
@sebastienros sebastienros merged commit b502360 into main May 9, 2025
170 checks passed
@sebastienros sebastienros deleted the sebros/bicep branch May 9, 2025 23:34
@davidfowl davidfowl changed the title Improve managed identity for container apps and sql server Improve managed identity for sql server May 10, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Jun 9, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-integrations Issues pertaining to Aspire Integrations packages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using multiple apps to talk to Azure SQL Server doesn't work with managed identites
4 participants