From 158cc42c07e49f4f80f9ab6fb4de6ac9ac0c24b1 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Tue, 29 Nov 2022 16:41:18 -0500 Subject: [PATCH] basic service reservation support (#41) --- cmd/zrok/loop.go | 4 ++-- cmd/zrok/share_private.go | 6 +++--- cmd/zrok/share_public.go | 4 ++-- controller/share.go | 21 +++++++++++++-------- controller/util.go | 2 +- rest_model_zrok/share_request.go | 9 ++++++--- rest_model_zrok/share_response.go | 4 ++-- rest_server_zrok/embedded_spec.go | 22 ++++++++++++++-------- specs/zrok.yml | 28 ++++++++++++++++++---------- ui/src/api/types.js | 5 +++-- 10 files changed, 64 insertions(+), 41 deletions(-) diff --git a/cmd/zrok/loop.go b/cmd/zrok/loop.go index a2217708..9f6762cc 100644 --- a/cmd/zrok/loop.go +++ b/cmd/zrok/loop.go @@ -184,7 +184,7 @@ func (l *looper) startup() { l.auth = httptransport.APIKeyAuth("x-token", "header", l.env.Token) tunnelReq := service.NewShareParams() tunnelReq.Body = &rest_model_zrok.ShareRequest{ - ZID: l.env.ZId, + EnvZID: l.env.ZId, ShareMode: "public", BackendMode: "proxy", BackendProxyEndpoint: fmt.Sprintf("looper#%d", l.id), @@ -195,7 +195,7 @@ func (l *looper) startup() { if err != nil { panic(err) } - l.service = tunnelResp.Payload.SvcName + l.service = tunnelResp.Payload.SvcToken l.proxyEndpoint = tunnelResp.Payload.FrontendProxyEndpoint } diff --git a/cmd/zrok/share_private.go b/cmd/zrok/share_private.go index 09c769b0..a3b7c5dd 100644 --- a/cmd/zrok/share_private.go +++ b/cmd/zrok/share_private.go @@ -84,7 +84,7 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) { auth := httptransport.APIKeyAuth("X-TOKEN", "header", env.Token) req := service.NewShareParams() req.Body = &rest_model_zrok.ShareRequest{ - ZID: env.ZId, + EnvZID: env.ZId, ShareMode: "private", BackendMode: "proxy", BackendProxyEndpoint: cfg.EndpointAddress, @@ -110,7 +110,7 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) { } panic(err) } - cfg.Service = resp.Payload.SvcName + cfg.Service = resp.Payload.SvcToken c := make(chan os.Signal) signal.Notify(c, os.Interrupt, syscall.SIGTERM) @@ -138,7 +138,7 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) { } }() - logrus.Infof("share your zrok service; use this command for access: 'zrok access private %v'", resp.Payload.SvcName) + logrus.Infof("share your zrok service; use this command for access: 'zrok access private %v'", resp.Payload.SvcToken) for { time.Sleep(30 * time.Second) diff --git a/cmd/zrok/share_public.go b/cmd/zrok/share_public.go index 8c9d4476..5ef0c0ca 100644 --- a/cmd/zrok/share_public.go +++ b/cmd/zrok/share_public.go @@ -102,7 +102,7 @@ func (self *sharePublicCommand) run(_ *cobra.Command, args []string) { auth := httptransport.APIKeyAuth("X-TOKEN", "header", env.Token) req := service.NewShareParams() req.Body = &rest_model_zrok.ShareRequest{ - ZID: env.ZId, + EnvZID: env.ZId, ShareMode: "public", BackendMode: "proxy", BackendProxyEndpoint: cfg.EndpointAddress, @@ -128,7 +128,7 @@ func (self *sharePublicCommand) run(_ *cobra.Command, args []string) { } panic(err) } - cfg.Service = resp.Payload.SvcName + cfg.Service = resp.Payload.SvcToken c := make(chan os.Signal) signal.Notify(c, os.Interrupt, syscall.SIGTERM) diff --git a/controller/share.go b/controller/share.go index 063b8e66..2ba9f09c 100644 --- a/controller/share.go +++ b/controller/share.go @@ -22,7 +22,7 @@ func (h *shareHandler) Handle(params service.ShareParams, principal *rest_model_ } defer func() { _ = tx.Rollback() }() - envZId := params.Body.ZID + envZId := params.Body.EnvZID envId := 0 if envs, err := str.FindEnvironmentsForAccount(int(principal.ID), tx); err == nil { found := false @@ -48,7 +48,7 @@ func (h *shareHandler) Handle(params service.ShareParams, principal *rest_model_ logrus.Error(err) return service.NewShareInternalServerError() } - svcName, err := createServiceName() + svcToken, err := createServiceToken() if err != nil { logrus.Error(err) return service.NewShareInternalServerError() @@ -58,14 +58,14 @@ func (h *shareHandler) Handle(params service.ShareParams, principal *rest_model_ var frontendEndpoints []string switch params.Body.ShareMode { case "public": - svcZId, frontendEndpoints, err = newPublicResourceAllocator().allocate(envZId, svcName, params, edge) + svcZId, frontendEndpoints, err = newPublicResourceAllocator().allocate(envZId, svcToken, params, edge) if err != nil { logrus.Error(err) return service.NewShareInternalServerError() } case "private": - svcZId, frontendEndpoints, err = newPrivateResourceAllocator().allocate(envZId, svcName, params, edge) + svcZId, frontendEndpoints, err = newPrivateResourceAllocator().allocate(envZId, svcToken, params, edge) if err != nil { logrus.Error(err) return service.NewShareInternalServerError() @@ -76,15 +76,20 @@ func (h *shareHandler) Handle(params service.ShareParams, principal *rest_model_ return service.NewShareInternalServerError() } - logrus.Debugf("allocated service '%v'", svcName) + logrus.Debugf("allocated service '%v'", svcToken) + reserved := false + if params.Body.Reserve { + reserved = true + } sid, err := str.CreateService(envId, &store.Service{ ZId: svcZId, - Name: svcName, + Name: svcToken, ShareMode: params.Body.ShareMode, BackendMode: params.Body.BackendMode, FrontendEndpoint: &frontendEndpoints[0], BackendProxyEndpoint: ¶ms.Body.BackendProxyEndpoint, + Reserved: reserved, }, tx) if err != nil { logrus.Errorf("error creating service record: %v", err) @@ -95,10 +100,10 @@ func (h *shareHandler) Handle(params service.ShareParams, principal *rest_model_ logrus.Errorf("error committing service record: %v", err) return service.NewShareInternalServerError() } - logrus.Infof("recorded service '%v' with id '%v' for '%v'", svcName, sid, principal.Email) + logrus.Infof("recorded service '%v' with id '%v' for '%v'", svcToken, sid, principal.Email) return service.NewShareCreated().WithPayload(&rest_model_zrok.ShareResponse{ FrontendProxyEndpoint: frontendEndpoints[0], - SvcName: svcName, + SvcToken: svcToken, }) } diff --git a/controller/util.go b/controller/util.go index f69e11b4..e559066c 100644 --- a/controller/util.go +++ b/controller/util.go @@ -43,7 +43,7 @@ func edgeClient() (*rest_management_api_client.ZitiEdgeManagement, error) { return rest_util.NewEdgeManagementClientWithUpdb(cfg.Ziti.Username, cfg.Ziti.Password, cfg.Ziti.ApiEndpoint, caPool) } -func createServiceName() (string, error) { +func createServiceToken() (string, error) { gen, err := nanoid.CustomASCII("abcdefghijklmnopqrstuvwxyz0123456789", 12) if err != nil { return "", err diff --git a/rest_model_zrok/share_request.go b/rest_model_zrok/share_request.go index d561448e..06e689c4 100644 --- a/rest_model_zrok/share_request.go +++ b/rest_model_zrok/share_request.go @@ -34,15 +34,18 @@ type ShareRequest struct { // backend proxy endpoint BackendProxyEndpoint string `json:"backendProxyEndpoint,omitempty"` + // env z Id + EnvZID string `json:"envZId,omitempty"` + // frontend selection FrontendSelection []string `json:"frontendSelection"` + // reserve + Reserve bool `json:"reserve,omitempty"` + // share mode // Enum: [public private] ShareMode string `json:"shareMode,omitempty"` - - // z Id - ZID string `json:"zId,omitempty"` } // Validate validates this share request diff --git a/rest_model_zrok/share_response.go b/rest_model_zrok/share_response.go index 9eea7d8d..cf6753e9 100644 --- a/rest_model_zrok/share_response.go +++ b/rest_model_zrok/share_response.go @@ -20,8 +20,8 @@ type ShareResponse struct { // frontend proxy endpoint FrontendProxyEndpoint string `json:"frontendProxyEndpoint,omitempty"` - // svc name - SvcName string `json:"svcName,omitempty"` + // svc token + SvcToken string `json:"svcToken,omitempty"` } // Validate validates this share response diff --git a/rest_server_zrok/embedded_spec.go b/rest_server_zrok/embedded_spec.go index 47e6ed58..09a47cb7 100644 --- a/rest_server_zrok/embedded_spec.go +++ b/rest_server_zrok/embedded_spec.go @@ -669,21 +669,24 @@ func init() { "backendProxyEndpoint": { "type": "string" }, + "envZId": { + "type": "string" + }, "frontendSelection": { "type": "array", "items": { "type": "string" } }, + "reserve": { + "type": "boolean" + }, "shareMode": { "type": "string", "enum": [ "public", "private" ] - }, - "zId": { - "type": "string" } } }, @@ -693,7 +696,7 @@ func init() { "frontendProxyEndpoint": { "type": "string" }, - "svcName": { + "svcToken": { "type": "string" } } @@ -1403,21 +1406,24 @@ func init() { "backendProxyEndpoint": { "type": "string" }, + "envZId": { + "type": "string" + }, "frontendSelection": { "type": "array", "items": { "type": "string" } }, + "reserve": { + "type": "boolean" + }, "shareMode": { "type": "string", "enum": [ "public", "private" ] - }, - "zId": { - "type": "string" } } }, @@ -1427,7 +1433,7 @@ func init() { "frontendProxyEndpoint": { "type": "string" }, - "svcName": { + "svcToken": { "type": "string" } } diff --git a/specs/zrok.yml b/specs/zrok.yml index b01543ca..5ede0dec 100644 --- a/specs/zrok.yml +++ b/specs/zrok.yml @@ -323,10 +323,6 @@ definitions: identity: type: string - environments: - type: array - items: - $ref: "#/definitions/environment" environment: type: object properties: @@ -345,10 +341,16 @@ definitions: updatedAt: type: integer + environments: + type: array + items: + $ref: "#/definitions/environment" + environmentServicesList: type: array items: $ref: "#/definitions/environmentServices" + environmentServices: type: object properties: @@ -367,6 +369,7 @@ definitions: type: string password: type: string + loginResponse: type: string @@ -387,16 +390,13 @@ definitions: type: string password: type: string + registerResponse: type: object properties: token: type: string - services: - type: array - items: - $ref: "#/definitions/service" service: type: object properties: @@ -415,6 +415,11 @@ definitions: updatedAt: type: integer + services: + type: array + items: + $ref: "#/definitions/service" + serviceMetrics: type: array items: @@ -423,7 +428,7 @@ definitions: shareRequest: type: object properties: - zId: + envZId: type: string shareMode: type: string @@ -443,12 +448,15 @@ definitions: type: array items: $ref: "#/definitions/authUser" + reserve: + type: boolean + shareResponse: type: object properties: frontendProxyEndpoint: type: string - svcName: + svcToken: type: string unaccessRequest: diff --git a/ui/src/api/types.js b/ui/src/api/types.js index 2da429a1..f819044c 100644 --- a/ui/src/api/types.js +++ b/ui/src/api/types.js @@ -124,13 +124,14 @@ * @typedef shareRequest * @memberof module:types * - * @property {string} zId + * @property {string} envZId * @property {string} shareMode * @property {string[]} frontendSelection * @property {string} backendMode * @property {string} backendProxyEndpoint * @property {string} authScheme * @property {module:types.authUser[]} authUsers + * @property {boolean} reserve */ /** @@ -138,7 +139,7 @@ * @memberof module:types * * @property {string} frontendProxyEndpoint - * @property {string} svcName + * @property {string} svcToken */ /**