diff --git a/pkg/common/http/http.go b/pkg/common/http/http.go index 7c4e35f0..50169d3a 100644 --- a/pkg/common/http/http.go +++ b/pkg/common/http/http.go @@ -3,27 +3,26 @@ package http import ( "errors" "fmt" - "net/http" "github.com/labstack/echo/v4" ) // 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 { - if err := ctx.NoContent(http.StatusOK); err != nil { +// BodilessResponse wraps error echo body-less responses. +func BodilessResponse(ctx echo.Context, code int) error { + if err := ctx.NoContent(code); err != nil { return fmt.Errorf("failed to respond without body: %v", err) } diff --git a/pkg/common/http/http_test.go b/pkg/common/http/http_test.go index 75fc59d7..cd7ef396 100644 --- a/pkg/common/http/http_test.go +++ b/pkg/common/http/http_test.go @@ -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, http.StatusOK, nil) 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{} @@ -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) @@ -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) diff --git a/pkg/server/api/admin/admin.gen.go b/pkg/server/api/admin/admin.gen.go index c748c6d5..36e960ea 100644 --- a/pkg/server/api/admin/admin.gen.go +++ b/pkg/server/api/admin/admin.gen.go @@ -22,13 +22,6 @@ import ( "github.com/labstack/echo/v4" ) -// Defines values for GetRelationshipsParamsStatus. -const ( - Approved GetRelationshipsParamsStatus = "approved" - Denied GetRelationshipsParamsStatus = "denied" - Pending GetRelationshipsParamsStatus = "pending" -) - // JoinTokenResult defines model for JoinTokenResult. type JoinTokenResult struct { Token externalRef0.JoinToken `json:"token"` @@ -52,23 +45,20 @@ type Default = externalRef0.ApiError // GetRelationshipsParams defines parameters for GetRelationships. type GetRelationshipsParams struct { // Status relationship status from a Trust Domain perspective, - Status *GetRelationshipsParamsStatus `form:"status,omitempty" json:"status,omitempty"` + Status *externalRef0.ConsentStatus `form:"status,omitempty" json:"status,omitempty"` // TrustDomainName TrustDomain TrustDomainName *externalRef0.TrustDomainName `form:"trustDomainName,omitempty" json:"trustDomainName,omitempty"` } -// GetRelationshipsParamsStatus defines parameters for GetRelationships. -type GetRelationshipsParamsStatus string - -// PutRelationshipsJSONRequestBody defines body for PutRelationships for application/json ContentType. -type PutRelationshipsJSONRequestBody = RelationshipRequest +// PutRelationshipJSONRequestBody defines body for PutRelationship for application/json ContentType. +type PutRelationshipJSONRequestBody = RelationshipRequest // PutTrustDomainJSONRequestBody defines body for PutTrustDomain for application/json ContentType. type PutTrustDomainJSONRequestBody = TrustDomainPut -// PutTrustDomainTrustDomainNameJSONRequestBody defines body for PutTrustDomainTrustDomainName for application/json ContentType. -type PutTrustDomainTrustDomainNameJSONRequestBody = externalRef0.TrustDomain +// PutTrustDomainByNameJSONRequestBody defines body for PutTrustDomainByName for application/json ContentType. +type PutTrustDomainByNameJSONRequestBody = externalRef0.TrustDomain // RequestEditorFn is the function signature for the RequestEditor callback function type RequestEditorFn func(ctx context.Context, req *http.Request) error @@ -146,26 +136,26 @@ type ClientInterface interface { // GetRelationships request GetRelationships(ctx context.Context, params *GetRelationshipsParams, reqEditors ...RequestEditorFn) (*http.Response, error) - // PutRelationships request with any body - PutRelationshipsWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) + // PutRelationship request with any body + PutRelationshipWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) - PutRelationships(ctx context.Context, body PutRelationshipsJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) + PutRelationship(ctx context.Context, body PutRelationshipJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) - // GetRelationshipsRelationshipID request - GetRelationshipsRelationshipID(ctx context.Context, relationshipID externalRef0.UUID, reqEditors ...RequestEditorFn) (*http.Response, error) + // GetRelationshipByID request + GetRelationshipByID(ctx context.Context, relationshipID externalRef0.UUID, reqEditors ...RequestEditorFn) (*http.Response, error) // PutTrustDomain request with any body PutTrustDomainWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) PutTrustDomain(ctx context.Context, body PutTrustDomainJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) - // GetTrustDomainTrustDomainName request - GetTrustDomainTrustDomainName(ctx context.Context, trustDomainName externalRef0.TrustDomainName, reqEditors ...RequestEditorFn) (*http.Response, error) + // GetTrustDomainByName request + GetTrustDomainByName(ctx context.Context, trustDomainName externalRef0.TrustDomainName, reqEditors ...RequestEditorFn) (*http.Response, error) - // PutTrustDomainTrustDomainName request with any body - PutTrustDomainTrustDomainNameWithBody(ctx context.Context, trustDomainName externalRef0.UUID, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) + // PutTrustDomainByName request with any body + PutTrustDomainByNameWithBody(ctx context.Context, trustDomainName externalRef0.UUID, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) - PutTrustDomainTrustDomainName(ctx context.Context, trustDomainName externalRef0.UUID, body PutTrustDomainTrustDomainNameJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) + PutTrustDomainByName(ctx context.Context, trustDomainName externalRef0.UUID, body PutTrustDomainByNameJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) // GetJoinToken request GetJoinToken(ctx context.Context, trustDomainName externalRef0.TrustDomainName, reqEditors ...RequestEditorFn) (*http.Response, error) @@ -183,8 +173,8 @@ func (c *Client) GetRelationships(ctx context.Context, params *GetRelationshipsP return c.Client.Do(req) } -func (c *Client) PutRelationshipsWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) { - req, err := NewPutRelationshipsRequestWithBody(c.Server, contentType, body) +func (c *Client) PutRelationshipWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) { + req, err := NewPutRelationshipRequestWithBody(c.Server, contentType, body) if err != nil { return nil, err } @@ -195,8 +185,8 @@ func (c *Client) PutRelationshipsWithBody(ctx context.Context, contentType strin return c.Client.Do(req) } -func (c *Client) PutRelationships(ctx context.Context, body PutRelationshipsJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) { - req, err := NewPutRelationshipsRequest(c.Server, body) +func (c *Client) PutRelationship(ctx context.Context, body PutRelationshipJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) { + req, err := NewPutRelationshipRequest(c.Server, body) if err != nil { return nil, err } @@ -207,8 +197,8 @@ func (c *Client) PutRelationships(ctx context.Context, body PutRelationshipsJSON return c.Client.Do(req) } -func (c *Client) GetRelationshipsRelationshipID(ctx context.Context, relationshipID externalRef0.UUID, reqEditors ...RequestEditorFn) (*http.Response, error) { - req, err := NewGetRelationshipsRelationshipIDRequest(c.Server, relationshipID) +func (c *Client) GetRelationshipByID(ctx context.Context, relationshipID externalRef0.UUID, reqEditors ...RequestEditorFn) (*http.Response, error) { + req, err := NewGetRelationshipByIDRequest(c.Server, relationshipID) if err != nil { return nil, err } @@ -243,8 +233,8 @@ func (c *Client) PutTrustDomain(ctx context.Context, body PutTrustDomainJSONRequ return c.Client.Do(req) } -func (c *Client) GetTrustDomainTrustDomainName(ctx context.Context, trustDomainName externalRef0.TrustDomainName, reqEditors ...RequestEditorFn) (*http.Response, error) { - req, err := NewGetTrustDomainTrustDomainNameRequest(c.Server, trustDomainName) +func (c *Client) GetTrustDomainByName(ctx context.Context, trustDomainName externalRef0.TrustDomainName, reqEditors ...RequestEditorFn) (*http.Response, error) { + req, err := NewGetTrustDomainByNameRequest(c.Server, trustDomainName) if err != nil { return nil, err } @@ -255,8 +245,8 @@ func (c *Client) GetTrustDomainTrustDomainName(ctx context.Context, trustDomainN return c.Client.Do(req) } -func (c *Client) PutTrustDomainTrustDomainNameWithBody(ctx context.Context, trustDomainName externalRef0.UUID, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) { - req, err := NewPutTrustDomainTrustDomainNameRequestWithBody(c.Server, trustDomainName, contentType, body) +func (c *Client) PutTrustDomainByNameWithBody(ctx context.Context, trustDomainName externalRef0.UUID, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) { + req, err := NewPutTrustDomainByNameRequestWithBody(c.Server, trustDomainName, contentType, body) if err != nil { return nil, err } @@ -267,8 +257,8 @@ func (c *Client) PutTrustDomainTrustDomainNameWithBody(ctx context.Context, trus return c.Client.Do(req) } -func (c *Client) PutTrustDomainTrustDomainName(ctx context.Context, trustDomainName externalRef0.UUID, body PutTrustDomainTrustDomainNameJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) { - req, err := NewPutTrustDomainTrustDomainNameRequest(c.Server, trustDomainName, body) +func (c *Client) PutTrustDomainByName(ctx context.Context, trustDomainName externalRef0.UUID, body PutTrustDomainByNameJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) { + req, err := NewPutTrustDomainByNameRequest(c.Server, trustDomainName, body) if err != nil { return nil, err } @@ -354,19 +344,19 @@ func NewGetRelationshipsRequest(server string, params *GetRelationshipsParams) ( return req, nil } -// NewPutRelationshipsRequest calls the generic PutRelationships builder with application/json body -func NewPutRelationshipsRequest(server string, body PutRelationshipsJSONRequestBody) (*http.Request, error) { +// NewPutRelationshipRequest calls the generic PutRelationship builder with application/json body +func NewPutRelationshipRequest(server string, body PutRelationshipJSONRequestBody) (*http.Request, error) { var bodyReader io.Reader buf, err := json.Marshal(body) if err != nil { return nil, err } bodyReader = bytes.NewReader(buf) - return NewPutRelationshipsRequestWithBody(server, "application/json", bodyReader) + return NewPutRelationshipRequestWithBody(server, "application/json", bodyReader) } -// NewPutRelationshipsRequestWithBody generates requests for PutRelationships with any type of body -func NewPutRelationshipsRequestWithBody(server string, contentType string, body io.Reader) (*http.Request, error) { +// NewPutRelationshipRequestWithBody generates requests for PutRelationship with any type of body +func NewPutRelationshipRequestWithBody(server string, contentType string, body io.Reader) (*http.Request, error) { var err error serverURL, err := url.Parse(server) @@ -394,8 +384,8 @@ func NewPutRelationshipsRequestWithBody(server string, contentType string, body return req, nil } -// NewGetRelationshipsRelationshipIDRequest generates requests for GetRelationshipsRelationshipID -func NewGetRelationshipsRelationshipIDRequest(server string, relationshipID externalRef0.UUID) (*http.Request, error) { +// NewGetRelationshipByIDRequest generates requests for GetRelationshipByID +func NewGetRelationshipByIDRequest(server string, relationshipID externalRef0.UUID) (*http.Request, error) { var err error var pathParam0 string @@ -468,8 +458,8 @@ func NewPutTrustDomainRequestWithBody(server string, contentType string, body io return req, nil } -// NewGetTrustDomainTrustDomainNameRequest generates requests for GetTrustDomainTrustDomainName -func NewGetTrustDomainTrustDomainNameRequest(server string, trustDomainName externalRef0.TrustDomainName) (*http.Request, error) { +// NewGetTrustDomainByNameRequest generates requests for GetTrustDomainByName +func NewGetTrustDomainByNameRequest(server string, trustDomainName externalRef0.TrustDomainName) (*http.Request, error) { var err error var pathParam0 string @@ -502,19 +492,19 @@ func NewGetTrustDomainTrustDomainNameRequest(server string, trustDomainName exte return req, nil } -// NewPutTrustDomainTrustDomainNameRequest calls the generic PutTrustDomainTrustDomainName builder with application/json body -func NewPutTrustDomainTrustDomainNameRequest(server string, trustDomainName externalRef0.UUID, body PutTrustDomainTrustDomainNameJSONRequestBody) (*http.Request, error) { +// NewPutTrustDomainByNameRequest calls the generic PutTrustDomainByName builder with application/json body +func NewPutTrustDomainByNameRequest(server string, trustDomainName externalRef0.UUID, body PutTrustDomainByNameJSONRequestBody) (*http.Request, error) { var bodyReader io.Reader buf, err := json.Marshal(body) if err != nil { return nil, err } bodyReader = bytes.NewReader(buf) - return NewPutTrustDomainTrustDomainNameRequestWithBody(server, trustDomainName, "application/json", bodyReader) + return NewPutTrustDomainByNameRequestWithBody(server, trustDomainName, "application/json", bodyReader) } -// NewPutTrustDomainTrustDomainNameRequestWithBody generates requests for PutTrustDomainTrustDomainName with any type of body -func NewPutTrustDomainTrustDomainNameRequestWithBody(server string, trustDomainName externalRef0.UUID, contentType string, body io.Reader) (*http.Request, error) { +// NewPutTrustDomainByNameRequestWithBody generates requests for PutTrustDomainByName with any type of body +func NewPutTrustDomainByNameRequestWithBody(server string, trustDomainName externalRef0.UUID, contentType string, body io.Reader) (*http.Request, error) { var err error var pathParam0 string @@ -629,26 +619,26 @@ type ClientWithResponsesInterface interface { // GetRelationships request GetRelationshipsWithResponse(ctx context.Context, params *GetRelationshipsParams, reqEditors ...RequestEditorFn) (*GetRelationshipsResponse, error) - // PutRelationships request with any body - PutRelationshipsWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*PutRelationshipsResponse, error) + // PutRelationship request with any body + PutRelationshipWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*PutRelationshipResponse, error) - PutRelationshipsWithResponse(ctx context.Context, body PutRelationshipsJSONRequestBody, reqEditors ...RequestEditorFn) (*PutRelationshipsResponse, error) + PutRelationshipWithResponse(ctx context.Context, body PutRelationshipJSONRequestBody, reqEditors ...RequestEditorFn) (*PutRelationshipResponse, error) - // GetRelationshipsRelationshipID request - GetRelationshipsRelationshipIDWithResponse(ctx context.Context, relationshipID externalRef0.UUID, reqEditors ...RequestEditorFn) (*GetRelationshipsRelationshipIDResponse, error) + // GetRelationshipByID request + GetRelationshipByIDWithResponse(ctx context.Context, relationshipID externalRef0.UUID, reqEditors ...RequestEditorFn) (*GetRelationshipByIDResponse, error) // PutTrustDomain request with any body PutTrustDomainWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*PutTrustDomainResponse, error) PutTrustDomainWithResponse(ctx context.Context, body PutTrustDomainJSONRequestBody, reqEditors ...RequestEditorFn) (*PutTrustDomainResponse, error) - // GetTrustDomainTrustDomainName request - GetTrustDomainTrustDomainNameWithResponse(ctx context.Context, trustDomainName externalRef0.TrustDomainName, reqEditors ...RequestEditorFn) (*GetTrustDomainTrustDomainNameResponse, error) + // GetTrustDomainByName request + GetTrustDomainByNameWithResponse(ctx context.Context, trustDomainName externalRef0.TrustDomainName, reqEditors ...RequestEditorFn) (*GetTrustDomainByNameResponse, error) - // PutTrustDomainTrustDomainName request with any body - PutTrustDomainTrustDomainNameWithBodyWithResponse(ctx context.Context, trustDomainName externalRef0.UUID, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*PutTrustDomainTrustDomainNameResponse, error) + // PutTrustDomainByName request with any body + PutTrustDomainByNameWithBodyWithResponse(ctx context.Context, trustDomainName externalRef0.UUID, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*PutTrustDomainByNameResponse, error) - PutTrustDomainTrustDomainNameWithResponse(ctx context.Context, trustDomainName externalRef0.UUID, body PutTrustDomainTrustDomainNameJSONRequestBody, reqEditors ...RequestEditorFn) (*PutTrustDomainTrustDomainNameResponse, error) + PutTrustDomainByNameWithResponse(ctx context.Context, trustDomainName externalRef0.UUID, body PutTrustDomainByNameJSONRequestBody, reqEditors ...RequestEditorFn) (*PutTrustDomainByNameResponse, error) // GetJoinToken request GetJoinTokenWithResponse(ctx context.Context, trustDomainName externalRef0.TrustDomainName, reqEditors ...RequestEditorFn) (*GetJoinTokenResponse, error) @@ -677,7 +667,7 @@ func (r GetRelationshipsResponse) StatusCode() int { return 0 } -type PutRelationshipsResponse struct { +type PutRelationshipResponse struct { Body []byte HTTPResponse *http.Response JSON200 *externalRef0.Relationship @@ -685,7 +675,7 @@ type PutRelationshipsResponse struct { } // Status returns HTTPResponse.Status -func (r PutRelationshipsResponse) Status() string { +func (r PutRelationshipResponse) Status() string { if r.HTTPResponse != nil { return r.HTTPResponse.Status } @@ -693,14 +683,14 @@ func (r PutRelationshipsResponse) Status() string { } // StatusCode returns HTTPResponse.StatusCode -func (r PutRelationshipsResponse) StatusCode() int { +func (r PutRelationshipResponse) StatusCode() int { if r.HTTPResponse != nil { return r.HTTPResponse.StatusCode } return 0 } -type GetRelationshipsRelationshipIDResponse struct { +type GetRelationshipByIDResponse struct { Body []byte HTTPResponse *http.Response JSON200 *externalRef0.Relationship @@ -708,7 +698,7 @@ type GetRelationshipsRelationshipIDResponse struct { } // Status returns HTTPResponse.Status -func (r GetRelationshipsRelationshipIDResponse) Status() string { +func (r GetRelationshipByIDResponse) Status() string { if r.HTTPResponse != nil { return r.HTTPResponse.Status } @@ -716,7 +706,7 @@ func (r GetRelationshipsRelationshipIDResponse) Status() string { } // StatusCode returns HTTPResponse.StatusCode -func (r GetRelationshipsRelationshipIDResponse) StatusCode() int { +func (r GetRelationshipByIDResponse) StatusCode() int { if r.HTTPResponse != nil { return r.HTTPResponse.StatusCode } @@ -746,7 +736,7 @@ func (r PutTrustDomainResponse) StatusCode() int { return 0 } -type GetTrustDomainTrustDomainNameResponse struct { +type GetTrustDomainByNameResponse struct { Body []byte HTTPResponse *http.Response JSON200 *externalRef0.TrustDomain @@ -754,7 +744,7 @@ type GetTrustDomainTrustDomainNameResponse struct { } // Status returns HTTPResponse.Status -func (r GetTrustDomainTrustDomainNameResponse) Status() string { +func (r GetTrustDomainByNameResponse) Status() string { if r.HTTPResponse != nil { return r.HTTPResponse.Status } @@ -762,14 +752,14 @@ func (r GetTrustDomainTrustDomainNameResponse) Status() string { } // StatusCode returns HTTPResponse.StatusCode -func (r GetTrustDomainTrustDomainNameResponse) StatusCode() int { +func (r GetTrustDomainByNameResponse) StatusCode() int { if r.HTTPResponse != nil { return r.HTTPResponse.StatusCode } return 0 } -type PutTrustDomainTrustDomainNameResponse struct { +type PutTrustDomainByNameResponse struct { Body []byte HTTPResponse *http.Response JSON200 *externalRef0.TrustDomain @@ -777,7 +767,7 @@ type PutTrustDomainTrustDomainNameResponse struct { } // Status returns HTTPResponse.Status -func (r PutTrustDomainTrustDomainNameResponse) Status() string { +func (r PutTrustDomainByNameResponse) Status() string { if r.HTTPResponse != nil { return r.HTTPResponse.Status } @@ -785,7 +775,7 @@ func (r PutTrustDomainTrustDomainNameResponse) Status() string { } // StatusCode returns HTTPResponse.StatusCode -func (r PutTrustDomainTrustDomainNameResponse) StatusCode() int { +func (r PutTrustDomainByNameResponse) StatusCode() int { if r.HTTPResponse != nil { return r.HTTPResponse.StatusCode } @@ -824,30 +814,30 @@ func (c *ClientWithResponses) GetRelationshipsWithResponse(ctx context.Context, return ParseGetRelationshipsResponse(rsp) } -// PutRelationshipsWithBodyWithResponse request with arbitrary body returning *PutRelationshipsResponse -func (c *ClientWithResponses) PutRelationshipsWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*PutRelationshipsResponse, error) { - rsp, err := c.PutRelationshipsWithBody(ctx, contentType, body, reqEditors...) +// PutRelationshipWithBodyWithResponse request with arbitrary body returning *PutRelationshipResponse +func (c *ClientWithResponses) PutRelationshipWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*PutRelationshipResponse, error) { + rsp, err := c.PutRelationshipWithBody(ctx, contentType, body, reqEditors...) if err != nil { return nil, err } - return ParsePutRelationshipsResponse(rsp) + return ParsePutRelationshipResponse(rsp) } -func (c *ClientWithResponses) PutRelationshipsWithResponse(ctx context.Context, body PutRelationshipsJSONRequestBody, reqEditors ...RequestEditorFn) (*PutRelationshipsResponse, error) { - rsp, err := c.PutRelationships(ctx, body, reqEditors...) +func (c *ClientWithResponses) PutRelationshipWithResponse(ctx context.Context, body PutRelationshipJSONRequestBody, reqEditors ...RequestEditorFn) (*PutRelationshipResponse, error) { + rsp, err := c.PutRelationship(ctx, body, reqEditors...) if err != nil { return nil, err } - return ParsePutRelationshipsResponse(rsp) + return ParsePutRelationshipResponse(rsp) } -// GetRelationshipsRelationshipIDWithResponse request returning *GetRelationshipsRelationshipIDResponse -func (c *ClientWithResponses) GetRelationshipsRelationshipIDWithResponse(ctx context.Context, relationshipID externalRef0.UUID, reqEditors ...RequestEditorFn) (*GetRelationshipsRelationshipIDResponse, error) { - rsp, err := c.GetRelationshipsRelationshipID(ctx, relationshipID, reqEditors...) +// GetRelationshipByIDWithResponse request returning *GetRelationshipByIDResponse +func (c *ClientWithResponses) GetRelationshipByIDWithResponse(ctx context.Context, relationshipID externalRef0.UUID, reqEditors ...RequestEditorFn) (*GetRelationshipByIDResponse, error) { + rsp, err := c.GetRelationshipByID(ctx, relationshipID, reqEditors...) if err != nil { return nil, err } - return ParseGetRelationshipsRelationshipIDResponse(rsp) + return ParseGetRelationshipByIDResponse(rsp) } // PutTrustDomainWithBodyWithResponse request with arbitrary body returning *PutTrustDomainResponse @@ -867,30 +857,30 @@ func (c *ClientWithResponses) PutTrustDomainWithResponse(ctx context.Context, bo return ParsePutTrustDomainResponse(rsp) } -// GetTrustDomainTrustDomainNameWithResponse request returning *GetTrustDomainTrustDomainNameResponse -func (c *ClientWithResponses) GetTrustDomainTrustDomainNameWithResponse(ctx context.Context, trustDomainName externalRef0.TrustDomainName, reqEditors ...RequestEditorFn) (*GetTrustDomainTrustDomainNameResponse, error) { - rsp, err := c.GetTrustDomainTrustDomainName(ctx, trustDomainName, reqEditors...) +// GetTrustDomainByNameWithResponse request returning *GetTrustDomainByNameResponse +func (c *ClientWithResponses) GetTrustDomainByNameWithResponse(ctx context.Context, trustDomainName externalRef0.TrustDomainName, reqEditors ...RequestEditorFn) (*GetTrustDomainByNameResponse, error) { + rsp, err := c.GetTrustDomainByName(ctx, trustDomainName, reqEditors...) if err != nil { return nil, err } - return ParseGetTrustDomainTrustDomainNameResponse(rsp) + return ParseGetTrustDomainByNameResponse(rsp) } -// PutTrustDomainTrustDomainNameWithBodyWithResponse request with arbitrary body returning *PutTrustDomainTrustDomainNameResponse -func (c *ClientWithResponses) PutTrustDomainTrustDomainNameWithBodyWithResponse(ctx context.Context, trustDomainName externalRef0.UUID, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*PutTrustDomainTrustDomainNameResponse, error) { - rsp, err := c.PutTrustDomainTrustDomainNameWithBody(ctx, trustDomainName, contentType, body, reqEditors...) +// PutTrustDomainByNameWithBodyWithResponse request with arbitrary body returning *PutTrustDomainByNameResponse +func (c *ClientWithResponses) PutTrustDomainByNameWithBodyWithResponse(ctx context.Context, trustDomainName externalRef0.UUID, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*PutTrustDomainByNameResponse, error) { + rsp, err := c.PutTrustDomainByNameWithBody(ctx, trustDomainName, contentType, body, reqEditors...) if err != nil { return nil, err } - return ParsePutTrustDomainTrustDomainNameResponse(rsp) + return ParsePutTrustDomainByNameResponse(rsp) } -func (c *ClientWithResponses) PutTrustDomainTrustDomainNameWithResponse(ctx context.Context, trustDomainName externalRef0.UUID, body PutTrustDomainTrustDomainNameJSONRequestBody, reqEditors ...RequestEditorFn) (*PutTrustDomainTrustDomainNameResponse, error) { - rsp, err := c.PutTrustDomainTrustDomainName(ctx, trustDomainName, body, reqEditors...) +func (c *ClientWithResponses) PutTrustDomainByNameWithResponse(ctx context.Context, trustDomainName externalRef0.UUID, body PutTrustDomainByNameJSONRequestBody, reqEditors ...RequestEditorFn) (*PutTrustDomainByNameResponse, error) { + rsp, err := c.PutTrustDomainByName(ctx, trustDomainName, body, reqEditors...) if err != nil { return nil, err } - return ParsePutTrustDomainTrustDomainNameResponse(rsp) + return ParsePutTrustDomainByNameResponse(rsp) } // GetJoinTokenWithResponse request returning *GetJoinTokenResponse @@ -935,15 +925,15 @@ func ParseGetRelationshipsResponse(rsp *http.Response) (*GetRelationshipsRespons return response, nil } -// ParsePutRelationshipsResponse parses an HTTP response from a PutRelationshipsWithResponse call -func ParsePutRelationshipsResponse(rsp *http.Response) (*PutRelationshipsResponse, error) { +// ParsePutRelationshipResponse parses an HTTP response from a PutRelationshipWithResponse call +func ParsePutRelationshipResponse(rsp *http.Response) (*PutRelationshipResponse, error) { bodyBytes, err := io.ReadAll(rsp.Body) defer func() { _ = rsp.Body.Close() }() if err != nil { return nil, err } - response := &PutRelationshipsResponse{ + response := &PutRelationshipResponse{ Body: bodyBytes, HTTPResponse: rsp, } @@ -968,15 +958,15 @@ func ParsePutRelationshipsResponse(rsp *http.Response) (*PutRelationshipsRespons return response, nil } -// ParseGetRelationshipsRelationshipIDResponse parses an HTTP response from a GetRelationshipsRelationshipIDWithResponse call -func ParseGetRelationshipsRelationshipIDResponse(rsp *http.Response) (*GetRelationshipsRelationshipIDResponse, error) { +// ParseGetRelationshipByIDResponse parses an HTTP response from a GetRelationshipByIDWithResponse call +func ParseGetRelationshipByIDResponse(rsp *http.Response) (*GetRelationshipByIDResponse, error) { bodyBytes, err := io.ReadAll(rsp.Body) defer func() { _ = rsp.Body.Close() }() if err != nil { return nil, err } - response := &GetRelationshipsRelationshipIDResponse{ + response := &GetRelationshipByIDResponse{ Body: bodyBytes, HTTPResponse: rsp, } @@ -1034,15 +1024,15 @@ func ParsePutTrustDomainResponse(rsp *http.Response) (*PutTrustDomainResponse, e return response, nil } -// ParseGetTrustDomainTrustDomainNameResponse parses an HTTP response from a GetTrustDomainTrustDomainNameWithResponse call -func ParseGetTrustDomainTrustDomainNameResponse(rsp *http.Response) (*GetTrustDomainTrustDomainNameResponse, error) { +// ParseGetTrustDomainByNameResponse parses an HTTP response from a GetTrustDomainByNameWithResponse call +func ParseGetTrustDomainByNameResponse(rsp *http.Response) (*GetTrustDomainByNameResponse, error) { bodyBytes, err := io.ReadAll(rsp.Body) defer func() { _ = rsp.Body.Close() }() if err != nil { return nil, err } - response := &GetTrustDomainTrustDomainNameResponse{ + response := &GetTrustDomainByNameResponse{ Body: bodyBytes, HTTPResponse: rsp, } @@ -1067,15 +1057,15 @@ func ParseGetTrustDomainTrustDomainNameResponse(rsp *http.Response) (*GetTrustDo return response, nil } -// ParsePutTrustDomainTrustDomainNameResponse parses an HTTP response from a PutTrustDomainTrustDomainNameWithResponse call -func ParsePutTrustDomainTrustDomainNameResponse(rsp *http.Response) (*PutTrustDomainTrustDomainNameResponse, error) { +// ParsePutTrustDomainByNameResponse parses an HTTP response from a PutTrustDomainByNameWithResponse call +func ParsePutTrustDomainByNameResponse(rsp *http.Response) (*PutTrustDomainByNameResponse, error) { bodyBytes, err := io.ReadAll(rsp.Body) defer func() { _ = rsp.Body.Close() }() if err != nil { return nil, err } - response := &PutTrustDomainTrustDomainNameResponse{ + response := &PutTrustDomainByNameResponse{ Body: bodyBytes, HTTPResponse: rsp, } @@ -1140,19 +1130,19 @@ type ServerInterface interface { GetRelationships(ctx echo.Context, params GetRelationshipsParams) error // Create a relationship request between two Trust Domains // (PUT /relationships) - PutRelationships(ctx echo.Context) error + PutRelationship(ctx echo.Context) error // Get a specific relationship // (GET /relationships/{relationshipID}) - GetRelationshipsRelationshipID(ctx echo.Context, relationshipID externalRef0.UUID) error + GetRelationshipByID(ctx echo.Context, relationshipID externalRef0.UUID) error // Add a specific trust domain // (PUT /trust-domain) PutTrustDomain(ctx echo.Context) error // Get a specific trust domain // (GET /trust-domain/{trustDomainName}) - GetTrustDomainTrustDomainName(ctx echo.Context, trustDomainName externalRef0.TrustDomainName) error + GetTrustDomainByName(ctx echo.Context, trustDomainName externalRef0.TrustDomainName) error // Update a specific trust domain // (PUT /trust-domain/{trustDomainName}) - PutTrustDomainTrustDomainName(ctx echo.Context, trustDomainName externalRef0.UUID) error + PutTrustDomainByName(ctx echo.Context, trustDomainName externalRef0.UUID) error // Get a join token for a specific Trust Domain // (GET /trust-domain/{trustDomainName}/join-token) GetJoinToken(ctx echo.Context, trustDomainName externalRef0.TrustDomainName) error @@ -1188,17 +1178,17 @@ func (w *ServerInterfaceWrapper) GetRelationships(ctx echo.Context) error { return err } -// PutRelationships converts echo context to params. -func (w *ServerInterfaceWrapper) PutRelationships(ctx echo.Context) error { +// PutRelationship converts echo context to params. +func (w *ServerInterfaceWrapper) PutRelationship(ctx echo.Context) error { var err error // Invoke the callback with all the unmarshalled arguments - err = w.Handler.PutRelationships(ctx) + err = w.Handler.PutRelationship(ctx) return err } -// GetRelationshipsRelationshipID converts echo context to params. -func (w *ServerInterfaceWrapper) GetRelationshipsRelationshipID(ctx echo.Context) error { +// GetRelationshipByID converts echo context to params. +func (w *ServerInterfaceWrapper) GetRelationshipByID(ctx echo.Context) error { var err error // ------------- Path parameter "relationshipID" ------------- var relationshipID externalRef0.UUID @@ -1209,7 +1199,7 @@ func (w *ServerInterfaceWrapper) GetRelationshipsRelationshipID(ctx echo.Context } // Invoke the callback with all the unmarshalled arguments - err = w.Handler.GetRelationshipsRelationshipID(ctx, relationshipID) + err = w.Handler.GetRelationshipByID(ctx, relationshipID) return err } @@ -1222,8 +1212,8 @@ func (w *ServerInterfaceWrapper) PutTrustDomain(ctx echo.Context) error { return err } -// GetTrustDomainTrustDomainName converts echo context to params. -func (w *ServerInterfaceWrapper) GetTrustDomainTrustDomainName(ctx echo.Context) error { +// GetTrustDomainByName converts echo context to params. +func (w *ServerInterfaceWrapper) GetTrustDomainByName(ctx echo.Context) error { var err error // ------------- Path parameter "trustDomainName" ------------- var trustDomainName externalRef0.TrustDomainName @@ -1234,12 +1224,12 @@ func (w *ServerInterfaceWrapper) GetTrustDomainTrustDomainName(ctx echo.Context) } // Invoke the callback with all the unmarshalled arguments - err = w.Handler.GetTrustDomainTrustDomainName(ctx, trustDomainName) + err = w.Handler.GetTrustDomainByName(ctx, trustDomainName) return err } -// PutTrustDomainTrustDomainName converts echo context to params. -func (w *ServerInterfaceWrapper) PutTrustDomainTrustDomainName(ctx echo.Context) error { +// PutTrustDomainByName converts echo context to params. +func (w *ServerInterfaceWrapper) PutTrustDomainByName(ctx echo.Context) error { var err error // ------------- Path parameter "trustDomainName" ------------- var trustDomainName externalRef0.UUID @@ -1250,7 +1240,7 @@ func (w *ServerInterfaceWrapper) PutTrustDomainTrustDomainName(ctx echo.Context) } // Invoke the callback with all the unmarshalled arguments - err = w.Handler.PutTrustDomainTrustDomainName(ctx, trustDomainName) + err = w.Handler.PutTrustDomainByName(ctx, trustDomainName) return err } @@ -1299,11 +1289,11 @@ func RegisterHandlersWithBaseURL(router EchoRouter, si ServerInterface, baseURL } router.GET(baseURL+"/relationships", wrapper.GetRelationships) - router.PUT(baseURL+"/relationships", wrapper.PutRelationships) - router.GET(baseURL+"/relationships/:relationshipID", wrapper.GetRelationshipsRelationshipID) + router.PUT(baseURL+"/relationships", wrapper.PutRelationship) + router.GET(baseURL+"/relationships/:relationshipID", wrapper.GetRelationshipByID) router.PUT(baseURL+"/trust-domain", wrapper.PutTrustDomain) - router.GET(baseURL+"/trust-domain/:trustDomainName", wrapper.GetTrustDomainTrustDomainName) - router.PUT(baseURL+"/trust-domain/:trustDomainName", wrapper.PutTrustDomainTrustDomainName) + router.GET(baseURL+"/trust-domain/:trustDomainName", wrapper.GetTrustDomainByName) + router.PUT(baseURL+"/trust-domain/:trustDomainName", wrapper.PutTrustDomainByName) router.GET(baseURL+"/trust-domain/:trustDomainName/join-token", wrapper.GetJoinToken) } @@ -1311,31 +1301,31 @@ func RegisterHandlersWithBaseURL(router EchoRouter, si ServerInterface, baseURL // Base64 encoded, gzipped, json marshaled Swagger object var swaggerSpec = []string{ - "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", - "pHij/XXWBirBzif5fwEAAP//hjJN4p8cAAA=", + "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", } // GetSwagger returns the content of the embedded swagger specification file diff --git a/pkg/server/api/admin/admin.yaml b/pkg/server/api/admin/admin.yaml index 004b3101..8a721479 100644 --- a/pkg/server/api/admin/admin.yaml +++ b/pkg/server/api/admin/admin.yaml @@ -23,6 +23,7 @@ tags: paths: /trust-domain/{trustDomainName}: get: + operationId: GetTrustDomainByName tags: - Trust Domain summary: Get a specific trust domain @@ -43,6 +44,7 @@ paths: default: $ref: '#/components/responses/Default' put: + operationId: PutTrustDomainByName tags: - Trust Domain summary: Update a specific trust domain @@ -70,6 +72,7 @@ paths: /trust-domain: put: + operationId: PutTrustDomain tags: - Trust Domain summary: Add a specific trust domain @@ -90,6 +93,7 @@ paths: /relationships: get: + operationId: GetRelationships tags: - Relationships summary: Get relationships @@ -97,8 +101,7 @@ paths: - name: status in: query schema: - type: string - enum: [approved, denied, pending] + $ref: '../../../common/api/schemas.yaml#/components/schemas/ConsentStatus' description: relationship status from a Trust Domain perspective, - name: trustDomainName in: query @@ -117,6 +120,7 @@ paths: default: $ref: '#/components/responses/Default' put: + operationId: PutRelationship tags: - Relationships summary: Create a relationship request between two Trust Domains @@ -138,6 +142,7 @@ paths: /relationships/{relationshipID}: get: + operationId: GetRelationshipByID tags: - Relationships summary: Get a specific relationship diff --git a/pkg/server/datastore/fakedatabase.go b/pkg/server/datastore/fakedatabase.go index a8fee06c..c1fdafd5 100644 --- a/pkg/server/datastore/fakedatabase.go +++ b/pkg/server/datastore/fakedatabase.go @@ -74,14 +74,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 @@ -174,9 +175,7 @@ func (db *FakeDatabase) CreateOrUpdateBundle(ctx context.Context, req *entity.Bu } } - req.CreatedAt = time.Now() req.UpdatedAt = time.Now() - db.bundles[req.ID.UUID] = req return req, nil @@ -390,7 +389,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) { diff --git a/pkg/server/endpoints/auth_test.go b/pkg/server/endpoints/auth_test.go index f6909bed..5977e753 100644 --- a/pkg/server/endpoints/auth_test.go +++ b/pkg/server/endpoints/auth_test.go @@ -19,8 +19,6 @@ import ( "github.com/stretchr/testify/require" ) -const testTrustDomain = "test.com" - type AuthNTestSetup struct { EchoCtx echo.Context Middleware *AuthenticationMiddleware diff --git a/pkg/server/endpoints/harvester.go b/pkg/server/endpoints/harvester.go index 275146fa..bb515591 100644 --- a/pkg/server/endpoints/harvester.go +++ b/pkg/server/endpoints/harvester.go @@ -85,7 +85,7 @@ func (h *HarvesterAPIHandlers) GetRelationships(echoCtx echo.Context, params har apiRelationships = append(apiRelationships, api.RelationshipFromEntity(r)) } - return chttp.WriteResponse(echoCtx, apiRelationships) + return chttp.WriteResponse(echoCtx, http.StatusOK, apiRelationships) } // PatchRelationship accept/denies relationships requests - (PATCH /relationships/{relationshipID}) @@ -145,7 +145,7 @@ func (h *HarvesterAPIHandlers) PatchRelationship(echoCtx echo.Context, relations return h.handleErrorAndLog(err, msg, http.StatusInternalServerError) } - if err = chttp.BodylessResponse(echoCtx); err != nil { + if err = chttp.BodilessResponse(echoCtx, http.StatusOK); err != nil { return h.handleErrorAndLog(err, err.Error(), http.StatusInternalServerError) } @@ -215,7 +215,7 @@ func (h *HarvesterAPIHandlers) Onboard(echoCtx echo.Context, params harvester.On return h.handleErrorAndLog(err, msg, http.StatusInternalServerError) } - return chttp.WriteResponse(echoCtx, jwtToken) + return chttp.WriteResponse(echoCtx, http.StatusOK, jwtToken) } // GetNewJWTToken renews a JWT access token - (GET /trust-domain/jwt) @@ -255,7 +255,7 @@ func (h *HarvesterAPIHandlers) GetNewJWTToken(echoCtx echo.Context) error { return h.handleErrorAndLog(err, msg, http.StatusInternalServerError) } - return chttp.WriteResponse(echoCtx, newToken) + return chttp.WriteResponse(echoCtx, http.StatusOK, newToken) } // BundleSync synchronize the status of trust bundles between server and harvester - (POST /trust-domain/{trustDomainName}/bundles/sync) @@ -294,7 +294,7 @@ func (h *HarvesterAPIHandlers) BundleSync(echoCtx echo.Context, trustDomainName return h.handleErrorAndLog(err, msg, http.StatusInternalServerError) } - return chttp.WriteResponse(echoCtx, resp) + return chttp.WriteResponse(echoCtx, http.StatusOK, resp) } // BundlePut uploads a new trust bundle to the server - (PUT /trust-domain/{trustDomainName}/bundles) @@ -351,7 +351,7 @@ func (h *HarvesterAPIHandlers) BundlePut(echoCtx echo.Context, trustDomainName a return h.handleErrorAndLog(err, msg, http.StatusInternalServerError) } - if err = chttp.BodylessResponse(echoCtx); err != nil { + if err = chttp.BodilessResponse(echoCtx, http.StatusOK); err != nil { return h.handleErrorAndLog(err, err.Error(), http.StatusInternalServerError) } diff --git a/pkg/server/endpoints/harvester_test.go b/pkg/server/endpoints/harvester_test.go index 06dba651..55c0cef4 100644 --- a/pkg/server/endpoints/harvester_test.go +++ b/pkg/server/endpoints/harvester_test.go @@ -5,6 +5,7 @@ import ( "crypto/sha256" "encoding/base64" "encoding/json" + "io" "net/http" "net/http/httptest" "reflect" @@ -56,16 +57,23 @@ type HarvesterTestSetup struct { Recorder *httptest.ResponseRecorder } -func NewHarvesterTestSetup(t *testing.T, 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() logger := logrus.New() jwtAudience := []string{"test"} - jwtIssuer := fakejwtissuer.New(t, "test", testTrustDomain, jwtAudience) + jwtIssuer := fakejwtissuer.New(t, "test", td1, jwtAudience) jwtValidator := jwttest.NewJWTValidator(jwtIssuer.Signer, jwtAudience) return &HarvesterTestSetup{ @@ -78,14 +86,13 @@ func NewHarvesterTestSetup(t *testing.T, method, url, body string) *HarvesterTes } func SetupTrustDomain(t *testing.T, ds datastore.Datastore) *entity.TrustDomain { - td, err := spiffeid.TrustDomainFromString(testTrustDomain) + td, err := spiffeid.TrustDomainFromString(td1) assert.NoError(t, err) tdEntity := &entity.TrustDomain{ Name: td, Description: "Fake domain", } - trustDomain, err := ds.CreateOrUpdateTrustDomain(context.TODO(), tdEntity) require.NoError(t, err) @@ -143,7 +150,7 @@ func TestTCPGetRelationships(t *testing.T) { }) t.Run("Fails with invalid consent status", func(t *testing.T) { - setup := NewHarvesterTestSetup(t, http.MethodGet, relationshipsPath, "") + setup := NewHarvesterTestSetup(t, http.MethodGet, relationshipsPath, nil) echoCtx := setup.EchoCtx setup.EchoCtx.Set(authTrustDomainKey, tdA) @@ -161,7 +168,7 @@ func TestTCPGetRelationships(t *testing.T) { }) t.Run("Fails if no authenticated trust domain", func(t *testing.T) { - setup := NewHarvesterTestSetup(t, http.MethodGet, relationshipsPath, "") + setup := NewHarvesterTestSetup(t, http.MethodGet, relationshipsPath, nil) echoCtx := setup.EchoCtx tdName := tdA.Name.String() @@ -176,7 +183,7 @@ func TestTCPGetRelationships(t *testing.T) { }) t.Run("Fails if authenticated trust domain does not match trust domain parameter", func(t *testing.T) { - setup := NewHarvesterTestSetup(t, http.MethodGet, relationshipsPath, "") + setup := NewHarvesterTestSetup(t, http.MethodGet, relationshipsPath, nil) echoCtx := setup.EchoCtx setup.EchoCtx.Set(authTrustDomainKey, tdA) @@ -193,7 +200,7 @@ func TestTCPGetRelationships(t *testing.T) { } func testGetRelationships(t *testing.T, setupFn func(*HarvesterTestSetup, *entity.TrustDomain), status api.ConsentStatus, trustDomain *entity.TrustDomain, expectedRelationshipCount int) { - setup := NewHarvesterTestSetup(t, http.MethodGet, relationshipsPath, "") + setup := NewHarvesterTestSetup(t, http.MethodGet, relationshipsPath, nil) echoCtx := setup.EchoCtx setup.Datastore.WithTrustDomains(tdA, tdB, tdC) @@ -261,10 +268,7 @@ func testPatchRelationship(t *testing.T, f func(setup *HarvesterTestSetup, trust ConsentStatus: status, } - body, err := json.Marshal(requestBody) - require.NoError(t, err) - - setup := NewHarvesterTestSetup(t, http.MethodPatch, relationshipsPath+"/"+relationship.ID.UUID.String(), string(body)) + setup := NewHarvesterTestSetup(t, http.MethodPatch, relationshipsPath+"/"+relationship.ID.UUID.String(), &requestBody) echoCtx := setup.EchoCtx setup.Datastore.WithTrustDomains(tdA, tdB, tdC) @@ -272,7 +276,7 @@ func testPatchRelationship(t *testing.T, f func(setup *HarvesterTestSetup, trust f(setup, trustDomain) - err = setup.Handler.PatchRelationship(echoCtx, relationship.ID.UUID) + err := setup.Handler.PatchRelationship(echoCtx, relationship.ID.UUID) assert.NoError(t, err) recorder := setup.Recorder @@ -297,7 +301,7 @@ func testPatchRelationship(t *testing.T, f func(setup *HarvesterTestSetup, trust func TestTCPOnboard(t *testing.T) { t.Run("Successfully onboard a new agent", func(t *testing.T) { - harvesterTestSetup := NewHarvesterTestSetup(t, http.MethodGet, onboardPath, "") + harvesterTestSetup := NewHarvesterTestSetup(t, http.MethodGet, onboardPath, nil) echoCtx := harvesterTestSetup.EchoCtx td := SetupTrustDomain(t, harvesterTestSetup.Handler.Datastore) @@ -318,7 +322,7 @@ func TestTCPOnboard(t *testing.T) { assert.Equal(t, harvesterTestSetup.JWTIssuer.Token, jwtToken) }) t.Run("Onboard without join token fails", func(t *testing.T) { - harvesterTestSetup := NewHarvesterTestSetup(t, http.MethodGet, onboardPath, "") + harvesterTestSetup := NewHarvesterTestSetup(t, http.MethodGet, onboardPath, nil) echoCtx := harvesterTestSetup.EchoCtx SetupTrustDomain(t, harvesterTestSetup.Handler.Datastore) @@ -334,7 +338,7 @@ func TestTCPOnboard(t *testing.T) { assert.Contains(t, httpErr.Message, "join token is required") }) t.Run("Onboard with join token that does not exist fails", func(t *testing.T) { - harvesterTestSetup := NewHarvesterTestSetup(t, http.MethodGet, onboardPath, "") + harvesterTestSetup := NewHarvesterTestSetup(t, http.MethodGet, onboardPath, nil) echoCtx := harvesterTestSetup.EchoCtx td := SetupTrustDomain(t, harvesterTestSetup.Handler.Datastore) @@ -351,7 +355,7 @@ func TestTCPOnboard(t *testing.T) { assert.Contains(t, httpErr.Message, "token not found") }) t.Run("Onboard with join token that was used", func(t *testing.T) { - harvesterTestSetup := NewHarvesterTestSetup(t, http.MethodGet, onboardPath, "") + harvesterTestSetup := NewHarvesterTestSetup(t, http.MethodGet, onboardPath, nil) echoCtx := harvesterTestSetup.EchoCtx td := SetupTrustDomain(t, harvesterTestSetup.Handler.Datastore) @@ -375,7 +379,7 @@ func TestTCPOnboard(t *testing.T) { func TestTCPGetNewJWTToken(t *testing.T) { t.Run("Successfully get a new JWT token", func(t *testing.T) { - harvesterTestSetup := NewHarvesterTestSetup(t, http.MethodGet, jwtPath, "") + harvesterTestSetup := NewHarvesterTestSetup(t, http.MethodGet, jwtPath, nil) echoCtx := harvesterTestSetup.EchoCtx SetupTrustDomain(t, harvesterTestSetup.Handler.Datastore) @@ -399,7 +403,7 @@ func TestTCPGetNewJWTToken(t *testing.T) { assert.Equal(t, harvesterTestSetup.JWTIssuer.Token, jwtToken) }) t.Run("Fails if no JWT token was sent", func(t *testing.T) { - harvesterTestSetup := NewHarvesterTestSetup(t, http.MethodGet, jwtPath, "") + harvesterTestSetup := NewHarvesterTestSetup(t, http.MethodGet, jwtPath, nil) echoCtx := harvesterTestSetup.EchoCtx err := harvesterTestSetup.Handler.GetNewJWTToken(echoCtx) @@ -518,10 +522,7 @@ func TestTCPBundleSync(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - body, err := json.Marshal(tc.bundleState) - require.NoError(t, err) - - setup := NewHarvesterTestSetup(t, http.MethodPost, "/trust-domain/:trustDomainName/bundles/sync", string(body)) + setup := NewHarvesterTestSetup(t, http.MethodPost, "/trust-domain/:trustDomainName/bundles/sync", &tc.bundleState) echoCtx := setup.EchoCtx setup.EchoCtx.Set(authTrustDomainKey, tdA) @@ -530,7 +531,7 @@ func TestTCPBundleSync(t *testing.T) { setup.Datastore.WithBundles(bundleA, bundleB, bundleC) // test bundle sync - err = setup.Handler.BundleSync(echoCtx, tdA.Name.String()) + err := setup.Handler.BundleSync(echoCtx, tdA.Name.String()) assert.NoError(t, err) recorder := setup.Recorder @@ -571,16 +572,13 @@ func TestBundlePut(t *testing.T) { Signature: "bundle signature", SigningCertificate: "certificate PEM", TrustBundle: "a new bundle", - TrustDomain: testTrustDomain, + TrustDomain: td1, } - body, err := json.Marshal(bundlePut) - require.NoError(t, err) - - setup := NewHarvesterTestSetup(t, http.MethodPut, "/trust-domain/:trustDomainName/bundles", string(body)) + setup := NewHarvesterTestSetup(t, http.MethodPut, "/trust-domain/:trustDomainName/bundles", &bundlePut) setup.EchoCtx.Set(authTrustDomainKey, "") - err = setup.Handler.BundlePut(setup.EchoCtx, testTrustDomain) + err := setup.Handler.BundlePut(setup.EchoCtx, td1) require.Error(t, err) assert.Equal(t, http.StatusUnauthorized, err.(*echo.HTTPError).Code) assert.Contains(t, err.(*echo.HTTPError).Message, "no authenticated trust domain") @@ -599,7 +597,7 @@ func TestBundlePut(t *testing.T) { }) t.Run("Fail post bundle bundle trust domain does not match authenticated trust domain", func(t *testing.T) { - testInvalidBundleRequest(t, "TrustDomain", "other-trust-domain", http.StatusUnauthorized, "trust domain in request bundle \"other-trust-domain\" does not match authenticated trust domain: \"test.com\"") + testInvalidBundleRequest(t, "TrustDomain", "other-trust-domain", http.StatusUnauthorized, "trust domain in request bundle \"other-trust-domain\" does not match authenticated trust domain: \"test1.com\"") }) } @@ -608,18 +606,15 @@ func testBundlePut(t *testing.T, setupFunc func(*HarvesterTestSetup) *entity.Tru Signature: "bundle signature", SigningCertificate: "certificate PEM", TrustBundle: "a new bundle", - TrustDomain: testTrustDomain, + TrustDomain: td1, } - body, err := json.Marshal(bundlePut) - require.NoError(t, err) - - setup := NewHarvesterTestSetup(t, http.MethodPut, "/trust-domain/:trustDomainName/bundles", string(body)) + setup := NewHarvesterTestSetup(t, http.MethodPut, "/trust-domain/:trustDomainName/bundles", &bundlePut) echoCtx := setup.EchoCtx td := setupFunc(setup) - err = setup.Handler.BundlePut(echoCtx, testTrustDomain) + err := setup.Handler.BundlePut(echoCtx, td1) require.NoError(t, err) recorder := setup.Recorder @@ -639,20 +634,17 @@ func testInvalidBundleRequest(t *testing.T, fieldName string, fieldValue interfa Signature: "test-signature", SigningCertificate: "certificate PEM", TrustBundle: "test trust bundle", - TrustDomain: testTrustDomain, + TrustDomain: td1, } reflect.ValueOf(&bundlePut).Elem().FieldByName(fieldName).Set(reflect.ValueOf(fieldValue)) - body, err := json.Marshal(bundlePut) - require.NoError(t, err) - - setup := NewHarvesterTestSetup(t, http.MethodPut, "/trust-domain/:trustDomainName/bundles", string(body)) + setup := NewHarvesterTestSetup(t, http.MethodPut, "/trust-domain/:trustDomainName/bundles", &bundlePut) echoCtx := setup.EchoCtx td := SetupTrustDomain(t, setup.Handler.Datastore) echoCtx.Set(authTrustDomainKey, td) - err = setup.Handler.BundlePut(echoCtx, testTrustDomain) + err := setup.Handler.BundlePut(echoCtx, td1) require.Error(t, err) assert.Equal(t, expectedStatusCode, err.(*echo.HTTPError).Code) assert.Contains(t, err.(*echo.HTTPError).Message, expectedErrorMessage) diff --git a/pkg/server/endpoints/management.go b/pkg/server/endpoints/management.go index ea691aa8..a0b81257 100644 --- a/pkg/server/endpoints/management.go +++ b/pkg/server/endpoints/management.go @@ -3,6 +3,7 @@ package endpoints import ( "context" + "errors" "fmt" "net/http" "time" @@ -33,134 +34,255 @@ func NewAdminAPIHandlers(l logrus.FieldLogger, ds datastore.Datastore) *AdminAPI } // GetRelationships list all relationships filtered by the request params - (GET /relationships) -func (h AdminAPIHandlers) GetRelationships(ctx echo.Context, params admin.GetRelationshipsParams) error { - gctx := ctx.Request().Context() +func (h *AdminAPIHandlers) GetRelationships(echoContext echo.Context, params admin.GetRelationshipsParams) error { + ctx := echoContext.Request().Context() - rels, err := h.Datastore.ListRelationships(gctx) - if err != nil { - err = fmt.Errorf("failed listing relationships: %v", err) - return h.HandleAndLog(err, http.StatusInternalServerError) + var err error + var rels []*entity.Relationship + + if params.TrustDomainName != nil { + td, err := h.findTrustDomainByName(ctx, *params.TrustDomainName) + if err != nil { + err = fmt.Errorf("failed looking up trust domain name: %w", err) + return h.handleAndLog(err, http.StatusBadRequest) + } + + rels, err = h.Datastore.FindRelationshipsByTrustDomainID(ctx, td.ID.UUID) + if err != nil { + err = fmt.Errorf("failed looking up relationships: %v", err) + return h.handleAndLog(err, http.StatusInternalServerError) + } + } else { + rels, err = h.Datastore.ListRelationships(ctx) + if err != nil { + err = fmt.Errorf("failed listing relationships: %v", err) + return h.handleAndLog(err, http.StatusInternalServerError) + } + } + + if params.Status != nil { + rels, err = h.adminFilterRelationshipsByConsentStatus(rels, api.ConsentStatus(*params.Status)) + if err != nil { + return err + } } - rels, err = h.populateTrustDomainNames(gctx, rels) + rels, err = h.populateTrustDomainNames(ctx, rels) if err != nil { err = fmt.Errorf("failed populating relationships entities: %v", err) - return h.HandleAndLog(err, http.StatusInternalServerError) + return h.handleAndLog(err, http.StatusInternalServerError) } cRelationships := mapRelationships(rels) - err = chttp.WriteResponse(ctx, cRelationships) + err = chttp.WriteResponse(echoContext, http.StatusOK, cRelationships) if err != nil { err = fmt.Errorf("relationships entities - %v", err.Error()) - return h.HandleAndLog(err, http.StatusInternalServerError) + return h.handleAndLog(err, http.StatusInternalServerError) } return nil } // PutRelationships create a new relationship request between two trust domains - (PUT /relationships) -func (h AdminAPIHandlers) PutRelationships(ctx echo.Context) error { - gctx := ctx.Request().Context() +func (h *AdminAPIHandlers) PutRelationship(echoCtx echo.Context) error { + ctx := echoCtx.Request().Context() - reqBody := &admin.PutRelationshipsJSONRequestBody{} - err := chttp.FromBody(ctx, reqBody) + reqBody := &admin.PutRelationshipJSONRequestBody{} + err := chttp.FromBody(echoCtx, reqBody) if err != nil { err := fmt.Errorf("failed to read relationship put body: %v", err) - return h.HandleAndLog(err, http.StatusBadRequest) + return h.handleAndLog(err, http.StatusBadRequest) } - eRelationship := reqBody.ToEntity() - rel, err := h.Datastore.CreateOrUpdateRelationship(gctx, eRelationship) + + _, err = h.lookupTrustDomain(ctx, eRelationship.TrustDomainAID, http.StatusBadRequest) + if err != nil { + return err + } + + _, err = h.lookupTrustDomain(ctx, eRelationship.TrustDomainBID, http.StatusBadRequest) + if err != nil { + return err + } + + rel, err := h.Datastore.CreateOrUpdateRelationship(ctx, eRelationship) if err != nil { err = fmt.Errorf("failed creating relationship: %v", err) - return h.HandleAndLog(err, http.StatusInternalServerError) + return h.handleAndLog(err, http.StatusInternalServerError) } h.Logger.Printf("Created relationship between trust domains %s and %s", rel.TrustDomainAID, rel.TrustDomainBID) response := api.RelationshipFromEntity(rel) - err = chttp.WriteResponse(ctx, response) + err = chttp.WriteResponse(echoCtx, http.StatusCreated, response) if err != nil { err = fmt.Errorf("relationships - %v", err.Error()) - return h.HandleAndLog(err, http.StatusInternalServerError) + return h.handleAndLog(err, http.StatusInternalServerError) } 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 { +// GetRelationshipByID retrieve a specific relationship based on its id - (GET /relationships/{relationshipID}) +func (h *AdminAPIHandlers) GetRelationshipByID(echoCtx echo.Context, relationshipID api.UUID) error { + ctx := echoCtx.Request().Context() + + r, err := h.Datastore.FindRelationshipByID(ctx, relationshipID) + if err != nil { + err = fmt.Errorf("failed getting relationships: %v", err) + return h.handleAndLog(err, http.StatusInternalServerError) + } + + if r == nil { + err = errors.New("relationship not found") + return h.handleAndLog(err, http.StatusNotFound) + } + + response := api.RelationshipFromEntity(r) + err = chttp.WriteResponse(echoCtx, http.StatusOK, response) + if err != nil { + err = fmt.Errorf("relationship entity - %v", err.Error()) + return h.handleAndLog(err, http.StatusInternalServerError) + } + return nil } // PutTrustDomain create a new trust domain - (PUT /trust-domain) -func (h AdminAPIHandlers) PutTrustDomain(ctx echo.Context) error { +func (h *AdminAPIHandlers) PutTrustDomain(echoCtx echo.Context) error { // Getting golang context - gctx := ctx.Request().Context() + ctx := echoCtx.Request().Context() reqBody := &admin.PutTrustDomainJSONRequestBody{} - err := chttp.FromBody(ctx, reqBody) + err := chttp.FromBody(echoCtx, reqBody) if err != nil { - return h.HandleAndLog(err, http.StatusBadRequest) + return h.handleAndLog(err, http.StatusBadRequest) } dbTD, err := reqBody.ToEntity() if err != nil { - return h.HandleAndLog(err, http.StatusBadRequest) + return h.handleAndLog(err, http.StatusBadRequest) } - td, err := h.Datastore.FindTrustDomainByName(gctx, dbTD.Name) + td, err := h.Datastore.FindTrustDomainByName(ctx, dbTD.Name) if err != nil { err = fmt.Errorf("failed looking up trust domain: %v", err) - return h.HandleAndLog(err, http.StatusInternalServerError) + return h.handleAndLog(err, http.StatusInternalServerError) } + if td != nil { err = fmt.Errorf("trust domain already exists: %q", dbTD.Name) - return h.HandleAndLog(err, http.StatusInternalServerError) + return h.handleAndLog(err, http.StatusBadRequest) } - m, err := h.Datastore.CreateOrUpdateTrustDomain(gctx, dbTD) + m, err := h.Datastore.CreateOrUpdateTrustDomain(ctx, dbTD) if err != nil { err = fmt.Errorf("failed creating trustDomain: %v", err) - return h.HandleAndLog(err, http.StatusInternalServerError) + return h.handleAndLog(err, http.StatusInternalServerError) } h.Logger.Printf("Created trustDomain for trust domain: %s", dbTD.Name) response := api.TrustDomainFromEntity(m) - err = chttp.WriteResponse(ctx, response) + err = chttp.WriteResponse(echoCtx, http.StatusCreated, response) if err != nil { err = fmt.Errorf("trustDomain entity - %v", err.Error()) - return h.HandleAndLog(err, http.StatusInternalServerError) + return h.handleAndLog(err, http.StatusInternalServerError) } return nil } // TODO: customize these names. -// GetTrustDomainTrustDomainName retrieve a specific trust domain by its name - (GET /trust-domain/{trustDomainName}) -func (h AdminAPIHandlers) GetTrustDomainTrustDomainName(ctx echo.Context, trustDomainName api.TrustDomainName) error { +// GetTrustDomainByName retrieve a specific trust domain by its name - (GET /trust-domain/{trustDomainName}) +func (h *AdminAPIHandlers) GetTrustDomainByName(echoCtx echo.Context, trustDomainName api.TrustDomainName) error { + ctx := echoCtx.Request().Context() + + tdName, err := spiffeid.TrustDomainFromString(trustDomainName) + if err != nil { + err = fmt.Errorf("failed parsing trust domain name: %v", err) + return h.handleAndLog(err, http.StatusBadRequest) + } + + td, err := h.Datastore.FindTrustDomainByName(ctx, tdName) + if err != nil { + err = fmt.Errorf("failed getting trust domain: %v", err) + return h.handleAndLog(err, http.StatusInternalServerError) + } + + if td == nil { + err = fmt.Errorf("trust domain does not exists") + return h.handleAndLog(err, http.StatusNotFound) + } + + response := api.TrustDomainFromEntity(td) + err = chttp.WriteResponse(echoCtx, http.StatusOK, response) + if err != nil { + err = fmt.Errorf("trust domain entity - %v", err.Error()) + return h.handleAndLog(err, http.StatusInternalServerError) + } + 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) PutTrustDomainByName(echoCtx echo.Context, trustDomainID api.UUID) error { + ctx := echoCtx.Request().Context() + + reqBody := &admin.PutTrustDomainByNameJSONRequestBody{} + err := chttp.FromBody(echoCtx, reqBody) + if err != nil { + err := fmt.Errorf("failed to read trust domain put body: %v", err) + return h.handleAndLog(err, http.StatusBadRequest) + } + + etd, err := reqBody.ToEntity() + if err != nil { + err := fmt.Errorf("failed to read trust domain put body: %v", err) + return h.handleAndLog(err, http.StatusBadRequest) + } + + _, err = h.lookupTrustDomain(ctx, trustDomainID, http.StatusNotFound) + if err != nil { + return err + } + + td, err := h.Datastore.CreateOrUpdateTrustDomain(ctx, etd) + if err != nil { + err = fmt.Errorf("failed creating/updating trust domain: %v", err) + return h.handleAndLog(err, http.StatusInternalServerError) + } + + h.Logger.Printf("Trust Bundle %v updated", td.Name) + + response := api.TrustDomainFromEntity(td) + err = chttp.WriteResponse(echoCtx, http.StatusOK, response) + if err != nil { + err = fmt.Errorf("relationships - %v", err.Error()) + return h.handleAndLog(err, http.StatusInternalServerError) + } + return nil } -// GetJoinToken generate a join token for the trust domain - (POST /trust-domain/{trustDomainName}/join-token) -func (h AdminAPIHandlers) GetJoinToken(echoCtx echo.Context, trustDomainName api.TrustDomainName) error { +// GetJoinToken generate a join token for the trust domain - (GET /trust-domain/{trustDomainName}/join-token) +func (h *AdminAPIHandlers) GetJoinToken(echoCtx echo.Context, trustDomainName api.TrustDomainName) error { ctx := echoCtx.Request().Context() tdName, err := spiffeid.TrustDomainFromString(trustDomainName) if err != nil { err = fmt.Errorf("failed parsing trust domain: %v", err) - return h.HandleAndLog(err, http.StatusBadRequest) + return h.handleAndLog(err, http.StatusBadRequest) } td, err := h.Datastore.FindTrustDomainByName(ctx, tdName) if err != nil { err = fmt.Errorf("failed looking up trust domain: %v", err) - return h.HandleAndLog(err, http.StatusInternalServerError) + return h.handleAndLog(err, http.StatusInternalServerError) + } + + if td == nil { + errMsg := fmt.Errorf("trust domain exists: %q", trustDomainName) + return h.handleAndLog(errMsg, http.StatusBadRequest) } token := uuid.New() @@ -174,21 +296,41 @@ func (h AdminAPIHandlers) GetJoinToken(echoCtx echo.Context, trustDomainName api _, err = h.Datastore.CreateJoinToken(ctx, joinToken) if err != nil { err = fmt.Errorf("failed creating join token: %v", err) - return h.HandleAndLog(err, http.StatusInternalServerError) + return h.handleAndLog(err, http.StatusInternalServerError) } h.Logger.Infof("Created join token for trust domain: %s", tdName) - err = chttp.WriteResponse(echoCtx, token) + response := admin.JoinTokenResult{ + Token: token, + } + + err = chttp.WriteResponse(echoCtx, http.StatusOK, response) if err != nil { err = fmt.Errorf("failed to write join token response: %v", err.Error()) - return h.HandleAndLog(err, http.StatusInternalServerError) + return h.handleAndLog(err, http.StatusInternalServerError) } return nil } -func (h AdminAPIHandlers) populateTrustDomainNames(ctx context.Context, relationships []*entity.Relationship) ([]*entity.Relationship, error) { +func (h *AdminAPIHandlers) findTrustDomainByName(ctx context.Context, trustDomain string) (*entity.TrustDomain, error) { + tdName, err := spiffeid.TrustDomainFromString(trustDomain) + if err != nil { + err = fmt.Errorf("failed parsing trust domain name: %v", err) + return nil, h.handleAndLog(err, http.StatusBadRequest) + } + + td, err := h.Datastore.FindTrustDomainByName(ctx, tdName) + if err != nil { + err = fmt.Errorf("failed getting trust domain: %v", err) + return nil, h.handleAndLog(err, http.StatusInternalServerError) + } + + return td, nil +} + +func (h *AdminAPIHandlers) populateTrustDomainNames(ctx context.Context, relationships []*entity.Relationship) ([]*entity.Relationship, error) { for _, r := range relationships { tda, err := h.Datastore.FindTrustDomainByID(ctx, r.TrustDomainAID) if err != nil { @@ -205,6 +347,70 @@ func (h AdminAPIHandlers) populateTrustDomainNames(ctx context.Context, relation return relationships, nil } +func (h *AdminAPIHandlers) lookupTrustDomain(ctx context.Context, trustDomainID uuid.UUID, code int) (*entity.TrustDomain, error) { + td, err := h.Datastore.FindTrustDomainByID(ctx, trustDomainID) + if err != nil { + msg := errors.New("error looking up trust domain") + errMsg := fmt.Errorf("%s: %w", msg, err) + return nil, h.handleAndLog(errMsg, http.StatusInternalServerError) + } + + if td == nil { + errMsg := fmt.Errorf("trust domain exists: %q", trustDomainID) + return nil, h.handleAndLog(errMsg, code) + } + + return td, nil +} + +func (h *AdminAPIHandlers) adminFilterRelationshipsByConsentStatus( + relationships []*entity.Relationship, + status api.ConsentStatus, +) ([]*entity.Relationship, error) { + filtered := make([]*entity.Relationship, 0) + + var checkConsentF func(api.ConsentStatus, api.ConsentStatus) bool + switch status { + case api.Denied: + checkConsentF = denied + case api.Accepted: + checkConsentF = accepted + case api.Pending: + checkConsentF = pending + case "": + checkConsentF = pending + default: + err := fmt.Errorf( + "status filter [`%v`] is not supported, accepted values [%v, %v, %v]", + status, api.Accepted, api.Denied, api.Pending, + ) + return nil, h.handleAndLog(err, http.StatusBadRequest) + } + + for _, relationship := range relationships { + trustDomainAConsent := api.ConsentStatus(relationship.TrustDomainAConsent) + trustDomainBConsent := api.ConsentStatus(relationship.TrustDomainBConsent) + + if checkConsentF(trustDomainAConsent, trustDomainBConsent) { + filtered = append(filtered, relationship) + } + } + + return filtered, nil +} + +func denied(ca api.ConsentStatus, cb api.ConsentStatus) bool { + return ca == api.Denied || cb == api.Denied +} + +func accepted(ca api.ConsentStatus, cb api.ConsentStatus) bool { + return ca == api.Accepted && cb == api.Accepted +} + +func pending(ca api.ConsentStatus, cb api.ConsentStatus) bool { + return (ca == api.Pending || cb == api.Pending) && ca != api.Denied && cb != api.Denied +} + func mapRelationships(relationships []*entity.Relationship) []*api.Relationship { cRelationships := []*api.Relationship{} @@ -216,7 +422,7 @@ func mapRelationships(relationships []*entity.Relationship) []*api.Relationship return cRelationships } -func (h AdminAPIHandlers) HandleAndLog(err error, code int) error { +func (h *AdminAPIHandlers) handleAndLog(err error, code int) error { errMsg := util.LogSanitize(err.Error()) h.Logger.Errorf(errMsg) return echo.NewHTTPError(code, err.Error()) diff --git a/pkg/server/endpoints/management_test.go b/pkg/server/endpoints/management_test.go index 0cc941d2..be973155 100644 --- a/pkg/server/endpoints/management_test.go +++ b/pkg/server/endpoints/management_test.go @@ -1,31 +1,565 @@ package endpoints -import "testing" +import ( + "encoding/json" + "fmt" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/HewlettPackard/galadriel/pkg/common/api" + "github.com/HewlettPackard/galadriel/pkg/common/entity" + "github.com/HewlettPackard/galadriel/pkg/server/api/admin" + "github.com/HewlettPackard/galadriel/pkg/server/datastore" + "github.com/google/uuid" + "github.com/labstack/echo/v4" + "github.com/sirupsen/logrus" + "github.com/spiffe/go-spiffe/v2/spiffeid" + "github.com/stretchr/testify/assert" +) + +const ( + // Trust Domains + td1 = "test1.com" + td2 = "test2.com" + td3 = "test3.com" +) + +var ( + // Relationships ID's + r1ID = NewNullableID() + r2ID = NewNullableID() + r3ID = NewNullableID() + r4ID = NewNullableID() + r5ID = NewNullableID() + + // Trust Domains ID's + tdUUID1 = NewNullableID() + tdUUID2 = NewNullableID() + tdUUID3 = NewNullableID() +) + +type ManagementTestSetup struct { + EchoCtx echo.Context + Handler *AdminAPIHandlers + Recorder *httptest.ResponseRecorder + FakeDatabase *datastore.FakeDatabase + + // Helpers + bodyReader io.Reader + + url string + method string +} + +func NewManagementTestSetup(t *testing.T, method, url string, body interface{}) *ManagementTestSetup { + var bodyReader io.Reader = nil + 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, bodyReader) + req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) + rec := httptest.NewRecorder() + fakeDB := datastore.NewFakeDB() + logger := logrus.New() + + return &ManagementTestSetup{ + EchoCtx: e.NewContext(req, rec), + Recorder: rec, + Handler: NewAdminAPIHandlers(logger, fakeDB), + FakeDatabase: fakeDB, + // Helpers + url: url, + method: method, + bodyReader: bodyReader, + } +} + +func (setup *ManagementTestSetup) Refresh() { + e := echo.New() + req := httptest.NewRequest(setup.method, setup.url, setup.bodyReader) + req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) + rec := httptest.NewRecorder() + + // Refreshing Request context and Recorder + setup.EchoCtx = e.NewContext(req, rec) + setup.Recorder = rec +} func TestUDSGetRelationships(t *testing.T) { - t.Skip("Missing tests will be added when the API be implemented") + relationshipsPath := "/relationships" + + t.Run("Successfully filter by trust domain", func(t *testing.T) { + // Setup + managementTestSetup := NewManagementTestSetup(t, http.MethodGet, relationshipsPath, nil) + echoCtx := managementTestSetup.EchoCtx + + td1Name := NewTrustDomain(t, td1) + + fakeTrustDomains := []*entity.TrustDomain{ + {ID: tdUUID1, Name: td1Name}, + {ID: tdUUID2, Name: NewTrustDomain(t, td2)}, + {ID: tdUUID3, Name: NewTrustDomain(t, td3)}, + } + + fakeRelationships := []*entity.Relationship{ + {ID: r1ID, TrustDomainAID: tdUUID1.UUID, TrustDomainBID: tdUUID2.UUID, TrustDomainAConsent: entity.ConsentStatus(api.Accepted), TrustDomainBConsent: entity.ConsentStatus(api.Accepted)}, + {ID: r2ID, TrustDomainBID: tdUUID1.UUID, TrustDomainAID: tdUUID3.UUID, TrustDomainAConsent: entity.ConsentStatus(api.Denied), TrustDomainBConsent: entity.ConsentStatus(api.Accepted)}, + {ID: NewNullableID(), TrustDomainAID: uuid.New(), TrustDomainBID: uuid.New(), TrustDomainAConsent: entity.ConsentStatus(api.Accepted), TrustDomainBConsent: entity.ConsentStatus(api.Denied)}, + {ID: NewNullableID(), TrustDomainAID: uuid.New(), TrustDomainBID: uuid.New(), TrustDomainAConsent: entity.ConsentStatus(api.Denied), TrustDomainBConsent: entity.ConsentStatus(api.Denied)}, + {ID: NewNullableID(), TrustDomainAID: uuid.New(), TrustDomainBID: uuid.New(), TrustDomainAConsent: entity.ConsentStatus(api.Pending), TrustDomainBConsent: entity.ConsentStatus(api.Pending)}, + } + + managementTestSetup.FakeDatabase.WithTrustDomains(fakeTrustDomains...) + managementTestSetup.FakeDatabase.WithRelationships(fakeRelationships...) + + tdName := td1 + params := admin.GetRelationshipsParams{ + TrustDomainName: &tdName, + } + + err := managementTestSetup.Handler.GetRelationships(echoCtx, params) + assert.NoError(t, err) + + recorder := managementTestSetup.Recorder + assert.Equal(t, http.StatusOK, recorder.Code) + assert.NotEmpty(t, recorder.Body) + + relationships := []*api.Relationship{} + err = json.Unmarshal(recorder.Body.Bytes(), &relationships) + assert.NoError(t, err) + + assert.Len(t, relationships, 2) + + apiRelations := mapRelationships([]*entity.Relationship{ + {ID: r1ID, TrustDomainAID: tdUUID1.UUID, TrustDomainBID: tdUUID2.UUID, TrustDomainAConsent: entity.ConsentStatus(api.Accepted), TrustDomainBConsent: entity.ConsentStatus(api.Accepted)}, + {ID: r2ID, TrustDomainBID: tdUUID1.UUID, TrustDomainAID: tdUUID3.UUID, TrustDomainAConsent: entity.ConsentStatus(api.Denied), TrustDomainBConsent: entity.ConsentStatus(api.Accepted)}, + }) + + assert.ElementsMatch(t, relationships, apiRelations, "trust domain name filter does not work properly") + }) + + t.Run("Successfully filter by status", func(t *testing.T) { + // Setup + setup := NewManagementTestSetup(t, http.MethodGet, relationshipsPath, nil) + + td1Name := NewTrustDomain(t, td1) + + fakeTrustDomains := []*entity.TrustDomain{ + {ID: tdUUID1, Name: td1Name}, + {ID: tdUUID2, Name: NewTrustDomain(t, td2)}, + {ID: tdUUID3, Name: NewTrustDomain(t, td3)}, + } + + fakeRelationships := []*entity.Relationship{ + {ID: r1ID, TrustDomainAID: tdUUID1.UUID, TrustDomainBID: tdUUID3.UUID, TrustDomainAConsent: entity.ConsentStatus(api.Accepted), TrustDomainBConsent: entity.ConsentStatus(api.Accepted)}, + {ID: r2ID, TrustDomainAID: tdUUID1.UUID, TrustDomainBID: tdUUID2.UUID, TrustDomainAConsent: entity.ConsentStatus(api.Pending), TrustDomainBConsent: entity.ConsentStatus(api.Pending)}, + {ID: r3ID, TrustDomainAID: tdUUID2.UUID, TrustDomainBID: tdUUID3.UUID, TrustDomainAConsent: entity.ConsentStatus(api.Denied), TrustDomainBConsent: entity.ConsentStatus(api.Pending)}, + {ID: r4ID, TrustDomainAID: tdUUID2.UUID, TrustDomainBID: tdUUID3.UUID, TrustDomainAConsent: entity.ConsentStatus(api.Denied), TrustDomainBConsent: entity.ConsentStatus(api.Denied)}, + {ID: r5ID, TrustDomainAID: tdUUID2.UUID, TrustDomainBID: tdUUID3.UUID, TrustDomainAConsent: entity.ConsentStatus(api.Accepted), TrustDomainBConsent: entity.ConsentStatus(api.Pending)}, + } + + setup.FakeDatabase.WithTrustDomains(fakeTrustDomains...) + setup.FakeDatabase.WithRelationships(fakeRelationships...) + + t.Run("Accepted Filter", func(t *testing.T) { + expectedRelations := mapRelationships([]*entity.Relationship{ + {ID: r1ID, TrustDomainAID: tdUUID1.UUID, TrustDomainBID: tdUUID3.UUID, TrustDomainAConsent: entity.ConsentStatus(api.Accepted), TrustDomainBConsent: entity.ConsentStatus(api.Accepted)}, + }) + + assertFilter(t, setup, expectedRelations, api.Accepted) + }) + + t.Run("Denied Filter", func(t *testing.T) { + expectedRelations := mapRelationships([]*entity.Relationship{ + {ID: r3ID, TrustDomainAID: tdUUID2.UUID, TrustDomainBID: tdUUID3.UUID, TrustDomainAConsent: entity.ConsentStatus(api.Denied), TrustDomainBConsent: entity.ConsentStatus(api.Pending)}, + {ID: r4ID, TrustDomainAID: tdUUID2.UUID, TrustDomainBID: tdUUID3.UUID, TrustDomainAConsent: entity.ConsentStatus(api.Denied), TrustDomainBConsent: entity.ConsentStatus(api.Denied)}, + }) + + assertFilter(t, setup, expectedRelations, api.Denied) + }) + + t.Run("Pending Filter", func(t *testing.T) { + expectedRelations := mapRelationships([]*entity.Relationship{ + {ID: r2ID, TrustDomainAID: tdUUID1.UUID, TrustDomainBID: tdUUID2.UUID, TrustDomainAConsent: entity.ConsentStatus(api.Pending), TrustDomainBConsent: entity.ConsentStatus(api.Pending)}, + {ID: r5ID, TrustDomainAID: tdUUID2.UUID, TrustDomainBID: tdUUID3.UUID, TrustDomainAConsent: entity.ConsentStatus(api.Accepted), TrustDomainBConsent: entity.ConsentStatus(api.Pending)}, + }) + + assertFilter(t, setup, expectedRelations, api.Pending) + }) + }) + + t.Run("Should raise a bad request when receiving undefined status filter", func(t *testing.T) { + + // Setup + setup := NewManagementTestSetup(t, http.MethodGet, relationshipsPath, nil) + + // Approved filter + var randomFilter api.ConsentStatus = "a random filter" + params := admin.GetRelationshipsParams{ + Status: &randomFilter, + } + + err := setup.Handler.GetRelationships(setup.EchoCtx, params) + assert.Error(t, err) + + httpErr := err.(*echo.HTTPError) + assert.Equal(t, http.StatusBadRequest, httpErr.Code) + assert.Empty(t, setup.Recorder.Body) + + expectedMsg := fmt.Sprintf( + "status filter [`%v`] is not supported, accepted values [%v, %v, %v]", + randomFilter, api.Accepted, api.Denied, api.Pending, + ) + + assert.ErrorContains(t, err, expectedMsg) + }) +} + +func assertFilter( + t *testing.T, + setup *ManagementTestSetup, + expectedRelations []*api.Relationship, + status api.ConsentStatus, +) { + setup.Refresh() + + strAddress := status + params := admin.GetRelationshipsParams{ + Status: &strAddress, + } + + err := setup.Handler.GetRelationships(setup.EchoCtx, params) + assert.NoError(t, err) + + assert.Equal(t, http.StatusOK, setup.Recorder.Code) + assert.NotEmpty(t, setup.Recorder.Body) + + relationships := []*api.Relationship{} + err = json.Unmarshal(setup.Recorder.Body.Bytes(), &relationships) + assert.NoError(t, err) + + assert.Len(t, relationships, len(expectedRelations)) + + assert.ElementsMatchf(t, relationships, expectedRelations, "%v status filter does not work properly", status) } func TestUDSPutRelationships(t *testing.T) { - t.Skip("Missing tests will be added when the API be implemented") + relationshipsPath := "/relationships" + + t.Run("Successfully create a new relationship request", func(t *testing.T) { + + fakeTrustDomains := []*entity.TrustDomain{ + {ID: tdUUID1, Name: NewTrustDomain(t, td1)}, + {ID: tdUUID2, Name: NewTrustDomain(t, td2)}, + } + + reqBody := &admin.PutRelationshipJSONRequestBody{ + TrustDomainAId: tdUUID1.UUID, + TrustDomainBId: tdUUID2.UUID, + } + + // Setup + setup := NewManagementTestSetup(t, http.MethodPut, relationshipsPath, reqBody) + setup.FakeDatabase.WithTrustDomains(fakeTrustDomains...) + + err := setup.Handler.PutRelationship(setup.EchoCtx) + assert.NoError(t, err) + + assert.Equal(t, http.StatusCreated, setup.Recorder.Code) + + apiRelation := api.Relationship{} + err = json.Unmarshal(setup.Recorder.Body.Bytes(), &apiRelation) + assert.NoError(t, err) + + assert.NotNil(t, apiRelation) + assert.Equal(t, tdUUID1.UUID, apiRelation.TrustDomainAId) + assert.Equal(t, tdUUID2.UUID, apiRelation.TrustDomainBId) + }) + + t.Run("Should not allow relationships request between inexistent trust domains", func(t *testing.T) { + + fakeTrustDomains := []*entity.TrustDomain{ + {ID: tdUUID1, Name: NewTrustDomain(t, td1)}, + } + + reqBody := &admin.PutRelationshipJSONRequestBody{ + TrustDomainAId: tdUUID1.UUID, + TrustDomainBId: tdUUID2.UUID, + } + + // Setup + setup := NewManagementTestSetup(t, http.MethodPut, relationshipsPath, reqBody) + setup.FakeDatabase.WithTrustDomains(fakeTrustDomains...) + + err := setup.Handler.PutRelationship(setup.EchoCtx) + assert.Error(t, err) + assert.Empty(t, setup.Recorder.Body.Bytes()) + + echoHTTPErr := err.(*echo.HTTPError) + assert.Equal(t, http.StatusBadRequest, echoHTTPErr.Code) + + expectedErrorMsg := fmt.Sprintf("trust domain exists: %q", tdUUID2.UUID) + assert.Equal(t, expectedErrorMsg, echoHTTPErr.Message) + }) + + // Should we test sending wrong body formats ? } -func TestUDSGetRelationshipsRelationshipID(t *testing.T) { - t.Skip("Missing tests will be added when the API be implemented") +func TestUDSGetRelationshipsByID(t *testing.T) { + relationshipsPath := "/relationships/%v" + + t.Run("Successfully get relationship information", func(t *testing.T) { + + fakeTrustDomains := []*entity.TrustDomain{ + {ID: tdUUID1, Name: NewTrustDomain(t, td1)}, + {ID: tdUUID2, Name: NewTrustDomain(t, td2)}, + } + + r1ID := NewNullableID() + fakeRelationship := &entity.Relationship{ + ID: r1ID, + TrustDomainAID: tdUUID1.UUID, + TrustDomainBID: tdUUID2.UUID, + } + + completePath := fmt.Sprintf(relationshipsPath, r1ID.UUID) + + // Setup + setup := NewManagementTestSetup(t, http.MethodGet, completePath, nil) + setup.FakeDatabase.WithTrustDomains(fakeTrustDomains...) + setup.FakeDatabase.WithRelationships(fakeRelationship) + + err := setup.Handler.GetRelationshipByID(setup.EchoCtx, r1ID.UUID) + assert.NoError(t, err) + + assert.Equal(t, http.StatusOK, setup.Recorder.Code) + + apiRelation := api.Relationship{} + err = json.Unmarshal(setup.Recorder.Body.Bytes(), &apiRelation) + assert.NoError(t, err) + + assert.NotNil(t, apiRelation) + assert.Equal(t, tdUUID1.UUID, apiRelation.TrustDomainAId) + assert.Equal(t, tdUUID2.UUID, apiRelation.TrustDomainBId) + }) + + t.Run("Should raise a not found request when try to get information about a relationship that doesn't exists", func(t *testing.T) { + completePath := fmt.Sprintf(relationshipsPath, r1ID.UUID) + + // Setup + setup := NewManagementTestSetup(t, http.MethodGet, completePath, nil) + + err := setup.Handler.GetRelationshipByID(setup.EchoCtx, r1ID.UUID) + assert.Error(t, err) + assert.Empty(t, setup.Recorder.Body.Bytes()) + + echoHTTPerr := err.(*echo.HTTPError) + assert.Equal(t, http.StatusNotFound, echoHTTPerr.Code) + assert.Equal(t, "relationship not found", echoHTTPerr.Message) + }) } func TestUDSPutTrustDomain(t *testing.T) { - t.Skip("Missing tests will be added when the API be implemented") + trustDomainPath := "/trust-domain" + t.Run("Successfully create a new trust domain", func(t *testing.T) { + description := "A test trust domain" + reqBody := &admin.TrustDomainPut{ + Name: td1, + Description: &description, + } + + // Setup + setup := NewManagementTestSetup(t, http.MethodPut, trustDomainPath, reqBody) + + err := setup.Handler.PutTrustDomain(setup.EchoCtx) + assert.NoError(t, err) + + assert.Equal(t, http.StatusCreated, setup.Recorder.Code) + + apiTrustDomain := api.TrustDomain{} + err = json.Unmarshal(setup.Recorder.Body.Bytes(), &apiTrustDomain) + assert.NoError(t, err) + + assert.NotNil(t, apiTrustDomain) + assert.Equal(t, td1, apiTrustDomain.Name) + assert.Equal(t, description, *apiTrustDomain.Description) + + }) + + t.Run("Should not allow creating trust domain with the same name of one already created", func(t *testing.T) { + reqBody := &admin.TrustDomainPut{ + Name: td1, + } + + fakeTrustDomains := entity.TrustDomain{ID: NewNullableID(), Name: NewTrustDomain(t, td1)} + + // Setup + setup := NewManagementTestSetup(t, http.MethodPut, trustDomainPath, reqBody) + setup.FakeDatabase.WithTrustDomains(&fakeTrustDomains) + + err := setup.Handler.PutTrustDomain(setup.EchoCtx) + assert.Error(t, err) + + echoHttpErr := err.(*echo.HTTPError) + + assert.Equal(t, http.StatusBadRequest, echoHttpErr.Code) + assert.ErrorContains(t, echoHttpErr, "trust domain already exists") + }) } -func TestUDSGetTrustDomainTrustDomainName(t *testing.T) { - t.Skip("Missing tests will be added when the API be implemented") +func TestUDSGetTrustDomainByName(t *testing.T) { + trustDomainPath := "/trust-domain/%v" + + t.Run("Successfully retrieve trust domain information", func(t *testing.T) { + fakeTrustDomains := entity.TrustDomain{ID: tdUUID1, Name: NewTrustDomain(t, td1)} + + completePath := fmt.Sprintf(trustDomainPath, tdUUID1.UUID) + + // Setup + setup := NewManagementTestSetup(t, http.MethodPut, completePath, nil) + setup.FakeDatabase.WithTrustDomains(&fakeTrustDomains) + + err := setup.Handler.GetTrustDomainByName(setup.EchoCtx, td1) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, setup.Recorder.Code) + + apiTrustDomain := api.TrustDomain{} + err = json.Unmarshal(setup.Recorder.Body.Bytes(), &apiTrustDomain) + assert.NoError(t, err) + + assert.Equal(t, td1, apiTrustDomain.Name) + assert.Equal(t, tdUUID1.UUID, apiTrustDomain.Id) + }) + + t.Run("Raise a not found when trying to retrieve a trust domain that does not exists", func(t *testing.T) { + completePath := fmt.Sprintf(trustDomainPath, tdUUID1.UUID) + + // Setup + setup := NewManagementTestSetup(t, http.MethodPut, completePath, nil) + + err := setup.Handler.GetTrustDomainByName(setup.EchoCtx, td1) + assert.Error(t, err) + + echoHttpErr := err.(*echo.HTTPError) + assert.Equal(t, http.StatusNotFound, echoHttpErr.Code) + assert.Equal(t, "trust domain does not exists", echoHttpErr.Message) + }) +} + +func TestUDSPutTrustDomainByName(t *testing.T) { + trustDomainPath := "/trust-domain/%v" + + t.Run("Successfully updated a trust domain", func(t *testing.T) { + fakeTrustDomains := entity.TrustDomain{ID: tdUUID1, Name: NewTrustDomain(t, td1)} + + completePath := fmt.Sprintf(trustDomainPath, tdUUID1.UUID) + + description := "I am being updated" + reqBody := &admin.PutTrustDomainByNameJSONRequestBody{ + Id: tdUUID1.UUID, + Name: td1, + Description: &description, + } + + // Setup + setup := NewManagementTestSetup(t, http.MethodPut, completePath, reqBody) + setup.FakeDatabase.WithTrustDomains(&fakeTrustDomains) + + err := setup.Handler.PutTrustDomainByName(setup.EchoCtx, tdUUID1.UUID) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, setup.Recorder.Code) + + apiTrustDomain := api.TrustDomain{} + err = json.Unmarshal(setup.Recorder.Body.Bytes(), &apiTrustDomain) + assert.NoError(t, err) + + assert.Equal(t, td1, apiTrustDomain.Name) + assert.Equal(t, tdUUID1.UUID, apiTrustDomain.Id) + assert.Equal(t, description, *apiTrustDomain.Description) + }) + + t.Run("Raise a not found when trying to updated a trust domain that does not exists", func(t *testing.T) { + completePath := fmt.Sprintf(trustDomainPath, tdUUID1.UUID) + + // Fake Request body + description := "I am being updated" + reqBody := &admin.PutTrustDomainByNameJSONRequestBody{ + Id: tdUUID1.UUID, + Name: td1, + Description: &description, + } + + // Setup + setup := NewManagementTestSetup(t, http.MethodPut, completePath, reqBody) + + err := setup.Handler.PutTrustDomainByName(setup.EchoCtx, tdUUID1.UUID) + assert.Error(t, err) + assert.Empty(t, setup.Recorder.Body.Bytes()) + + echoHTTPErr := err.(*echo.HTTPError) + assert.Equal(t, http.StatusNotFound, echoHTTPErr.Code) + expectedErrorMsg := fmt.Sprintf("trust domain exists: %q", tdUUID1.UUID) + assert.Equal(t, expectedErrorMsg, echoHTTPErr.Message) + }) +} + +func TestUDSGetJoinToken(t *testing.T) { + trustDomainPath := "/trust-domain/%v/join-token" + + t.Run("Successfully generates a join token for the trust domain", func(t *testing.T) { + td1ID := NewNullableID() + fakeTrustDomains := entity.TrustDomain{ID: td1ID, Name: NewTrustDomain(t, td1)} + + completePath := fmt.Sprintf(trustDomainPath, td1) + + // Setup + setup := NewManagementTestSetup(t, http.MethodGet, completePath, nil) + setup.FakeDatabase.WithTrustDomains(&fakeTrustDomains) + + err := setup.Handler.GetJoinToken(setup.EchoCtx, td1) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, setup.Recorder.Code) + + apiJToken := admin.JoinTokenResult{} + err = json.Unmarshal(setup.Recorder.Body.Bytes(), &apiJToken) + assert.NoError(t, err) + + assert.NotEmpty(t, apiJToken) + }) + + t.Run("Raise a bad request when trying to generates a join token for the trust domain that does not exists", func(t *testing.T) { + completePath := fmt.Sprintf(trustDomainPath, td1) + + // Setup + setup := NewManagementTestSetup(t, http.MethodGet, completePath, nil) + + err := setup.Handler.GetJoinToken(setup.EchoCtx, td1) + assert.Error(t, err) + + echoHttpErr := err.(*echo.HTTPError) + assert.Equal(t, http.StatusBadRequest, echoHttpErr.Code) + + expectedMsg := fmt.Errorf("trust domain exists: %q", td1) + assert.Equal(t, expectedMsg.Error(), echoHttpErr.Message) + }) } -func TestUDSPutTrustDomainTrustDomainName(t *testing.T) { - t.Skip("Missing tests will be added when the API be implemented") +func NewNullableID() uuid.NullUUID { + return uuid.NullUUID{ + Valid: true, + UUID: uuid.New(), + } } -func TestUDSPostTrustDomainTrustDomainNameJoinToken(t *testing.T) { - t.Skip("Missing tests will be added when the API be implemented") +func NewTrustDomain(t *testing.T, tdName string) spiffeid.TrustDomain { + td, err := spiffeid.TrustDomainFromString(tdName) + assert.NoError(t, err) + return td }