aspire icon indicating copy to clipboard operation
aspire copied to clipboard

aspire publish produces invalid helm configuration for database secrets

Open gibsonsam opened this issue 1 year ago • 4 comments

Is there an existing issue for this?

  • [x] I have searched the existing issues

Describe the bug

The aspire publish produces an invalid helm secret when a project references a database resource. The generated Helm secret embeds the database password secret using the project's name instead of the DB service’s name.

var builder = DistributedApplication.CreateBuilder(args);

var k8s = builder.AddKubernetesEnvironment("k8s");

var db = builder
    .AddPostgres("postgres")
    .AddDatabase("exampledb");

builder.AddProject<Projects.Example>("exampleapp")
    .WithReference(db);

secrets.yaml refers to an invalid secret .Values.secrets.exampleapp.postgres_password (note exampleapp not postgres):

...
stringData:
  ConnectionStrings__exampledb: "Host=postgres-service;Port=5432;Username=postgres;Password={{ .Values.secrets.exampleapp.postgres_password }};Database=exampledb"
...

values.yaml (for reference)

parameters:
  example:
    example_image: "example:latest"
secrets:
  postgres:
    POSTGRES_PASSWORD: ""
...

Expected Behavior

The aspire publish command produces a valid secret:

...
stringData:
  ConnectionStrings__exampledb: "Host=postgres-service;Port=5432;Username=postgres;Password={{ .Values.secrets.postgres.postgres_password }};Database=exampledb"
...

Steps To Reproduce

See steps above

Exceptions (if any)

No response

.NET Version info

.NET SDK: Version: 9.0.302 Commit: bb2550b9af Workload version: 9.0.300-manifests.5c1085e6 MSBuild version: 17.14.13+65391c53b

Runtime Environment: OS Name: Windows OS Version: 10.0.26100 OS Platform: Windows RID: win-x64 Base Path: C:\Program Files\dotnet\sdk\9.0.302\

Anything else?

No response

gibsonsam avatar Aug 17 '25 18:08 gibsonsam

(Version of Aspire.Cli is 9.4.1)

gibsonsam avatar Aug 17 '25 18:08 gibsonsam

I noticed the same behavior when referencing qdrant. It tries to get the api key from the secrets of the service that references qdrant instead of from qdrant itself.

It might be that each service only picks secrets from its own section in values.yaml.

It also emits the wrong key in values.yaml:

In api secrets.yaml: ConnectionStrings__qdrant: "Endpoint=http://qdrant-service:6334;Key={{ .Values.secrets.api.qdrantApiKey }}"

In values.yaml:

secrets:
  qdrant:
    QDRANT__SERVICE__API_KEY: ""

It should be:

secrets:
  qdrant:
    qdrantApiKey: ""

erikhermansson79 avatar Aug 18 '25 09:08 erikhermansson79

I noticed the same behavior when referencing qdrant. It tries to get the api key from the secrets of the service that references qdrant instead of from qdrant itself.

It might be that each service only picks secrets from its own section in values.yaml.

Yep. It appears to be an issue with how it constructs secrets for IResourceWithConnectionString

samgibsonmoj avatar Aug 18 '25 09:08 samgibsonmoj

What happens (for passwords at least), with a concrete example for a stock standard builder.AddRabbitMQ("messaging"):

Stage One: KubernetesInfrastructure.OnBeforeStartAsync()

Adds a DeploymentTargetAnnotation that is basically a (YourK8sEnvironment, resource.ToKubernetesResource()) tuple by way of CreateKubernetesResourceAsync() => ProcessResourceAsync().

ProcessResourceAsync() calls ProcessEnvironmentAsync() where context.EnvironmentVariables is iterated over. context.EnvironmentVariables at this point:

    [0]: {[RABBITMQ_DEFAULT_USER]=ReferenceExpression}
    [1]: {[RABBITMQ_DEFAULT_PASS]=Type = ParameterResource, Name = "messaging-password", Annotations = 0}

