Skip to content

[PM-12612] SDK-Managed Repository support #301

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

Conversation

dani-garcia
Copy link
Member

@dani-garcia dani-garcia commented Jun 9, 2025

๐ŸŽŸ๏ธ Tracking

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

๐Ÿ“” Objective

A continuation of #213, this PR implements some demo SDK managed data store based on SQLite (on non-wasm) and IndexedDB (on wasm). The client implementation of both these PRs is in bitwarden/clients#14839

Both databases are wrapped in a Database trait and conditionally compiled based on platform. Then from each database we can get multiple Repository implementations that can be used to read and write data persistently. Each repository is mapped to a separate table in SQLite and Object Store in IndexedDb.

Some limitations of the current system:

  • There's currently no way to initialize the database path, IndexedDb uses a fixed path and SQLite uses an in-memory database. We should provide a way to provide that in the ClientSettings maybe?
    • For SQLite we probably want to provide a folder path, and then create a database with the user UUID
    • For IndexedDb we can provide a string prefix and combine it with the user UUID
  • IndexedDb can only create Object Stores during initialization, and only as part of the upgrade callback, which gets triggered when the provided version number is larger than what it was before. This forces us to create all the stores ahead of time and means we need to do migrations and versioning. This is something we will need to do anyway, we just have to keep it in mind.
  • The database currently needs to be initialized as a separate step, I feel like ideally we can do that as part of Client initialization, but that would require us to make it async. I've left that for the future.
  • Currently we don't have any indexes beyond the main key, but both sqlite and indexeddb support adding more.

โฐ 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 Jun 9, 2025

Logo
Checkmarx One โ€“ Scan Summary & Details โ€“ 72a201a6-a405-4039-a24b-b1a5398b4712

Great job, no security vulnerabilities found in this Pull Request

Copy link

codecov bot commented Jun 9, 2025

Codecov Report

Attention: Patch coverage is 1.97044% with 199 lines in your changes missing coverage. Please review.

Project coverage is 70.66%. Comparing base (025f818) to head (d3b903c).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
crates/bitwarden-state/src/sdk_managed/sqlite.rs 0.00% 73 Missing โš ๏ธ
crates/bitwarden-state/src/registry.rs 14.28% 24 Missing โš ๏ธ
crates/bitwarden-state/src/sdk_managed/mod.rs 0.00% 23 Missing โš ๏ธ
crates/bitwarden-uniffi/src/platform/mod.rs 0.00% 23 Missing โš ๏ธ
crates/bitwarden-wasm-internal/src/platform/mod.rs 0.00% 23 Missing โš ๏ธ
crates/bitwarden-core/src/platform/state_client.rs 0.00% 18 Missing โš ๏ธ
crates/bitwarden-state/src/repository.rs 0.00% 10 Missing โš ๏ธ
...tes/bitwarden-threading/src/thread_bound_runner.rs 0.00% 5 Missing โš ๏ธ
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #301      +/-   ##
==========================================
- Coverage   71.38%   70.66%   -0.73%     
==========================================
  Files         237      239       +2     
  Lines       18938    19135     +197     
==========================================
+ Hits        13519    13521       +2     
- Misses       5419     5614     +195     

โ˜” View full report in Codecov by Sentry.
๐Ÿ“ข Have feedback on the report? Share it here.

๐Ÿš€ New features to boost your workflow:
  • โ„๏ธ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • ๐Ÿ“ฆ JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

dani-garcia added a commit 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
Base automatically changed from ps/state-traits to main June 24, 2025 08:51
# Conflicts:
#	Cargo.lock
#	crates/bitwarden-state/Cargo.toml
#	crates/bitwarden-state/src/registry.rs
#	crates/bitwarden-state/src/repository.rs
#	crates/bitwarden-vault/src/cipher/cipher.rs

# Conflicts:
#	crates/bitwarden-vault/src/vault_client.rs

# Conflicts:
#	Cargo.lock
#	crates/bitwarden-state/Cargo.toml

Use bundled sqlite to solve compile issues

Update readme

Don't stringify the data in indexeddb

Remove the comment

Update readme
coroiu and others added 2 commits June 26, 2025 13:21
โ€ฆes (#329)

## ๐ŸŽŸ๏ธ Tracking

<!-- Paste the link to the Jira or GitHub issue or otherwise describe /
point to where this change is coming from. -->

## ๐Ÿ“” Objective

<!-- Describe what the purpose of this PR is, for example what bug
you're fixing or new feature you're adding. -->

## โฐ 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

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.

2 participants