Skip to content

[PM-19479] Client-Managed SDK state definition #14839

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

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

dani-garcia
Copy link
Member

@dani-garcia dani-garcia commented May 19, 2025

🎟️ Tracking

https://bitwarden.atlassian.net/browse/PM-19479
https://bitwarden.atlassian.net/browse/PM-22593

📔 Objective

Initial setup of the Client-managed SDK state framework for the TS clients. This framework connects directly with State Providers and requires users to implement a SdkRecordMapper to return the user key definition, and define conversion to and from SDK objects. At the moment only Record based user key definitions are supported.

This also implements support for Ciphers as an example, but only for read-only operations, as the SDK->TS conversion is not implemented yet. I've placed the SdkRecordMapper in the same file where the model is defined, but let me know if you can think of a better place for it.

📸 Screenshots

⏰ Reminders before review

  • Contributor guidelines followed
  • All formatters and local linters executed and passed
  • Written new unit and / or integration tests where applicable
  • Protected functional changes with optionality (feature flags)
  • Used internationalization (i18n) for all UI strings
  • CI builds passed
  • Communicated to DevOps any deployment requirements
  • Updated any necessary documentation (Confluence, contributing docs) or informed the documentation team

🦮 Reviewer guidelines

  • 👍 (:+1:) or similar for great changes
  • 📝 (:memo:) or ℹ️ (:information_source:) for notes or general info
  • ❓ (:question:) for questions
  • 🤔 (:thinking:) or 💭 (:thought_balloon:) for more open inquiry that's not quite a confirmed issue and could potentially benefit from discussion
  • 🎨 (:art:) for suggestions / improvements
  • ❌ (:x:) or ⚠️ (:warning:) for more significant problems or concerns needing attention
  • 🌱 (:seedling:) or ♻️ (:recycle:) for future improvements or indications of technical debt
  • ⛏ (:pick:) for minor or nitpick changes

Copy link
Contributor

github-actions bot commented May 19, 2025

Logo
Checkmarx One – Scan Summary & Details683ce88a-2afb-4848-ba9d-98a00b71dfe9

Great job, no security vulnerabilities found in this Pull Request

# Conflicts:
#	libs/common/src/platform/services/sdk/default-sdk.service.ts
dani-garcia added a commit to bitwarden/sdk-internal that referenced this pull request Jun 24, 2025
## 🎟️ Tracking

https://bitwarden.atlassian.net/browse/PM-19479

## 📔 Objective

Implement a generic trait for accessing the client application's data
storage directly. Because we want the store access to be typed, but
`bitwarden_core` isn't aware of the models, `bitwarden_core` only
implements a generic way to set and retrieve generic `impl
Repository<T>` instances, somewhat like dependency injection. Then it's
up to each team/feature crates to define which models and which stores
are available.

This feature is created in a new `bitwarden-state`, which will be
expanded by a separate PR with the addition of SDK-managed state
(Sqlite+IndexedDB). At the moment this crate contains:
- A `Repository` trait which will be implemented by the clients (and the
SDK in the future), and a `RepositoryItem` marker trait which will be
used to mark which models are meant to be used with the repositories.
- A `StateRegistry` which stores all the client-managed Repositories,
and in the future will also handle the SDK-managed repositories.
- A new `StateClient` subclient under platform that will be used by the
applications to register their Repositories. Both the WASM and UniFFI
crates also need some conversion code to implement the `Repository`
traits. I've tried to simplify it as much as possible, and hide it
behind a macro when that wasn't possible.

Some limitations on the current design:
- The current integration with web clients requires the State Provider
definition to be `UserKeyDefinition<Record<string, T>>` to match the
key-value pattern in `Repository`. This usually matches with what is
being used for vault (encrypted ciphers/folders, etc), but it might fall
short on other domains, like profile data, or user keys.
- There's no great way of ensuring that all the client-managed
Repositories have been registered, other than keeping a list. I've tried
to use the `inventory` crate to keep a global list of all the existing
implementations and then validating that they've all been registered,
but that doesn't work for WASM. We might be able to use the `inventory`
crate and just run it in tests though.

