-
Notifications
You must be signed in to change notification settings - Fork 843
Use DevCert for SQL Server #13131
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
Open
afscrome
wants to merge
2
commits into
microsoft:main
Choose a base branch
from
afscrome:sqltls
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Use DevCert for SQL Server #13131
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
21 changes: 13 additions & 8 deletions
21
...oEnd/SqlServerEndToEnd.AppHost/Program.cs → ...oEnd/SqlServerEndToEnd.AppHost/AppHost.cs
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
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
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 |
|---|---|---|
|
|
@@ -9,9 +9,12 @@ | |
| using Microsoft.Data.SqlClient; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; | ||
| using Aspire.Hosting.Utils; | ||
|
|
||
| namespace Aspire.Hosting; | ||
|
|
||
| #pragma warning disable ASPIRECERTIFICATES001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed. | ||
|
|
||
| /// <summary> | ||
| /// Provides extension methods for adding SQL Server resources to the application model. | ||
| /// </summary> | ||
|
|
@@ -49,7 +52,7 @@ public static IResourceBuilder<SqlServerServerResource> AddSqlServer(this IDistr | |
| var healthCheckKey = $"{name}_check"; | ||
| builder.Services.AddHealthChecks().AddSqlServer(sp => connectionString ?? throw new InvalidOperationException("Connection string is unavailable"), name: healthCheckKey); | ||
|
|
||
| return builder.AddResource(sqlServer) | ||
| var sqlBuilder = builder.AddResource(sqlServer) | ||
| .WithEndpoint(port: port, targetPort: 1433, name: SqlServerServerResource.PrimaryEndpointName) | ||
| .WithImage(SqlServerContainerImageTags.Image, SqlServerContainerImageTags.Tag) | ||
| .WithImageRegistry(SqlServerContainerImageTags.Registry) | ||
|
|
@@ -89,6 +92,55 @@ public static IResourceBuilder<SqlServerServerResource> AddSqlServer(this IDistr | |
| await CreateDatabaseAsync(sqlConnection, sqlDatabase, @event.Services, ct).ConfigureAwait(false); | ||
| } | ||
| }); | ||
|
|
||
| return sqlBuilder | ||
| .SubscribeHttpsEndpointsUpdate(ctx => | ||
| { | ||
| var executionContext = ctx.Services.GetRequiredService<DistributedApplicationExecutionContext>(); | ||
|
|
||
| // Dev cert versions prior to 6 don't include "127.0.0.1" in the SAN, and so won't be trusted | ||
| // So only enable TLS if we have a custom cert, or a dev cert with version 6 or higher. | ||
| if (executionContext.IsRunMode) | ||
| { | ||
| ctx.Resource.TryGetLastAnnotation<HttpsCertificateAnnotation>(out var certAnnotation); | ||
|
|
||
| if (certAnnotation == null || certAnnotation.UseDeveloperCertificate == true) | ||
| { | ||
| var devCertService = ctx.Services.GetRequiredService<IDeveloperCertificateService>(); | ||
| var cert = devCertService.Certificates.First(); | ||
| if (cert.GetCertificateVersion() < 6) | ||
| { | ||
|
Comment on lines
+109
to
+112
|
||
| return; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| sqlBuilder.WithEndpoint(SqlServerServerResource.PrimaryEndpointName, x => x.TlsEnabled = true); | ||
| }) | ||
| .WithContainerFiles("/var/opt/mssql/", async (ctx, ct) => | ||
| { | ||
| var certContext = ctx.HttpsCertificateContext; | ||
|
|
||
| if (certContext is null) | ||
| { | ||
| return []; | ||
| } | ||
|
|
||
| var config = $""" | ||
| [network] | ||
| tlscert = {await certContext.CertificatePath.GetValueAsync(ct).ConfigureAwait(false)} | ||
| tlskey = {await certContext.KeyPath.GetValueAsync(ct).ConfigureAwait(false)} | ||
| forceencryption = 1 | ||
| """; | ||
afscrome marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return [ | ||
| new ContainerFile | ||
| { | ||
| Name = "mssql.conf", | ||
| Contents = config | ||
| } | ||
| ]; | ||
| }); | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
||
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
Oops, something went wrong.
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.
In run mode, the dev-cert version gate can be skipped when an
HttpsCertificateAnnotationexists withUseDeveloperCertificate == null(default behavior). In that case, TLS gets enabled even for older dev certs, and the connection string will omitTrustServerCertificate, which is what this gate is trying to avoid. Consider computing whether a dev cert is in use viaUseDeveloperCertificate.GetValueOrDefault(devCertService.UseForHttps)(andCertificate is null) rather than checking only== true.