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

Add support for managed identity in Azure Cosmos DB hosting component #7092

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

sebastienros
Copy link
Member

Description

Fixes #3792

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?

@@ -205,7 +156,7 @@ public static IResourceBuilder<AzureCosmosDBEmulatorResource> WithGatewayPort(th
/// <param name="builder">Builder for the Cosmos emulator container</param>
/// <param name="count">Desired partition count.</param>
/// <returns>Cosmos emulator resource builder.</returns>
/// <remarks>Not calling this method will result in the default of 25 partitions. The actual started partitions is always one more than specified.
/// <remarks>Not calling this method will result in the default of 10 partitions. The actual started partitions is always one more than specified.
Copy link
Member Author

Choose a reason for hiding this comment

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

11 (actual) is the default number of partitions when nothing is set, so updated to 10 since the actual is argument + 1

@danmoseley danmoseley added the area-integrations Issues pertaining to Aspire Integrations packages label Jan 13, 2025

internal EndpointReference EmulatorEndpoint => new(this, "emulator");

/// <summary>
/// Gets the "connectionString" reference from the secret outputs of the Azure Cosmos DB resource.
///
/// This is used when Entra ID authentication is used. The connection string is an output of the bicep template.
/// </summary>
public BicepSecretOutputReference ConnectionString => new("connectionString", this);
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this supposed to be:

Suggested change
public BicepSecretOutputReference ConnectionString => new("connectionString", this);
public BicepOutputReference ConnectionString => new("connectionString", this);

It's not a secret reference anymore - it is just a normal bicep output.

Copy link
Member Author

Choose a reason for hiding this comment

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

That would be a breaking change. How do you suggest we deal with it?

Copy link
Member

Choose a reason for hiding this comment

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

This is why I opt to not make things public unless we absolutely need to make them public.

I would suggest just making the breaking change, and document it. The fact that we are switching to managed identity by default is going to be an even bigger breaking change.

Copy link
Member

Choose a reason for hiding this comment

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

This should be public for sure, we'll need to make this a reference expression.

Copy link
Member

Choose a reason for hiding this comment

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

Actually, we can just expose another property

namespace Aspire.Hosting.Azure.CosmosDB;

/// <summary>
/// This health check also creates default databases and containers for the Azure CosmosDB Emulator.
Copy link
Member

Choose a reason for hiding this comment

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

This feels like too much responsibility. Everytime we are checking for "health" we are rechecking all the dbs and containers.

I think it would be better if the health check was only a health check. And then we "waited" for it to be healthy and only ensure the db and containers are created once.

Copy link
Member Author

Choose a reason for hiding this comment

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

The default health check is checking only one db and its containers (because its state is only for one db, I assume one could create more instances for other dbs). At least we could do the same, pick the first db.

But I did what you suggested at first, though this could introduce undesired effects that it's stated as healthy but we haven't created the db/containers yet. So while we are creating them other services could have started using them.

Another option would be to change the implementation to do like it does right now, but then store a bool and only check for the first db afterwards (not even the containers).

var healthCheckKey = $"{builder.Resource.Name}_check";
builder.ApplicationBuilder.Services.AddHealthChecks().AddAzureCosmosDB(sp =>
Copy link
Member

Choose a reason for hiding this comment

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

Do we still need a reference on AspNetCore.HealthChecks.CosmosDb if we aren't using it?

var principalIdParameter = new ProvisioningParameter(AzureBicepResource.KnownParameters.PrincipalId, typeof(string));
infrastructure.Add(principalIdParameter);

cosmosAccount.CreateRoleAssignment(CosmosDBBuiltInRole.DocumentDBAccountContributor, RoleManagementPrincipalType.ServicePrincipal, principalIdParameter);
Copy link
Member

Choose a reason for hiding this comment

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

Why isn't this generating a prinicpalType like all the other Azure integrations?

var principalTypeParameter = new ProvisioningParameter(AzureBicepResource.KnownParameters.PrincipalType, typeof(string));
infrastructure.Add(principalTypeParameter);
var principalIdParameter = new ProvisioningParameter(AzureBicepResource.KnownParameters.PrincipalId, typeof(string));
infrastructure.Add(principalIdParameter);
infrastructure.Add(store.CreateRoleAssignment(AppConfigurationBuiltInRole.AppConfigurationDataOwner, principalTypeParameter, principalIdParameter));

Copy link
Member Author

Choose a reason for hiding this comment

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

The method to create the role on cosmos doesn't take any. It's working though, you think it must still be missing?

Copy link
Member

Choose a reason for hiding this comment

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

Yes this should be a parameter, @tg-msft?

@sebastienros
Copy link
Member Author

/azd run

@sebastienros
Copy link
Member Author

/azd run

@sebastienros
Copy link
Member Author

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

internal BicepSecretOutputReference? ConnectionStringSecretOutput { get; set; }

[MemberNotNullWhen(true, nameof(ConnectionStringSecretOutput))]
internal bool UseAccessKeyAuthentication => ConnectionStringSecretOutput is not null;
Copy link
Member

Choose a reason for hiding this comment

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

Maybe this should be public too.

@davidfowl
Copy link
Member

Do we want this to be a breaking change?

@eerhardt
Copy link
Member

Do we want this to be a breaking change?

At some point, yes. We want managed identity to be the default everywhere, and this is the last integration that still uses secrets by default.

So the question in my mind is: "When do we want to make the breaking change?". I think now is a good time. But I can see an argument to wait until the next major version.

# Conflicts:
#	playground/CosmosEndToEnd/CosmosEndToEnd.ApiService/Program.cs
#	playground/CosmosEndToEnd/CosmosEndToEnd.AppHost/Program.cs
#	src/Aspire.Hosting.Azure.CosmosDB/AzureCosmosDBExtensions.cs
#	src/Aspire.Hosting.Azure.CosmosDB/AzureCosmosDBResource.cs
#	src/Aspire.Hosting.Azure.CosmosDB/PublicAPI.Unshipped.txt
#	tests/Aspire.Hosting.Azure.Tests/AzureCosmosDBEmulatorFunctionalTests.cs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-integrations Issues pertaining to Aspire Integrations packages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Cosmos .NET SDK] - Introduce Managed Identity authentication for Cosmos
4 participants