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

Implementing Galadriel Server Management Handlers API #150

Merged

Conversation

Victorblsilveira
Copy link
Collaborator

Pull request check list

  • Proper tests/regressions included?
  • Documentation updated?

Affected functionality

All endpoints of the Server Management API.

Description of change

Implementing some endpoints from de UDS OpenAPI specifications.
Unit Tests included.

Which issue this pull requests fixes

@Victorblsilveira Victorblsilveira self-assigned this May 8, 2023
Copy link
Member

Choose a reason for hiding this comment

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

can we rename this to admin.go?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm modifying the same file, can we postpone the renaming for now?

pkg/server/datastore/fakedatabase.go Outdated Show resolved Hide resolved
pkg/server/datastore/fakedatabase.go Outdated Show resolved Hide resolved
@Victorblsilveira Victorblsilveira marked this pull request as ready for review May 11, 2023 14:46
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.

Looks pretty good for me, lets see what others think. Thanks for putting this together, Victor! amazing work.

Comment on lines 83 to 85
tdUUID1 := NewNullableID()
tdUUID2 := NewNullableID()
tdUUID3 := NewNullableID()
Copy link
Collaborator

Choose a reason for hiding this comment

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

can we declare this to be reused in the other tests ? something like:

var (
	// Relationships ID's
	r1ID = NewNullableID()
	r2ID = NewNullableID()
	r3ID = NewNullableID()

	// Trust Domains ID's
	tdUUID1 = NewNullableID()
	tdUUID2 = NewNullableID()
	tdUUID3 = NewNullableID()
)

pkg/server/endpoints/management.go Outdated Show resolved Hide resolved
pkg/server/endpoints/management.go Outdated Show resolved Hide resolved
}

return nil
}

// GetRelationshipsRelationshipID retrieve a specific relationship based on its id - (GET /relationships/{relationshipID})
func (h AdminAPIHandlers) GetRelationshipsRelationshipID(ctx echo.Context, relationshipID api.UUID) error {
func (h AdminAPIHandlers) GetRelationshipsRelationshipID(echoCtx echo.Context, relationshipID api.UUID) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

Change the spec adding the operation ID=GetRelationshipByID to avoid having this awful name.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That makes sense

}

return nil
}

// GetTrustDomainTrustDomainName retrieve a specific trust domain by its name - (GET /trust-domain/{trustDomainName})
func (h AdminAPIHandlers) GetTrustDomainTrustDomainName(ctx echo.Context, trustDomainName api.TrustDomainName) error {
func (h AdminAPIHandlers) GetTrustDomainTrustDomainName(echoCtx echo.Context, trustDomainName api.TrustDomainName) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here, change the open api spec to generate a better name: GetTrustDomainByName

return nil
}

// PutTrustDomainTrustDomainName updates the trust domain - (PUT /trust-domain/{trustDomainName})
func (h AdminAPIHandlers) PutTrustDomainTrustDomainName(ctx echo.Context, trustDomainName api.UUID) error {
func (h AdminAPIHandlers) PutTrustDomainTrustDomainName(echoCtx echo.Context, trustDomainID api.UUID) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here.

pkg/server/endpoints/management.go Outdated Show resolved Hide resolved
return td, nil
}

