Skip to content

Azure Cosmos SDK upgrade for kv store - #3559

Open
ChihweiLHBird wants to merge 1 commit into
spinframework:mainfrom
ChihweiLHBird:zhiwei/azure-kv-store-sdk-migration
Open

Azure Cosmos SDK upgrade for kv store#3559
ChihweiLHBird wants to merge 1 commit into
spinframework:mainfrom
ChihweiLHBird:zhiwei/azure-kv-store-sdk-migration

Conversation

@ChihweiLHBird

@ChihweiLHBird ChihweiLHBird commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

Closes #3021.

Summary

Migrates spin-key-value-azure from the legacy Azure Cosmos SDK to the current azure_data_cosmos 0.37 (driver 0.6.0) / azure_core 1.0 / azure_identity 1.0 stack. Reworks client construction and the item/query APIs, keeps account-key auth, and replaces the old ambient-auth path with explicit Azure AD credential selection.

What Changed

  • Reworked Cosmos client creation to use AccountReference, async container-client init, and SDK-managed routing (RoutingStrategy::ProximityTo); moved CRUD/batch/increment/CAS to the new APIs (get is now a read_item point read) and dropped the old custom connection pooling and reqwest transport setup.
  • Built with default-features = false + key_auth/native_tls/hmac_rust so the Azure stack uses OpenSSL instead of pulling reqwest's aws-lc-rs rustls provider alongside Spin's workspace-pinned ring.
  • Replaced ambient auth with explicit AAD credential selection via auth_type, including user-assigned managed identity by client_id; account-key auth is unchanged.
  • Added optional region runtime config (defaults to East US) used as the routing proximity anchor, and account validation at spin up.
  • Compare-and-swap now maps only optimistic-concurrency failures — 412 (stale etag), 409 (raced create), and 404 (item deleted between current and swap) — to retryable CAS failures, and everything else to hard errors.
  • get-many now returns (key, none) for missing keys per the wasi:keyvalue batch contract instead of dropping them.

Runtime Config

Existing account-key configuration works unchanged. When key is omitted, auth_type selects the AAD credential: developer_tools (default), managed_identity (optionally with client_id), workload_identity, or service_principal (reads AZURE_TENANT_ID/AZURE_CLIENT_ID/AZURE_CLIENT_SECRET). There is no fallback between types. Optionally set region to bias routing toward the closest replica.

Comment thread crates/key-value-azure/src/store.rs Outdated
@ChihweiLHBird
ChihweiLHBird force-pushed the zhiwei/azure-kv-store-sdk-migration branch 4 times, most recently from c54b8f4 to 0ef22f4 Compare June 5, 2026 21:01
@ChihweiLHBird
ChihweiLHBird marked this pull request as ready for review June 5, 2026 21:02
Copilot AI review requested due to automatic review settings June 5, 2026 21:02
@ChihweiLHBird
ChihweiLHBird marked this pull request as draft June 5, 2026 21:02
@ChihweiLHBird
ChihweiLHBird force-pushed the zhiwei/azure-kv-store-sdk-migration branch from 0ef22f4 to 047b4d5 Compare June 5, 2026 21:02

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Modernizes the Azure Cosmos-backed key-value store to use newer Azure SDK APIs and adds explicit Azure AD authentication options plus region-aware routing.

Changes:

  • Migrates Cosmos DB interactions from older CollectionClient/document APIs to ContainerClient item APIs with lazy client initialization.
  • Adds explicit Azure AD credential selection (auth_type + optional client_id) and optional region configuration for proximity-based routing.
  • Updates Azure SDK dependency versions and removes the old connection-pooling feature wiring.

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 6 comments.

File Description
crates/key-value-azure/src/store.rs Refactors Cosmos client initialization + CRUD/CAS operations to new SDK APIs; adds auth parsing/utilities and unit tests.
crates/key-value-azure/src/lib.rs Extends runtime config with region, auth_type, and client_id; wires them into store construction and exports AzureCredentialKind.
crates/key-value-azure/Cargo.toml Updates Azure SDK dependencies to newer major versions; removes old connection-pooling feature block.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/key-value-azure/src/lib.rs
Comment thread crates/key-value-azure/src/store.rs Outdated
Comment thread crates/key-value-azure/src/store.rs Outdated
Comment thread crates/key-value-azure/src/store.rs Outdated
Comment thread crates/key-value-azure/src/store.rs Outdated
Comment thread crates/key-value-azure/src/store.rs
@ChihweiLHBird
ChihweiLHBird force-pushed the zhiwei/azure-kv-store-sdk-migration branch 4 times, most recently from 35982d4 to 198e1a1 Compare June 10, 2026 19:31
@ChihweiLHBird
ChihweiLHBird force-pushed the zhiwei/azure-kv-store-sdk-migration branch 3 times, most recently from 64546c4 to b5f6d83 Compare June 23, 2026 10:10
@ChihweiLHBird
ChihweiLHBird force-pushed the zhiwei/azure-kv-store-sdk-migration branch from b5f6d83 to 4e6e1ba Compare July 18, 2026 06:37
@ChihweiLHBird
ChihweiLHBird requested a review from Copilot July 22, 2026 06:21

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 5 changed files in this pull request and generated 5 comments.

