2022-07-26 22:21:49 +02:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/go-openapi/runtime/middleware"
|
2022-11-22 17:35:25 +01:00
|
|
|
"github.com/openziti-test-kitchen/zrok/controller/store"
|
2022-07-26 22:21:49 +02:00
|
|
|
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
|
2022-11-18 21:36:55 +01:00
|
|
|
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/service"
|
2022-11-22 17:35:25 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-07-26 22:21:49 +02:00
|
|
|
)
|
|
|
|
|
2022-11-21 19:45:08 +01:00
|
|
|
type shareHandler struct{}
|
2022-08-09 17:49:40 +02:00
|
|
|
|
2022-11-18 21:36:55 +01:00
|
|
|
func newShareHandler() *shareHandler {
|
|
|
|
return &shareHandler{}
|
2022-08-09 17:49:40 +02:00
|
|
|
}
|
|
|
|
|
2022-11-18 21:36:55 +01:00
|
|
|
func (h *shareHandler) Handle(params service.ShareParams, principal *rest_model_zrok.Principal) middleware.Responder {
|
2022-12-02 22:18:28 +01:00
|
|
|
logrus.Infof("handling")
|
|
|
|
|
2022-11-22 17:35:25 +01:00
|
|
|
tx, err := str.Begin()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error starting transaction: %v", err)
|
|
|
|
return service.NewShareInternalServerError()
|
|
|
|
}
|
|
|
|
defer func() { _ = tx.Rollback() }()
|
|
|
|
|
2022-11-29 22:41:18 +01:00
|
|
|
envZId := params.Body.EnvZID
|
2022-11-22 17:35:25 +01:00
|
|
|
envId := 0
|
|
|
|
if envs, err := str.FindEnvironmentsForAccount(int(principal.ID), tx); err == nil {
|
|
|
|
found := false
|
|
|
|
for _, env := range envs {
|
|
|
|
if env.ZId == envZId {
|
|
|
|
logrus.Debugf("found identity '%v' for user '%v'", envZId, principal.Email)
|
|
|
|
envId = env.Id
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
logrus.Errorf("environment '%v' not found for user '%v'", envZId, principal.Email)
|
|
|
|
return service.NewShareUnauthorized()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logrus.Errorf("error finding environments for account '%v'", principal.Email)
|
|
|
|
return service.NewShareInternalServerError()
|
|
|
|
}
|
|
|
|
|
|
|
|
edge, err := edgeClient()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
return service.NewShareInternalServerError()
|
|
|
|
}
|
2022-11-29 22:41:18 +01:00
|
|
|
svcToken, err := createServiceToken()
|
2022-11-22 17:35:25 +01:00
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
return service.NewShareInternalServerError()
|
|
|
|
}
|
|
|
|
|
|
|
|
var svcZId string
|
|
|
|
var frontendEndpoints []string
|
2022-11-21 19:45:08 +01:00
|
|
|
switch params.Body.ShareMode {
|
|
|
|
case "public":
|
2022-12-06 18:00:25 +01:00
|
|
|
if len(params.Body.FrontendSelection) < 1 {
|
|
|
|
logrus.Info("no frontend selection provided")
|
|
|
|
return service.NewShareNotFound()
|
|
|
|
}
|
|
|
|
|
2022-12-06 17:48:59 +01:00
|
|
|
var frontendZIds []string
|
|
|
|
var frontendTemplates []string
|
|
|
|
for _, frontendSelection := range params.Body.FrontendSelection {
|
|
|
|
sfe, err := str.FindFrontendPubliclyNamed(frontendSelection, tx)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
2022-12-06 18:00:25 +01:00
|
|
|
return service.NewShareNotFound()
|
2022-12-06 17:48:59 +01:00
|
|
|
}
|
|
|
|
if sfe != nil && sfe.UrlTemplate != nil {
|
|
|
|
frontendZIds = append(frontendZIds, sfe.ZId)
|
|
|
|
frontendTemplates = append(frontendTemplates, *sfe.UrlTemplate)
|
|
|
|
logrus.Infof("added frontend selection '%v' with ziti identity '%v' for service '%v'", svcToken)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
svcZId, frontendEndpoints, err = newPublicResourceAllocator().allocate(envZId, svcToken, frontendZIds, frontendTemplates, params, edge)
|
2022-11-22 21:31:02 +01:00
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
return service.NewShareInternalServerError()
|
|
|
|
}
|
|
|
|
|
|
|
|
case "private":
|
2022-11-29 22:41:18 +01:00
|
|
|
svcZId, frontendEndpoints, err = newPrivateResourceAllocator().allocate(envZId, svcToken, params, edge)
|
2022-11-22 17:35:25 +01:00
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
return service.NewShareInternalServerError()
|
|
|
|
}
|
|
|
|
|
2022-11-21 19:45:08 +01:00
|
|
|
default:
|
2022-11-22 17:35:25 +01:00
|
|
|
logrus.Errorf("unknown share mode '%v", params.Body.ShareMode)
|
2022-11-18 21:36:55 +01:00
|
|
|
return service.NewShareInternalServerError()
|
2022-07-27 15:49:54 +02:00
|
|
|
}
|
2022-11-22 17:35:25 +01:00
|
|
|
|
2022-11-29 22:41:18 +01:00
|
|
|
logrus.Debugf("allocated service '%v'", svcToken)
|
2022-11-22 17:35:25 +01:00
|
|
|
|
2022-11-30 19:45:57 +01:00
|
|
|
reserved := params.Body.Reserved
|
2022-11-22 17:35:25 +01:00
|
|
|
sid, err := str.CreateService(envId, &store.Service{
|
|
|
|
ZId: svcZId,
|
2022-11-30 18:10:00 +01:00
|
|
|
Token: svcToken,
|
2022-11-22 17:39:00 +01:00
|
|
|
ShareMode: params.Body.ShareMode,
|
|
|
|
BackendMode: params.Body.BackendMode,
|
2022-11-22 17:35:25 +01:00
|
|
|
FrontendEndpoint: &frontendEndpoints[0],
|
|
|
|
BackendProxyEndpoint: ¶ms.Body.BackendProxyEndpoint,
|
2022-11-30 19:45:57 +01:00
|
|
|
Reserved: reserved,
|
2022-11-22 17:35:25 +01:00
|
|
|
}, tx)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error creating service record: %v", err)
|
|
|
|
return service.NewShareInternalServerError()
|
|
|
|
}
|
2022-12-01 22:14:42 +01:00
|
|
|
|
2022-11-22 17:35:25 +01:00
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
logrus.Errorf("error committing service record: %v", err)
|
|
|
|
return service.NewShareInternalServerError()
|
|
|
|
}
|
2022-11-29 22:41:18 +01:00
|
|
|
logrus.Infof("recorded service '%v' with id '%v' for '%v'", svcToken, sid, principal.Email)
|
2022-11-22 17:35:25 +01:00
|
|
|
|
|
|
|
return service.NewShareCreated().WithPayload(&rest_model_zrok.ShareResponse{
|
2022-12-06 18:00:25 +01:00
|
|
|
FrontendProxyEndpoints: frontendEndpoints,
|
|
|
|
SvcToken: svcToken,
|
2022-11-22 17:35:25 +01:00
|
|
|
})
|
2022-09-15 21:18:37 +02:00
|
|
|
}
|