Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8de8d4b

Browse files
[release/9.2] Don't fail for Azure role assignments in run mode (#8807)
* Don't fail for Azure role assignments in run mode We are throwing too early. When users only want to dotnet run their app and use role assignments, we shouldn't be blocking them on using role assignments. Only when you go to publish, should we throw saying that your infrastructure doesn't support targeted role assignments. Fix #8778 * Fix run mode for Azure resources that aren't referenced by compute resources. In those cases we still want the default role assignments to apply. --------- Co-authored-by: Eric Erhardt <[email protected]>
1 parent 4b42283 commit 8de8d4b

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

src/Aspire.Hosting.Azure/AzureResourcePreparer.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public async Task BeforeStartAsync(DistributedApplicationModel appModel, Cancell
3030
}
3131

3232
var options = provisioningOptions.Value;
33-
if (!options.SupportsTargetedRoleAssignments)
33+
if (!EnvironmentSupportsTargetedRoleAssignments(options))
3434
{
3535
// If the app infrastructure does not support targeted role assignments, then we need to ensure that
3636
// there are no role assignment annotations in the app model because they won't be honored otherwise.
@@ -79,6 +79,13 @@ public async Task BeforeStartAsync(DistributedApplicationModel appModel, Cancell
7979
return azureResources;
8080
}
8181

82+
private bool EnvironmentSupportsTargetedRoleAssignments(AzureProvisioningOptions options)
83+
{
84+
// run mode always supports targeted role assignments
85+
// publish mode only supports targeted role assignments if the environment supports it
86+
return executionContext.IsRunMode || options.SupportsTargetedRoleAssignments;
87+
}
88+
8289
private static void EnsureNoRoleAssignmentAnnotations(DistributedApplicationModel appModel)
8390
{
8491
foreach (var resource in appModel.Resources)
@@ -94,7 +101,7 @@ private async Task BuildRoleAssignmentAnnotations(DistributedApplicationModel ap
94101
{
95102
var globalRoleAssignments = new Dictionary<AzureProvisioningResource, HashSet<RoleDefinition>>();
96103

97-
if (!options.SupportsTargetedRoleAssignments)
104+
if (!EnvironmentSupportsTargetedRoleAssignments(options))
98105
{
99106
// when the app infrastructure doesn't support targeted role assignments, just copy all the default role assignments to applied role assignments
100107
foreach (var resource in azureResources.Select(r => r.AzureResource).OfType<AzureProvisioningResource>())
@@ -183,6 +190,19 @@ private async Task BuildRoleAssignmentAnnotations(DistributedApplicationModel ap
183190
}
184191
}
185192
}
193+
194+
if (executionContext.IsRunMode)
195+
{
196+
// in RunMode, any Azure resources that are not referenced by a compute resource should have their default role assignments applied
197+
foreach (var azureResource in azureResources.Select(r => r.AzureResource).OfType<AzureProvisioningResource>())
198+
{
199+
if (!globalRoleAssignments.TryGetValue(azureResource, out _) &&
200+
azureResource.TryGetLastAnnotation<DefaultRoleAssignmentsAnnotation>(out var defaultRoleAssignments))
201+
{
202+
AppendGlobalRoleAssignments(globalRoleAssignments, azureResource, defaultRoleAssignments.Roles);
203+
}
204+
}
205+
}
186206
}
187207

188208
if (globalRoleAssignments.Count > 0)

tests/Aspire.Hosting.Azure.Tests/AzureResourcePreparerTests.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ namespace Aspire.Hosting.Azure.Tests;
1313

1414
public class AzureResourcePreparerTests(ITestOutputHelper output)
1515
{
16-
[Fact]
17-
public void ThrowsExceptionsIfRoleAssignmentUnsupported()
16+
[Theory]
17+
[InlineData(DistributedApplicationOperation.Publish)]
18+
[InlineData(DistributedApplicationOperation.Run)]
19+
public async Task ThrowsExceptionsIfRoleAssignmentUnsupported(DistributedApplicationOperation operation)
1820
{
19-
using var builder = TestDistributedApplicationBuilder.Create();
21+
using var builder = TestDistributedApplicationBuilder.Create(operation);
2022

2123
var storage = builder.AddAzureStorage("storage");
2224

@@ -25,8 +27,16 @@ public void ThrowsExceptionsIfRoleAssignmentUnsupported()
2527

2628
var app = builder.Build();
2729

28-
var ex = Assert.Throws<InvalidOperationException>(app.Start);
29-
Assert.Contains("role assignments", ex.Message);
30+
if (operation == DistributedApplicationOperation.Publish)
31+
{
32+
var ex = Assert.Throws<InvalidOperationException>(app.Start);
33+
Assert.Contains("role assignments", ex.Message);
34+
}
35+
else
36+
{
37+
await app.StartAsync();
38+
// no exception is thrown in Run mode
39+
}
3040
}
3141

3242
[Theory]

0 commit comments

Comments
 (0)