each ev is passed into ProcessAsync(), which branches on the type of ev.Value. key: RABBITMQ_DEFAULT_USER (ReferenceExpression) just ends up with what boils down to a string.Format("guest"), returning a string. key: RABBITMQ_DEFAULT_PASS (ParameterExpression) goes off to AllocateParameter() and ends up returning a HelmExpressionWithValue.

AllocateParameter() is where things start getting out of sync - it basically returns a helm expression built up with helm_secret_prefix(resource.Name) + helm_formatted_name(parameter.Name): {{ Values.secrets.messaging.messaging_password }}.

Back in ProcessEnvironmentAsync(), our simple string user gets sent to ProcessEnvironmentStringValue() that creates a helm config expression just like AllocateParameter(), but this time from the key, since there's no parameter.Name. This config expression is saved to EnvironmentVariables[key], with a default value of "guest".

The password is a helmExpression at this point, so it goes to ProcessEnvironmentHelmExpression(helmExpression, key) instead. This method basically just checks if the expression contains ".Values.secrets", and dumps it as-is to Secrets[key].

Our environment is processed, and we now have this on our KubernetesResource: EnvironmentVariables["RABBITMQ_DEFAULT_USER"] = {{ .Values.config.messaging.RABBITMQ_DEFAULT_USER }}, default value "guest" Secrets["RABBITMQ_DEFAULT_PASS"] = "{{ .Values.secrets.messaging.messaging_password }}".

The mismatch is obvious already, though not really a problem just yet. Different property naming methods ain't a bug :)

NOW with the environment processed, we move on to the final part: kubernetesResource.BuildKubernetesResource(). This one is a simple boi: Set labels, create the StatefulSet/Deployment, create ConfigMap and Secrets, then finish off with creating the service.

The ConfigMap is created as expected: iterate over EnvironmentVariables, and set Data[key] = expression. Same for Secrets, through StringData[key] instead.

The service is bog-standard.

So: the KubernetesResource itself is created correctly, and has no bugs (related to this particular thing, atleast).

Stage the second: KubernetesEnvironmentResource.PublishAsync()

A KubernetesPublisingContext is created, and through WriteModelAsync() we get to WriteKubernetesOutputAsync(), where it's time to iterate over the resources again!

In the iteration there's some per-k8s-resource DockerFileFactory and ConsumizationAnnotation stuff we'll ignore, and two final calls: WriteKubernetesTemplatesForResource(...) and AppendResourceContextToHelmValuesAsync(...).

WriteKubernetesTemplatesForResource() is simple. It's the thing that writes statefulset.yaml, config.yaml etc.

AppendResourceContextToHelmValuesAsync(), though, does this:

        await AddValuesToHelmSectionAsync(resource, resourceContext.Parameters, HelmExtensions.ParametersKey).ConfigureAwait(false);
        await AddValuesToHelmSectionAsync(resource, resourceContext.EnvironmentVariables, HelmExtensions.ConfigKey).ConfigureAwait(false);
        await AddValuesToHelmSectionAsync(resource, resourceContext.Secrets, HelmExtensions.SecretsKey).ConfigureAwait(false);

AddValuesToHelmSectionAsync(), here's where it goes wrong. It handles the parameter keying the same way as ProcessEnvironmentAsync() does, and basically does helmSection[resource.Name][value.Key] = value.expr.

Nothing of note after this, the next step for helmValues is serialization to yaml and output as values.yaml.

And the bug has happened!

KubernetesResource.AllocateParameter goes you should be resolving this by it's parameter name

KubernetesPublishingContext::AddValuesToHelmSectionAsync() goes alright but I'm only gonna save it by its key

The formattedName from AllocateParameter() needs to make an appearance during AddValuesToHelmSectionAsync(), so that the correct property is created.

fredriks-eltele avatar Jan 23 '26 14:01 fredriks-eltele