Validate environment variable names for Foundry hosted agents#16884
Conversation
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 16884Or
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 16884" |
Foundry Hosted Agents only support environment variable names containing ASCII letters, digits, and underscores. Service discovery names (with dashes/dots) and other generated names would silently produce invalid agent versions at deploy time. Add validation in HostedAgentConfiguration.ToProjectsAgentVersionCreationOptions using a source-generated regex; throw DistributedApplicationException with the offending names if any are invalid. Rename target resources in the existing publish test to non-dashed names so the test exercises the happy path. Fixes #16858 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
30df0c9 to
79ae822
Compare
There was a problem hiding this comment.
Pull request overview
Adds proactive validation for environment variable names used by Foundry Hosted Agents so deploy-time silent rejection becomes an actionable, early failure with clear diagnostics.
Changes:
- Validate hosted-agent environment variable names against
^[A-Za-z0-9_]+$and throw aDistributedApplicationExceptionthat includes the target resource name and offending variables. - Thread the target resource name into the conversion path used during deployment.
- Update and add tests to cover the new validation and keep existing publish-path tests passing.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| tests/Aspire.Hosting.Foundry.Tests/HostedAgentConfigurationTests.cs | Adds coverage ensuring invalid env-var names cause a clear exception and valid names aren’t reported. |
| tests/Aspire.Hosting.Azure.Tests/FoundryExtensionsTests.cs | Renames test resource names to avoid dashes so generated env-var keys remain valid under the new restriction. |
| src/Aspire.Hosting.Foundry/HostedAgent/HostedAgentConfiguration.cs | Implements source-generated-regex validation and improves error reporting by including target resource context. |
| src/Aspire.Hosting.Foundry/HostedAgent/AzureHostedAgentResource.cs | Passes the target resource name into the options-conversion method during deploy. |
joperezr
left a comment
There was a problem hiding this comment.
LGTM. Left a few thoughts inline.
| Assert.Contains("Foundry hosted agent for target resource 'target'", ex.Message); | ||
| Assert.Contains("Environment variable names must contain only ASCII letters, digits, or underscores.", ex.Message); | ||
| Assert.Contains("'INVALID-NAME'", ex.Message); | ||
| Assert.Contains("'invalid.name'", ex.Message); | ||
| Assert.DoesNotContain("VALID_NAME_1", ex.Message); |
There was a problem hiding this comment.
nit: Use Assert.Equal + raw string literal to exactly test the results.
|
Re-running the failed jobs in the CI workflow for this pull request because 2 jobs were identified as retry-safe transient failures in the CI run attempt.
|
|
🎬 CLI E2E Test Recordings — 78 recordings uploaded (commit View all recordings
📹 Recordings uploaded automatically from CI run #25693901621 |
|
✅ No documentation update needed. Bug fix only — this PR adds up-front validation that environment variable names for Foundry hosted agents contain only ASCII letters, digits, and underscores, surfacing an existing Foundry service constraint as a clear |
…oft#16884) * Validate environment variable names for Foundry hosted agents Foundry Hosted Agents only support environment variable names containing ASCII letters, digits, and underscores. Service discovery names (with dashes/dots) and other generated names would silently produce invalid agent versions at deploy time. Add validation in HostedAgentConfiguration.ToProjectsAgentVersionCreationOptions using a source-generated regex; throw DistributedApplicationException with the offending names if any are invalid. Rename target resources in the existing publish test to non-dashed names so the test exercises the happy path. Fixes microsoft#16858 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * PR feedback --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Description
Foundry Hosted Agents only accept environment variable names containing ASCII letters, digits, and underscores. Other characters (dashes, dots, etc.) are silently rejected at deploy time, producing a confusing failure. This change validates names up-front.
Changes
HostedAgentConfiguration.ToProjectsAgentVersionCreationOptionsnow takes the target resource name and validates each environment variable name against a source-generated regex (^[A-Za-z0-9_]+$).DistributedApplicationExceptionlisting the offending names and the target resource.internal(only the resource-side deploy path needs it).PublishAsHostedAgent_ResolvesExternalContainerAppReferenceto non-dashed names so the existing happy-path test continues to pass.Fixes
Fixes #16858
Checklist
Aspire.Hosting.Foundry.Tests84/84,FoundryExtensionsTests19/19)