Skip to content
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

feat: Enabling DefaultAzureCredential for AuthenticationMode #2578

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion changelog/content/deprecated/authentication-modes.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ However, as of Promitor Scraper v2.2.0 & Resource Discovery v0.3.0, users can ch

```yaml
authentication:
# Options are ServicePrincipal, SystemAssignedManagedIdentity, UserAssignedManagedIdentity.
# Options are ServicePrincipal, SystemAssignedManagedIdentity, UserAssignedManagedIdentity , SdkDefault.
mode: ServicePrincipal
identityId: xxxx-xxxx-xxxx
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ private string DetermineApplicationId(AzureAuthenticationInfo azureAuthenticatio
return azureAuthenticationInfo.GetIdentityIdOrDefault("externally-configured-user-assigned-identity");
case AuthenticationMode.SystemAssignedManagedIdentity:
return "system-assigned-identity";
case AuthenticationMode.SdkDefault:
return "default-azure-credentials";
default:
throw new ArgumentOutOfRangeException(nameof(azureAuthenticationInfo.Mode));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
{
public class StubResourceDiscoveryRepository : IResourceDiscoveryRepository
{
private static readonly List<AzureResourceDefinition> emptyResourceDefinitions = new();
private static readonly AgentHealthReport healthReport = new();
private static readonly List<AzureResourceDefinition> EmptyResourceDefinitions = new();

Check warning on line 11 in src/Promitor.Agents.Scraper/Discovery/StubResourceDiscoveryRepository.cs

View workflow job for this annotation

GitHub Actions / Code Quality (R#)

"[InconsistentNaming] Name 'EmptyResourceDefinitions' does not match rule 'Static readonly fields (private)'. Suggested name is 'emptyResourceDefinitions'." on /home/runner/work/promitor/promitor/src/Promitor.Agents.Scraper/Discovery/StubResourceDiscoveryRepository.cs(11,63)
jayendranarumugam marked this conversation as resolved.
Show resolved Hide resolved
private static readonly AgentHealthReport HealthReport = new();

public Task<List<AzureResourceDefinition>> GetResourceDiscoveryGroupAsync(string resourceDiscoveryGroupName)
{
return Task.FromResult(emptyResourceDefinitions);
return Task.FromResult(EmptyResourceDefinitions);
}

public Task<AgentHealthReport> GetHealthAsync()
{
return Task.FromResult(healthReport);
return Task.FromResult(HealthReport);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
{
public class AggregationDeserializer : Deserializer<AggregationV1>
{
private static readonly TimeSpan defaultAggregationInterval = TimeSpan.FromMinutes(5);
private static readonly TimeSpan DefaultAggregationInterval = TimeSpan.FromMinutes(5);

Check warning on line 9 in src/Promitor.Core.Scraping/Configuration/Serialization/v1/Core/AggregationDeserializer.cs

View workflow job for this annotation

GitHub Actions / Code Quality (R#)

"[InconsistentNaming] Name 'DefaultAggregationInterval' does not match rule 'Static readonly fields (private)'. Suggested name is 'defaultAggregationInterval'." on /home/runner/work/promitor/promitor/src/Promitor.Core.Scraping/Configuration/Serialization/v1/Core/AggregationDeserializer.cs(9,42)

public AggregationDeserializer(ILogger<AggregationDeserializer> logger) : base(logger)
{
Map(aggregation => aggregation.Interval)
.WithDefault(defaultAggregationInterval);
.WithDefault(DefaultAggregationInterval);
}
}
}
4 changes: 2 additions & 2 deletions src/Promitor.Core/Serialization/Yaml/YamlSerialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ namespace Promitor.Core.Serialization.Yaml
{
public static class YamlSerialization
{
private static readonly INamingConvention namingConvention = CamelCaseNamingConvention.Instance;
private static readonly INamingConvention NamingConvention = CamelCaseNamingConvention.Instance;

public static ISerializer CreateSerializer()
{
var builder = new SerializerBuilder();
builder.WithNamingConvention(namingConvention);
builder.WithNamingConvention(NamingConvention);
builder.ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitDefaults);

return builder.Build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ public enum AuthenticationMode
ServicePrincipal = 0,
UserAssignedManagedIdentity = 1,
SystemAssignedManagedIdentity = 2,
SdkDefault = 3
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ private string DetermineApplicationId(AzureAuthenticationInfo azureAuthenticatio
return azureAuthenticationInfo.GetIdentityIdOrDefault("externally-configured-user-assigned-identity");
case AuthenticationMode.SystemAssignedManagedIdentity:
return "system-assigned-identity";
case AuthenticationMode.SdkDefault:
return "default-azure-credentials";
jayendranarumugam marked this conversation as resolved.
Show resolved Hide resolved
default:
throw new ArgumentOutOfRangeException(nameof(azureAuthenticationInfo.Mode));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ private string DetermineApplicationId(AzureAuthenticationInfo azureAuthenticatio
return azureAuthenticationInfo.GetIdentityIdOrDefault("externally-configured-user-assigned-identity");
case AuthenticationMode.SystemAssignedManagedIdentity:
return "system-assigned-identity";
case AuthenticationMode.SdkDefault:
return "default-azure-credentials";
jayendranarumugam marked this conversation as resolved.
Show resolved Hide resolved
default:
throw new ArgumentOutOfRangeException(nameof(azureAuthenticationInfo.Mode));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public class OpenTelemetryCollectorMetricSink : MetricSink, IMetricSink
{
private readonly ILogger<OpenTelemetryCollectorMetricSink> _logger;
private static readonly Meter azureMonitorMeter = new Meter("Promitor.Scraper.Metrics.AzureMonitor", "1.0");
private static readonly Meter AzureMonitorMeter = new Meter("Promitor.Scraper.Metrics.AzureMonitor", "1.0");

Check warning on line 20 in src/Promitor.Integrations.Sinks.OpenTelemetry/OpenTelemetryCollectorMetricSink.cs

View workflow job for this annotation

GitHub Actions / Code Quality (R#)

"[InconsistentNaming] Name 'AzureMonitorMeter' does not match rule 'Static readonly fields (private)'. Suggested name is 'azureMonitorMeter'." on /home/runner/work/promitor/promitor/src/Promitor.Integrations.Sinks.OpenTelemetry/OpenTelemetryCollectorMetricSink.cs(20,39)

public MetricSinkType Type => MetricSinkType.OpenTelemetryCollector;

Expand Down Expand Up @@ -74,7 +74,7 @@

private void InitializeNewMetric(string metricName, string metricDescription)
{
var gauge = azureMonitorMeter.CreateObservableGauge<double>(metricName, description: metricDescription, observeValues: () => ReportMeasurementsForMetric(metricName));
var gauge = AzureMonitorMeter.CreateObservableGauge<double>(metricName, description: metricDescription, observeValues: () => ReportMeasurementsForMetric(metricName));
_gauges.TryAdd(metricName, gauge);

_measurements.TryAdd(metricName, []);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@ public void GetConfiguredAzureAuthentication_SystemAssignedManagedIdentityIsVali
Assert.Null(authenticationInfo.Secret);
}

[Fact]
public void GetConfiguredAzureAuthentication_SdkDefaultIsValid_Succeeds()
{
// Arrange
var expectedAuthenticationMode = AuthenticationMode.SdkDefault;
var inMemoryConfiguration = new Dictionary<string, string>
{
{ConfigurationKeys.Authentication.Mode, expectedAuthenticationMode.ToString()},
};
var config = CreateConfiguration(inMemoryConfiguration);

// Act
var authenticationInfo = AzureAuthenticationFactory.GetConfiguredAzureAuthentication(config);

// Assert
Assert.Equal(expectedAuthenticationMode, authenticationInfo.Mode);
Assert.Null(authenticationInfo.IdentityId);
Assert.Null(authenticationInfo.Secret);
}

[Fact]
public void GetConfiguredAzureAuthentication_UserAssignedManagedIdentityIsValid_Succeeds()
{
Expand Down Expand Up @@ -309,6 +329,27 @@ public void CreateAzureAuthentication_SystemAssignedManagedIdentityIsValid_Succe
Assert.Null(azureCredentials.ClientId);
}

[Fact]
public void CreateAzureAuthentication_SdkDefaultIsValid_Succeeds()
{
// Arrange
var expectedTenantId = Guid.NewGuid().ToString();
var azureCloud = AzureEnvironment.AzureChinaCloud;
var azureAuthenticationInfo = new AzureAuthenticationInfo
{
Mode = AuthenticationMode.SdkDefault
};
var azureCredentialFactory = new AzureCredentialsFactory();

// Act
var azureCredentials = AzureAuthenticationFactory.CreateAzureAuthentication(azureCloud, expectedTenantId, azureAuthenticationInfo, azureCredentialFactory);

// Assert
Assert.Equal(expectedTenantId, azureCredentials.TenantId);
Assert.Equal(azureCloud, azureCredentials.Environment);
Assert.Null(azureCredentials.ClientId);
}

[Fact]
public void CreateAzureAuthentication_UserAssignedManagedIdentityIsValid_Succeeds()
{
Expand Down
Loading
Loading