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-07-27 18:55:51 +02:00
|
|
|
"github.com/openziti/edge/rest_management_api_client/service"
|
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 {
|
|
|
|
}
|
|
|
|
|
2022-10-19 19:20:47 +02:00
|
|
|
func newUntunnelHandler() *untunnelHandler {
|
|
|
|
return &untunnelHandler{}
|
2022-08-12 17:03:15 +02:00
|
|
|
}
|
|
|
|
|
2022-11-08 21:28:13 +01:00
|
|
|
func (h *untunnelHandler) Handle(params tunnel.UntunnelParams, principal *rest_model_zrok.Principal) middleware.Responder {
|
2022-08-01 21:44:26 +02:00
|
|
|
tx, err := str.Begin()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error starting transaction: %v", err)
|
2022-09-26 22:35:06 +02:00
|
|
|
return tunnel.NewUntunnelInternalServerError()
|
2022-08-01 21:44:26 +02:00
|
|
|
}
|
|
|
|
defer func() { _ = tx.Rollback() }()
|
|
|
|
|
2022-10-19 19:20:47 +02:00
|
|
|
edge, err := edgeClient()
|
2022-07-27 18:17:37 +02:00
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
2022-09-26 22:35:06 +02:00
|
|
|
return tunnel.NewUntunnelInternalServerError()
|
2022-07-27 18:17:37 +02:00
|
|
|
}
|
2022-10-19 18:35:29 +02:00
|
|
|
svcName := params.Body.SvcName
|
2022-11-08 21:28:13 +01:00
|
|
|
svcZId, err := h.findServiceZId(svcName, edge)
|
2022-08-01 21:44:26 +02:00
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
2022-09-26 22:35:06 +02:00
|
|
|
return tunnel.NewUntunnelInternalServerError()
|
2022-08-01 21:44:26 +02:00
|
|
|
}
|
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 {
|
2022-10-19 18:35:29 +02:00
|
|
|
if env.ZId == params.Body.ZID {
|
2022-08-03 20:58:11 +02:00
|
|
|
senv = env
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if senv == nil {
|
2022-10-19 18:35:29 +02:00
|
|
|
err := errors.Errorf("environment with id '%v' not found for '%v", params.Body.ZID, principal.Email)
|
2022-08-03 20:58:11 +02:00
|
|
|
logrus.Error(err)
|
2022-09-26 22:35:06 +02:00
|
|
|
return tunnel.NewUntunnelNotFound()
|
2022-08-03 20:58:11 +02:00
|
|
|
}
|
|
|
|
} else {
|
2022-09-09 16:20:05 +02:00
|
|
|
logrus.Errorf("error finding environments for account '%v': %v", principal.Email, err)
|
2022-09-26 22:35:06 +02:00
|
|
|
return tunnel.NewUntunnelInternalServerError()
|
2022-08-03 20:58:11 +02:00
|
|
|
}
|
|
|
|
|
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-10-19 18:10:22 +02:00
|
|
|
if svc.ZId == svcZId {
|
2022-08-01 21:44:26 +02:00
|
|
|
ssvc = svc
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ssvc == nil {
|
2022-10-19 18:10:22 +02:00
|
|
|
err := errors.Errorf("service with id '%v' not found for '%v'", svcZId, principal.Email)
|
2022-08-01 21:44:26 +02:00
|
|
|
logrus.Error(err)
|
2022-09-26 22:35:06 +02:00
|
|
|
return tunnel.NewUntunnelNotFound()
|
2022-08-01 21:44:26 +02:00
|
|
|
}
|
|
|
|
} else {
|
2022-09-09 16:20:05 +02:00
|
|
|
logrus.Errorf("error finding services for account '%v': %v", principal.Email, err)
|
2022-09-26 22:35:06 +02:00
|
|
|
return tunnel.NewUntunnelInternalServerError()
|
2022-08-01 21:44:26 +02:00
|
|
|
}
|
|
|
|
|
2022-11-08 21:07:18 +01:00
|
|
|
if err := deleteServiceEdgeRouterPolicy(senv.ZId, svcName, edge); err != nil {
|
2022-07-27 18:55:51 +02:00
|
|
|
logrus.Error(err)
|
2022-09-26 22:35:06 +02:00
|
|
|
return tunnel.NewUntunnelInternalServerError()
|
2022-07-27 18:55:51 +02:00
|
|
|
}
|
2022-11-08 21:07:18 +01:00
|
|
|
if err := deleteServicePolicyDial(senv.ZId, svcName, edge); err != nil {
|
2022-07-27 18:55:51 +02:00
|
|
|
logrus.Error(err)
|
2022-09-26 22:35:06 +02:00
|
|
|
return tunnel.NewUntunnelInternalServerError()
|
2022-07-27 18:55:51 +02:00
|
|
|
}
|
2022-11-08 21:07:18 +01:00
|
|
|
if err := deleteServicePolicyBind(senv.ZId, svcName, edge); err != nil {
|
2022-07-27 18:55:51 +02:00
|
|
|
logrus.Error(err)
|
2022-09-26 22:35:06 +02:00
|
|
|
return tunnel.NewUntunnelInternalServerError()
|
2022-07-27 18:55:51 +02:00
|
|
|
}
|
2022-11-08 21:07:18 +01:00
|
|
|
if err := deleteConfig(senv.ZId, svcName, edge); err != nil {
|
2022-08-15 21:30:26 +02:00
|
|
|
logrus.Error(err)
|
2022-09-26 22:35:06 +02:00
|
|
|
return tunnel.NewTunnelInternalServerError()
|
2022-08-15 21:30:26 +02:00
|
|
|
}
|
2022-11-08 21:07:18 +01:00
|
|
|
if err := deleteService(senv.ZId, svcZId, edge); err != nil {
|
2022-07-27 18:55:51 +02:00
|
|
|
logrus.Error(err)
|
2022-09-26 22:35:06 +02:00
|
|
|
return tunnel.NewUntunnelInternalServerError()
|
2022-07-27 18:17:37 +02:00
|
|
|
}
|
|
|
|
|
2022-11-08 21:25:27 +01:00
|
|
|
logrus.Debugf("deallocated service '%v'", svcName)
|
2022-07-27 18:55:51 +02:00
|
|
|
|
2022-10-19 21:21:15 +02:00
|
|
|
if err := str.DeleteService(ssvc.Id, tx); err != nil {
|
2022-10-19 18:10:22 +02:00
|
|
|
logrus.Errorf("error deactivating service '%v': %v", svcZId, err)
|
2022-09-26 22:35:06 +02:00
|
|
|
return tunnel.NewUntunnelInternalServerError()
|
2022-07-29 22:21:47 +02:00
|
|
|
}
|
2022-08-01 21:44:26 +02:00
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
logrus.Errorf("error committing: %v", err)
|
2022-09-26 22:35:06 +02:00
|
|
|
return tunnel.NewUntunnelInternalServerError()
|
2022-07-29 22:21:47 +02:00
|
|
|
}
|
|
|
|
|
2022-07-27 18:17:37 +02:00
|
|
|
return tunnel.NewUntunnelOK()
|
|
|
|
}
|
|
|
|
|
2022-11-08 21:28:13 +01:00
|
|
|
func (h *untunnelHandler) findServiceZId(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)
|
|
|
|
}
|