basic service reservation support (#41)

This commit is contained in:
Michael Quigley 2022-11-29 16:41:18 -05:00
parent 37be8fc4e0
commit 158cc42c07
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
10 changed files with 64 additions and 41 deletions

View File

@ -184,7 +184,7 @@ func (l *looper) startup() {
l.auth = httptransport.APIKeyAuth("x-token", "header", l.env.Token) l.auth = httptransport.APIKeyAuth("x-token", "header", l.env.Token)
tunnelReq := service.NewShareParams() tunnelReq := service.NewShareParams()
tunnelReq.Body = &rest_model_zrok.ShareRequest{ tunnelReq.Body = &rest_model_zrok.ShareRequest{
ZID: l.env.ZId, EnvZID: l.env.ZId,
ShareMode: "public", ShareMode: "public",
BackendMode: "proxy", BackendMode: "proxy",
BackendProxyEndpoint: fmt.Sprintf("looper#%d", l.id), BackendProxyEndpoint: fmt.Sprintf("looper#%d", l.id),
@ -195,7 +195,7 @@ func (l *looper) startup() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
l.service = tunnelResp.Payload.SvcName l.service = tunnelResp.Payload.SvcToken
l.proxyEndpoint = tunnelResp.Payload.FrontendProxyEndpoint l.proxyEndpoint = tunnelResp.Payload.FrontendProxyEndpoint
} }

View File

@ -84,7 +84,7 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) {
auth := httptransport.APIKeyAuth("X-TOKEN", "header", env.Token) auth := httptransport.APIKeyAuth("X-TOKEN", "header", env.Token)
req := service.NewShareParams() req := service.NewShareParams()
req.Body = &rest_model_zrok.ShareRequest{ req.Body = &rest_model_zrok.ShareRequest{
ZID: env.ZId, EnvZID: env.ZId,
ShareMode: "private", ShareMode: "private",
BackendMode: "proxy", BackendMode: "proxy",
BackendProxyEndpoint: cfg.EndpointAddress, BackendProxyEndpoint: cfg.EndpointAddress,
@ -110,7 +110,7 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) {
} }
panic(err) panic(err)
} }
cfg.Service = resp.Payload.SvcName cfg.Service = resp.Payload.SvcToken
c := make(chan os.Signal) c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM) 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 { for {
time.Sleep(30 * time.Second) time.Sleep(30 * time.Second)

View File

@ -102,7 +102,7 @@ func (self *sharePublicCommand) run(_ *cobra.Command, args []string) {
auth := httptransport.APIKeyAuth("X-TOKEN", "header", env.Token) auth := httptransport.APIKeyAuth("X-TOKEN", "header", env.Token)
req := service.NewShareParams() req := service.NewShareParams()
req.Body = &rest_model_zrok.ShareRequest{ req.Body = &rest_model_zrok.ShareRequest{
ZID: env.ZId, EnvZID: env.ZId,
ShareMode: "public", ShareMode: "public",
BackendMode: "proxy", BackendMode: "proxy",
BackendProxyEndpoint: cfg.EndpointAddress, BackendProxyEndpoint: cfg.EndpointAddress,
@ -128,7 +128,7 @@ func (self *sharePublicCommand) run(_ *cobra.Command, args []string) {
} }
panic(err) panic(err)
} }
cfg.Service = resp.Payload.SvcName cfg.Service = resp.Payload.SvcToken
c := make(chan os.Signal) c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM) signal.Notify(c, os.Interrupt, syscall.SIGTERM)

View File

@ -22,7 +22,7 @@ func (h *shareHandler) Handle(params service.ShareParams, principal *rest_model_
} }
defer func() { _ = tx.Rollback() }() defer func() { _ = tx.Rollback() }()
envZId := params.Body.ZID envZId := params.Body.EnvZID
envId := 0 envId := 0
if envs, err := str.FindEnvironmentsForAccount(int(principal.ID), tx); err == nil { if envs, err := str.FindEnvironmentsForAccount(int(principal.ID), tx); err == nil {
found := false found := false
@ -48,7 +48,7 @@ func (h *shareHandler) Handle(params service.ShareParams, principal *rest_model_
logrus.Error(err) logrus.Error(err)
return service.NewShareInternalServerError() return service.NewShareInternalServerError()
} }
svcName, err := createServiceName() svcToken, err := createServiceToken()
if err != nil { if err != nil {
logrus.Error(err) logrus.Error(err)
return service.NewShareInternalServerError() return service.NewShareInternalServerError()
@ -58,14 +58,14 @@ func (h *shareHandler) Handle(params service.ShareParams, principal *rest_model_
var frontendEndpoints []string var frontendEndpoints []string
switch params.Body.ShareMode { switch params.Body.ShareMode {
case "public": case "public":
svcZId, frontendEndpoints, err = newPublicResourceAllocator().allocate(envZId, svcName, params, edge) svcZId, frontendEndpoints, err = newPublicResourceAllocator().allocate(envZId, svcToken, params, edge)
if err != nil { if err != nil {
logrus.Error(err) logrus.Error(err)
return service.NewShareInternalServerError() return service.NewShareInternalServerError()
} }
case "private": case "private":
svcZId, frontendEndpoints, err = newPrivateResourceAllocator().allocate(envZId, svcName, params, edge) svcZId, frontendEndpoints, err = newPrivateResourceAllocator().allocate(envZId, svcToken, params, edge)
if err != nil { if err != nil {
logrus.Error(err) logrus.Error(err)
return service.NewShareInternalServerError() return service.NewShareInternalServerError()
@ -76,15 +76,20 @@ func (h *shareHandler) Handle(params service.ShareParams, principal *rest_model_
return service.NewShareInternalServerError() 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{ sid, err := str.CreateService(envId, &store.Service{
ZId: svcZId, ZId: svcZId,
Name: svcName, Name: svcToken,
ShareMode: params.Body.ShareMode, ShareMode: params.Body.ShareMode,
BackendMode: params.Body.BackendMode, BackendMode: params.Body.BackendMode,
FrontendEndpoint: &frontendEndpoints[0], FrontendEndpoint: &frontendEndpoints[0],
BackendProxyEndpoint: &params.Body.BackendProxyEndpoint, BackendProxyEndpoint: &params.Body.BackendProxyEndpoint,
Reserved: reserved,
}, tx) }, tx)
if err != nil { if err != nil {
logrus.Errorf("error creating service record: %v", err) 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) logrus.Errorf("error committing service record: %v", err)
return service.NewShareInternalServerError() 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{ return service.NewShareCreated().WithPayload(&rest_model_zrok.ShareResponse{
FrontendProxyEndpoint: frontendEndpoints[0], FrontendProxyEndpoint: frontendEndpoints[0],
SvcName: svcName, SvcToken: svcToken,
}) })
} }

View File

@ -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) 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) gen, err := nanoid.CustomASCII("abcdefghijklmnopqrstuvwxyz0123456789", 12)
if err != nil { if err != nil {
return "", err return "", err

View File

@ -34,15 +34,18 @@ type ShareRequest struct {
// backend proxy endpoint // backend proxy endpoint
BackendProxyEndpoint string `json:"backendProxyEndpoint,omitempty"` BackendProxyEndpoint string `json:"backendProxyEndpoint,omitempty"`
// env z Id
EnvZID string `json:"envZId,omitempty"`
// frontend selection // frontend selection
FrontendSelection []string `json:"frontendSelection"` FrontendSelection []string `json:"frontendSelection"`
// reserve
Reserve bool `json:"reserve,omitempty"`
// share mode // share mode
// Enum: [public private] // Enum: [public private]
ShareMode string `json:"shareMode,omitempty"` ShareMode string `json:"shareMode,omitempty"`
// z Id
ZID string `json:"zId,omitempty"`
} }
// Validate validates this share request // Validate validates this share request

View File

@ -20,8 +20,8 @@ type ShareResponse struct {
// frontend proxy endpoint // frontend proxy endpoint
FrontendProxyEndpoint string `json:"frontendProxyEndpoint,omitempty"` FrontendProxyEndpoint string `json:"frontendProxyEndpoint,omitempty"`
// svc name // svc token
SvcName string `json:"svcName,omitempty"` SvcToken string `json:"svcToken,omitempty"`
} }
// Validate validates this share response // Validate validates this share response

View File

@ -669,21 +669,24 @@ func init() {
"backendProxyEndpoint": { "backendProxyEndpoint": {
"type": "string" "type": "string"
}, },
"envZId": {
"type": "string"
},
"frontendSelection": { "frontendSelection": {
"type": "array", "type": "array",
"items": { "items": {
"type": "string" "type": "string"
} }
}, },
"reserve": {
"type": "boolean"
},
"shareMode": { "shareMode": {
"type": "string", "type": "string",
"enum": [ "enum": [
"public", "public",
"private" "private"
] ]
},
"zId": {
"type": "string"
} }
} }
}, },
@ -693,7 +696,7 @@ func init() {
"frontendProxyEndpoint": { "frontendProxyEndpoint": {
"type": "string" "type": "string"
}, },
"svcName": { "svcToken": {
"type": "string" "type": "string"
} }
} }
@ -1403,21 +1406,24 @@ func init() {
"backendProxyEndpoint": { "backendProxyEndpoint": {
"type": "string" "type": "string"
}, },
"envZId": {
"type": "string"
},
"frontendSelection": { "frontendSelection": {
"type": "array", "type": "array",
"items": { "items": {
"type": "string" "type": "string"
} }
}, },
"reserve": {
"type": "boolean"
},
"shareMode": { "shareMode": {
"type": "string", "type": "string",
"enum": [ "enum": [
"public", "public",
"private" "private"
] ]
},
"zId": {
"type": "string"
} }
} }
}, },
@ -1427,7 +1433,7 @@ func init() {
"frontendProxyEndpoint": { "frontendProxyEndpoint": {
"type": "string" "type": "string"
}, },
"svcName": { "svcToken": {
"type": "string" "type": "string"
} }
} }

