Skip to content

Commit

Permalink
adding default-credentials for local debug
Browse files Browse the repository at this point in the history
  • Loading branch information
jayendranarumugam committed Nov 24, 2024
1 parent 5e93ede commit 52ee147
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 1 deletion.
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 , DefaultAzureCredential.
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.DefaultAzureCredential:
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 @@ -5,5 +5,6 @@ public enum AuthenticationMode
ServicePrincipal = 0,
UserAssignedManagedIdentity = 1,
SystemAssignedManagedIdentity = 2,
DefaultAzureCredential = 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.DefaultAzureCredential:
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 @@ -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.DefaultAzureCredential:
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 @@ -35,6 +35,26 @@ public void GetConfiguredAzureAuthentication_SystemAssignedManagedIdentityIsVali
Assert.Null(authenticationInfo.Secret);
}

[Fact]
public void GetConfiguredAzureAuthentication_DefaultAzureCredentialIsValid_Succeeds()
{
// Arrange
var expectedAuthenticationMode = AuthenticationMode.DefaultAzureCredential;
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_DefaultAzureCredentialIsValid_Succeeds()
{
// Arrange
var expectedTenantId = Guid.NewGuid().ToString();
var azureCloud = AzureEnvironment.AzureChinaCloud;
var azureAuthenticationInfo = new AzureAuthenticationInfo
{
Mode = AuthenticationMode.DefaultAzureCredential
};
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
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,26 @@ public void SystemAssignedManagedIdentity_ValidWithoutApplicationKey_Succeeds()
PromitorAssert.ValidationIsSuccessful(validationResult);
}


[Fact]
public void DefaultAzureCredential_ValidWithoutApplicationKey_Succeeds()
{
// Arrange
var inMemoryConfiguration = new Dictionary<string, string>
{
{ConfigurationKeys.Authentication.Mode, AuthenticationMode.DefaultAzureCredential.ToString()},
};

var config = CreateConfiguration(inMemoryConfiguration);

// Act
var azureAuthenticationValidationStep = new AzureAuthenticationValidationStep(config, NullLogger<AzureAuthenticationValidationStep>.Instance);
var validationResult = azureAuthenticationValidationStep.Run();

// Assert
PromitorAssert.ValidationIsSuccessful(validationResult);
}

private IConfigurationRoot CreateConfiguration(Dictionary<string, string> inMemoryConfiguration)
{
return new ConfigurationBuilder()
Expand Down

0 comments on commit 52ee147

Please sign in to comment.