zrok/controller/access.go

91 lines
2.7 KiB
Go
Raw Normal View History

2022-11-23 18:24:35 +01:00
package controller
import (
"github.com/go-openapi/runtime/middleware"
"github.com/openziti/zrok/controller/store"
"github.com/openziti/zrok/controller/zrokEdgeSdk"
"github.com/openziti/zrok/rest_model_zrok"
"github.com/openziti/zrok/rest_server_zrok/operations/share"
2022-11-23 18:24:35 +01:00
"github.com/sirupsen/logrus"
)
type accessHandler struct{}
func newAccessHandler() *accessHandler {
return &accessHandler{}
}
func (h *accessHandler) Handle(params share.AccessParams, principal *rest_model_zrok.Principal) middleware.Responder {
2022-11-23 18:24:35 +01:00
tx, err := str.Begin()
if err != nil {
logrus.Errorf("error starting transaction: %v", err)
return share.NewAccessInternalServerError()
2022-11-23 18:24:35 +01:00
}
defer func() { _ = tx.Rollback() }()
envZId := params.Body.EnvZID
2022-11-23 18:24:35 +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 share.NewAccessUnauthorized()
2022-11-23 18:24:35 +01:00
}
} else {
logrus.Errorf("error finding environments for account '%v'", principal.Email)
return share.NewAccessNotFound()
2022-11-23 18:24:35 +01:00
}
2023-01-04 20:21:23 +01:00
shrToken := params.Body.ShrToken
sshr, err := str.FindShareWithToken(shrToken, tx)
2022-11-23 18:24:35 +01:00
if err != nil {
2023-01-04 20:21:23 +01:00
logrus.Errorf("error finding share")
return share.NewAccessNotFound()
2022-11-23 18:24:35 +01:00
}
if sshr == nil {
2023-01-04 20:21:23 +01:00
logrus.Errorf("unable to find share '%v' for user '%v'", shrToken, principal.Email)
return share.NewAccessNotFound()
2022-11-23 18:24:35 +01:00
}
feToken, err := createToken()
2022-11-23 18:24:35 +01:00
if err != nil {
logrus.Error(err)
return share.NewAccessInternalServerError()
2022-11-23 18:24:35 +01:00
}
if _, err := str.CreateFrontend(envId, &store.Frontend{Token: feToken, ZId: envZId}, tx); err != nil {
logrus.Errorf("error creating frontend record: %v", err)
return share.NewAccessInternalServerError()
}
edge, err := edgeClient()
if err != nil {
logrus.Error(err)
return share.NewAccessInternalServerError()
}
addlTags := map[string]interface{}{
"zrokEnvironmentZId": envZId,
"zrokFrontendToken": feToken,
2023-01-04 20:21:23 +01:00
"zrokShareToken": shrToken,
}
if err := zrokEdgeSdk.CreateServicePolicyDial(envZId+"-"+sshr.ZId+"-dial", sshr.ZId, []string{envZId}, addlTags, edge); err != nil {
2022-11-23 18:24:35 +01:00
logrus.Errorf("unable to create dial policy: %v", err)
return share.NewAccessInternalServerError()
2022-11-23 18:24:35 +01:00
}
if err := tx.Commit(); err != nil {
logrus.Errorf("error committing frontend record: %v", err)
return share.NewAccessInternalServerError()
}
return share.NewAccessCreated().WithPayload(&rest_model_zrok.AccessResponse{FrontendToken: feToken})
2022-11-23 18:24:35 +01:00
}