-
Notifications
You must be signed in to change notification settings - Fork 727
Add option to ignore Cosmos emulator certificate. #1668
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
Changes from all commits
6630c25
d0ef65b
0a0674a
21264bd
6617eb6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Aspire.Hosting.Azure.Cosmos; | ||
| using Aspire.Microsoft.Azure.Cosmos; | ||
| using Azure.Identity; | ||
| using Microsoft.Azure.Cosmos; | ||
|
|
@@ -92,6 +93,16 @@ private static void AddAzureCosmosDB( | |
| }); | ||
| } | ||
|
|
||
| if (settings.IgnoreEmulatorCertificate && CosmosUtils.IsEmulatorConnectionString(settings.ConnectionString)) | ||
| { | ||
| clientOptions.HttpClientFactory = () => new HttpClient(new HttpClientHandler() | ||
| { | ||
| ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator | ||
| }); | ||
| clientOptions.ConnectionMode = ConnectionMode.Gateway; | ||
| clientOptions.LimitToEndpoint = true; | ||
|
Member
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. This line isn't in It just has the
Member
Author
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. I'm not 100% sure of the reason why but without
Member
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. Should the docs be updated then? - cc @Pilchie @sourabh1007 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. Looks like document is already updated and
Member
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.
Then why is @mitchdenny saying it is required above?
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. @eerhardt docs was update just a day back or so. Emulator will only have a single region; functional wise it will not impact but unnecessary and confuses. Ability to ignore SSLCert through connection string is currently in PR stage and next release might ship it. Post that hopefully all above can be updated accordingly.
Member
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. @mitchdenny - can you verify that we don't need the line setting 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. Whoever wrote those docs did not consult with the SDK team. The original version of that doc had it and that might have brought the confusion. We discovered the issue, we corrected the docs (https://github.com/MicrosoftDocs/azure-docs-pr/pull/263406). |
||
| } | ||
|
|
||
| configureClientOptions?.Invoke(clientOptions); | ||
|
|
||
| if (serviceKey is null) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -109,6 +109,26 @@ The `AddAzureCosmosDB` method will read connection information from the AppHost' | |
| builder.AddCosmosDbContext<MyDbContext>("cosmosdb"); | ||
| ``` | ||
|
|
||
| ### Emulator usage | ||
|
|
||
| Aspire supports the usage of the Azure Cosmos DB emulator to use the emulator, add the following to your AppHost project: | ||
|
|
||
| ```csharp | ||
| // AppHost | ||
| var cosmosdb = builder.AddAzureCosmosDB("cosmos").UseEmulator(); | ||
| ``` | ||
|
|
||
| When the AppHost starts up a local container running the Azure CosmosDB will also be started. Inside the project that uses CosmosDB you also need to specify that you want to ignore the server certificate (so you don't need to manually download and install it): | ||
|
Member
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. This should have had the same changes as the other README. |
||
|
|
||
| ```csharp | ||
| // Service code | ||
| builder.AddCosmosDbContext<MyDbContext>("cosmos", "mydb", (settings) => | ||
| { | ||
| settings.IgnoreEmulatorCertificate = true; | ||
| }); | ||
|
|
||
| ``` | ||
|
|
||
| ## Additional documentation | ||
|
|
||
| * https://learn.microsoft.com/ef/core/ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace Aspire.Hosting.Azure.Cosmos; | ||
|
|
||
| internal static class CosmosConstants | ||
| { | ||
| /// <summary> | ||
| /// Gets the well-known and documented Azure Cosmos DB emulator account key. | ||
| /// See <a href="https://learn.microsoft.com/azure/cosmos-db/emulator#authentication"></a> | ||
| /// </summary> | ||
| internal const string EmulatorAccountKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Data.Common; | ||
|
|
||
| namespace Aspire.Hosting.Azure.Cosmos; | ||
|
|
||
| internal static class CosmosUtils | ||
| { | ||
| internal static bool IsEmulatorConnectionString(string? connectionString) | ||
| { | ||
| if (connectionString == null) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| var builder = new DbConnectionStringBuilder(); | ||
| builder.ConnectionString = connectionString; | ||
| var accountKeyFromConnectionString = builder["AccountKey"].ToString(); | ||
mitchdenny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return accountKeyFromConnectionString == CosmosConstants.EmulatorAccountKey; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Aspire.Hosting.Tests.Helpers; | ||
| using Polly; | ||
| using Polly.Retry; | ||
| using Xunit; | ||
|
|
||
| namespace Aspire.Hosting.Tests.Cosmos; | ||
|
|
||
| [Collection("IntegrationServices")] | ||
| public class CosmosFunctionalTests | ||
| { | ||
| private readonly IntegrationServicesFixture _integrationServicesFixture; | ||
|
|
||
| public CosmosFunctionalTests(IntegrationServicesFixture integrationServicesFixture) | ||
| { | ||
| _integrationServicesFixture = integrationServicesFixture; | ||
| } | ||
|
|
||
| [LocalOnlyFact()] | ||
| public async Task VerifyCosmosWorks() | ||
| { | ||
| var testProgram = _integrationServicesFixture.TestProgram; | ||
| var client = _integrationServicesFixture.HttpClient; | ||
|
|
||
| using var cts = new CancellationTokenSource(TimeSpan.FromMinutes(6)); | ||
|
|
||
| await RetryPolicy.Handle<HttpRequestException>() | ||
| .WaitAndRetryAsync(20, (count) => TimeSpan.FromSeconds(15)) | ||
mitchdenny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .ExecuteAsync(async () => | ||
| { | ||
| var response = await testProgram.IntegrationServiceABuilder!.HttpGetAsync(client, "http", "/cosmos/verify", cts.Token); | ||
| response.EnsureSuccessStatusCode(); | ||
|
|
||
| var responseContent = await response.Content.ReadAsStringAsync(); | ||
| Assert.True(response.IsSuccessStatusCode, responseContent); | ||
| }); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Microsoft.Azure.Cosmos; | ||
|
|
||
| public static class CosmosExtensions | ||
| { | ||
| public static void MapCosmosApi(this WebApplication app) | ||
| { | ||
| app.MapGet("/cosmos/verify", VerifyCosmosAsync); | ||
| } | ||
|
|
||
| private static async Task<IResult> VerifyCosmosAsync(CosmosClient cosmosClient) | ||
| { | ||
| try | ||
| { | ||
| var db = (await cosmosClient.CreateDatabaseIfNotExistsAsync("db")).Database; | ||
| var container = (await db.CreateContainerIfNotExistsAsync("todos", "/id")).Container; | ||
|
|
||
| var id = Guid.NewGuid().ToString(); | ||
| var title = "Do some work."; | ||
|
|
||
| var item = await container.CreateItemAsync(new | ||
| { | ||
| id = id, | ||
| title = title | ||
| }); | ||
|
|
||
| return item.Resource.id == id ? Results.Ok() : Results.Problem(); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| return Results.Problem(e.ToString()); | ||
| } | ||
| } | ||
| } |

Uh oh!
There was an error while loading. Please reload this page.