Skip to content

Change the Managed Group to accept a KeyStore ref instead of keeping a CredentialBundle ref - #385

Merged
kkohbrok merged 15 commits into
mainfrom
konrad/interop_keystore
Apr 19, 2021
Merged

kkohbrok merged 15 commits into
mainfrom
konrad/interop_keystore

Conversation

@kkohbrok

@kkohbrok kkohbrok commented Apr 7, 2021 •

Copy link
Copy Markdown
Member

This PR replaces #334, as it goes a step further and doesn't keep the KeyStore reference in the ManagedGroup struct, but instead passes a KeyStore reference into every call into the Managed Group API. This allows us to avoid lifetime issues, as Rust doesn't allow struct-internal references.

One caveat in this PR that might be worth discussing:

  • We currently ignore (i.e. unwrap) PoisonErrors when accessing KeyPackageBundles and CredentialBundles. The argument being that there is (to my knowledge) no way that the code can panic while it holds a write lock. In particular while holding a write lock, we only interact with the HashMap using get, insert or remove, which should be safe.

The PR is rather large, but should be easy to review, as a lot of the changes are due to the change of API, so function calls will look different, but in most places, there are no changes in terms of functionality.

@github-actions

github-actions Bot commented Apr 7, 2021

Copy link
Copy Markdown

This PR is so big! Please, split it 😊

@github-actions

Copy link
Copy Markdown

This PR is so big! Please, split it 😊

@kkohbrok
kkohbrok marked this pull request as ready for review April 12, 2021 14:10
@github-actions

Copy link
Copy Markdown

This PR is so big! Please, split it 😊

@github-actions

Copy link
Copy Markdown

This PR is so big! Please, split it 😊

@franziskuskiefer franziskuskiefer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! I think we're getting there this time 🙂 A couple comments, mostly around the API and some cleanup.

