From 9aba5f3524b00e83564e989ea3a8a62fd9f35dc6 Mon Sep 17 00:00:00 2001 From: Cam Otts Date: Wed, 1 Feb 2023 13:13:15 -0600 Subject: [PATCH 1/2] added error checking for name collision on frontend and send corresponding error --- .gitignore | 1 + cmd/zrok/adminCreateFrontend.go | 12 +++- controller/createFrontend.go | 19 +++++++ .../admin/create_frontend_responses.go | 57 +++++++++++++++++++ rest_server_zrok/embedded_spec.go | 6 ++ .../admin/create_frontend_responses.go | 25 ++++++++ specs/zrok.yml | 2 + 7 files changed, 121 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 412f2e93..8426cb6b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .$* .idea +.vscode *.db automated-release-build etc/dev.yml diff --git a/cmd/zrok/adminCreateFrontend.go b/cmd/zrok/adminCreateFrontend.go index 8c3fc98c..e3f09c1d 100644 --- a/cmd/zrok/adminCreateFrontend.go +++ b/cmd/zrok/adminCreateFrontend.go @@ -1,8 +1,11 @@ package main import ( + "os" + "github.com/openziti/zrok/rest_client_zrok/admin" "github.com/openziti/zrok/rest_model_zrok" + "github.com/openziti/zrok/tui" "github.com/openziti/zrok/zrokdir" "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -52,7 +55,14 @@ func (cmd *adminCreateFrontendCommand) run(_ *cobra.Command, args []string) { resp, err := zrok.Admin.CreateFrontend(req, mustGetAdminAuth()) if err != nil { - panic(err) + switch err.(type) { + case *admin.CreateFrontendBadRequest: + tui.Error("create frontend request failed: name already exists", err) + os.Exit(1) + default: + tui.Error("create frontend request failed", err) + os.Exit(1) + } } logrus.Infof("created global public frontend '%v'", resp.Payload.Token) diff --git a/controller/createFrontend.go b/controller/createFrontend.go index a2b73a91..38720549 100644 --- a/controller/createFrontend.go +++ b/controller/createFrontend.go @@ -1,7 +1,11 @@ package controller import ( + "errors" + "github.com/go-openapi/runtime/middleware" + "github.com/lib/pq" + "github.com/mattn/go-sqlite3" "github.com/openziti/zrok/controller/store" "github.com/openziti/zrok/controller/zrokEdgeSdk" "github.com/openziti/zrok/rest_model_zrok" @@ -60,6 +64,21 @@ func (h *createFrontendHandler) Handle(params admin.CreateFrontendParams, princi Reserved: true, } if _, err := str.CreateGlobalFrontend(fe, tx); err != nil { + perr := &pq.Error{} + sqliteErr := &sqlite3.Error{} + switch { + case errors.As(err, &perr): + if perr.Code == pq.ErrorCode("23505") { + return admin.NewCreateFrontendBadRequest() + } + case errors.As(err, sqliteErr): + if errors.Is(sqliteErr.Code, sqlite3.ErrConstraint) { + return admin.NewCreateFrontendBadRequest() + } + default: + logrus.Error("None?", err) + } + logrus.Errorf("error creating frontend record: %v", err) return admin.NewCreateFrontendInternalServerError() } diff --git a/rest_client_zrok/admin/create_frontend_responses.go b/rest_client_zrok/admin/create_frontend_responses.go index 6ef6399b..35689cc5 100644 --- a/rest_client_zrok/admin/create_frontend_responses.go +++ b/rest_client_zrok/admin/create_frontend_responses.go @@ -29,6 +29,12 @@ func (o *CreateFrontendReader) ReadResponse(response runtime.ClientResponse, con return nil, err } return result, nil + case 400: + result := NewCreateFrontendBadRequest() + if err := result.readResponse(response, consumer, o.formats); err != nil { + return nil, err + } + return nil, result case 401: result := NewCreateFrontendUnauthorized() if err := result.readResponse(response, consumer, o.formats); err != nil { @@ -115,6 +121,57 @@ func (o *CreateFrontendCreated) readResponse(response runtime.ClientResponse, co return nil } +// NewCreateFrontendBadRequest creates a CreateFrontendBadRequest with default headers values +func NewCreateFrontendBadRequest() *CreateFrontendBadRequest { + return &CreateFrontendBadRequest{} +} + +/* +CreateFrontendBadRequest describes a response with status code 400, with default header values. + +bad request +*/ +type CreateFrontendBadRequest struct { +} + +// IsSuccess returns true when this create frontend bad request response has a 2xx status code +func (o *CreateFrontendBadRequest) IsSuccess() bool { + return false +} + +// IsRedirect returns true when this create frontend bad request response has a 3xx status code +func (o *CreateFrontendBadRequest) IsRedirect() bool { + return false +} + +// IsClientError returns true when this create frontend bad request response has a 4xx status code +func (o *CreateFrontendBadRequest) IsClientError() bool { + return true +} + +// IsServerError returns true when this create frontend bad request response has a 5xx status code +func (o *CreateFrontendBadRequest) IsServerError() bool { + return false +} + +// IsCode returns true when this create frontend bad request response a status code equal to that given +func (o *CreateFrontendBadRequest) IsCode(code int) bool { + return code == 400 +} + +func (o *CreateFrontendBadRequest) Error() string { + return fmt.Sprintf("[POST /frontend][%d] createFrontendBadRequest ", 400) +} + +func (o *CreateFrontendBadRequest) String() string { + return fmt.Sprintf("[POST /frontend][%d] createFrontendBadRequest ", 400) +} + +func (o *CreateFrontendBadRequest) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error { + + return nil +} + // NewCreateFrontendUnauthorized creates a CreateFrontendUnauthorized with default headers values func NewCreateFrontendUnauthorized() *CreateFrontendUnauthorized { return &CreateFrontendUnauthorized{} diff --git a/rest_server_zrok/embedded_spec.go b/rest_server_zrok/embedded_spec.go index 15b6324f..c35eeafe 100644 --- a/rest_server_zrok/embedded_spec.go +++ b/rest_server_zrok/embedded_spec.go @@ -265,6 +265,9 @@ func init() { "$ref": "#/definitions/createFrontendResponse" } }, + "400": { + "description": "bad request" + }, "401": { "description": "unauthorized" }, @@ -1537,6 +1540,9 @@ func init() { "$ref": "#/definitions/createFrontendResponse" } }, + "400": { + "description": "bad request" + }, "401": { "description": "unauthorized" }, diff --git a/rest_server_zrok/operations/admin/create_frontend_responses.go b/rest_server_zrok/operations/admin/create_frontend_responses.go index 3b3f4865..ae1847d7 100644 --- a/rest_server_zrok/operations/admin/create_frontend_responses.go +++ b/rest_server_zrok/operations/admin/create_frontend_responses.go @@ -58,6 +58,31 @@ func (o *CreateFrontendCreated) WriteResponse(rw http.ResponseWriter, producer r } } +// CreateFrontendBadRequestCode is the HTTP code returned for type CreateFrontendBadRequest +const CreateFrontendBadRequestCode int = 400 + +/* +CreateFrontendBadRequest bad request + +swagger:response createFrontendBadRequest +*/ +type CreateFrontendBadRequest struct { +} + +// NewCreateFrontendBadRequest creates CreateFrontendBadRequest with default headers values +func NewCreateFrontendBadRequest() *CreateFrontendBadRequest { + + return &CreateFrontendBadRequest{} +} + +// WriteResponse to the client +func (o *CreateFrontendBadRequest) WriteResponse(rw http.ResponseWriter, producer runtime.Producer) { + + rw.Header().Del(runtime.HeaderContentType) //Remove Content-Type on empty responses + + rw.WriteHeader(400) +} + // CreateFrontendUnauthorizedCode is the HTTP code returned for type CreateFrontendUnauthorized const CreateFrontendUnauthorizedCode int = 401 diff --git a/specs/zrok.yml b/specs/zrok.yml index 5bc2131b..7b5d1ab4 100644 --- a/specs/zrok.yml +++ b/specs/zrok.yml @@ -151,6 +151,8 @@ paths: description: frontend created schema: $ref: "#/definitions/createFrontendResponse" + 400: + description: bad request 401: description: unauthorized 404: From 8203811f348812ae74e071efc919440d08c37b12 Mon Sep 17 00:00:00 2001 From: Cam Otts Date: Thu, 2 Feb 2023 09:24:06 -0600 Subject: [PATCH 2/2] removed debugging log --- controller/createFrontend.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/controller/createFrontend.go b/controller/createFrontend.go index 38720549..9e328529 100644 --- a/controller/createFrontend.go +++ b/controller/createFrontend.go @@ -75,8 +75,6 @@ func (h *createFrontendHandler) Handle(params admin.CreateFrontendParams, princi if errors.Is(sqliteErr.Code, sqlite3.ErrConstraint) { return admin.NewCreateFrontendBadRequest() } - default: - logrus.Error("None?", err) } logrus.Errorf("error creating frontend record: %v", err)