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
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
bfdfcc4
feat: Implementing Admin Handlers
Victorblsilveira May 5, 2023
cbdaf26
feat: Adding unit testes helpers and implemenenting get relationships…
Victorblsilveira May 8, 2023
c55c71b
Merge branch 'main' into victor/admin-handlers
Victorblsilveira May 8, 2023
664cb58
feat: Creating unit tests for relationshio request creation and impro…
Victorblsilveira May 9, 2023
d02e836
Merge remote-tracking branch 'origin/main' into victor/admin-handlers
Victorblsilveira May 9, 2023
a55e8fa
fix: Unmashaling assertion
Victorblsilveira May 9, 2023
a7edd5d
fix: Removin code duplication
Victorblsilveira May 9, 2023
c621801
fix: Organization
Victorblsilveira May 9, 2023
4b49fcc
feat: Adding more unit tests
Victorblsilveira May 10, 2023
5f3dcbd
feat: Changin return code for relationship request creation
Victorblsilveira May 10, 2023
9bc7b3a
feat: Finishing unit tests
Victorblsilveira May 11, 2023
46ca412
Update pkg/server/endpoints/management.go
Victorblsilveira May 11, 2023
02de6e6
Update pkg/server/endpoints/management.go
Victorblsilveira May 11, 2023
990cd61
Merge remote-tracking branch 'origin/main' into victor/admin-handlers
Victorblsilveira May 15, 2023
85ce235
fix: Function names
Victorblsilveira May 15, 2023
63306ec
fix: Centralizing common stuff
Victorblsilveira May 15, 2023
2b5d958
fix: Removing const_test file and unit test changes
Victorblsilveira May 15, 2023
38449f0
fix: Misstypes and const places
Victorblsilveira May 15, 2023
21b679b
fix: Refactoring trust domain lookup functions
Victorblsilveira May 15, 2023
d9b7d39
Merge branch 'HewlettPackard:main' into victor/admin-handlers
Victorblsilveira May 15, 2023
7eafbe5
fix: Unit tests failing
Victorblsilveira May 15, 2023
f085b5d
Merge remote-tracking branch 'origin/main' into victor/admin-handlers
Victorblsilveira May 16, 2023
b60cec3
fix: Removing unused values
Victorblsilveira May 16, 2023
4da954f
fix: Unit test failing
Victorblsilveira May 16, 2023
b92ffcb
fix: Unit test checking
Victorblsilveira May 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pkg/common/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ import (
)

// WriteResponse parses a struct into a json and writes in the response
func WriteResponse(ctx echo.Context, body interface{}) error {
func WriteResponse(ctx echo.Context, code int, body interface{}) error {
if body == nil {
return errors.New("body is required")
}

if err := ctx.JSON(http.StatusOK, body); err != nil {
if err := ctx.JSON(code, body); err != nil {
return fmt.Errorf("failed to write response body: %v", err)
}

return nil
}

// 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

if err := ctx.NoContent(http.StatusOK); err != nil {
return fmt.Errorf("failed to respond without body: %v", err)
}
Expand Down
13 changes: 7 additions & 6 deletions pkg/common/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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?

assert.EqualError(t, err, "body is required")
assert.Empty(t, setup.Recorder.Body)
})

t.Run("No error when an empty body is passed", func(t *testing.T) {
setup := Setup()
err := WriteResponse(setup.EchoContext, TestBody{})
err := WriteResponse(setup.EchoContext, http.StatusOK, TestBody{})
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, setup.Recorder.Code)
})

t.Run("Ensuring that the body is being full filled with the entity", func(t *testing.T) {
expectedResponseBody := TestBody{Name: "teste"}
setup := Setup()
err := WriteResponse(setup.EchoContext, expectedResponseBody)
err := WriteResponse(setup.EchoContext, http.StatusOK, expectedResponseBody)
assert.NoError(t, err)

responseBody := TestBody{}
Expand All @@ -60,10 +61,10 @@ func TestWriteResponse(t *testing.T) {
})
}

