Skip to content
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

Disables Integrated Security in the ConnectionString when using SQL hosting integrations. #6839

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"resources": {
"sql1": {
"type": "container.v0",
"connectionString": "Server={sql1.bindings.tcp.host},{sql1.bindings.tcp.port};User ID=sa;Password={sql1-password.value};TrustServerCertificate=true",
"connectionString": "Server={sql1.bindings.tcp.host},{sql1.bindings.tcp.port};User ID=sa;Password={sql1-password.value};TrustServerCertificate=true;Integrated Security=false",
"image": "mcr.microsoft.com/mssql/server:2022-latest",
"env": {
"ACCEPT_EULA": "Y",
Expand Down Expand Up @@ -77,4 +77,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"sql2": {
"type": "container.v0",
"connectionString": "Server={sql2.bindings.tcp.host},{sql2.bindings.tcp.port};User ID=sa;Password={sql2-password.value};TrustServerCertificate=true",
"connectionString": "Server={sql2.bindings.tcp.host},{sql2.bindings.tcp.port};User ID=sa;Password={sql2-password.value};TrustServerCertificate=true;Integrated Security=false",
"image": "mcr.microsoft.com/mssql/server:2022-latest",
"env": {
"ACCEPT_EULA": "Y",
Expand Down Expand Up @@ -106,4 +106,4 @@
}
}
}
}
}
4 changes: 2 additions & 2 deletions src/Aspire.Hosting.SqlServer/SqlServerServerResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public SqlServerServerResource(string name, ParameterResource password) : base(n

private ReferenceExpression ConnectionString =>
ReferenceExpression.Create(
$"Server={PrimaryEndpoint.Property(EndpointProperty.IPV4Host)},{PrimaryEndpoint.Property(EndpointProperty.Port)};User ID=sa;Password={PasswordParameter};TrustServerCertificate=true");
$"Server={PrimaryEndpoint.Property(EndpointProperty.IPV4Host)},{PrimaryEndpoint.Property(EndpointProperty.Port)};User ID=sa;Password={PasswordParameter};TrustServerCertificate=true;Integrated Security=false");

/// <summary>
/// Gets the connection string expression for the SQL Server.
Expand All @@ -57,7 +57,7 @@ public ReferenceExpression ConnectionStringExpression
/// Gets the connection string for the SQL Server.
/// </summary>
/// <param name="cancellationToken"> A <see cref="CancellationToken"/> to observe while waiting for the task to complete.</param>
/// <returns>A connection string for the SQL Server in the form "Server=host,port;User ID=sa;Password=password;TrustServerCertificate=true".</returns>
/// <returns>A connection string for the SQL Server in the form "Server=host,port;User ID=sa;Password=password;TrustServerCertificate=true;Integrated Security=false".</returns>
public ValueTask<string?> GetConnectionStringAsync(CancellationToken cancellationToken = default)
{
if (this.TryGetLastAnnotation<ConnectionStringRedirectAnnotation>(out var connectionStringAnnotation))
Expand Down
6 changes: 3 additions & 3 deletions tests/Aspire.Hosting.Azure.Tests/AzureSqlExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,15 @@ public async Task AddAzureSqlServerRunAsContainerProducesCorrectConnectionString
Assert.True(sql.Resource.IsContainer(), "The resource should now be a container resource.");
var serverConnectionString = await sql.Resource.ConnectionStringExpression.GetValueAsync(CancellationToken.None);
Assert.StartsWith("Server=127.0.0.1,12455;User ID=sa;Password=", serverConnectionString);
Assert.EndsWith(";TrustServerCertificate=true", serverConnectionString);
Assert.EndsWith(";TrustServerCertificate=true;Integrated Security=false", serverConnectionString);

var db1ConnectionString = await db1.Resource.ConnectionStringExpression.GetValueAsync(CancellationToken.None);
Assert.StartsWith("Server=127.0.0.1,12455;User ID=sa;Password=", db1ConnectionString);
Assert.EndsWith(";TrustServerCertificate=true;Database=db1", db1ConnectionString);
Assert.EndsWith(";TrustServerCertificate=true;Integrated Security=false;Database=db1", db1ConnectionString);

var db2ConnectionString = await db2.Resource.ConnectionStringExpression.GetValueAsync(CancellationToken.None);
Assert.StartsWith("Server=127.0.0.1,12455;User ID=sa;Password=", db2ConnectionString);
Assert.EndsWith(";TrustServerCertificate=true;Database=db2Name", db2ConnectionString);
Assert.EndsWith(";TrustServerCertificate=true;Integrated Security=false;Database=db2Name", db2ConnectionString);
}