View File

@ -323,10 +323,6 @@ definitions:
identity: identity:
type: string type: string
environments:
type: array
items:
$ref: "#/definitions/environment"
environment: environment:
type: object type: object
properties: properties:
@ -345,10 +341,16 @@ definitions:
updatedAt: updatedAt:
type: integer type: integer
environments:
type: array
items:
$ref: "#/definitions/environment"
environmentServicesList: environmentServicesList:
type: array type: array
items: items:
$ref: "#/definitions/environmentServices" $ref: "#/definitions/environmentServices"
environmentServices: environmentServices:
type: object type: object
properties: properties:
@ -367,6 +369,7 @@ definitions:
type: string type: string
password: password:
type: string type: string
loginResponse: loginResponse:
type: string type: string
@ -387,16 +390,13 @@ definitions:
type: string type: string
password: password:
type: string type: string
registerResponse: registerResponse:
type: object type: object
properties: properties:
token: token:
type: string type: string
services:
type: array
items:
$ref: "#/definitions/service"
service: service:
type: object type: object
properties: properties:
@ -415,6 +415,11 @@ definitions:
updatedAt: updatedAt:
type: integer type: integer
services:
type: array
items:
$ref: "#/definitions/service"
serviceMetrics: serviceMetrics:
type: array type: array
items: items:
@ -423,7 +428,7 @@ definitions:
shareRequest: shareRequest:
type: object type: object
properties: properties:
zId: envZId:
type: string type: string
shareMode: shareMode:
type: string type: string
@ -443,12 +448,15 @@ definitions:
type: array type: array
items: items:
$ref: "#/definitions/authUser" $ref: "#/definitions/authUser"
reserve:
type: boolean
shareResponse: shareResponse:
type: object type: object
properties: properties:
frontendProxyEndpoint: frontendProxyEndpoint:
type: string type: string
svcName: svcToken:
type: string type: string
unaccessRequest: unaccessRequest:

View File

@ -124,13 +124,14 @@
* @typedef shareRequest * @typedef shareRequest
* @memberof module:types * @memberof module:types
* *
* @property {string} zId * @property {string} envZId
* @property {string} shareMode * @property {string} shareMode
* @property {string[]} frontendSelection * @property {string[]} frontendSelection
* @property {string} backendMode * @property {string} backendMode
* @property {string} backendProxyEndpoint * @property {string} backendProxyEndpoint
* @property {string} authScheme * @property {string} authScheme
* @property {module:types.authUser[]} authUsers * @property {module:types.authUser[]} authUsers
* @property {boolean} reserve
*/ */
/** /**
@ -138,7 +139,7 @@
* @memberof module:types * @memberof module:types
* *
* @property {string} frontendProxyEndpoint * @property {string} frontendProxyEndpoint
* @property {string} svcName * @property {string} svcToken
*/ */
/** /**