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 key revocation checks, for credentials creation #546

Merged
merged 2 commits into from
Jun 26, 2023

Conversation

vlad-tim
Copy link
Contributor

@vlad-tim vlad-tim commented Jun 17, 2023

Overview

Add key revocation check when creating a credential, expose key revocation endpoint.

Description

  • Expose key revocation REST endpoint.
  • Add key revocation check for credential creation.
  • Add tests.

How Has This Been Tested?

Manual test:

  1. Create a new DID
  2. Create a new key, make controller be ID of the DID.
  3. Create a new credential using DID id and key ID from the previous steps. It succeeds.
  4. Revoke the key.
  5. Repeat the step 3. Now it fails with "error": "could not create credential: execute: failed to execute after retrying: signing credential: cannot use revoked key<MyKeyID>"

The above steps has been automated within pkg/server/router/credential_test.go file.

Also, a unit test added to pkg/service/keystore/service_test.go.

Checklist

Before submitting this PR, please make sure:

  • I have read the CONTRIBUTING document.
  • My code is consistent with the rest of the project
  • I have tagged the relevant reviewers and/or interested parties
  • I have updated the READMEs and other documentation of affected packages

References

Related to #451 .

@codecov-commenter
Copy link

codecov-commenter commented Jun 17, 2023

Codecov Report

Merging #546 (2e74105) into main (0e4d0fc) will increase coverage by 0.21%.
The diff coverage is 100.00%.

❗ Current head 2e74105 differs from pull request most recent head d7b70d6. Consider uploading reports for the commit d7b70d6 to get more accurate results

@@            Coverage Diff             @@
##             main     #546      +/-   ##
==========================================
+ Coverage   23.98%   24.19%   +0.21%     
==========================================
  Files          46       46              
  Lines        5119     5120       +1     
==========================================
+ Hits         1228     1239      +11     
+ Misses       3663     3650      -13     
- Partials      228      231       +3     
Impacted Files Coverage Δ
pkg/service/keystore/service.go 41.21% <ø> (+3.37%) ⬆️
pkg/server/server.go 58.96% <100.00%> (+0.16%) ⬆️

... and 1 file with indirect coverage changes

@vlad-tim
Copy link
Contributor Author

At this point I wonder if the proposed approach (using credential creation as example) is good enough. If yes, I will implement the check in other similar places, e.g. schema creation.

A potential alternative is to move the revocation check inside keyStore.GetKey (or something like that). Such approach may help not to forget making this check where necessary in the future.

@vlad-tim vlad-tim changed the title Add key revocation checks Add key revocation checks, credentials creation Jun 17, 2023
@vlad-tim vlad-tim marked this pull request as ready for review June 17, 2023 18:13
@vlad-tim vlad-tim changed the title Add key revocation checks, credentials creation Add key revocation checks, for credentials creation Jun 17, 2023
Copy link
Contributor

@andresuribe87 andresuribe87 left a comment

Choose a reason for hiding this comment

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

This is an improvement, but I'm torn on whether we should support a generic "state" vs a specific revoked field, which is a boolean.

After seeing the APIs for a couple of KMS providers, I noticed that's the approach they took.

pkg/service/keystore/service_test.go Outdated Show resolved Hide resolved
pkg/service/keystore/service_test.go Outdated Show resolved Hide resolved
assert.NotEmpty(t, keyResponse)
assert.Equal(t, privKey, keyResponse.Key)
assert.Equal(t, true, keyResponse.Revoked)
assert.NotEmpty(t, keyResponse.RevokedAt)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it makes sense to test the value here as well. You can do so by injecting a clock instance to the service.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have tried to do it, but stopped after discovering that the keystore service does not use Clock, it uses time.Now() instead. I noticed that all other services also use time.Now() with the only exception being the manifest service that uses Clock. However, the Clock module is unmaintained. Are you sure?

Copy link
Contributor

@andresuribe87 andresuribe87 Jun 23, 2023

Choose a reason for hiding this comment

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

Thanks for pointing out that Clock is now archived. I wasn't aware of that! That said, I think it's still a good approach so that times are tested.

I'm make a ticket to clean up other places that use time.Now().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could you elaborate on how the times should be tested within this PR specifically?

Copy link
Contributor

Choose a reason for hiding this comment

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

Following the same pattern as manifest service.

  1. Create a field of type Clock in the Service struct.
  2. Set that instance in the constructor by calling clock.New()
  3. In this test, override the field by setting up a mock clock keyStore.Clock = mockClock.
  4. Assert that the time in the mock clock is the same as the RevokedAt time.

This will need a little bit of refactoring of the Storage.RevokeKey method, but that's ok. The code will look much better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, updated.

@andresuribe87
Copy link
Contributor

Thank you!

@vlad-tim
Copy link
Contributor Author

I believe that all necessary changes have been addressed. Please let me know if there's anything else I need to do before this can be merged. Next, I will implement the revocation check in other places.

@andresuribe87
Copy link
Contributor

@decentralgabe PTAL

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