For some documentation on how to use it, you can check the README:
https://github.com/bitwarden/sdk-internal/blob/ps/state-traits/crates/bitwarden-state/README.md

The continuation of this is in
#301, which contains SDK
managed repository support. The client implementation of both these PRs
is in bitwarden/clients#14839


## ⏰ Reminders before review

- Contributor guidelines followed
- All formatters and local linters executed and passed
- Written new unit and / or integration tests where applicable
- Protected functional changes with optionality (feature flags)
- Used internationalization (i18n) for all UI strings
- CI builds passed
- Communicated to DevOps any deployment requirements
- Updated any necessary documentation (Confluence, contributing docs) or
informed the documentation
  team

## 🦮 Reviewer guidelines

<!-- Suggested interactions but feel free to use (or not) as you desire!
-->

- 👍 (`:+1:`) or similar for great changes
- 📝 (`:memo:`) or ℹ️ (`:information_source:`) for notes or general info
- ❓ (`:question:`) for questions
- 🤔 (`:thinking:`) or 💭 (`:thought_balloon:`) for more open inquiry
that's not quite a confirmed
  issue and could potentially benefit from discussion
- 🎨 (`:art:`) for suggestions / improvements
- ❌ (`:x:`) or ⚠️ (`:warning:`) for more significant problems or
concerns needing attention
- 🌱 (`:seedling:`) or ♻️ (`:recycle:`) for future improvements or
indications of technical debt
- ⛏ (`:pick:`) for minor or nitpick changes
Copy link

codecov bot commented Jun 30, 2025

Codecov Report

Attention: Patch coverage is 20.00000% with 28 lines in your changes missing coverage. Please review.

Project coverage is 36.91%. Comparing base (8fec956) to head (2100ced).

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
.../src/platform/services/sdk/client-managed-state.ts 11.53% 23 Missing ⚠️
libs/common/src/vault/models/domain/cipher.ts 40.00% 3 Missing ⚠️
...n/src/platform/services/sdk/default-sdk.service.ts 50.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #14839      +/-   ##
==========================================
- Coverage   36.91%   36.91%   -0.01%     
==========================================
  Files        3232     3233       +1     
  Lines       93590    93625      +35     
  Branches    14084    14087       +3     
==========================================
+ Hits        34552    34559       +7     
- Misses      57602    57630      +28     
  Partials     1436     1436              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link

@dani-garcia dani-garcia requested a review from justindbaur June 30, 2025 12:21
@dani-garcia dani-garcia marked this pull request as ready for review June 30, 2025 12:21
@dani-garcia dani-garcia requested review from a team as code owners June 30, 2025 12:21
@nick-livefront
Copy link
Collaborator

@shane-melton @gbubemismith @Jingo88 Adding you as reviewers just to make sure you see this as it implements Cipher as an example.

Copy link
Collaborator

@nick-livefront nick-livefront left a comment

Choose a reason for hiding this comment

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

Pretty minimal vault changes ✅

@@ -409,3 +412,17 @@ export class Cipher extends Domain implements Decryptable<CipherView> {
return sdkCipher;
}
}

export class CipherRecordMapper implements SdkRecordMapper<CipherData, SdkCipher> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

🎨 non blocking and could always be done in the future: I probably would put this into a separate file only to keep this file dedicated to Cipher

Copy link
Contributor

Choose a reason for hiding this comment

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

+1

Copy link
Contributor

@gbubemismith gbubemismith left a comment

Choose a reason for hiding this comment

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

Vault changes look good

@@ -409,3 +412,17 @@ export class Cipher extends Domain implements Decryptable<CipherView> {
return sdkCipher;
}
}

export class CipherRecordMapper implements SdkRecordMapper<CipherData, SdkCipher> {
Copy link
Contributor

Choose a reason for hiding this comment

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

+1

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.

4 participants