zrok/controller/disable.go

164 lines
5.7 KiB
Go
Raw Normal View History

2022-09-02 16:50:13 +02:00
package controller
import (
"fmt"
2022-09-02 16:50:13 +02:00
"github.com/go-openapi/runtime/middleware"
"github.com/jmoiron/sqlx"
2023-05-25 17:50:38 +02:00
"github.com/openziti/edge-api/rest_management_api_client"
"github.com/openziti/zrok/controller/zrokEdgeSdk"
"github.com/openziti/zrok/rest_model_zrok"
"github.com/openziti/zrok/rest_server_zrok/operations/environment"
"github.com/pkg/errors"
2022-09-02 16:50:13 +02:00
"github.com/sirupsen/logrus"
)
type disableHandler struct {
}
func newDisableHandler() *disableHandler {
return &disableHandler{}
2022-09-02 16:50:13 +02:00
}
2022-11-30 17:43:00 +01:00
func (h *disableHandler) Handle(params environment.DisableParams, principal *rest_model_zrok.Principal) middleware.Responder {
tx, err := str.Begin()
2022-09-02 16:50:13 +02:00
if err != nil {
logrus.Errorf("error starting transaction for user '%v': %v", principal.Email, err)
2022-11-30 17:43:00 +01:00
return environment.NewDisableInternalServerError()
2022-09-02 16:50:13 +02:00
}
defer func() { _ = tx.Rollback() }()
2022-11-30 17:43:00 +01:00
envId, err := h.checkZitiIdentity(params.Body.Identity, principal, tx)
if err != nil {
logrus.Errorf("identity check failed for user '%v': %v", principal.Email, err)
2022-11-30 17:43:00 +01:00
return environment.NewDisableUnauthorized()
}
env, err := str.GetEnvironment(envId, tx)
if err != nil {
logrus.Errorf("error getting environment for user '%v': %v", principal.Email, err)
2022-11-30 17:43:00 +01:00
return environment.NewDisableInternalServerError()
}
2023-03-10 16:57:59 +01:00
if env.Deleted {
logrus.Errorf("environment '%v' for user '%v' deleted", env.ZId, principal.Email)
return environment.NewDisableUnauthorized()
}
2023-03-07 20:31:39 +01:00
edge, err := zrokEdgeSdk.Client(cfg.Ziti)
if err != nil {
logrus.Errorf("error getting edge client for user '%v': %v", principal.Email, err)
2022-11-30 17:43:00 +01:00
return environment.NewDisableInternalServerError()
}
2023-01-04 20:21:23 +01:00
if err := h.removeSharesForEnvironment(envId, tx, edge); err != nil {
logrus.Errorf("error removing shares for environment for user '%v': %v", principal.Email, err)
2022-11-30 17:43:00 +01:00
return environment.NewDisableInternalServerError()
}
if err := h.removeFrontendsForEnvironment(envId, tx, edge); err != nil {
logrus.Errorf("error removing frontends for environment for user '%v': %v", principal.Email, err)
return environment.NewDisableInternalServerError()
}
2022-11-30 17:43:00 +01:00
if err := h.removeEnvironment(envId, tx); err != nil {
logrus.Errorf("error removing environment for user '%v': %v", principal.Email, err)
2022-11-30 17:43:00 +01:00
return environment.NewDisableInternalServerError()
}
2022-12-14 20:40:45 +01:00
if err := zrokEdgeSdk.DeleteEdgeRouterPolicy(env.ZId, edge); err != nil {
logrus.Errorf("error deleting edge router policy for user '%v': %v", principal.Email, err)
2022-11-30 17:43:00 +01:00
return environment.NewDisableInternalServerError()
}
2022-12-14 20:40:45 +01:00
if err := zrokEdgeSdk.DeleteIdentity(params.Body.Identity, edge); err != nil {
logrus.Errorf("error deleting identity for user '%v': %v", principal.Email, err)
2022-11-30 17:43:00 +01:00
return environment.NewDisableInternalServerError()
}
if err := tx.Commit(); err != nil {
logrus.Errorf("error committing for user '%v': %v", principal.Email, err)
}
2022-11-30 17:43:00 +01:00
return environment.NewDisableOK()
2022-09-02 16:50:13 +02:00
}
2022-11-30 17:43:00 +01:00
func (h *disableHandler) checkZitiIdentity(id string, principal *rest_model_zrok.Principal, tx *sqlx.Tx) (int, error) {
envs, err := str.FindEnvironmentsForAccount(int(principal.ID), tx)
if err != nil {
return -1, err
}
for _, env := range envs {
2022-10-19 18:24:43 +02:00
if env.ZId == id {
return env.Id, nil
}
}
return -1, errors.Errorf("no such environment '%v'", id)
}
2023-01-04 20:21:23 +01:00
func (h *disableHandler) removeSharesForEnvironment(envId int, tx *sqlx.Tx, edge *rest_management_api_client.ZitiEdgeManagement) error {
env, err := str.GetEnvironment(envId, tx)
if err != nil {
return err
}
2023-03-10 16:57:59 +01:00
if !env.Deleted {
shrs, err := str.FindSharesForEnvironment(envId, tx)
if err != nil {
return err
}
2023-03-10 16:57:59 +01:00
for _, shr := range shrs {
shrToken := shr.Token
logrus.Infof("garbage collecting share '%v' for environment '%v'", shrToken, env.ZId)
if err := zrokEdgeSdk.DeleteServiceEdgeRouterPolicy(env.ZId, shrToken, edge); err != nil {
logrus.Error(err)
}
if err := zrokEdgeSdk.DeleteServicePoliciesDial(env.ZId, shrToken, edge); err != nil {
2023-03-10 16:57:59 +01:00
logrus.Error(err)
}
if err := zrokEdgeSdk.DeleteServicePoliciesBind(env.ZId, shrToken, edge); err != nil {
2023-03-10 16:57:59 +01:00
logrus.Error(err)
}
if err := zrokEdgeSdk.DeleteConfig(env.ZId, shrToken, edge); err != nil {
logrus.Error(err)
}
if err := zrokEdgeSdk.DeleteService(env.ZId, shr.ZId, edge); err != nil {
logrus.Error(err)
}
logrus.Infof("removed share '%v' for environment '%v'", shr.Token, env.ZId)
}
}
return nil
}
func (h *disableHandler) removeFrontendsForEnvironment(envId int, tx *sqlx.Tx, edge *rest_management_api_client.ZitiEdgeManagement) error {
env, err := str.GetEnvironment(envId, tx)
if err != nil {
return err
}
2023-03-10 16:57:59 +01:00
if !env.Deleted {
fes, err := str.FindFrontendsForEnvironment(envId, tx)
if err != nil {
return err
}
for _, fe := range fes {
if err := zrokEdgeSdk.DeleteServicePolicies(env.ZId, fmt.Sprintf("tags.zrokFrontendToken=\"%v\" and type=1", fe.Token), edge); err != nil {
2023-03-10 16:57:59 +01:00
logrus.Errorf("error removing frontend access for '%v': %v", fe.Token, err)
}
}
}
return nil
}
2022-11-30 17:43:00 +01:00
func (h *disableHandler) removeEnvironment(envId int, tx *sqlx.Tx) error {
shrs, err := str.FindSharesForEnvironment(envId, tx)
if err != nil {
2023-01-04 20:21:23 +01:00
return errors.Wrapf(err, "error finding shares for environment '%d'", envId)
}
for _, shr := range shrs {
if err := str.DeleteShare(shr.Id, tx); err != nil {
2023-01-04 20:21:23 +01:00
return errors.Wrapf(err, "error deleting share '%d' for environment '%d'", shr.Id, envId)
}
}
fes, err := str.FindFrontendsForEnvironment(envId, tx)
if err != nil {
return errors.Wrapf(err, "error finding frontends for environment '%d'", envId)
}
for _, fe := range fes {
if err := str.DeleteFrontend(fe.Id, tx); err != nil {
return errors.Wrapf(err, "error deleting frontend '%d' for environment '%d'", fe.Id, envId)
}
}
if err := str.DeleteEnvironment(envId, tx); err != nil {
return errors.Wrapf(err, "error deleting environment '%d'", envId)
}
return nil
}