pub fn add_members(
&mut self,
key_store: &KeyStore,
key_packages: &[KeyPackage],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Shouldn't we pass in the key package hashes here now?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I guess that would mean that the KeyStore also stores KPs from other users. I'm not against it, but it's probably not in scope just now.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We might want to have that functionality in the KeyStore at some point, but I'd prefer to land it as it currently is. We can add more stuff in later, smaller PRs.

Comment thread src/key_store/mod.rs
//! `CredentialBundle` instances, `KeyPackageBundle` instances should not be
//! used across groups.
//!
//! This design is temporary and only until the `ManagedGroup` is refactored to

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you add an issue number here?

Comment thread src/key_store/mod.rs Outdated
/// Generate a fresh `CredentialBundle` with the given parameters and store
/// it in the `KeyStore`. Returns the corresponding `Credential` or an error
/// if the creation of the `CredentialBundle` fails.
pub fn generate_credential(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It is called generate_credential but returns a CredentialBundle that's confusing.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also, why only return the credential?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Wait, your comments are contradicting one another 😄
The idea is to generate a CredentialBundle, store it in the KeyStore and return the corresponding credential. As remarked by @raphaelrobert in the comment above, the name is in line with the return type. That's not to say that we can't name both functions differently, though.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That should be a generates, not returns in the first comment.
My issue is not with returning a credential, but you don't generate a credential but a credential bundle. That's unexpected.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I see. So generate_credential_bundle (and _key_package_bundle) as a replacement? The documentation should then explain why it only returns a Credential (or KeyPackage respectively).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Both isn't really great. I don't have a good suggestion for how to resolve this. But generate_credential_bundle is at least doing what it says. You could also have a separate generate and get function, i.e. don't return anything. But really this is some sort of generate_key_pair, not a generate_random_point, so this has to be made clear.
But since the API will change anyway I'm also fine with an explicit follow up issue.

Comment thread src/key_store/mod.rs Outdated
//! `KeyPackage` instances are meant to be published so other parties can use
//! them to add the publishing party to groups.
//!
//! ### `KeyPackageBundle` Ownership

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This makes me expect a "CredentialBundle Ownership" section.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'll rename it then, as the section discusses both KPB and CB ownership.

Comment thread src/key_store/mod.rs Outdated
/// error if no `CredentialBundle` can be found in the `KeyStore`
/// corresponding to the given `Credential` or if an error occurred during
/// the creation of the `KeyPackageBundle`.
pub fn generate_key_package(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This generates a KeyPackageBundle not a key_package.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this is fine though, because the bundle is now only stored within the KeyStore. From an outside perspective, we only ever deal with key packages. The return type is consistent with the function name and the caller doesn't need to know what's happening internally.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's in line with the other functions. But it's still not doing what it says, i.e. it's not generating only a key package.

&self.credential_bundle.credential()
/// Returns own credential. If the group is inactive, it returns a
/// `UseAfterEviction` error.
pub fn credential(&self) -> Result<Credential, ManagedGroupError> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why do you need this to return a copy?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Because the tree lives in a Refcell and goes out of scope at the end of this function, which would invalidate any reference to the Credential. We'd have to return some sort of guard structure to keep that reference alive. I thought copying the credential would be easier for now.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ok, for the sake of getting this landed and as you say this should chnage in future, please add a ToDo and issue number.

return Err(ManagedGroupError::UseAfterEviction(UseAfterEviction::Error));
}

let credential = self.credential()?;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see this block a lot in here. The code should be factored out somehow to avoid having so many copies of this code.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That is tricky due to lifetime concerns. The credential_bundle is actually a CbGuard, which contains a reference to the SignaturePublicKey in the credential. I could change the CbGuard to contain a full instance of the key rather than just a reference, which might make a separate function possible, but I haven't tried that yet.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't understand why this would be a problem for a helper function, but I haven't tried it myself. I was thinking about something along the lines of:

fn fetch_credential_bundle(credential: &Credential, key_store: &'a KeyStore) -> Result<CbGuard, ManagedGroupError> { 
    ... 
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Right, I don't think you can clean this up without changing self.credential() to return a reference.

Comment thread src/lib.rs Outdated
pub mod framing;
pub mod group;
mod key_packages;
mod key_store;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

pub?

Comment thread tests/test_decryption_key_index.rs Outdated
let number_of_clients = 20;
let setup = ManagedTestSetup::new(managed_group_config, number_of_clients);
setup.create_clients();
// Create a basic group with more than 4 members to create a tree with intermediate nodes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you fix the indentation here?

@raphaelrobert raphaelrobert left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for tackling this again! Definitely looks good, and we can always change things later if we need them.

Comment thread src/group/managed_group/mod.rs Resolved
Comment thread src/group/managed_group/mod.rs Resolved
pub fn add_members(
&mut self,
key_store: &KeyStore,
key_packages: &[KeyPackage],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I guess that would mean that the KeyStore also stores KPs from other users. I'm not against it, but it's probably not in scope just now.

Comment thread src/group/managed_group/mod.rs Resolved
Comment thread src/group/managed_group/mod.rs Resolved
Comment thread src/group/managed_group/ser.rs Outdated
}

impl<'a> Serialize for ManagedGroup<'a> {
impl<'a> Serialize for ManagedGroup {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think you can also drop the first lifetime here

Comment thread src/key_store/mod.rs Outdated
/// error if no `CredentialBundle` can be found in the `KeyStore`
/// corresponding to the given `Credential` or if an error occurred during
/// the creation of the `KeyPackageBundle`.
pub fn generate_key_package(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this is fine though, because the bundle is now only stored within the KeyStore. From an outside perspective, we only ever deal with key packages. The return type is consistent with the function name and the caller doesn't need to know what's happening internally.


use super::{KeyStore, KeyStoreError};

ctest_ciphersuites!(key_storage, test(param: CiphersuiteName) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A brief description of the test would be good

@kkohbrok kkohbrok left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks for the reviews! I tried to answer to all comments that I didn't immediately address in the code.

Comment thread src/group/managed_group/mod.rs Resolved
Comment thread src/group/managed_group/mod.rs Resolved
pub fn add_members(
&mut self,
key_store: &KeyStore,
key_packages: &[KeyPackage],

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We might want to have that functionality in the KeyStore at some point, but I'd prefer to land it as it currently is. We can add more stuff in later, smaller PRs.

Comment thread src/group/managed_group/mod.rs Resolved
&self.credential_bundle.credential()
/// Returns own credential. If the group is inactive, it returns a
/// `UseAfterEviction` error.
pub fn credential(&self) -> Result<Credential, ManagedGroupError> {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Because the tree lives in a Refcell and goes out of scope at the end of this function, which would invalidate any reference to the Credential. We'd have to return some sort of guard structure to keep that reference alive. I thought copying the credential would be easier for now.

return Err(ManagedGroupError::UseAfterEviction(UseAfterEviction::Error));
}

let credential = self.credential()?;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That is tricky due to lifetime concerns. The credential_bundle is actually a CbGuard, which contains a reference to the SignaturePublicKey in the credential. I could change the CbGuard to contain a full instance of the key rather than just a reference, which might make a separate function possible, but I haven't tried that yet.

Comment thread src/group/managed_group/mod.rs Resolved
Comment thread src/key_store/mod.rs Outdated
//! `KeyPackage` instances are meant to be published so other parties can use
//! them to add the publishing party to groups.
//!
//! ### `KeyPackageBundle` Ownership

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'll rename it then, as the section discusses both KPB and CB ownership.

Comment thread src/key_store/mod.rs Outdated
/// Generate a fresh `CredentialBundle` with the given parameters and store
/// it in the `KeyStore`. Returns the corresponding `Credential` or an error
/// if the creation of the `CredentialBundle` fails.
pub fn generate_credential(

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Wait, your comments are contradicting one another 😄
The idea is to generate a CredentialBundle, store it in the KeyStore and return the corresponding credential. As remarked by @raphaelrobert in the comment above, the name is in line with the return type. That's not to say that we can't name both functions differently, though.

@github-actions

Copy link
Copy Markdown

This PR is so big! Please, split it 😊

@codecov

codecov Bot commented Apr 16, 2021 •

Copy link
Copy Markdown

Codecov Report

Merging #385 (a9cec47) into main (5b6371b) will increase coverage by 0.22%.
The diff coverage is 83.40%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #385      +/-   ##
==========================================
+ Coverage   83.62%   83.84%   +0.22%     
==========================================
  Files         108      111       +3     
  Lines        7784     7878      +94     
==========================================
+ Hits         6509     6605      +96     
+ Misses       1275     1273       -2     
Impacted Files Coverage Δ
src/group/errors.rs 26.08% <0.00%> (ø)
src/group/managed_group/config.rs 100.00% <ø> (ø)
src/group/managed_group/events.rs 50.00% <0.00%> (ø)
src/key_store/errors.rs 0.00% <0.00%> (ø)
src/messages/mod.rs 93.93% <ø> (+1.86%) ⬆️
src/messages/tests/test_proposals.rs 100.00% <ø> (ø)
src/schedule/errors.rs 0.00% <ø> (ø)
src/schedule/mod.rs 81.78% <ø> (+0.65%) ⬆️
src/utils.rs 76.41% <ø> (ø)
tests/test_managed_api.rs 100.00% <ø> (ø)
... and 49 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0a77f29...a9cec47. Read the comment docs.

raphaelrobert
raphaelrobert previously approved these changes Apr 16, 2021

@raphaelrobert raphaelrobert left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Except for the code duplication isuue raised by @franziskuskiefer, all other comments were addressed, so I will approve this now from my side.

@github-actions

Copy link
Copy Markdown

This PR is so big! Please, split it 😊

@github-actions

Copy link
Copy Markdown

This PR is so big! Please, split it 😊

@franziskuskiefer franziskuskiefer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's get this in 🚢 and follow up on the issues to improve later.

@kkohbrok
kkohbrok merged commit 82c6fa5 into main Apr 19, 2021
@kkohbrok
kkohbrok deleted the konrad/interop_keystore branch April 19, 2021 12:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants