Skip to content

Fix InitClientEncryption configuration not being respected in CosmosDataSinkExtension #190

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
merged 3 commits into from
Jun 2, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 1, 2025

Problem

When using the Cosmos data migration tool with UseRbacAuth: true and InitClientEncryption: false, the tool was incorrectly attempting to initialize encryption on the Cosmos container, causing the following error:

System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. 
(Parameter 'InitializeEncryptionAsync requires the use of an encryption-enabled client. 
Please refer to https://aka.ms/CosmosClientEncryption for more details.')

This was a regression where the InitClientEncryption: false setting was being ignored when UseRbacAuth: true.

Root Cause

The CosmosDataSinkExtension.WriteAsync() method had faulty logic that unconditionally called InitializeEncryptionAsync() whenever UseRbacAuth was true, regardless of the InitClientEncryption setting:

// Buggy code - always initializes encryption when UseRbacAuth is true
Container container = settings.UseRbacAuth
    ? await client.GetContainer(settings.Database, settings.Container).InitializeEncryptionAsync(cancellationToken)
    : await CreateDatabaseAndContainerAsync(client, settings, logger, cancellationToken);

Solution

Fixed the container initialization logic to properly respect both settings:

// Fixed code - only initializes encryption when both UseRbacAuth AND InitClientEncryption are true
Container container;
if (settings.UseRbacAuth)
{
    var cosmosContainer = client.GetContainer(settings.Database, settings.Container);
    container = settings.InitClientEncryption
        ? await cosmosContainer.InitializeEncryptionAsync(cancellationToken)
        : cosmosContainer;
}
else
{
    container = await CreateDatabaseAndContainerAsync(client, settings, logger, cancellationToken);
}

Behavior After Fix

  • UseRbacAuth=false: Uses CreateDatabaseAndContainerAsync (unchanged)
  • UseRbacAuth=true, InitClientEncryption=false: Gets container without encryption initialization
  • UseRbacAuth=true, InitClientEncryption=true: Gets container with encryption initialization

Additional Changes

  • Added comprehensive unit tests for InitClientEncryption validation scenarios
  • Aligned sink extension behavior with source extension (which already had correct logic)
  • All existing tests continue to pass, ensuring no regressions

Testing

Verified the fix with:

  • All existing unit tests pass (25 passed, 2 skipped)
  • New validation tests for all InitClientEncryption scenarios
  • Full solution builds successfully
  • Manual verification of the logic paths

Fixes #189.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • www.microsoft.com
    • Triggering command: dotnet build CosmosDbDataMigrationTool.sln (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Copilot Copilot AI changed the title [WIP] Release 2.1.5 for win-x64 does not respect InitClientEncryption configuration Fix InitClientEncryption configuration not being respected in CosmosDataSinkExtension Jun 1, 2025
@Copilot Copilot AI requested a review from philnach June 1, 2025 12:45
Copilot finished work on behalf of philnach June 1, 2025 12:45
@philnach
Copy link
Collaborator

philnach commented Jun 1, 2025

@copilot , Good catch copilot. Re-reading the issue I do see that it's with the setting :false and your change makes complete sense. I was also the one who introduced the regression.

Copilot finished work on behalf of philnach June 1, 2025 12:59
@philnach philnach requested review from markjbrown and bowencode June 1, 2025 13:08
@philnach
Copy link
Collaborator

philnach commented Jun 1, 2025

@bowencode @markjbrown , I caused this regression. CoPilot coded up a fix and added new unit tests. This will resolve the issue. Could either one of you code review? We'll want to cut a new release after this goes in.

@philnach philnach marked this pull request as ready for review June 1, 2025 19:52
@markjbrown markjbrown merged commit 3dadaac into main Jun 2, 2025
15 checks passed
@markjbrown
Copy link
Collaborator

@philnach published new binaries and package. rev to 2.4.0 Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Release 2.3.0 for win-x64 does not respect InitClientEncryption configuration
3 participants