2022-07-27 18:17:37 +02:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"github.com/go-openapi/runtime/middleware"
|
2023-05-25 17:50:38 +02:00
|
|
|
"github.com/openziti/edge-api/rest_management_api_client"
|
|
|
|
edge_service "github.com/openziti/edge-api/rest_management_api_client/service"
|
2023-01-13 21:01:34 +01:00
|
|
|
"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-08-01 21:44:26 +02:00
|
|
|
"github.com/pkg/errors"
|
2022-07-27 18:17:37 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
2023-05-25 20:59:39 +02:00
|
|
|
"time"
|
2022-07-27 18:17:37 +02:00
|
|
|
)
|
|
|
|
|
2022-11-21 21:11:35 +01:00
|
|
|
type unshareHandler struct{}
|
2022-08-12 17:03:15 +02:00
|
|
|
|
2022-11-18 21:36:55 +01:00
|
|
|
func newUnshareHandler() *unshareHandler {
|
|
|
|
return &unshareHandler{}
|
2022-08-12 17:03:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-04 19:43:37 +01:00
|
|
|
func (h *unshareHandler) Handle(params share.UnshareParams, principal *rest_model_zrok.Principal) middleware.Responder {
|
2022-08-01 21:44:26 +02:00
|
|
|
tx, err := str.Begin()
|
|
|
|
if err != nil {
|
2023-01-30 17:38:55 +01:00
|
|
|
logrus.Errorf("error starting transaction for '%v': %v", principal.Email, err)
|
2023-01-04 19:43:37 +01:00
|
|
|
return share.NewUnshareInternalServerError()
|
2022-08-01 21:44:26 +02:00
|
|
|
}
|
|
|
|
defer func() { _ = tx.Rollback() }()
|
|
|
|
|
2023-03-07 20:31:39 +01:00
|
|
|
edge, err := zrokEdgeSdk.Client(cfg.Ziti)
|
2022-07-27 18:17:37 +02:00
|
|
|
if err != nil {
|
2023-01-30 17:38:55 +01:00
|
|
|
logrus.Errorf("error getting edge client for '%v': %v", principal.Email, err)
|
2023-01-04 19:43:37 +01:00
|
|
|
return share.NewUnshareInternalServerError()
|
2022-07-27 18:17:37 +02:00
|
|
|
}
|
2023-01-04 19:43:37 +01:00
|
|
|
shrToken := params.Body.ShrToken
|
2023-01-04 20:42:58 +01:00
|
|
|
shrZId, err := h.findShareZId(shrToken, edge)
|
2022-08-01 21:44:26 +02:00
|
|
|
if err != nil {
|
2023-01-30 17:38:55 +01:00
|
|
|
logrus.Errorf("error finding share identity for '%v' (%v): %v", shrToken, principal.Email, err)
|
2023-01-04 19:43:37 +01:00
|
|
|
return share.NewUnshareNotFound()
|
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-11-30 18:46:19 +01:00
|
|
|
if env.ZId == params.Body.EnvZID {
|
2022-08-03 20:58:11 +02:00
|
|
|
senv = env
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if senv == nil {
|
2023-01-30 17:38:55 +01:00
|
|
|
logrus.Errorf("environment with id '%v' not found for '%v", params.Body.EnvZID, principal.Email)
|
2023-01-04 19:43:37 +01:00
|
|
|
return share.NewUnshareNotFound()
|
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)
|
2023-01-04 19:43:37 +01:00
|
|
|
return share.NewUnshareNotFound()
|
2022-08-03 20:58:11 +02:00
|
|
|
}
|
|
|
|
|
2023-01-04 19:13:50 +01:00
|
|
|
var sshr *store.Share
|
2023-01-04 20:21:23 +01:00
|
|
|
if shrs, err := str.FindSharesForEnvironment(senv.Id, tx); err == nil {
|
|
|
|
for _, shr := range shrs {
|
2023-01-04 20:42:58 +01:00
|
|
|
if shr.ZId == shrZId {
|
2023-01-04 20:21:23 +01:00
|
|
|
sshr = shr
|
2022-08-01 21:44:26 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2023-01-04 19:13:50 +01:00
|
|
|
if sshr == nil {
|
2023-01-30 17:38:55 +01:00
|
|
|
logrus.Errorf("share with id '%v' not found for '%v'", shrZId, principal.Email)
|
2023-01-04 19:43:37 +01:00
|
|
|
return share.NewUnshareNotFound()
|
2022-08-01 21:44:26 +02:00
|
|
|
}
|
|
|
|
} else {
|
2023-01-04 20:21:23 +01:00
|
|
|
logrus.Errorf("error finding shares for account '%v': %v", principal.Email, err)
|
2023-01-04 19:43:37 +01:00
|
|
|
return share.NewUnshareInternalServerError()
|
2022-08-01 21:44:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-04 19:13:50 +01:00
|
|
|
if sshr.Reserved == params.Body.Reserved {
|
2023-01-04 20:21:23 +01:00
|
|
|
// single tag-based share deallocator; should work regardless of sharing mode
|
2024-01-17 17:02:34 +01:00
|
|
|
h.deallocateResources(senv, shrToken, shrZId, edge)
|
2023-01-04 20:21:23 +01:00
|
|
|
logrus.Debugf("deallocated share '%v'", shrToken)
|
2022-07-27 18:55:51 +02:00
|
|
|
|
2024-03-05 18:51:06 +01:00
|
|
|
if err := str.DeleteAccessGrantsForShare(sshr.Id, tx); err != nil {
|
|
|
|
logrus.Errorf("error deleting access grants for share '%v': %v", shrToken, err)
|
|
|
|
return share.NewUnshareInternalServerError()
|
|
|
|
}
|
2023-01-04 19:13:50 +01:00
|
|
|
if err := str.DeleteShare(sshr.Id, tx); err != nil {
|
2024-03-05 18:51:06 +01:00
|
|
|
logrus.Errorf("error deleting share '%v': %v", shrToken, err)
|
2023-01-04 19:43:37 +01:00
|
|
|
return share.NewUnshareInternalServerError()
|
2022-11-29 22:43:35 +01:00
|
|
|
}
|
|
|
|
if err := tx.Commit(); err != nil {
|
2023-01-04 20:42:58 +01:00
|
|
|
logrus.Errorf("error committing transaction for '%v': %v", shrZId, err)
|
2023-01-04 19:43:37 +01:00
|
|
|
return share.NewUnshareInternalServerError()
|
2022-11-29 22:43:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2023-01-04 20:21:23 +01:00
|
|
|
logrus.Infof("share '%v' is reserved, skipping deallocation", shrToken)
|
2022-07-29 22:21:47 +02:00
|
|
|
}
|
|
|
|
|
2023-01-04 19:43:37 +01:00
|
|
|
return share.NewUnshareOK()
|
2022-07-27 18:17:37 +02:00
|
|
|
}
|
|
|
|
|
2023-01-04 20:42:58 +01:00
|
|
|
func (h *unshareHandler) findShareZId(shrToken string, edge *rest_management_api_client.ZitiEdgeManagement) (string, error) {
|
2023-01-04 20:21:23 +01:00
|
|
|
filter := fmt.Sprintf("name=\"%v\"", shrToken)
|
2022-08-01 21:44:26 +02:00
|
|
|
limit := int64(1)
|
|
|
|
offset := int64(0)
|
2022-11-18 21:36:55 +01:00
|
|
|
listReq := &edge_service.ListServicesParams{
|
2022-08-01 21:44:26 +02:00
|
|
|
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
|
|
|
|
}
|
2023-01-04 20:21:23 +01:00
|
|
|
return "", errors.Errorf("share '%v' not found", shrToken)
|
2022-08-01 21:44:26 +02:00
|
|
|
}
|
2022-11-22 20:24:41 +01:00
|
|
|
|
2024-01-17 17:02:34 +01:00
|
|
|
func (h *unshareHandler) deallocateResources(senv *store.Environment, shrToken, shrZId string, edge *rest_management_api_client.ZitiEdgeManagement) {
|
2023-01-04 20:21:23 +01:00
|
|
|
if err := zrokEdgeSdk.DeleteServiceEdgeRouterPolicy(senv.ZId, shrToken, edge); err != nil {
|
2024-01-17 17:02:34 +01:00
|
|
|
logrus.Warnf("error deleting service edge router policies for share '%v' in environment '%v': %v", shrToken, senv.ZId, err)
|
2022-11-22 20:24:41 +01:00
|
|
|
}
|
2023-05-18 20:25:53 +02:00
|
|
|
if err := zrokEdgeSdk.DeleteServicePoliciesDial(senv.ZId, shrToken, edge); err != nil {
|
2024-01-17 17:02:34 +01:00
|
|
|
logrus.Warnf("error deleting dial service policies for share '%v' in environment '%v': %v", shrToken, senv.ZId, err)
|
2022-11-22 20:24:41 +01:00
|
|
|
}
|
2023-05-18 20:25:53 +02:00
|
|
|
if err := zrokEdgeSdk.DeleteServicePoliciesBind(senv.ZId, shrToken, edge); err != nil {
|
2024-01-17 17:02:34 +01:00
|
|
|
logrus.Warnf("error deleting bind service policies for share '%v' in environment '%v': %v", shrToken, senv.ZId, err)
|
2022-11-22 20:24:41 +01:00
|
|
|
}
|
2023-01-04 20:21:23 +01:00
|
|
|
if err := zrokEdgeSdk.DeleteConfig(senv.ZId, shrToken, edge); err != nil {
|
2024-01-17 17:02:34 +01:00
|
|
|
logrus.Warnf("error deleting config for share '%v' in environment '%v': %v", shrToken, senv.ZId, err)
|
2022-11-22 20:24:41 +01:00
|
|
|
}
|
2023-01-04 20:42:58 +01:00
|
|
|
if err := zrokEdgeSdk.DeleteService(senv.ZId, shrZId, edge); err != nil {
|
2024-01-17 17:02:34 +01:00
|
|
|
logrus.Warnf("error deleting service '%v' for share '%v' in environment '%v': %v", shrZId, shrToken, senv.ZId, err)
|
2022-11-22 20:24:41 +01:00
|
|
|
}
|
|
|
|
}
|