Skip to content

Added support for AppConfig Feature Flag profiles #229

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

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -214,6 +214,51 @@ namespace CustomParameterProcessorExample
}
```

### Simple AppConfig FeatureFlag Sample
Copy link
Contributor

Choose a reason for hiding this comment

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

```csharp
//
// AppConfigFeatureFlag.cs
//
public class AppConfigFeatureFlag
{
[JsonPropertyName("enabled")]
public bool Enabled { get; set; }
}

//
// Program.cs
//
// Map feature flag configuration
builder.Services.Configure<Dictionary<string, AppConfigFeatureFlag>>(builder.Configuration.GetSection("FeatureFlags"));
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of injecting Dictionary<string, AppConfigFeatureFlag> into the DI container, why not create an AppConfigFeatureFlagConfiguration object that contains the dictionary you are trying to access? I'd prefer users to access the config using AppConfigFeatureFlagConfiguration instead of Dictionary<string, AppConfigFeatureFlag>


// Add AppConfig configuration source
var awsAppConfigClient = new AmazonAppConfigDataClient();
builder.Configuration
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddAppConfig(new AppConfigConfigurationSource()
{
ApplicationId = "application",
EnvironmentId = "Default",
ConfigProfileId = "my-appconfig-feature-profile",
AwsOptions = new AWSOptions(),
ReloadAfter = new TimeSpan(0, 0, 30),
WrapperNodeName = "FeatureFlags" // Nest AppConfig FeatureFlag data under this node
});

//
// TestApi.cs
//
public class TestApi : ITestApi
{
private readonly IOptionsMonitor<Dictionary<string, AppConfigFeatureFlag>> _FeatureFlags;
public TestApi(IOptionsMonitor<Dictionary<string, AppConfigFeatureFlag>> featureFlags)
{
_FeatureFlags = featureFlags;
}
}
```

For more complete examples, take a look at sample projects available in [samples directory](https://github.com/aws/aws-dotnet-extensions-configuration/tree/master/samples).

# Configuring Systems Manager Client
Original file line number Diff line number Diff line change
@@ -51,6 +51,11 @@ public class AppConfigConfigurationSource : ISystemsManagerConfigurationSource

/// <inheritdoc />
public TimeSpan? ReloadAfter { get; set; }

/// <summary>
/// Name of the node to wrap the configuration data in.
/// </summary>
public string WrapperNodeName { get; set; }

/// <summary>
/// Indicates to use configured lambda extension HTTP client to retrieve AppConfig data.
Original file line number Diff line number Diff line change
@@ -238,7 +238,8 @@ private static AppConfigConfigurationSource ConfigureSource(
string configProfileId,
AWSOptions awsOptions = null,
bool optional = false,
TimeSpan? reloadAfter = null
TimeSpan? reloadAfter = null,
string wrapperNodeName = null
)
{
return new AppConfigConfigurationSource
@@ -248,7 +249,8 @@ private static AppConfigConfigurationSource ConfigureSource(
ConfigProfileId = configProfileId,
AwsOptions = awsOptions,
Optional = optional,
ReloadAfter = reloadAfter
ReloadAfter = reloadAfter,
WrapperNodeName = wrapperNodeName
};
}
}
Original file line number Diff line number Diff line change
@@ -93,6 +93,19 @@ public async Task<IDictionary<string, string>> GetDataAsync()
?? new Dictionary<string, string>();
}
}

private async Task AddWrapperNodeAsync(Stream configStream, Stream wrappedStream)
{
string wrappedConfig;
using (var reader = new StreamReader(configStream))
{
wrappedConfig = $"{{\"{Source.WrapperNodeName}\":{await reader.ReadToEndAsync().ConfigureAwait(false)}}}";
}

var wrappedConfigBytes = System.Text.Encoding.UTF8.GetBytes(wrappedConfig);
await wrappedStream.WriteAsync(wrappedConfigBytes, 0, wrappedConfigBytes.Length).ConfigureAwait(false);
wrappedStream.Position = 0;
}

private async Task<IDictionary<string,string>> GetDataFromLambdaExtensionAsync()
Copy link
Preview

Copilot AI Jul 17, 2025

Choose a reason for hiding this comment

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

The WrapperNodeName wrapping is not applied in the GetDataFromLambdaExtensionAsync path, so feature-flag JSON returned by the Lambda extension won’t be nested. Consider invoking AddWrapperNodeAsync (as in the service path) before parsing when WrapperNodeName is set.

Copilot uses AI. Check for mistakes.

Copy link
Contributor

@GarrettBeatty GarrettBeatty Jul 17, 2025

Choose a reason for hiding this comment

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

i was thinking about this comment. i couldn't think of a reason to not add the same logic here.

[Question] Can/should we add the new logic you added here too (or is there a specific reason you didn't)

{
@@ -134,7 +147,18 @@ private async Task<IDictionary<string, string>> GetDataFromServiceAsync()
// so only attempt to parse the AppConfig response when it is not empty
if (response.ContentLength > 0)
{
LastConfig = ParseConfig(response.ContentType, response.Configuration);
if (string.IsNullOrWhiteSpace(Source.WrapperNodeName))
{
LastConfig = ParseConfig(response.ContentType, response.Configuration);
}
else
{
using (var wrappedConfiguration = new MemoryStream())
{
await AddWrapperNodeAsync(response.Configuration, wrappedConfiguration).ConfigureAwait(false);
LastConfig = ParseConfig(response.ContentType, wrappedConfiguration);
}
}
}
}
finally
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

using System.IO;
using System.Text;
using System.Threading.Tasks;
using Amazon.Extensions.Configuration.SystemsManager.AppConfig;
using Amazon.Extensions.NETCore.Setup;
using Xunit;

namespace Amazon.Extensions.Configuration.SystemsManager.Tests
{
public class AppConfigProcessorTests
{
[Fact]
public async Task AddWrapperNode_WithWrapperNodeName_WrapsConfigurationCorrectly()
{
// Arrange
var source = new AppConfigConfigurationSource
{
ApplicationId = "appId",
EnvironmentId = "envId",
ConfigProfileId = "profileId",
WrapperNodeName = "FeatureFlags",
AwsOptions = new AWSOptions()
};

var processor = new AppConfigProcessor(source);
var inputJson = "{\"test-flag-1\":{\"enabled\":false},\"test-flag-2\":{\"enabled\":true}}";
using var inputStream = new MemoryStream(Encoding.UTF8.GetBytes(inputJson));
using var outputStream = new MemoryStream();

// Act
await InvokeAddWrapperNode(processor, inputStream, outputStream);

// Assert
string resultJson;
using (var reader = new StreamReader(outputStream))
{
resultJson = reader.ReadToEnd();
}

var expectedJson = "{\"FeatureFlags\":{\"test-flag-1\":{\"enabled\":false},\"test-flag-2\":{\"enabled\":true}}}";
Assert.Equal(expectedJson, resultJson);
}

// Helper method to invoke the private AddWrapperNode method using reflection
private async Task InvokeAddWrapperNode(AppConfigProcessor processor, Stream configuration, Stream wrappedConfig)
{
var method = typeof(AppConfigProcessor).GetMethod("AddWrapperNodeAsync",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
await (Task)method.Invoke(processor, new object[] { configuration, wrappedConfig });
}
}
}