Skip to content

Commit

Permalink
Dapr redis ext fixes (#544)
Browse files Browse the repository at this point in the history
* fix version typo

* Fixes for dapr redis - version format, identity

* Update Directory.Packages.props
  • Loading branch information
FullStackChef authored Mar 9, 2025
1 parent 237af62 commit 2f19e50
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@
<ItemGroup Label=".NET 9 Overrides" Condition="'$(TargetFramework)' == 'net9.0'">
<PackageVersion Update="Microsoft.Extensions.Logging.Abstractions" Version="9.0.2" />
</ItemGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,36 @@ private static IResourceBuilder<IDaprComponentResource> ConfigureRedisStateCompo
ArgumentNullException.ThrowIfNull(builder, nameof(builder));
ArgumentNullException.ThrowIfNull(redisBuilder, nameof(redisBuilder));

var daprComponent = AzureDaprHostingExtensions.CreateDaprComponent(redisDaprState, "state.redis", "v1.0");
var daprComponent = AzureDaprHostingExtensions.CreateDaprComponent(redisDaprState, "state.redis", "v1");

var redisHost = new ProvisioningParameter("redisHost", typeof(string));

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

var configureInfrastructure = AzureDaprHostingExtensions.GetInfrastructureConfigurationAction(daprComponent, [redisHost]);

var daprResourceBuilder = builder.AddAzureDaprResource(redisDaprState, configureInfrastructure);


daprResourceBuilder.WithParameter("redisHost", redisBuilder.GetOutput("daprConnectionString"));

bool useAccesskey = redisBuilder.Resource.UseAccessKeyAuthentication;

if (useAccesskey)
{
daprResourceBuilder.WithParameter("redisPasswordSecretUri", redisBuilder.GetOutput("redisPasswordSecretUri"));
}
else
{
daprResourceBuilder.ConfigureInfrastructure(infrastructure => infrastructure.Add(principalIdParameter));
}


redisBuilder.ConfigureInfrastructure(infra =>
{
var redisCacheResource = infra.GetProvisionableResources().OfType<AzureRedisResource>().Single();

// Make necessary changes to the redis resource
bool useEntraID = redisCacheResource.RedisConfiguration.IsAadEnabled.Value == "true";
bool enableTLS = redisCacheResource.EnableNonSslPort.Value == false;

BicepValue<int> port = enableTLS ? redisCacheResource.SslPort : redisCacheResource.Port;
Expand All @@ -68,35 +82,34 @@ private static IResourceBuilder<IDaprComponentResource> ConfigureRedisStateCompo
Value = BicepFunction.Interpolate($"{redisCacheResource.HostName}:{port}")
});

daprResourceBuilder.WithParameter("redisHost", redisBuilder.GetOutput("daprConnectionString"));

daprComponent.Metadata = [
new ContainerAppDaprMetadata { Name = "redisHost", Value = redisHost },
new ContainerAppDaprMetadata { Name = "enableTLS", Value = enableTLS? "true":"false"},
new ContainerAppDaprMetadata { Name = "actorStateStore", Value = "true" }
];


if (useEntraID)
if (useAccesskey)
{
infra.ConfigureSecretAccess(daprComponent, redisCacheResource);
}
else
{
daprComponent.Metadata.Add(new ContainerAppDaprMetadata
{
Name = "useEntraID",
Value = "true"
});

daprComponent.Metadata.Add(new ContainerAppDaprMetadata
{
Name = "azureClientId",
Value = principalIdParameter
});
}
else
{
infra.ConfigureSecretAccess(daprComponent, redisCacheResource);
daprResourceBuilder.WithParameter("redisPasswordSecretUri", redisBuilder.GetOutput("redisPasswordSecretUri"));
}
});




// return the original builder to allow chaining
return builder;
}
Expand All @@ -114,7 +127,7 @@ private static void ConfigureSecretAccess(this AzureResourceInfrastructure redis
ArgumentNullException.ThrowIfNull(redisCache, nameof(redisCache));
ArgumentNullException.ThrowIfNull(daprComponent, nameof(daprComponent));
ArgumentNullException.ThrowIfNull(redisCacheResource, nameof(redisCacheResource));

var redisPasswordSecret = new ProvisioningParameter("redisPasswordSecretUri", typeof(Uri));

var keyVault = redisCache.GetProvisionableResources()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,4 @@
<ProjectReference Include="../CommunityToolkit.Aspire.Hosting.Azure.Dapr/CommunityToolkit.Aspire.Hosting.Azure.Dapr.csproj" />
</ItemGroup>

<PropertyGroup>
<IsPackable>false</IsPackable>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ param keyVaultName string
output redisPasswordSecretUri string = daprRedisPassword.properties.secretUri
""";

Assert.Equal(expectedRedisBicep, redisBicep);
Assert.Equal(NormalizeLineEndings(expectedRedisBicep), NormalizeLineEndings(redisBicep));


var daprResource = Assert.Single(appModel.Resources.OfType<AzureDaprComponentResource>());

Expand Down Expand Up @@ -124,13 +125,13 @@ param redisPasswordSecretUri string
keyVaultUrl: redisPasswordSecretUri
}
]
version: 'v1.0'
version: 'v1'
}
parent: containerAppEnvironment
}
""";
Assert.Equal(NormalizeLineEndings(expectedDaprBicep), NormalizeLineEndings(daprBicep));

Assert.Equal(expectedDaprBicep, daprBicep);
}

[Fact]
Expand Down Expand Up @@ -195,7 +196,8 @@ param principalName string
output daprConnectionString string = '${redisState.properties.hostName}:${redisState.properties.sslPort}'
""";

Assert.Equal(expectedRedisBicep, redisBicep);
Assert.Equal(NormalizeLineEndings(expectedRedisBicep), NormalizeLineEndings(redisBicep));


var daprResource = Assert.Single(appModel.Resources.OfType<AzureDaprComponentResource>());

Expand All @@ -207,6 +209,8 @@ param principalName string
param redisHost string
param principalId string
var resourceToken = uniqueString(resourceGroup().id)
resource containerAppEnvironment 'Microsoft.App/managedEnvironments@2024-03-01' existing = {
Expand Down Expand Up @@ -239,13 +243,14 @@ param redisHost string
value: principalId
}
]
version: 'v1.0'
version: 'v1'
}
parent: containerAppEnvironment
}
""";

Assert.Equal(expectedDaprBicep, daprBicep);

Assert.Equal(NormalizeLineEndings(expectedDaprBicep), NormalizeLineEndings(daprBicep));

}

Expand Down Expand Up @@ -319,7 +324,7 @@ param principalName string
""";


Assert.Equal(expectedRedisBicep, redisBicep);
Assert.Equal(NormalizeLineEndings(expectedRedisBicep), NormalizeLineEndings(redisBicep));
}

[Fact]
Expand All @@ -336,4 +341,9 @@ public void WithReference_WhenNonStateType_ThrowsException()

Assert.Contains("Unsupported Dapr component type: pubsub", ex.Message);
}
public static string NormalizeLineEndings(string input)
{
return input.Replace("\r\n", "\n");
}

}

0 comments on commit 2f19e50

Please sign in to comment.