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 JWT issuing and validation. Change Harvester onboarding and authentication processes. #151

Merged

Conversation

maxlambrecht
Copy link
Contributor

Pull request check list

  • Proper tests/regressions included?
  • Documentation updated?

Affected functionality

Changed the harvester onboarding and authentication process:

  • The onboarding process uses a join_token, validates it's still valid and not used, and returns a JWT token with the subject equal to the onboarded trust domain name.

  • The authentication of API calls is done using a JWT validator to verify the Bearer token from the Authorization header.

Description of change

  • Added interfaces jwt.Issuer, jwt.Validator, and KeyManager, to issue and validate JWT tokens, using keys managed by a KeyManager.

  • The implementation added for the KeyManager interface is a simple in-memory manager.

  • Added a goroutine that request a new JWT token every 12 hours.

  • Added decorator to every API call in the Harvester to add the JWT Bearer token.

Which issue this pull requests fixes

Fixes GLCP-45111

@maxlambrecht
Copy link
Contributor Author

WIP: unit tests for the Onboard and GetNewJWTToken

Copy link
Collaborator

@Victorblsilveira Victorblsilveira left a comment

Choose a reason for hiding this comment

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

Excellent work here @maxlambrecht really good changes. I have just some minor doubts.|
See if make sense.

Copy link
Collaborator

@Victorblsilveira Victorblsilveira left a comment

Choose a reason for hiding this comment

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

I left 1 nit comment. So feel free to skip it if necessary.
All good, good job @maxlambrecht

@maxlambrecht
Copy link
Contributor Author

I left 1 nit comment. So feel free to skip it if necessary. All good, good job @maxlambrecht

Thanks @Victorblsilveira.
I added tests for the Onboard handler, and I'm adding tests for the GetJWT handler.

Signed-off-by: Max Lambrecht <[email protected]>
Signed-off-by: Max Lambrecht <[email protected]>
Signed-off-by: Max Lambrecht <[email protected]>
Signed-off-by: Max Lambrecht <[email protected]>
Signed-off-by: Max Lambrecht <[email protected]>
@maxlambrecht maxlambrecht force-pushed the feature/jwt-issuer-validator branch from 1e0fbf3 to 273e5a4 Compare May 10, 2023 17:29
@maxlambrecht
Copy link
Contributor Author

@Victorblsilveira I refactored the Onboard and GetNewJWTToken requests using a method callAPI to remove duplication. The rest the the harvester request method could use the same.

@sonarqubecloud
Copy link

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 7 Code Smells

No Coverage information No Coverage information
3.6% 3.6% Duplication

Signed-off-by: Max Lambrecht <[email protected]>
@Victorblsilveira
Copy link
Collaborator

@Victorblsilveira I refactored the Onboard and GetNewJWTToken requests using a method callAPI to remove duplication. The rest the the harvester request method could use the same.

I just realized that yesterday, on my bed, when trying to sleep. Why don't we use the client boilerplate for making that calls?

Copy link
Collaborator

@Victorblsilveira Victorblsilveira left a comment

Choose a reason for hiding this comment

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

Really good, there are some minors, like, Can we use the client boilerplates to consume Galadriel API? But I really think that you can decide whether to use it or not. Besides that, I think that your PR is good to go. +1

Copy link
Collaborator

@mgbcaio mgbcaio left a comment

Choose a reason for hiding this comment

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

just 2 comments, see if it makes sense =) Great job, Max. 🦕

assert.NotNil(t, key1)
assert.Equal(t, "foo", key1.ID())

key2, err := km.GenerateKey(ctx, "bar", cryptoutil.RSA2048)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
key2, err := km.GenerateKey(ctx, "bar", cryptoutil.RSA2048)
key2, err := km.GenerateKey(ctx, "bar", cryptoutil.RSA4096)

is this suppposed to be like this ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, what matters is the key id "bar".

if err != nil {
err := fmt.Errorf("failed to read bundle put body: %v", err)
return h.handleErrorAndLog(err, http.StatusBadRequest)
return h.handleErrorAndLog(err, err, http.StatusBadRequest)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we need to pass the err multiple times, cant the function use the same parameter ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because the method both logs the error and handles the error sending back a message to the caller, thus it takes in an error to log and an error to return, that usually are not the same.

@maxlambrecht
Copy link
Contributor Author

Really good, there are some minors, like, Can we use the client boilerplates to consume Galadriel API? But I really think that you can decide whether to use it or not. Besides that, I think that your PR is good to go. +1

That's a good question. I guess we can use the generated client. We can do the refactor in another PR.

Copy link
Collaborator

@mgbcaio mgbcaio left a comment

Choose a reason for hiding this comment

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

🍦

@maxlambrecht maxlambrecht merged commit ba2c8bd into HewlettPackard:main May 11, 2023
@maxlambrecht maxlambrecht deleted the feature/jwt-issuer-validator branch May 11, 2023 16:00
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.

3 participants