func TestBodylessResponse(t *testing.T) {
func TestBodilessResponse(t *testing.T) {
t.Run("Ensuring that the body is empty", func(t *testing.T) {
setup := Setup()
err := BodylessResponse(setup.EchoContext)
err := BodilessResponse(setup.EchoContext, http.StatusOK)
assert.NoError(t, err)

assert.NoError(t, err)
Expand All @@ -75,7 +76,7 @@ func TestBodylessResponse(t *testing.T) {
func TestFromBody(t *testing.T) {
t.Run("Ensuring that the body is empty", func(t *testing.T) {
setup := Setup()
err := BodylessResponse(setup.EchoContext)
err := BodilessResponse(setup.EchoContext, http.StatusOK)
assert.NoError(t, err)

assert.NoError(t, err)
Expand Down
57 changes: 46 additions & 11 deletions pkg/server/datastore/fakedatabase.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@ func (db *FakeDatabase) CreateOrUpdateTrustDomain(ctx context.Context, req *enti
return nil, err
}

req.ID = uuid.NullUUID{
UUID: uuid.New(),
Valid: true,
if !req.ID.Valid {
req.ID = uuid.NullUUID{
UUID: uuid.New(),
Valid: true,
}
req.CreatedAt = time.Now()
}

req.CreatedAt = time.Now()
req.UpdatedAt = time.Now()

db.trustDomains[req.ID.UUID] = req

return req, nil
Expand Down Expand Up @@ -133,14 +134,15 @@ func (db *FakeDatabase) CreateOrUpdateBundle(ctx context.Context, req *entity.Bu
return nil, err
}

req.ID = uuid.NullUUID{
UUID: uuid.New(),
Valid: true,
if !req.ID.Valid {
req.ID = uuid.NullUUID{
UUID: uuid.New(),
Valid: true,
}
req.CreatedAt = time.Now()
}

req.CreatedAt = time.Now()
req.UpdatedAt = time.Now()

db.bundles[req.ID.UUID] = req

return req, nil
Expand Down Expand Up @@ -354,7 +356,18 @@ func (db *FakeDatabase) CreateOrUpdateRelationship(ctx context.Context, req *ent
return nil, err
}

return nil, nil
if !req.ID.Valid {
req.ID = uuid.NullUUID{
Valid: true,
UUID: uuid.New(),
}
req.CreatedAt = time.Now()
}

req.UpdatedAt = time.Now()
db.relationships[req.ID.UUID] = req

return req, nil
}

func (db *FakeDatabase) FindRelationshipByID(ctx context.Context, relationshipID uuid.UUID) (*entity.Relationship, error) {
Expand Down Expand Up @@ -431,6 +444,28 @@ func (db *FakeDatabase) DeleteRelationship(ctx context.Context, relationshipID u
return nil
}

// WithRelationships overrides all relationships
func (db *FakeDatabase) WithRelationships(relationships ...*entity.Relationship) {
db.mutex.Lock()
defer db.mutex.Unlock()

db.relationships = make(map[uuid.UUID]*entity.Relationship)
for _, r := range relationships {
db.relationships[r.ID.UUID] = r
}
}

// WithRelationships overrides all trust domains
func (db *FakeDatabase) WithTrustDomains(trustDomains ...*entity.TrustDomain) {
db.mutex.Lock()
defer db.mutex.Unlock()

db.trustDomains = make(map[uuid.UUID]*entity.TrustDomain)
for _, td := range trustDomains {
db.trustDomains[td.ID.UUID] = td
}
}

func (db *FakeDatabase) SetNextError(err error) {
db.mutex.Lock()
defer db.mutex.Unlock()
Expand Down
6 changes: 3 additions & 3 deletions pkg/server/endpoints/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.

td, err := spiffeid.TrustDomainFromString(tdName)
assert.NoError(t, err)

jt := &entity.JoinToken{
Expand All @@ -67,7 +67,7 @@ func TestAuthenticate(t *testing.T) {
t.Run("Authorized tokens must be able to pass authn verification", func(t *testing.T) {
authnSetup := SetupMiddleware()
token := GenerateSecureToken(10)
SetupToken(t, authnSetup.FakeDatabase, token, uuid.New())
SetupToken(t, authnSetup.FakeDatabase, uuid.New(), token, td1)

authorized, err := authnSetup.Middleware.Authenticate(token, authnSetup.EchoCtx)
assert.NoError(t, err)
Expand Down
4 changes: 3 additions & 1 deletion pkg/server/endpoints/const_test.go
Original file line number Diff line number Diff line change
@@ -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.

td1 = "test1.com"
td2 = "test2.com"
td3 = "test3.com"
)
2 changes: 1 addition & 1 deletion pkg/server/endpoints/harvester.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (h HarvesterAPIHandlers) BundlePut(ctx echo.Context, trustDomainName api.Tr
return h.handleErrorAndLog(err, http.StatusInternalServerError)
}

if err = chttp.BodylessResponse(ctx); err != nil {
if err = chttp.BodilessResponse(ctx, http.StatusOK); err != nil {
return h.handleErrorAndLog(err, http.StatusInternalServerError)
}

Expand Down
27 changes: 16 additions & 11 deletions pkg/server/endpoints/harvester_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package endpoints
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
Expand All @@ -23,9 +24,16 @@ type HarvesterTestSetup struct {
Recorder *httptest.ResponseRecorder
}

func NewHarvesterTestSetup(method, url, body string) *HarvesterTestSetup {
func NewHarvesterTestSetup(t *testing.T, method, url string, body interface{}) *HarvesterTestSetup {
var bodyReader io.Reader
if body != nil {
bodyStr, err := json.Marshal(body)
assert.NoError(t, err)
bodyReader = strings.NewReader(string(bodyStr))
}

e := echo.New()
req := httptest.NewRequest(method, url, strings.NewReader(body))
req := httptest.NewRequest(method, url, bodyReader)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
fakeDB := datastore.NewFakeDB()
Expand All @@ -39,7 +47,7 @@ func NewHarvesterTestSetup(method, url, body string) *HarvesterTestSetup {
}

func SetupTrustDomain(t *testing.T, ds datastore.Datastore) (*entity.TrustDomain, error) {
td, err := spiffeid.TrustDomainFromString(testTrustDomain)
td, err := spiffeid.TrustDomainFromString(td1)
assert.NoError(t, err)

tdEntity := &entity.TrustDomain{
Expand Down Expand Up @@ -67,18 +75,15 @@ func TestTCPBundleSync(t *testing.T) {
}

func TestTCPBundlePut(t *testing.T) {
t.Run("Succesfully register bundles for a trust domain", func(t *testing.T) {
t.Run("Successfully register bundles for a trust domain", func(t *testing.T) {
bundlePut := harvester.BundlePut{
Signature: "",
SigningCertificate: "",
TrustBundle: "a new bundle",
TrustDomain: testTrustDomain,
TrustDomain: td1,
}

body, err := json.Marshal(bundlePut)
assert.NoError(t, err)

harvesterTestSetup := NewHarvesterTestSetup(http.MethodPut, "/trust-domain/:trustDomainName/bundles", string(body))
harvesterTestSetup := NewHarvesterTestSetup(t, http.MethodPut, "/trust-domain/:trustDomainName/bundles", bundlePut)
echoCtx := harvesterTestSetup.EchoCtx

// Creating Trust Domain
Expand All @@ -87,12 +92,12 @@ func TestTCPBundlePut(t *testing.T) {

// Creating Auth token to bypass AuthN layer
token := GenerateSecureToken(10)
jt := SetupToken(t, harvesterTestSetup.Handler.Datastore, token, td.ID.UUID)
jt := SetupToken(t, harvesterTestSetup.Handler.Datastore, td.ID.UUID, token, td.Name.String())
assert.NoError(t, err)
echoCtx.Set(tokenKey, jt)

// Test Main Objective
err = harvesterTestSetup.Handler.BundlePut(echoCtx, testTrustDomain)
err = harvesterTestSetup.Handler.BundlePut(echoCtx, td1)
assert.NoError(t, err)

recorder := harvesterTestSetup.Recorder
Expand Down
Loading