Skip to content
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

Make client internally mutable #837

Merged
merged 9 commits into from
Jun 13, 2024
Merged

Make client internally mutable #837

merged 9 commits into from
Jun 13, 2024

Conversation

dani-garcia
Copy link
Member

@dani-garcia dani-garcia commented Jun 12, 2024

๐Ÿ“” Objective

We're having some deadlock problems with the Passkey API by calling certain SDK functions on the passkey-rs callbacks. These issues are caused because we're holding a reference to the RwLock during the execution of the Passkey operation and so calling into the SDK from inside that will deadlock.

To solve it we can't just use a lock at the client level, and so we need to move to using interior mutability, I've revived an old PR (#70) where we started doing that.

This PR is split into two commits, for ease of review:

Some notes of what the change entails:

  • I had to wrap EncryptionSettings in a RwLock<Arc<>> to get the API working, this is quite unfortunate as it forces us to clone EncryptionSettings to modify it when calling set_org_keys. We can't wrap the org_keys in a RwLock as we will get lifetime issues in get_key. I think the best solution for this will be the work on creating a Crypto store that only returns opaque key references, so we might want to prioritize that.
  • I extracted external_client from ApiConfigurations, as it's never mutated and it allows us to keep the same API for get_http_client

I hate that Mutex and RwLock return a Result in case they are poisoned, that's never our case and it makes their usage so cumbersome ๐Ÿ˜ข

Note: I've tried to change the smallest amount of code to make the change, so the secrets manager APIs (and bitwarden-json) are still mutable. If we're okay with these changes I can go back to update those in a separate PR.

โฐ 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 12, 2024

Logo
Checkmarx One โ€“ Scan Summary & Details โ€“ 7e2fdc15-9cf0-41fa-a91e-b844ec2eca82

No New Or Fixed Issues Found

Copy link

codecov bot commented Jun 12, 2024

Codecov Report

Attention: Patch coverage is 47.31458% with 206 lines in your changes missing coverage. Please review.

Project coverage is 60.07%. Comparing base (e5a8dba) to head (46f4b2c).

Files Patch % Lines
crates/bitwarden-uniffi/src/auth/mod.rs 0.00% 18 Missing โš ๏ธ
crates/bitwarden-uniffi/src/tool/sends.rs 0.00% 15 Missing โš ๏ธ
crates/bitwarden/src/client/client.rs 85.29% 15 Missing โš ๏ธ
crates/bitwarden/src/auth/renew.rs 50.00% 12 Missing โš ๏ธ
crates/bitwarden-uniffi/src/crypto.rs 0.00% 11 Missing โš ๏ธ
crates/bitwarden-uniffi/src/platform/fido2.rs 0.00% 10 Missing โš ๏ธ
crates/bitwarden/src/auth/client_auth.rs 25.00% 9 Missing โš ๏ธ
crates/bitwarden-uniffi/src/tool/mod.rs 0.00% 8 Missing โš ๏ธ
crates/bitwarden-uniffi/src/vault/ciphers.rs 0.00% 8 Missing โš ๏ธ
crates/bitwarden-uniffi/src/vault/attachments.rs 0.00% 7 Missing โš ๏ธ
... and 35 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #837      +/-   ##
==========================================
+ Coverage   59.22%   60.07%   +0.85%     
==========================================
  Files         186      186              
  Lines       12396    12246     -150     
==========================================
+ Hits         7341     7357      +16     
+ Misses       5055     4889     -166     

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

@Hinton Hinton self-requested a review June 13, 2024 10:22
# Conflicts:
#	crates/bitwarden-uniffi/src/crypto.rs
#	crates/bitwarden-uniffi/src/tool/mod.rs
#	crates/bitwarden-uniffi/src/tool/sends.rs
#	crates/bitwarden-uniffi/src/vault/attachments.rs
#	crates/bitwarden-uniffi/src/vault/ciphers.rs
#	crates/bitwarden-uniffi/src/vault/collections.rs
#	crates/bitwarden-uniffi/src/vault/folders.rs
#	crates/bitwarden/src/mobile/client_crypto.rs
#	crates/bitwarden/src/mobile/tool/client_sends.rs
crates/bitwarden/src/secrets_manager/secrets/update.rs Outdated Show resolved Hide resolved
crates/bitwarden/src/secrets_manager/secrets/create.rs Outdated Show resolved Hide resolved
crates/bitwarden/src/secrets_manager/projects/update.rs Outdated Show resolved Hide resolved
crates/bitwarden/src/secrets_manager/projects/create.rs Outdated Show resolved Hide resolved
crates/bitwarden/src/mobile/tool/client_sends.rs Outdated Show resolved Hide resolved
Comment on lines 72 to 74
token: RwLock<Option<String>>,
pub(crate) refresh_token: RwLock<Option<String>>,
pub(crate) token_expires_on: RwLock<Option<i64>>,
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we should find some way to group these together. You shouldn't be able to write to just one of these.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, that's true, I extracted them to a tokens struct, which at least simplifies a bit all the locking and unlocking.

# Conflicts:
#	crates/bitwarden/src/client/client.rs
#	crates/bitwarden/src/platform/fido2/authenticator.rs
#	crates/bitwarden/src/platform/fido2/mod.rs
@dani-garcia dani-garcia merged commit 07abbc7 into main Jun 13, 2024
105 checks passed
@dani-garcia dani-garcia deleted the ps/interior-mutability branch June 13, 2024 14:03
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.

None yet

2 participants