Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Diagnostics;
using System.Linq;
using System.Net.Mime;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
Expand All @@ -18,12 +19,20 @@ namespace Microsoft.Extensions.Configuration.AzureAppConfiguration.FeatureManage
{
internal class FeatureManagementKeyValueAdapter : IKeyValueAdapter
{
private const string DisableFmSchemaCompatibilityEnvironmentVariable = "AZURE_APP_CONFIGURATION_FM_SCHEMA_COMPATIBILITY_DISABLED";
private FeatureFlagTracing _featureFlagTracing;
private int _featureFlagIndex = 0;
private bool _fmSchemaCompatibilityDisabled = false;

public FeatureManagementKeyValueAdapter(FeatureFlagTracing featureFlagTracing)
{
_featureFlagTracing = featureFlagTracing ?? throw new ArgumentNullException(nameof(featureFlagTracing));

try
{
_fmSchemaCompatibilityDisabled = bool.TryParse(Environment.GetEnvironmentVariable(DisableFmSchemaCompatibilityEnvironmentVariable), out bool disabled) ? disabled : false;
}
catch (SecurityException) { }
}

public Task<IEnumerable<KeyValuePair<string, string>>> ProcessKeyValue(ConfigurationSetting setting, Uri endpoint, Logger logger, CancellationToken cancellationToken)
Expand All @@ -33,7 +42,10 @@ public Task<IEnumerable<KeyValuePair<string, string>>> ProcessKeyValue(Configura
var keyValues = new List<KeyValuePair<string, string>>();

// Check if we need to process the feature flag using the microsoft schema
if ((featureFlag.Variants != null && featureFlag.Variants.Any()) || featureFlag.Allocation != null || featureFlag.Telemetry != null)
if (_fmSchemaCompatibilityDisabled ||
(featureFlag.Variants != null && featureFlag.Variants.Any()) ||
featureFlag.Allocation != null ||
featureFlag.Telemetry != null)
{
keyValues = ProcessMicrosoftSchemaFeatureFlag(featureFlag, setting, endpoint);
}
Expand Down
70 changes: 70 additions & 0 deletions tests/Tests.AzureAppConfiguration/Unit/FeatureManagementTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2291,6 +2291,76 @@ public void ThrowsOnIncorrectJsonTypes()
}
}

[Fact]
public void EnvironmentVariableForcesMicrosoftSchemaForAllFlags()
{
var mixedSchemaFlags = new List<ConfigurationSetting>
{
_kv,
_variantFeatureFlagCollection[0]
};

var mockResponse = new Mock<Response>();
var mockClient = new Mock<ConfigurationClient>(MockBehavior.Strict);

mockClient.Setup(c => c.GetConfigurationSettingsAsync(It.IsAny<SettingSelector>(), It.IsAny<CancellationToken>()))
.Returns(new MockAsyncPageable(mixedSchemaFlags));

try
{
// Act - Set environment variable to force Microsoft schema
Environment.SetEnvironmentVariable("AZURE_APP_CONFIGURATION_FM_SCHEMA_COMPATIBILITY_DISABLED", "true");

var config = new ConfigurationBuilder()
.AddAzureAppConfiguration(options =>
{
options.ClientManager = TestHelpers.CreateMockedConfigurationClientManager(mockClient.Object);
options.UseFeatureFlags();
})
.Build();

// Assert - Both flags should be in Microsoft schema format
// First flag (originally .NET schema) should now be in Microsoft schema
Assert.Equal("Beta", config["feature_management:feature_flags:0:id"]);
Assert.Equal("True", config["feature_management:feature_flags:0:enabled"]);
Assert.Equal("Browser", config["feature_management:feature_flags:0:conditions:client_filters:0:name"]);
Assert.Equal("Firefox", config["feature_management:feature_flags:0:conditions:client_filters:0:parameters:AllowedBrowsers:0"]);

// Second flag (already Microsoft schema) should still be in Microsoft schema
Assert.Equal("VariantsFeature1", config["feature_management:feature_flags:1:id"]);
Assert.Equal("True", config["feature_management:feature_flags:1:enabled"]);
Assert.Equal("Big", config["feature_management:feature_flags:1:variants:0:name"]);
Assert.Equal("600px", config["feature_management:feature_flags:1:variants:0:configuration_value"]);

// Verify .NET schema paths are NOT present
Assert.Null(config["FeatureManagement:myFeature:EnabledFor:0:Name"]);
Assert.Null(config["FeatureManagement:VariantsFeature1:EnabledFor:0:Name"]);
}
finally
{
// Cleanup - Reset environment variable
Environment.SetEnvironmentVariable("AZURE_APP_CONFIGURATION_FM_SCHEMA_COMPATIBILITY_DISABLED", null);
}

// Act - Verify normal behavior when environment variable is not set
var configWithoutEnvVar = new ConfigurationBuilder()
.AddAzureAppConfiguration(options =>
{
options.ClientManager = TestHelpers.CreateMockedConfigurationClientManager(mockClient.Object);
options.UseFeatureFlags();
})
.Build();

// Assert - First flag should be in .NET schema, second in Microsoft schema
// First flag (no variants) should be in .NET schema
Assert.Equal("Browser", configWithoutEnvVar["FeatureManagement:myFeature:EnabledFor:0:Name"]);
Assert.Equal("Firefox", configWithoutEnvVar["FeatureManagement:myFeature:EnabledFor:0:Parameters:AllowedBrowsers:0"]);

// Second flag (has variants) should be in Microsoft schema
Assert.Equal("VariantsFeature1", configWithoutEnvVar["feature_management:feature_flags:0:id"]);
Assert.Equal("Big", configWithoutEnvVar["feature_management:feature_flags:0:variants:0:name"]);
}

Response<ConfigurationSetting> GetIfChanged(ConfigurationSetting setting, bool onlyIfChanged, CancellationToken cancellationToken)
{
return Response.FromValue(FirstKeyValue, new MockResponse(200));
Expand Down
Loading