2022-07-27 18:17:37 +02:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"github.com/go-openapi/runtime/middleware"
|
2022-08-01 21:44:26 +02:00
|
|
|
"github.com/openziti-test-kitchen/zrok/controller/store"
|
2022-07-27 19:38:35 +02:00
|
|
|
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
|
2022-07-27 18:17:37 +02:00
|
|
|
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/tunnel"
|
|
|
|
"github.com/openziti/edge/rest_management_api_client"
|
2022-08-15 21:30:26 +02:00
|
|
|
"github.com/openziti/edge/rest_management_api_client/config"
|
2022-07-27 18:55:51 +02:00
|
|
|
"github.com/openziti/edge/rest_management_api_client/service"
|
|
|
|
"github.com/openziti/edge/rest_management_api_client/service_edge_router_policy"
|
|
|
|
"github.com/openziti/edge/rest_management_api_client/service_policy"
|
2022-08-01 21:44:26 +02:00
|
|
|
"github.com/pkg/errors"
|
2022-07-27 18:17:37 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-08-12 17:03:15 +02:00
|
|
|
type untunnelHandler struct {
|
|
|
|
cfg *Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func newUntunnelHandler(cfg *Config) *untunnelHandler {
|
|
|
|
return &untunnelHandler{cfg: cfg}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *untunnelHandler) Handle(params tunnel.UntunnelParams, principal *rest_model_zrok.Principal) middleware.Responder {
|
2022-07-28 20:32:49 +02:00
|
|
|
logrus.Infof("untunneling for '%v' (%v)", principal.Username, principal.Token)
|
2022-07-29 22:21:47 +02:00
|
|
|
|
2022-08-01 21:44:26 +02:00
|
|
|
tx, err := str.Begin()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error starting transaction: %v", err)
|
|
|
|
return tunnel.NewUntunnelInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
|
|
|
|
}
|
|
|
|
defer func() { _ = tx.Rollback() }()
|
|
|
|
|
2022-08-12 17:03:15 +02:00
|
|
|
edge, err := edgeClient(self.cfg.Ziti)
|
2022-07-27 18:17:37 +02:00
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
2022-07-29 22:21:47 +02:00
|
|
|
return tunnel.NewUntunnelInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
|
2022-07-27 18:17:37 +02:00
|
|
|
}
|
2022-07-27 18:55:51 +02:00
|
|
|
svcName := params.Body.Service
|
2022-08-12 17:03:15 +02:00
|
|
|
svcId, err := self.findServiceId(svcName, edge)
|
2022-08-01 21:44:26 +02:00
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
return tunnel.NewUntunnelInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
|
|
|
|
}
|
2022-08-03 20:58:11 +02:00
|
|
|
var senv *store.Environment
|
|
|
|
if envs, err := str.FindEnvironmentsForAccount(int(principal.ID), tx); err == nil {
|
|
|
|
for _, env := range envs {
|
|
|
|
if env.ZitiIdentityId == params.Body.ZitiIdentityID {
|
|
|
|
senv = env
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if senv == nil {
|
|
|
|
err := errors.Errorf("environment with id '%v' not found for '%v", params.Body.ZitiIdentityID, principal.Username)
|
|
|
|
logrus.Error(err)
|
|
|
|
return tunnel.NewUntunnelNotFound().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logrus.Errorf("error finding environments for account '%v': %v", principal.Username, err)
|
|
|
|
return tunnel.NewUntunnelInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
|
|
|
|
}
|
|
|
|
|
2022-08-01 21:44:26 +02:00
|
|
|
var ssvc *store.Service
|
2022-08-03 20:58:11 +02:00
|
|
|
if svcs, err := str.FindServicesForEnvironment(senv.Id, tx); err == nil {
|
2022-08-01 21:44:26 +02:00
|
|
|
for _, svc := range svcs {
|
2022-08-03 19:43:54 +02:00
|
|
|
if svc.ZitiServiceId == svcId {
|
2022-08-01 21:44:26 +02:00
|
|
|
ssvc = svc
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ssvc == nil {
|
|
|
|
err := errors.Errorf("service with id '%v' not found for '%v'", svcId, principal.Username)
|
|
|
|
logrus.Error(err)
|
|
|
|
return tunnel.NewUntunnelNotFound().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
|
|
|
|
}
|
|
|
|
} else {
|
2022-08-03 20:58:11 +02:00
|
|
|
logrus.Errorf("error finding services for account '%v': %v", principal.Username, err)
|
2022-08-01 21:44:26 +02:00
|
|
|
return tunnel.NewUntunnelInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
|
|
|
|
}
|
|
|
|
|
2022-08-12 17:03:15 +02:00
|
|
|
if err := self.deleteServiceEdgeRouterPolicy(svcName, edge); err != nil {
|
2022-07-27 18:55:51 +02:00
|
|
|
logrus.Error(err)
|
2022-07-29 22:21:47 +02:00
|
|
|
return tunnel.NewUntunnelInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
|
2022-07-27 18:55:51 +02:00
|
|
|
}
|
2022-08-12 17:03:15 +02:00
|
|
|
if err := self.deleteServicePolicyDial(svcName, edge); err != nil {
|
2022-07-27 18:55:51 +02:00
|
|
|
logrus.Error(err)
|
2022-07-29 22:21:47 +02:00
|
|
|
return tunnel.NewUntunnelInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
|
2022-07-27 18:55:51 +02:00
|
|
|
}
|
2022-08-12 17:03:15 +02:00
|
|
|
if err := self.deleteServicePolicyBind(svcName, edge); err != nil {
|
2022-07-27 18:55:51 +02:00
|
|
|
logrus.Error(err)
|
2022-07-29 22:21:47 +02:00
|
|
|
return tunnel.NewUntunnelInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
|
2022-07-27 18:55:51 +02:00
|
|
|
}
|
2022-08-15 21:30:26 +02:00
|
|
|
if err := self.deleteConfig(svcName, edge); err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
return tunnel.NewTunnelInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
|
|
|
|
}
|
2022-08-12 17:03:15 +02:00
|
|
|
if err := self.deleteService(svcId, edge); err != nil {
|
2022-07-27 18:55:51 +02:00
|
|
|
logrus.Error(err)
|
2022-07-27 19:38:35 +02:00
|
|
|
return tunnel.NewUntunnelInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
|
2022-07-27 18:17:37 +02:00
|
|
|
}
|
|
|
|
|
2022-07-27 18:55:51 +02:00
|
|
|
logrus.Infof("deallocated service '%v'", svcName)
|
|
|
|
|
2022-08-01 21:44:26 +02:00
|
|
|
ssvc.Active = false
|
|
|
|
if err := str.UpdateService(ssvc, tx); err != nil {
|
|
|
|
logrus.Errorf("error deactivating service '%v': %v", svcId, err)
|
2022-07-29 22:21:47 +02:00
|
|
|
return tunnel.NewUntunnelInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
|
|
|
|
}
|
2022-08-01 21:44:26 +02:00
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
logrus.Errorf("error committing: %v", err)
|
2022-07-29 22:21:47 +02:00
|
|
|
return tunnel.NewUntunnelInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
|
|
|
|
}
|
|
|
|
|
2022-07-27 18:17:37 +02:00
|
|
|
return tunnel.NewUntunnelOK()
|
|
|
|
}
|
|
|
|
|
2022-08-12 17:03:15 +02:00
|
|
|
func (_ *untunnelHandler) findServiceId(svcName string, edge *rest_management_api_client.ZitiEdgeManagement) (string, error) {
|
2022-08-01 21:44:26 +02:00
|
|
|
filter := fmt.Sprintf("name=\"%v\"", svcName)
|
|
|
|
limit := int64(1)
|
|
|
|
offset := int64(0)
|
|
|
|
listReq := &service.ListServicesParams{
|
|
|
|
Filter: &filter,
|
|
|
|
Limit: &limit,
|
|
|
|
Offset: &offset,
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
listReq.SetTimeout(30 * time.Second)
|
|
|
|
listResp, err := edge.Service.ListServices(listReq, nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if len(listResp.Payload.Data) == 1 {
|
|
|
|
return *(listResp.Payload.Data[0].ID), nil
|
|
|
|
}
|
|
|
|
return "", errors.Errorf("service '%v' not found", svcName)
|
|
|
|
}
|
|
|
|
|
2022-08-12 17:03:15 +02:00
|
|
|
func (_ *untunnelHandler) deleteServiceEdgeRouterPolicy(svcName string, edge *rest_management_api_client.ZitiEdgeManagement) error {
|
2022-07-27 18:55:51 +02:00
|
|
|
filter := fmt.Sprintf("name=\"%v\"", svcName)
|
|
|
|
limit := int64(1)
|
|
|
|
offset := int64(0)
|
|
|
|
listReq := &service_edge_router_policy.ListServiceEdgeRouterPoliciesParams{
|
|
|
|
Filter: &filter,
|
|
|
|
Limit: &limit,
|
|
|
|
Offset: &offset,
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
listReq.SetTimeout(30 * time.Second)
|
|
|
|
listResp, err := edge.ServiceEdgeRouterPolicy.ListServiceEdgeRouterPolicies(listReq, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(listResp.Payload.Data) == 1 {
|
|
|
|
serpId := *(listResp.Payload.Data[0].ID)
|
|
|
|
req := &service_edge_router_policy.DeleteServiceEdgeRouterPolicyParams{
|
|
|
|
ID: serpId,
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
req.SetTimeout(30 * time.Second)
|
|
|
|
_, err := edge.ServiceEdgeRouterPolicy.DeleteServiceEdgeRouterPolicy(req, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logrus.Infof("deleted service edge router policy '%v'", serpId)
|
|
|
|
} else {
|
|
|
|
logrus.Infof("did not find a service edge router policy")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-12 17:03:15 +02:00
|
|
|
func (self *untunnelHandler) deleteServicePolicyBind(svcName string, edge *rest_management_api_client.ZitiEdgeManagement) error {
|
2022-08-31 20:49:41 +02:00
|
|
|
return self.deleteServicePolicy(fmt.Sprintf("name=\"%v-backend\"", svcName), edge)
|
2022-07-27 18:55:51 +02:00
|
|
|
}
|
|
|
|
|
2022-08-12 17:03:15 +02:00
|
|
|
func (self *untunnelHandler) deleteServicePolicyDial(svcName string, edge *rest_management_api_client.ZitiEdgeManagement) error {
|
|
|
|
return self.deleteServicePolicy(fmt.Sprintf("name=\"%v-dial\"", svcName), edge)
|
2022-07-27 18:55:51 +02:00
|
|
|
}
|
|
|
|
|
2022-08-12 17:03:15 +02:00
|
|
|
func (_ *untunnelHandler) deleteServicePolicy(filter string, edge *rest_management_api_client.ZitiEdgeManagement) error {
|
2022-07-27 18:55:51 +02:00
|
|
|
limit := int64(1)
|
|
|
|
offset := int64(0)
|
|
|
|
listReq := &service_policy.ListServicePoliciesParams{
|
|
|
|
Filter: &filter,
|
|
|
|
Limit: &limit,
|
|
|
|
Offset: &offset,
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
listReq.SetTimeout(30 * time.Second)
|
|
|
|
listResp, err := edge.ServicePolicy.ListServicePolicies(listReq, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(listResp.Payload.Data) == 1 {
|
|
|
|
spId := *(listResp.Payload.Data[0].ID)
|
|
|
|
req := &service_policy.DeleteServicePolicyParams{
|
|
|
|
ID: spId,
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
req.SetTimeout(30 * time.Second)
|
|
|
|
_, err := edge.ServicePolicy.DeleteServicePolicy(req, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logrus.Infof("deleted service policy '%v'", spId)
|
|
|
|
} else {
|
|
|
|
logrus.Infof("did not find a service policy")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-15 21:30:26 +02:00
|
|
|
func (_ *untunnelHandler) deleteConfig(svcName string, edge *rest_management_api_client.ZitiEdgeManagement) error {
|
|
|
|
filter := fmt.Sprintf("name=\"%v\"", svcName)
|
|
|
|
limit := int64(0)
|
|
|
|
offset := int64(0)
|
|
|
|
listReq := &config.ListConfigsParams{
|
|
|
|
Filter: &filter,
|
|
|
|
Limit: &limit,
|
|
|
|
Offset: &offset,
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
listReq.SetTimeout(30 * time.Second)
|
|
|
|
listResp, err := edge.Config.ListConfigs(listReq, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, cfg := range listResp.Payload.Data {
|
|
|
|
deleteReq := &config.DeleteConfigParams{
|
|
|
|
ID: *cfg.ID,
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
deleteReq.SetTimeout(30 * time.Second)
|
|
|
|
_, err := edge.Config.DeleteConfig(deleteReq, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logrus.Infof("deleted config '%v'", *cfg.ID)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-12 17:03:15 +02:00
|
|
|
func (_ *untunnelHandler) deleteService(svcId string, edge *rest_management_api_client.ZitiEdgeManagement) error {
|
2022-08-01 21:44:26 +02:00
|
|
|
req := &service.DeleteServiceParams{
|
|
|
|
ID: svcId,
|
2022-07-27 18:55:51 +02:00
|
|
|
Context: context.Background(),
|
|
|
|
}
|
2022-08-01 21:44:26 +02:00
|
|
|
req.SetTimeout(30 * time.Second)
|
|
|
|
_, err := edge.Service.DeleteService(req, nil)
|
2022-07-27 18:55:51 +02:00
|
|
|
if err != nil {
|
2022-08-01 21:44:26 +02:00
|
|
|
return err
|
2022-07-27 18:55:51 +02:00
|
|
|
}
|
2022-08-01 21:44:26 +02:00
|
|
|
logrus.Infof("deleted service '%v'", svcId)
|
|
|
|
return nil
|
2022-07-27 18:55:51 +02:00
|
|
|
}
|