Comment thread crates/key-value-azure/src/store.rs Outdated
Comment thread crates/key-value-azure/src/store.rs
Comment thread crates/key-value-azure/src/store.rs
Comment thread crates/key-value-azure/src/store.rs
Comment thread crates/key-value-azure/Cargo.toml
@ChihweiLHBird
ChihweiLHBird force-pushed the zhiwei/azure-kv-store-sdk-migration branch from cc3f18e to 964720e Compare July 22, 2026 06:45
@ChihweiLHBird ChihweiLHBird changed the title Azure SDK migration for kv store Azure Cosmos SDK upgrade for kv store Jul 22, 2026
@ChihweiLHBird
ChihweiLHBird marked this pull request as ready for review July 22, 2026 07:12
@ChihweiLHBird
ChihweiLHBird force-pushed the zhiwei/azure-kv-store-sdk-migration branch from a8d1b60 to 13d2cea Compare August 7, 2026 06:07
Signed-off-by: Zhiwei Liang <zhiwei.liang@zliang.me>
@itowlson
itowlson force-pushed the zhiwei/azure-kv-store-sdk-migration branch from 13d2cea to 7da00eb Compare August 26, 2026 00:59
@itowlson

Copy link
Copy Markdown
Collaborator

@ChihweiLHBird I rebased this to address merge conflicts, and squashed the commits (in case I ever have to rebase it again). Sorry for letting it linger!

@ChihweiLHBird

Copy link
Copy Markdown
Contributor Author

No problem, and thank you for the help, @itowlson!

@itowlson

Copy link
Copy Markdown
Collaborator

@ChihweiLHBird Would you be open to extending this PR to cover our other Azure dependency, the KeyVault stuff in the variables-azure crate? I started looking but couldn't get my head round some of the auth changes. Alternatively if you think it's better to roll this in first and then tackle variables then fair enough!

@ChihweiLHBird

Copy link
Copy Markdown
Contributor Author

Hi @itowlson, if variables-azure crate doesn't depend on this PR, I think we can work on it in a follow up PR? It may be easier to review that way.

@itowlson

Copy link
Copy Markdown
Collaborator

I'm looking at the runtime config stuff and I'm not clear if this is a breaking change. It sounds like ye olde key-style configuration is the same but what used to be "environmental" configuration may be different - you need to provide the auth_type entry to avoid being opted in to a CLI flow. Is that correct? (Or was it already the case before? I am not sure how managed identity fits into all this.)

It's not a problem if it is breaking, it just means we'll need to hold it for the next major release. (And it makes sense, because from what I've seen of the Azure identity client, it is much stricter about being specific about your auth type than before.)

@ChihweiLHBird

Copy link
Copy Markdown
Contributor Author

@itowlson Unfortunately yes, it's a breaking change by the Azure SDK redesign. And only users with Developer tools: Azure CLI (az login) or Azure Developer CLI (azd auth login) are not impacted.

@itowlson itowlson added the breaking-change Breaking change, can only go on major version label Aug 27, 2026
@itowlson

Copy link
Copy Markdown
Collaborator

@ChihweiLHBird no worries. I've added a label so that we're aware and don't merge it until we're okay with breakage. It would be super useful for docs purposes if you could jot down a quick note on what the runtime config TOML will look like once this is merged - your code is clearer than what we had before, but I'm still gonna need some help! Thanks!

@itowlson itowlson left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm no expert on this stuff but it looks good to me - thanks for slogging through it all. It looks rather nicer than what we had before (although I guess some of that is down to good work on the SDK), and I feel much more comfortable with the auth stuff. I had a couple of comments / questions but nothing blocking!

/// Developer tools: Azure CLI (`az login`), then Azure Developer CLI
/// (`azd auth login`). Intended for local development; the default when
/// `auth_type` is omitted.
#[default]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should force an explicit choice rather than defaulting? (This is not a suggestion: it's me not knowing what Azure tooling normally does.)

Some("service_principal") => Ok(Self::ServicePrincipal),
Some(other) => anyhow::bail!(
"unknown Azure Cosmos `auth_type` {other:?}; expected one of \
\"managed_identity\", \"workload_identity\", \"service_principal\", \

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be more readable to do this with a r#"..."# string

app_id: Option<String>,
}

/// Azure Cosmos Key / Value runtime config literal options for authentication

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good choice on breaking all this out into the auth module!

&& e.as_http_error().map(|e| e.status() != 404).unwrap_or(true)
{
return Err(log_error(e));
let partition_key = partition_key(self.store_id.as_deref(), key);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider self.partition_key(key) since we seem to be writing this out a lot!


async fn get_keys(&self, max_result_bytes: usize) -> Result<Vec<String>, Error> {
self.get_keys(max_result_bytes).await
let mut stream = self

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a lot of it is very similar to get_keys_async - could we have it sit on top of that? Or pull out the common code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking-change Breaking change, can only go on major version

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support global distribution configuration with Cosmos KV SDK

3 participants