func (h *AdminAPIHandlers) filterRelationshipsByStatus(
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I will need your changes to simplify that.

Copy link
Contributor

@maxlambrecht maxlambrecht May 15, 2023

Choose a reason for hiding this comment

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

you can copy my function and then once one of the PRs is merged refactor to remove the duplication.

Here is a new version more readable:

func filterRelationshipsByConsentStatus(trustDomain uuid.UUID, relationships []*entity.Relationship, status api.ConsentStatus) []*entity.Relationship {
	filtered := make([]*entity.Relationship, 0)

	for _, relationship := range relationships {
		trustDomainA := relationship.TrustDomainAID
		trustDomainB := relationship.TrustDomainBID
		trustDomainAConsent := api.ConsentStatus(relationship.TrustDomainAConsent)
		trustDomainBConsent := api.ConsentStatus(relationship.TrustDomainBConsent)

		isConsentStatusMatch := (trustDomainA == trustDomain && trustDomainAConsent == status) ||
			(trustDomainB == trustDomain && trustDomainBConsent == status)

		if isConsentStatusMatch {
			filtered = append(filtered, relationship)
		}
	}

	return filtered
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If I am not wrong, I think you changed the database models to support that. So functions like api.ConsenStatus does not exists.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think that we can push your PR first. Since is already on review

eRelationship := reqBody.ToEntity()
rel, err := h.Datastore.CreateOrUpdateRelationship(gctx, eRelationship)

if err := h.checkTrustDomains(ctx, http.StatusBadRequest, eRelationship.TrustDomainAID, eRelationship.TrustDomainBID); err != nil {
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 this can be removed and it would be better handled at the datastore layer, the CreateOrUpdateRelationship will fail if one of the trust domain doesn't exist because a DB constraint will fail.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, agreed that the database can handle that, the point of checking before is to properly identify the problem. I mean, it was a bad request from the user rather than an internal server error. We could do that by checking the error message instead but this will also make us couple with the specific database. Let me know what you think. Can we handle that in a different way?

Copy link
Contributor

@maxlambrecht maxlambrecht May 15, 2023

Choose a reason for hiding this comment

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

You can do it this way:

tdA, err := h.lookupTrustDomain(ctx, eRelationship.TrustDomainAID)
	if err != nil {
		return err
	}
	
tdB, err := h.lookupTrustDomain(ctx, eRelationship.TrustDomainBID)
	if err != nil {
		return err
	}	

...

func (h *HarvesterAPIHandlers) lookupTrustDomain(ctx context.Context, trustDomainID uuid.UUID) (*entity.TrustDomain, error) {
	td, err := h.Datastore.FindBundleByTrustDomainID(ctx, trustDomainID)
	if err != nil {
		msg := "error looking up trust domain"
		errMsg := fmt.Errorf("%s: %w", errorMsg, err)
		return nil, h.handleErrorAndLog(errMsg, msg, http.StatusInternalServerError)
	}

	if td == nil {
		errMsg := fmt.Sprintf("trust domain not found: %q", trustDomainID)
		return nil, h.handleErrorAndLog(errors.New(errMsg), errMsg, http.StatusBadRequest)
	}

	return td, nil
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Updated my comment here ☝️

Victorblsilveira and others added 2 commits May 11, 2023 17:08
Co-authored-by: Max Lambrecht <[email protected]>
Signed-off-by: Victor Vieira Barros Leal da Silveira <[email protected]>
Co-authored-by: Max Lambrecht <[email protected]>
Signed-off-by: Victor Vieira Barros Leal da Silveira <[email protected]>
@@ -1,5 +1,7 @@
package endpoints

const (
testTrustDomain = "test.com"
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 we don't need to create a separate file for constants used in tests.

@@ -46,8 +46,8 @@ func SetupMiddleware() *AuthNTestSetup {
}
}

func SetupToken(t *testing.T, ds datastore.Datastore, token string, tdID uuid.UUID) *entity.JoinToken {
td, err := spiffeid.TrustDomainFromString(testTrustDomain)
func SetupToken(t *testing.T, ds datastore.Datastore, tdID uuid.UUID, token, tdName string) *entity.JoinToken {
Copy link
Contributor

Choose a reason for hiding this comment

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

This function doesn't exist anymore.

@maxlambrecht
Copy link
Contributor

It would be a good idea to rebase your branch before continuing making changes.

// BodylessResponse wraps error echo body-less responses.
func BodylessResponse(ctx echo.Context) error {
// BodilessResponse wraps error echo body-less responses.
func BodilessResponse(ctx echo.Context, code int) error {
Copy link
Contributor

@maxlambrecht maxlambrecht May 15, 2023

Choose a reason for hiding this comment

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

why add this parameter that is not used?

Name suggestions for this function: NoContentResponse or EmptyResponse

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Misstype

@@ -34,21 +34,22 @@ func Setup() *HTTPSetup {
func TestWriteResponse(t *testing.T) {
t.Run("Error when nil body is passed", func(t *testing.T) {
setup := Setup()
err := WriteResponse(setup.EchoContext, nil)
err := WriteResponse(setup.EchoContext, 0, nil)
Copy link
Contributor

Choose a reason for hiding this comment

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

is 0 a valid http status code?

@@ -25,6 +26,23 @@ import (
const (
jwtPath = "/jwt"
onboardPath = "/onboard"

Copy link
Contributor

Choose a reason for hiding this comment

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

These changes shouldn't be part of this PR, it should be focused on the management and management_test. I'm working on the Harvester API and making changes to this file, so it will be very hard to merge.

Copy link
Collaborator Author

@Victorblsilveira Victorblsilveira May 15, 2023

Choose a reason for hiding this comment

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

I just add const, but I will move to another file.

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

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

No Coverage information No Coverage information
0.0% 0.0% Duplication

Copy link
Contributor

@maxlambrecht maxlambrecht 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 5ad8753 into HewlettPackard:main May 16, 2023
@Victorblsilveira Victorblsilveira deleted the victor/admin-handlers branch May 16, 2023 17:56
maxlambrecht pushed a commit that referenced this pull request May 16, 2023
Implementing Galadriel Server Management Handlers API (#150)

Signed-off-by: Max Lambrecht <[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