[Theory]
Expand Down
10 changes: 5 additions & 5 deletions tests/Aspire.Hosting.SqlServer.Tests/AddSqlServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ public async Task SqlServerCreatesConnectionString()
var connectionStringResource = Assert.Single(appModel.Resources.OfType<SqlServerServerResource>());
var connectionString = await connectionStringResource.GetConnectionStringAsync(default);

Assert.Equal("Server=127.0.0.1,1433;User ID=sa;Password=p@ssw0rd1;TrustServerCertificate=true", connectionString);
Assert.Equal("Server={sqlserver.bindings.tcp.host},{sqlserver.bindings.tcp.port};User ID=sa;Password={pass.value};TrustServerCertificate=true", connectionStringResource.ConnectionStringExpression.ValueExpression);
Assert.Equal("Server=127.0.0.1,1433;User ID=sa;Password=p@ssw0rd1;TrustServerCertificate=true;Integrated Security=false", connectionString);
Assert.Equal("Server={sqlserver.bindings.tcp.host},{sqlserver.bindings.tcp.port};User ID=sa;Password={pass.value};TrustServerCertificate=true;Integrated Security=false", connectionStringResource.ConnectionStringExpression.ValueExpression);
}

[Fact]
Expand All @@ -118,7 +118,7 @@ public async Task SqlServerDatabaseCreatesConnectionString()
var connectionStringResource = (IResourceWithConnectionString)sqlResource;
var connectionString = await connectionStringResource.GetConnectionStringAsync();

Assert.Equal("Server=127.0.0.1,1433;User ID=sa;Password=p@ssw0rd1;TrustServerCertificate=true;Database=mydb", connectionString);
Assert.Equal("Server=127.0.0.1,1433;User ID=sa;Password=p@ssw0rd1;TrustServerCertificate=true;Integrated Security=false;Database=mydb", connectionString);
Assert.Equal("{sqlserver.connectionString};Database=mydb", connectionStringResource.ConnectionStringExpression.ValueExpression);
}

Expand All @@ -135,7 +135,7 @@ public async Task VerifyManifest()
var expectedManifest = $$"""
{
"type": "container.v0",
"connectionString": "Server={sqlserver.bindings.tcp.host},{sqlserver.bindings.tcp.port};User ID=sa;Password={sqlserver-password.value};TrustServerCertificate=true",
"connectionString": "Server={sqlserver.bindings.tcp.host},{sqlserver.bindings.tcp.port};User ID=sa;Password={sqlserver-password.value};TrustServerCertificate=true;Integrated Security=false",
"image": "{{SqlServerContainerImageTags.Registry}}/{{SqlServerContainerImageTags.Image}}:{{SqlServerContainerImageTags.Tag}}",
"env": {
"ACCEPT_EULA": "Y",
Expand Down Expand Up @@ -175,7 +175,7 @@ public async Task VerifyManifestWithPasswordParameter()
var expectedManifest = $$"""
{
"type": "container.v0",
"connectionString": "Server={sqlserver.bindings.tcp.host},{sqlserver.bindings.tcp.port};User ID=sa;Password={pass.value};TrustServerCertificate=true",
"connectionString": "Server={sqlserver.bindings.tcp.host},{sqlserver.bindings.tcp.port};User ID=sa;Password={pass.value};TrustServerCertificate=true;Integrated Security=false",
"image": "{{SqlServerContainerImageTags.Registry}}/{{SqlServerContainerImageTags.Image}}:{{SqlServerContainerImageTags.Tag}}",
"env": {
"ACCEPT_EULA": "Y",
Expand Down
2 changes: 1 addition & 1 deletion tests/testproject/TestProject.AppHost/aspire-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
},
"sqlserver": {
"type": "container.v0",
"connectionString": "Server={sqlserver.bindings.tcp.host},{sqlserver.bindings.tcp.port};User ID=sa;Password={sqlserver-password.value};TrustServerCertificate=true",
"connectionString": "Server={sqlserver.bindings.tcp.host},{sqlserver.bindings.tcp.port};User ID=sa;Password={sqlserver-password.value};TrustServerCertificate=true;Integrated Security=false",
"image": "mcr.microsoft.com/mssql/server:2022-latest",
"env": {
"ACCEPT_EULA": "Y",
Expand Down