2022-07-26 22:21:49 +02:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/go-openapi/runtime/middleware"
|
2023-01-13 16:34:21 +01:00
|
|
|
"github.com/jmoiron/sqlx"
|
2023-01-13 21:01:34 +01:00
|
|
|
"github.com/openziti/zrok/controller/store"
|
2023-03-07 20:31:39 +01:00
|
|
|
"github.com/openziti/zrok/controller/zrokEdgeSdk"
|
2023-01-13 21:01:34 +01:00
|
|
|
"github.com/openziti/zrok/rest_model_zrok"
|
|
|
|
"github.com/openziti/zrok/rest_server_zrok/operations/share"
|
2023-11-21 20:27:17 +01:00
|
|
|
"github.com/openziti/zrok/sdk/golang/sdk"
|
2023-12-08 18:03:07 +01:00
|
|
|
"github.com/openziti/zrok/util"
|
2023-01-13 16:34:21 +01:00
|
|
|
"github.com/pkg/errors"
|
2022-11-22 17:35:25 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-07-26 22:21:49 +02:00
|
|
|
)
|
|
|
|
|
2023-03-21 21:34:45 +01:00
|
|
|
type shareHandler struct{}
|
2022-08-09 17:49:40 +02:00
|
|
|
|
2023-03-21 21:34:45 +01:00
|
|
|
func newShareHandler() *shareHandler {
|
|
|
|
return &shareHandler{}
|
2022-08-09 17:49:40 +02:00
|
|
|
}
|
|
|
|
|
2023-01-04 19:43:37 +01:00
|
|
|
func (h *shareHandler) Handle(params share.ShareParams, principal *rest_model_zrok.Principal) middleware.Responder {
|
2023-03-21 21:34:45 +01:00
|
|
|
trx, err := str.Begin()
|
2022-11-22 17:35:25 +01:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error starting transaction: %v", err)
|
2023-01-04 19:43:37 +01:00
|
|
|
return share.NewShareInternalServerError()
|
2022-11-22 17:35:25 +01:00
|
|
|
}
|
2023-03-21 21:34:45 +01:00
|
|
|
defer func() { _ = trx.Rollback() }()
|
2022-11-22 17:35:25 +01:00
|
|
|
|
2022-11-29 22:41:18 +01:00
|
|
|
envZId := params.Body.EnvZID
|
2022-11-22 17:35:25 +01:00
|
|
|
envId := 0
|
2023-03-21 21:34:45 +01:00
|
|
|
envs, err := str.FindEnvironmentsForAccount(int(principal.ID), trx)
|
2023-01-13 16:34:21 +01:00
|
|
|
if err == nil {
|
2022-11-22 17:35:25 +01:00
|
|
|
found := false
|
|
|
|
for _, env := range envs {
|
|
|
|
if env.ZId == envZId {
|
2023-12-08 18:03:07 +01:00
|
|
|
logrus.Debugf("found identity '%v' for account '%v'", envZId, principal.Email)
|
2022-11-22 17:35:25 +01:00
|
|
|
envId = env.Id
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2023-12-08 18:03:07 +01:00
|
|
|
logrus.Errorf("environment '%v' not found for account '%v'", envZId, principal.Email)
|
2023-01-04 19:43:37 +01:00
|
|
|
return share.NewShareUnauthorized()
|
2022-11-22 17:35:25 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logrus.Errorf("error finding environments for account '%v'", principal.Email)
|
2023-01-04 19:43:37 +01:00
|
|
|
return share.NewShareInternalServerError()
|
2022-11-22 17:35:25 +01:00
|
|
|
}
|
|
|
|
|
2023-03-29 19:29:12 +02:00
|
|
|
if err := h.checkLimits(envId, principal, trx); err != nil {
|
2023-01-13 16:34:21 +01:00
|
|
|
logrus.Errorf("limits error: %v", err)
|
|
|
|
return share.NewShareUnauthorized()
|
|
|
|
}
|
|
|
|
|
2023-03-07 20:31:39 +01:00
|
|
|
edge, err := zrokEdgeSdk.Client(cfg.Ziti)
|
2022-11-22 17:35:25 +01:00
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
2023-01-04 19:43:37 +01:00
|
|
|
return share.NewShareInternalServerError()
|
2022-11-22 17:35:25 +01:00
|
|
|
}
|
2023-12-08 18:03:07 +01:00
|
|
|
|
|
|
|
reserved := params.Body.Reserved
|
|
|
|
uniqueName := params.Body.UniqueName
|
2023-01-04 19:43:37 +01:00
|
|
|
shrToken, err := createShareToken()
|
2022-11-22 17:35:25 +01:00
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
2023-01-04 19:43:37 +01:00
|
|
|
return share.NewShareInternalServerError()
|
2022-11-22 17:35:25 +01:00
|
|
|
}
|
2023-12-08 18:03:07 +01:00
|
|
|
if reserved && uniqueName != "" {
|
|
|
|
if !util.IsValidUniqueName(uniqueName) {
|
|
|
|
logrus.Errorf("invalid unique name '%v' for account '%v'", uniqueName, principal.Email)
|
|
|
|
return share.NewShareUnprocessableEntity()
|
|
|
|
}
|
|
|
|
shrToken = uniqueName
|
|
|
|
}
|
2022-11-22 17:35:25 +01:00
|
|
|
|
2023-01-04 20:21:23 +01:00
|
|
|
var shrZId string
|
2022-11-22 17:35:25 +01:00
|
|
|
var frontendEndpoints []string
|
2022-11-21 19:45:08 +01:00
|
|
|
switch params.Body.ShareMode {
|
2023-07-17 22:21:29 +02:00
|
|
|
case string(sdk.PublicShareMode):
|
2022-12-06 18:00:25 +01:00
|
|
|
if len(params.Body.FrontendSelection) < 1 {
|
|
|
|
logrus.Info("no frontend selection provided")
|
2023-01-04 19:43:37 +01:00
|
|
|
return share.NewShareNotFound()
|
2022-12-06 18:00:25 +01:00
|
|
|
}
|
|
|
|
|
2022-12-06 17:48:59 +01:00
|
|
|
var frontendZIds []string
|
|
|
|
var frontendTemplates []string
|
|
|
|
for _, frontendSelection := range params.Body.FrontendSelection {
|
2023-03-21 21:34:45 +01:00
|
|
|
sfe, err := str.FindFrontendPubliclyNamed(frontendSelection, trx)
|
2022-12-06 17:48:59 +01:00
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
2023-01-04 19:43:37 +01:00
|
|
|
return share.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)
|
2023-01-04 20:21:23 +01:00
|
|
|
logrus.Infof("added frontend selection '%v' with ziti identity '%v' for share '%v'", frontendSelection, sfe.ZId, shrToken)
|
2022-12-06 17:48:59 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-04 20:21:23 +01:00
|
|
|
shrZId, frontendEndpoints, err = newPublicResourceAllocator().allocate(envZId, shrToken, frontendZIds, frontendTemplates, params, edge)
|
2022-11-22 21:31:02 +01:00
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
2023-01-04 19:43:37 +01:00
|
|
|
return share.NewShareInternalServerError()
|
2022-11-22 21:31:02 +01:00
|
|
|
}
|
|
|
|
|
2023-07-17 22:21:29 +02:00
|
|
|
case string(sdk.PrivateShareMode):
|
2023-01-04 20:21:23 +01:00
|
|
|
shrZId, frontendEndpoints, err = newPrivateResourceAllocator().allocate(envZId, shrToken, params, edge)
|
2022-11-22 17:35:25 +01:00
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
2023-01-04 19:43:37 +01:00
|
|
|
return share.NewShareInternalServerError()
|
2022-11-22 17:35:25 +01:00
|
|
|
}
|
|
|
|
|
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)
|
2023-01-04 19:43:37 +01:00
|
|
|
return share.NewShareInternalServerError()
|
2022-07-27 15:49:54 +02:00
|
|
|
}
|
2022-11-22 17:35:25 +01:00
|
|
|
|
2023-01-04 20:21:23 +01:00
|
|
|
logrus.Debugf("allocated share '%v'", shrToken)
|
2022-11-22 17:35:25 +01:00
|
|
|
|
2023-01-04 19:13:50 +01:00
|
|
|
sshr := &store.Share{
|
2023-01-04 20:21:23 +01:00
|
|
|
ZId: shrZId,
|
2023-01-04 19:43:37 +01:00
|
|
|
Token: shrToken,
|
2022-11-22 17:39:00 +01:00
|
|
|
ShareMode: params.Body.ShareMode,
|
|
|
|
BackendMode: params.Body.BackendMode,
|
2022-11-22 17:35:25 +01:00
|
|
|
BackendProxyEndpoint: ¶ms.Body.BackendProxyEndpoint,
|
2022-11-30 19:45:57 +01:00
|
|
|
Reserved: reserved,
|
2022-12-13 18:24:28 +01:00
|
|
|
}
|
2023-03-27 19:51:48 +02:00
|
|
|
if len(params.Body.FrontendSelection) > 0 {
|
|
|
|
sshr.FrontendSelection = ¶ms.Body.FrontendSelection[0]
|
|
|
|
}
|
2022-12-13 18:24:28 +01:00
|
|
|
if len(frontendEndpoints) > 0 {
|
2023-01-04 19:13:50 +01:00
|
|
|
sshr.FrontendEndpoint = &frontendEndpoints[0]
|
2023-07-17 22:21:29 +02:00
|
|
|
} else if sshr.ShareMode == string(sdk.PrivateShareMode) {
|
2023-01-04 19:13:50 +01:00
|
|
|
sshr.FrontendEndpoint = &sshr.ShareMode
|
2022-12-13 18:24:28 +01:00
|
|
|
}
|
|
|
|
|
2024-01-24 16:48:27 +01:00
|
|
|
sh, err := str.FindShareWithToken(sshr.Token, trx)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error checking share token collision: %v", err)
|
|
|
|
return share.NewShareInternalServerError()
|
|
|
|
}
|
|
|
|
if sh != nil {
|
|
|
|
logrus.Errorf("attempting to create share with conflcting token: %v", sshr.Token)
|
|
|
|
return share.NewShareConflict()
|
|
|
|
}
|
|
|
|
|
2023-03-21 21:34:45 +01:00
|
|
|
sid, err := str.CreateShare(envId, sshr, trx)
|
2022-11-22 17:35:25 +01:00
|
|
|
if err != nil {
|
2023-01-04 20:21:23 +01:00
|
|
|
logrus.Errorf("error creating share record: %v", err)
|
2023-01-04 19:43:37 +01:00
|
|
|
return share.NewShareInternalServerError()
|
2022-11-22 17:35:25 +01:00
|
|
|
}
|
2022-12-01 22:14:42 +01:00
|
|
|
|
2023-03-21 21:34:45 +01:00
|
|
|
if err := trx.Commit(); err != nil {
|
2023-01-04 20:21:23 +01:00
|
|
|
logrus.Errorf("error committing share record: %v", err)
|
2023-01-04 19:43:37 +01:00
|
|
|
return share.NewShareInternalServerError()
|
2022-11-22 17:35:25 +01:00
|
|
|
}
|
2023-01-04 20:21:23 +01:00
|
|
|
logrus.Infof("recorded share '%v' with id '%v' for '%v'", shrToken, sid, principal.Email)
|
2022-11-22 17:35:25 +01:00
|
|
|
|
2023-01-04 19:43:37 +01:00
|
|
|
return share.NewShareCreated().WithPayload(&rest_model_zrok.ShareResponse{
|
2022-12-06 18:00:25 +01:00
|
|
|
FrontendProxyEndpoints: frontendEndpoints,
|
2023-01-04 19:43:37 +01:00
|
|
|
ShrToken: shrToken,
|
2022-11-22 17:35:25 +01:00
|
|
|
})
|
2022-09-15 21:18:37 +02:00
|
|
|
}
|
2023-01-13 16:34:21 +01:00
|
|
|
|
2023-03-29 19:29:12 +02:00
|
|
|
func (h *shareHandler) checkLimits(envId int, principal *rest_model_zrok.Principal, trx *sqlx.Tx) error {
|
2023-03-21 21:34:45 +01:00
|
|
|
if !principal.Limitless {
|
|
|
|
if limitsAgent != nil {
|
2023-03-29 19:29:12 +02:00
|
|
|
ok, err := limitsAgent.CanCreateShare(int(principal.ID), envId, trx)
|
2023-01-13 16:34:21 +01:00
|
|
|
if err != nil {
|
2023-03-21 21:34:45 +01:00
|
|
|
return errors.Wrapf(err, "error checking share limits for '%v'", principal.Email)
|
2023-01-13 16:34:21 +01:00
|
|
|
}
|
2023-03-21 21:34:45 +01:00
|
|
|
if !ok {
|
|
|
|
return errors.Errorf("share limit check failed for '%v'", principal.Email)
|
2023-01-13 16:34:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|