zrok/controller/share.go

161 lines
4.7 KiB
Go
Raw Normal View History

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"
"github.com/openziti/zrok/controller/store"
"github.com/openziti/zrok/rest_model_zrok"
"github.com/openziti/zrok/rest_server_zrok/operations/share"
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-01-13 16:34:21 +01:00
type shareHandler struct {
cfg *LimitsConfig
}
2023-01-13 16:34:21 +01:00
func newShareHandler(cfg *LimitsConfig) *shareHandler {
return &shareHandler{cfg: cfg}
}
func (h *shareHandler) Handle(params share.ShareParams, principal *rest_model_zrok.Principal) middleware.Responder {
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 share.NewShareInternalServerError()
2022-11-22 17:35:25 +01:00
}
defer func() { _ = tx.Rollback() }()
envZId := params.Body.EnvZID
2022-11-22 17:35:25 +01:00
envId := 0
2023-01-13 16:34:21 +01:00
envs, err := str.FindEnvironmentsForAccount(int(principal.ID), tx)
if err == nil {
2022-11-22 17:35:25 +01:00
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 share.NewShareUnauthorized()
2022-11-22 17:35:25 +01:00
}
} else {
logrus.Errorf("error finding environments for account '%v'", principal.Email)
return share.NewShareInternalServerError()
2022-11-22 17:35:25 +01:00
}
2023-01-13 16:34:21 +01:00
if err := h.checkLimits(principal, envs, tx); err != nil {
logrus.Errorf("limits error: %v", err)
return share.NewShareUnauthorized()
}
2022-11-22 17:35:25 +01:00
edge, err := edgeClient()
if err != nil {
logrus.Error(err)
return share.NewShareInternalServerError()
2022-11-22 17:35:25 +01:00
}
shrToken, err := createShareToken()
2022-11-22 17:35:25 +01:00
if err != nil {
logrus.Error(err)
return share.NewShareInternalServerError()
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
switch params.Body.ShareMode {
case "public":
if len(params.Body.FrontendSelection) < 1 {
logrus.Info("no frontend selection provided")
return share.NewShareNotFound()
}
var frontendZIds []string
var frontendTemplates []string
for _, frontendSelection := range params.Body.FrontendSelection {
sfe, err := str.FindFrontendPubliclyNamed(frontendSelection, tx)
if err != nil {
logrus.Error(err)
return share.NewShareNotFound()
}
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)
}
}
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)
return share.NewShareInternalServerError()
2022-11-22 21:31:02 +01:00
}
case "private":
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)
return share.NewShareInternalServerError()
2022-11-22 17:35:25 +01:00
}
default:
2022-11-22 17:35:25 +01:00
logrus.Errorf("unknown share mode '%v", params.Body.ShareMode)
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
2022-11-30 19:45:57 +01:00
reserved := params.Body.Reserved
sshr := &store.Share{
2023-01-04 20:21:23 +01:00
ZId: shrZId,
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: &params.Body.BackendProxyEndpoint,
2022-11-30 19:45:57 +01:00
Reserved: reserved,
}
if len(frontendEndpoints) > 0 {
sshr.FrontendEndpoint = &frontendEndpoints[0]
} else if sshr.ShareMode == "private" {
sshr.FrontendEndpoint = &sshr.ShareMode
}
sid, err := str.CreateShare(envId, sshr, tx)
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)
return share.NewShareInternalServerError()
2022-11-22 17:35:25 +01:00
}
2022-12-01 22:14:42 +01:00
2022-11-22 17:35:25 +01:00
if err := tx.Commit(); err != nil {
2023-01-04 20:21:23 +01:00
logrus.Errorf("error committing share record: %v", err)
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
return share.NewShareCreated().WithPayload(&rest_model_zrok.ShareResponse{
FrontendProxyEndpoints: frontendEndpoints,
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
func (h *shareHandler) checkLimits(principal *rest_model_zrok.Principal, envs []*store.Environment, tx *sqlx.Tx) error {
2023-01-13 19:16:10 +01:00
if !principal.Limitless && h.cfg.Shares > Unlimited {
2023-01-13 16:34:21 +01:00
total := 0
for i := range envs {
shrs, err := str.FindSharesForEnvironment(envs[i].Id, tx)
if err != nil {
return errors.Errorf("unable to find shares for environment '%v': %v", envs[i].ZId, err)
}
total += len(shrs)
if total+1 > h.cfg.Shares {
return errors.Errorf("would exceed shares limit of %d for '%v'", h.cfg.Shares, principal.Email)
}
}
}
return nil
}