2022-11-23 18:24:35 +01:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/go-openapi/runtime/middleware"
|
|
|
|
"github.com/openziti-test-kitchen/zrok/controller/store"
|
2022-12-14 20:40:45 +01:00
|
|
|
"github.com/openziti-test-kitchen/zrok/controller/zrokEdgeSdk"
|
2022-11-23 18:24:35 +01:00
|
|
|
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
|
|
|
|
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/service"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type accessHandler struct{}
|
|
|
|
|
|
|
|
func newAccessHandler() *accessHandler {
|
|
|
|
return &accessHandler{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *accessHandler) Handle(params service.AccessParams, principal *rest_model_zrok.Principal) middleware.Responder {
|
|
|
|
tx, err := str.Begin()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error starting transaction: %v", err)
|
|
|
|
return service.NewAccessInternalServerError()
|
|
|
|
}
|
|
|
|
defer func() { _ = tx.Rollback() }()
|
|
|
|
|
2022-11-30 18:46:19 +01:00
|
|
|
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 service.NewAccessUnauthorized()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logrus.Errorf("error finding environments for account '%v'", principal.Email)
|
2022-11-29 19:07:49 +01:00
|
|
|
return service.NewAccessNotFound()
|
2022-11-23 18:24:35 +01:00
|
|
|
}
|
|
|
|
|
2022-11-30 18:46:19 +01:00
|
|
|
svcToken := params.Body.SvcToken
|
|
|
|
ssvc, err := str.FindServiceWithToken(svcToken, tx)
|
2022-11-23 18:24:35 +01:00
|
|
|
if err != nil {
|
2022-11-29 19:07:49 +01:00
|
|
|
logrus.Errorf("error finding service")
|
|
|
|
return service.NewAccessNotFound()
|
2022-11-23 18:24:35 +01:00
|
|
|
}
|
|
|
|
if ssvc == nil {
|
2022-11-30 18:46:19 +01:00
|
|
|
logrus.Errorf("unable to find service '%v' for user '%v'", svcToken, principal.Email)
|
2022-11-23 18:24:35 +01:00
|
|
|
return service.NewAccessNotFound()
|
|
|
|
}
|
|
|
|
|
2022-11-30 17:52:48 +01:00
|
|
|
feToken, err := createToken()
|
2022-11-23 18:24:35 +01:00
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
return service.NewAccessInternalServerError()
|
|
|
|
}
|
|
|
|
|
2022-11-30 17:52:48 +01:00
|
|
|
if _, err := str.CreateFrontend(envId, &store.Frontend{Token: feToken, ZId: envZId}, tx); err != nil {
|
2022-11-28 19:33:59 +01:00
|
|
|
logrus.Errorf("error creating frontend record: %v", err)
|
|
|
|
return service.NewAccessInternalServerError()
|
|
|
|
}
|
|
|
|
|
|
|
|
edge, err := edgeClient()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
return service.NewAccessInternalServerError()
|
|
|
|
}
|
2022-12-14 23:17:19 +01:00
|
|
|
addlTags := map[string]interface{}{
|
2022-11-28 19:33:59 +01:00
|
|
|
"zrokEnvironmentZId": envZId,
|
2022-11-30 17:52:48 +01:00
|
|
|
"zrokFrontendToken": feToken,
|
2022-12-14 23:17:19 +01:00
|
|
|
"zrokServiceToken": svcToken,
|
|
|
|
}
|
2022-12-21 20:31:10 +01:00
|
|
|
if err := zrokEdgeSdk.CreateServicePolicyDial(envZId+"-"+ssvc.ZId+"-dial", ssvc.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 service.NewAccessInternalServerError()
|
|
|
|
}
|
|
|
|
|
2022-11-28 19:33:59 +01:00
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
logrus.Errorf("error committing frontend record: %v", err)
|
|
|
|
return service.NewAccessInternalServerError()
|
|
|
|
}
|
|
|
|
|
2022-11-30 18:46:19 +01:00
|
|
|
return service.NewAccessCreated().WithPayload(&rest_model_zrok.AccessResponse{FrontendToken: feToken})
|
2022-11-23 18:24:35 +01:00
|
|
|
}
|