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

Add serde to Public and Private #466

Merged
merged 1 commit into from
Jan 23, 2024

Conversation

Firstyear
Copy link
Contributor

Add support for serde to Public and Private, going through the current Marshall and Unmarshall pathways. This pattern is quite "simple" so it could be possible to duplicate it to other structures, or macro them for types that implement Marshall and Unmarshall.

@Superhepper
Copy link
Collaborator

I think it would be a good idea to make serde support as an optional feature.

@Firstyear
Copy link
Contributor Author

I think it would be a good idea to make serde support as an optional feature.

Serde is already hard-required, so why make it optional?

@Superhepper
Copy link
Collaborator

Oh, you are right. That was unfortunate. Then it will not matter for this PR but rather something we should look into doing. Because it is nice in my opinion to make it possible for the user to opt out of non essential functionality.

@wiktor-k
Copy link
Collaborator

wiktor-k commented Nov 3, 2023

Agreed with @Superhepper, serde should be optional but it's not an issue with this PR.

Copy link
Member

@ionut-arm ionut-arm left a comment

Choose a reason for hiding this comment

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

Thanks for looking into implementing this!

tss-esapi/src/structures/buffers/private.rs Outdated Show resolved Hide resolved
tss-esapi/src/structures/buffers/private.rs Outdated Show resolved Hide resolved
Copy link
Member

@ionut-arm ionut-arm left a comment

Choose a reason for hiding this comment

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

Happy for this to go in, but maybe a couple of tests for (de)serialization would be good to catch any issues down the line! Also, the formatter seems to be complaining

@Firstyear
Copy link
Contributor Author

Looks like I stepped on a land-mine adding these tests.

Following the pattern of the marshall_unmarshall test I added these with a reflexive test using serde_json. serde_json was added as a dev-dependency.

This caused more than 1500 compiler errors - it turns out that once serde_json exists, this confuses the compiler about the partialEq trait in the macros "test_valid_conversion" used throughout the test suite. Because of this, assert_eq! fails to work because there are multiple possible satisfying types for the ::into calls.

An example is:

error[E0283]: type annotations needed
  --> /home/886b45ce-98b2-4d79-9069-c9ff25bc0232/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/macros/mod.rs:40:32
   |
36 |   macro_rules! assert_eq {
   |   ---------------------- in this expansion of `assert_eq!` (#2)
...
40 |                   if !(*left_val == *right_val) {
   |                                  ^^ cannot infer type
   |
  ::: tss-esapi/tests/integration_tests/constants_tests/algorithm_tests.rs:17:1
   |
17 | / macro_rules! test_conversion {
18 | |     ($tpm_alg_id:ident, $algorithm:ident) => {
19 | |         assert_eq!($tpm_alg_id, AlgorithmIdentifier::$algorithm.into());
   | |         --------------------------------------------------------------- in this macro invocation (#2)
20 | |         assert_eq!(
...  |
27 | |     };
28 | | }
   | |_- in this expansion of `test_conversion!` (#1)
...
32 |       test_conversion!(TPM2_ALG_CAMELLIA, Camellia);
   |       --------------------------------------------- in this macro invocation (#1)
   |
   = note: multiple `impl`s satisfying `u16: PartialEq<_>` found in the following crates: `core`, `serde_json`:
           - impl PartialEq for u16;
           - impl PartialEq<serde_json::Value> for u16;

I think this is beyond my ability to solve this, and probably needs some bigger work to clean up.

@Firstyear
Copy link
Contributor Author

In light of the above issue, what do you think I should do here @ionut-arm ?

@Superhepper
Copy link
Collaborator

Have you tried something trivial like:

if !(u16::from(*left_val) == u16::from(*right_val)) {

And see if it helps?

@Firstyear
Copy link
Contributor Author

Firstyear commented Nov 25, 2023

Have you tried something trivial like:

if !(u16::from(*left_val).eq(u16::from(*right_val))) {

And see if it helps?

There are hundreds of locations that need this update. I'm not doing it in this PR.

@Superhepper
Copy link
Collaborator

I can help you fix some of them.

Superhepper added a commit to Superhepper/rust-tss-esapi that referenced this pull request Nov 25, 2023
- Many of tests uses macro to create the test code and in this
  test code it is common practice to use ```into``` or
  ```try_into``` conversions. But this can cause ambiguities later
  on if new types introduced implements traits that previously only
  was implemented by one type. So in order to avoid this kind of
  ambiguity some of the conversions have been changed. This only
  a first step of changes that probably needs to be done. This fixes
  the errors that occurs if one tries to add ```serde_json``` to the
  ```cargo.yaml``` which is probably needed in order to be able to
  write tests for parallaxsecond#466.

Signed-off-by: Jesper Brynolf <[email protected]>
@Firstyear Firstyear force-pushed the 20231103-add-serde-feature branch from 8b4797d to fae4a03 Compare December 14, 2023 04:48
@Firstyear
Copy link
Contributor Author

Thanks to @Superhepper I was now able to add the serde tests :)

@Firstyear
Copy link
Contributor Author

Hey all, what else is needed here? We really want this merged so that we can use it for TPM support for Azure AD and other linux pam authentication subsystems.

@Superhepper
Copy link
Collaborator

Not anything I think. I would just want CI to work again so this can without getting some unrelated errors.

Superhepper
Superhepper previously approved these changes Jan 17, 2024
@Superhepper
Copy link
Collaborator

Try to rebase this on main now. The CI should be fine.

@Firstyear Firstyear force-pushed the 20231103-add-serde-feature branch 2 times, most recently from aa0b092 to 43b91f4 Compare January 19, 2024 05:29
wiktor-k
wiktor-k previously approved these changes Jan 19, 2024
Copy link
Collaborator

@wiktor-k wiktor-k left a comment

Choose a reason for hiding this comment

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

Just a couple of tiny lints remain. 👍

This allows serialisation and deserialisation of the Public and Private
structures, which is required for storing these in many database or
serialised forms.

Signed-off-by: William Brown <[email protected]>
@Firstyear Firstyear dismissed stale reviews from wiktor-k and Superhepper via 2f3cd59 January 20, 2024 00:01
@Firstyear Firstyear force-pushed the 20231103-add-serde-feature branch from 43b91f4 to 2f3cd59 Compare January 20, 2024 00:01
@Firstyear
Copy link
Contributor Author

All fixed :)

@Superhepper Superhepper requested a review from wiktor-k January 23, 2024 11:05
Copy link
Collaborator

@wiktor-k wiktor-k left a comment

Choose a reason for hiding this comment

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

Looks straightforward. Thanks!

@wiktor-k wiktor-k merged commit 8cc11fc into parallaxsecond:main Jan 23, 2024
11 checks passed
@Firstyear Firstyear deleted the 20231103-add-serde-feature branch January 24, 2024 00:13
@Firstyear
Copy link
Contributor Author

Thank you all so much!

Firstyear pushed a commit to Firstyear/rust-tss-esapi that referenced this pull request Apr 25, 2024
- Many of tests uses macro to create the test code and in this
  test code it is common practice to use ```into``` or
  ```try_into``` conversions. But this can cause ambiguities later
  on if new types introduced implements traits that previously only
  was implemented by one type. So in order to avoid this kind of
  ambiguity some of the conversions have been changed. This only
  a first step of changes that probably needs to be done. This fixes
  the errors that occurs if one tries to add ```serde_json``` to the
  ```cargo.yaml``` which is probably needed in order to be able to
  write tests for parallaxsecond#466.

Signed-off-by: Jesper Brynolf <[email protected]>
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