-
Notifications
You must be signed in to change notification settings - Fork 72
Cosmos extension/client secret credential support #242
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
Merged
philnach
merged 19 commits into
AzureCosmosDB:main
from
thucnguyen77:cosmos-extension/client-secret-credential-support
May 29, 2026
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
57c8fb1
Add ClientSecretCredential support
bd0916b
Potential fix for pull request finding
thucnguyen77 b4c80e7
Remove TokenCredentialOptions parameter
6e69a58
UseServicePrincipalAuth and UseRbacAuth are mutual exclusive
e922583
Add positive test
6b68126
Potential fix for pull request finding
thucnguyen77 208c6a7
Fold the Service Principal path into UseRbacAuth mode
43a81af
Fix copy-paste bug
228fa7d
Fix validation logic
26a5b02
Address various code review feedback for PR #242
1d4eb01
Apply suggestions from Copilot code review
thucnguyen77 191f978
Fix message-template placeholder
1bba638
Add more validations as per Copilot review comments
7ab7d0a
Cosmos RBAC SP auth hardening and test coverage
philnach 188abb7
Address latest Copilot feedback on PR 242
philnach 5487dd3
Address remaining PR 242 Copilot feedback
philnach addb745
Merge remote-tracking branch 'upstream/main' into pr242-sync
philnach 7707dc2
Harden Cosmos certificate auth polish
philnach 6d2af70
Clarify certificate password RBAC setting
philnach File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
244 changes: 244 additions & 0 deletions
244
...s/Cosmos.DataTransfer.CosmosExtension.UnitTests/CosmosExtensionServicesCredentialTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,244 @@ | ||
| using Azure.Identity; | ||
| using Microsoft.Extensions.Logging; | ||
| using Moq; | ||
| using System.Security.Cryptography; | ||
| using System.Security.Cryptography.X509Certificates; | ||
|
|
||
| namespace Cosmos.DataTransfer.CosmosExtension.UnitTests; | ||
|
|
||
| [TestClass] | ||
| public class CosmosExtensionServicesCredentialTests | ||
| { | ||
| [TestMethod] | ||
| public void GetTokenCredentialSelection_WithNoServicePrincipalInfo_ReturnsDefaultCredential() | ||
| { | ||
| var settings = new CosmosSourceSettings | ||
| { | ||
| UseRbacAuth = true, | ||
| AccountEndpoint = "https://localhost:8081/", | ||
| Database = "db", | ||
| Container = "container", | ||
| }; | ||
|
|
||
| var selection = CosmosExtensionServices.GetTokenCredentialSelection(settings); | ||
|
|
||
| Assert.AreEqual(CosmosExtensionServices.TokenCredentialSelection.DefaultAzureCredential, selection); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void GetTokenCredentialSelection_WithTenantClientAndSecret_ReturnsClientSecretCredential() | ||
| { | ||
| var settings = new CosmosSourceSettings | ||
| { | ||
| UseRbacAuth = true, | ||
| AccountEndpoint = "https://localhost:8081/", | ||
| Database = "db", | ||
| Container = "container", | ||
| TenantId = "tenant-id", | ||
| ClientId = "client-id", | ||
| ClientSecret = "client-secret", | ||
| }; | ||
|
|
||
| var selection = CosmosExtensionServices.GetTokenCredentialSelection(settings); | ||
|
|
||
| Assert.AreEqual(CosmosExtensionServices.TokenCredentialSelection.ClientSecretCredential, selection); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void GetTokenCredentialSelection_WithWhitespaceServicePrincipalInfo_ReturnsDefaultCredential() | ||
| { | ||
| var settings = new CosmosSourceSettings | ||
| { | ||
| UseRbacAuth = true, | ||
| AccountEndpoint = "https://localhost:8081/", | ||
| Database = "db", | ||
| Container = "container", | ||
| TenantId = " ", | ||
| ClientId = " ", | ||
| ClientSecret = "client-secret", | ||
| }; | ||
|
|
||
| var selection = CosmosExtensionServices.GetTokenCredentialSelection(settings); | ||
|
|
||
| Assert.AreEqual(CosmosExtensionServices.TokenCredentialSelection.DefaultAzureCredential, selection); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void GetTokenCredentialSelection_WithTenantClientAndCertificatePath_ReturnsClientCertificateCredential() | ||
| { | ||
| var settings = new CosmosSourceSettings | ||
| { | ||
| UseRbacAuth = true, | ||
| AccountEndpoint = "https://localhost:8081/", | ||
| Database = "db", | ||
| Container = "container", | ||
| TenantId = "tenant-id", | ||
| ClientId = "client-id", | ||
| ClientCertificatePath = "./certs/cert.pfx", | ||
| }; | ||
|
|
||
| var selection = CosmosExtensionServices.GetTokenCredentialSelection(settings); | ||
|
|
||
| Assert.AreEqual(CosmosExtensionServices.TokenCredentialSelection.ClientCertificateCredential, selection); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void CreateRbacTokenCredential_WithNoServicePrincipalInfo_ReturnsDefaultAzureCredential() | ||
| { | ||
| var loggerMock = new Mock<ILogger>(); | ||
| var settings = new CosmosSourceSettings | ||
| { | ||
| UseRbacAuth = true, | ||
| AccountEndpoint = "https://localhost:8081/", | ||
| Database = "db", | ||
| Container = "container", | ||
| }; | ||
|
|
||
| var credential = CosmosExtensionServices.CreateRbacTokenCredential(settings, loggerMock.Object); | ||
|
|
||
| Assert.IsInstanceOfType<DefaultAzureCredential>(credential); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void CreateRbacTokenCredential_WithInvalidCertificatePath_ThrowsFriendlyConfigurationError() | ||
| { | ||
| var loggerMock = new Mock<ILogger>(); | ||
| var settings = new CosmosSourceSettings | ||
| { | ||
| UseRbacAuth = true, | ||
| AccountEndpoint = "https://localhost:8081/", | ||
| Database = "db", | ||
| Container = "container", | ||
| TenantId = "tenant-id", | ||
| ClientId = "client-id", | ||
| ClientCertificatePath = "./certs/does-not-exist.pfx", | ||
| }; | ||
|
|
||
| var ex = Assert.ThrowsException<InvalidOperationException>(() => | ||
| CosmosExtensionServices.CreateRbacTokenCredential(settings, loggerMock.Object)); | ||
|
|
||
| StringAssert.Contains(ex.Message, "Failed to configure RBAC credentials"); | ||
| Assert.IsNotNull(ex.InnerException); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void CreateRbacTokenCredential_WithPasswordProtectedCertificate_ReturnsClientCertificateCredential() | ||
| { | ||
| var loggerMock = new Mock<ILogger>(); | ||
| const string certPassword = "test-password"; | ||
| var certPath = CreatePasswordProtectedPfx(certPassword); | ||
|
|
||
| try | ||
| { | ||
| var settings = new CosmosSourceSettings | ||
| { | ||
| UseRbacAuth = true, | ||
| AccountEndpoint = "https://localhost:8081/", | ||
| Database = "db", | ||
| Container = "container", | ||
| TenantId = "tenant-id", | ||
| ClientId = "client-id", | ||
| ClientCertificatePath = certPath, | ||
| ClientCertificatePassword = certPassword, | ||
| }; | ||
|
|
||
| var credential = CosmosExtensionServices.CreateRbacTokenCredential(settings, loggerMock.Object); | ||
|
|
||
| Assert.IsInstanceOfType<ClientCertificateCredential>(credential); | ||
| loggerMock.Verify( | ||
| x => x.Log( | ||
| LogLevel.Warning, | ||
| It.IsAny<EventId>(), | ||
| It.Is<It.IsAnyType>((v, t) => v.ToString()!.Contains(nameof(CosmosSourceSettings.ClientCertificatePassword))), | ||
| It.IsAny<Exception?>(), | ||
| It.IsAny<Func<It.IsAnyType, Exception?, string>>()), | ||
| Times.Once); | ||
| } | ||
| finally | ||
| { | ||
| if (File.Exists(certPath)) | ||
| { | ||
| File.Delete(certPath); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void CreateRbacTokenCredential_WithCertificateWithoutPrivateKey_ThrowsFriendlyConfigurationError() | ||
| { | ||
| var loggerMock = new Mock<ILogger>(); | ||
| var certPath = CreatePublicCertificate(); | ||
|
|
||
| try | ||
| { | ||
| var settings = new CosmosSourceSettings | ||
| { | ||
| UseRbacAuth = true, | ||
| AccountEndpoint = "https://localhost:8081/", | ||
| Database = "db", | ||
| Container = "container", | ||
| TenantId = "tenant-id", | ||
| ClientId = "client-id", | ||
| ClientCertificatePath = certPath, | ||
| }; | ||
|
|
||
| var ex = Assert.ThrowsException<InvalidOperationException>(() => | ||
| CosmosExtensionServices.CreateRbacTokenCredential(settings, loggerMock.Object)); | ||
|
|
||
| StringAssert.Contains(ex.Message, "Failed to configure RBAC credentials"); | ||
| Assert.IsInstanceOfType<CryptographicException>(ex.InnerException); | ||
| StringAssert.Contains(ex.InnerException!.Message, "private key"); | ||
| } | ||
| finally | ||
| { | ||
| if (File.Exists(certPath)) | ||
| { | ||
| File.Delete(certPath); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void CreateClientOptions_UsesAllowBulkExecutionSetting() | ||
| { | ||
| var loggerMock = new Mock<ILogger>(); | ||
| var settings = new CosmosSourceSettings | ||
| { | ||
| ConnectionString = "AccountEndpoint=https://localhost:8081/;AccountKey=key", | ||
| Database = "db", | ||
| Container = "container", | ||
| AllowBulkExecution = true, | ||
| }; | ||
|
|
||
| var clientOptions = CosmosExtensionServices.CreateClientOptions(settings, "test-agent", loggerMock.Object); | ||
|
|
||
| Assert.IsTrue(clientOptions.AllowBulkExecution); | ||
|
|
||
| settings.AllowBulkExecution = false; | ||
| clientOptions = CosmosExtensionServices.CreateClientOptions(settings, "test-agent", loggerMock.Object); | ||
|
|
||
| Assert.IsFalse(clientOptions.AllowBulkExecution); | ||
| } | ||
|
|
||
| private static string CreatePasswordProtectedPfx(string password) | ||
| { | ||
| using var rsa = RSA.Create(2048); | ||
| var request = new CertificateRequest("CN=unit-test-cert", rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); | ||
| using var certificate = request.CreateSelfSigned(DateTimeOffset.UtcNow.AddDays(-1), DateTimeOffset.UtcNow.AddDays(1)); | ||
| var pfxBytes = certificate.Export(X509ContentType.Pfx, password); | ||
| var certPath = Path.Combine(Path.GetTempPath(), $"dmt-test-{Guid.NewGuid():N}.pfx"); | ||
| File.WriteAllBytes(certPath, pfxBytes); | ||
| return certPath; | ||
| } | ||
|
|
||
| private static string CreatePublicCertificate() | ||
| { | ||
| using var rsa = RSA.Create(2048); | ||
| var request = new CertificateRequest("CN=unit-test-cert", rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); | ||
| using var certificate = request.CreateSelfSigned(DateTimeOffset.UtcNow.AddDays(-1), DateTimeOffset.UtcNow.AddDays(1)); | ||
| var certBytes = certificate.Export(X509ContentType.Cert); | ||
| var certPath = Path.Combine(Path.GetTempPath(), $"dmt-test-{Guid.NewGuid():N}.cer"); | ||
| File.WriteAllBytes(certPath, certBytes); | ||
| return certPath; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.