2022-09-02 16:50:13 +02:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/go-openapi/runtime/middleware"
|
2022-09-02 18:45:54 +02:00
|
|
|
"github.com/jmoiron/sqlx"
|
2022-10-07 20:27:01 +02:00
|
|
|
"github.com/openziti/edge/rest_management_api_client"
|
2023-01-13 21:01:34 +01:00
|
|
|
"github.com/openziti/zrok/controller/zrokEdgeSdk"
|
|
|
|
"github.com/openziti/zrok/rest_model_zrok"
|
|
|
|
"github.com/openziti/zrok/rest_server_zrok/operations/environment"
|
2022-09-02 18:45:54 +02:00
|
|
|
"github.com/pkg/errors"
|
2022-09-02 16:50:13 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type disableHandler struct {
|
|
|
|
}
|
|
|
|
|
2022-10-19 19:20:47 +02:00
|
|
|
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 {
|
2022-09-02 18:45:54 +02:00
|
|
|
tx, err := str.Begin()
|
2022-09-02 16:50:13 +02:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error starting transaction: %v", err)
|
2022-11-30 17:43:00 +01:00
|
|
|
return environment.NewDisableInternalServerError()
|
2022-09-02 16:50:13 +02:00
|
|
|
}
|
2022-09-02 18:45:54 +02:00
|
|
|
defer func() { _ = tx.Rollback() }()
|
2022-11-30 17:43:00 +01:00
|
|
|
envId, err := h.checkZitiIdentity(params.Body.Identity, principal, tx)
|
2022-09-02 18:45:54 +02:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("identity check failed: %v", err)
|
2022-11-30 17:43:00 +01:00
|
|
|
return environment.NewDisableUnauthorized()
|
2022-09-02 18:45:54 +02:00
|
|
|
}
|
2022-11-08 21:07:18 +01:00
|
|
|
env, err := str.GetEnvironment(envId, tx)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error getting environment: %v", err)
|
2022-11-30 17:43:00 +01:00
|
|
|
return environment.NewDisableInternalServerError()
|
2022-11-08 21:07:18 +01:00
|
|
|
}
|
2022-10-19 19:20:47 +02:00
|
|
|
edge, err := edgeClient()
|
2022-09-02 18:45:54 +02:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error getting edge client: %v", err)
|
2022-11-30 17:43:00 +01:00
|
|
|
return environment.NewDisableInternalServerError()
|
2022-09-02 18:45:54 +02:00
|
|
|
}
|
2023-01-04 20:21:23 +01:00
|
|
|
if err := h.removeSharesForEnvironment(envId, tx, edge); err != nil {
|
|
|
|
logrus.Errorf("error removing shares for environment: %v", err)
|
2022-11-30 17:43:00 +01:00
|
|
|
return environment.NewDisableInternalServerError()
|
2022-10-07 20:27:01 +02:00
|
|
|
}
|
2022-11-30 17:43:00 +01:00
|
|
|
if err := h.removeEnvironment(envId, tx); err != nil {
|
2022-10-07 20:27:01 +02:00
|
|
|
logrus.Errorf("error removing environment: %v", err)
|
2022-11-30 17:43:00 +01:00
|
|
|
return environment.NewDisableInternalServerError()
|
2022-10-07 20:27:01 +02:00
|
|
|
}
|
2022-12-14 20:40:45 +01:00
|
|
|
if err := zrokEdgeSdk.DeleteEdgeRouterPolicy(env.ZId, edge); err != nil {
|
2022-09-02 18:45:54 +02:00
|
|
|
logrus.Errorf("error deleting edge router policy: %v", err)
|
2022-11-30 17:43:00 +01:00
|
|
|
return environment.NewDisableInternalServerError()
|
2022-09-02 18:45:54 +02:00
|
|
|
}
|
2022-12-14 20:40:45 +01:00
|
|
|
if err := zrokEdgeSdk.DeleteIdentity(params.Body.Identity, edge); err != nil {
|
2022-09-02 18:45:54 +02:00
|
|
|
logrus.Errorf("error deleting identity: %v", err)
|
2022-11-30 17:43:00 +01:00
|
|
|
return environment.NewDisableInternalServerError()
|
2022-09-02 18:45:54 +02:00
|
|
|
}
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
logrus.Errorf("error committing: %v", err)
|
|
|
|
}
|
2022-11-30 17:43:00 +01:00
|
|
|
return environment.NewDisableOK()
|
2022-09-02 16:50:13 +02:00
|
|
|
}
|
2022-09-02 18:45:54 +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) {
|
2022-09-02 18:45:54 +02:00
|
|
|
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 {
|
2022-09-02 18:45:54 +02:00
|
|
|
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 {
|
2022-11-08 21:07:18 +01:00
|
|
|
env, err := str.GetEnvironment(envId, tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-04 19:13:50 +01:00
|
|
|
shrs, err := str.FindSharesForEnvironment(envId, tx)
|
2022-10-07 20:27:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-04 19:13:50 +01:00
|
|
|
for _, shr := range shrs {
|
2023-01-04 20:21:23 +01:00
|
|
|
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 {
|
2022-10-07 20:27:01 +02:00
|
|
|
logrus.Error(err)
|
|
|
|
}
|
2023-01-04 20:21:23 +01:00
|
|
|
if err := zrokEdgeSdk.DeleteServicePolicyDial(env.ZId, shrToken, edge); err != nil {
|
2022-10-07 20:27:01 +02:00
|
|
|
logrus.Error(err)
|
|
|
|
}
|
2023-01-04 20:21:23 +01:00
|
|
|
if err := zrokEdgeSdk.DeleteServicePolicyBind(env.ZId, shrToken, edge); err != nil {
|
2022-10-07 20:27:01 +02:00
|
|
|
logrus.Error(err)
|
|
|
|
}
|
2023-01-04 20:21:23 +01:00
|
|
|
if err := zrokEdgeSdk.DeleteConfig(env.ZId, shrToken, edge); err != nil {
|
2022-10-07 20:27:01 +02:00
|
|
|
logrus.Error(err)
|
|
|
|
}
|
2023-01-04 19:13:50 +01:00
|
|
|
if err := zrokEdgeSdk.DeleteService(env.ZId, shr.ZId, edge); err != nil {
|
2022-10-07 20:27:01 +02:00
|
|
|
logrus.Error(err)
|
|
|
|
}
|
2023-01-04 20:21:23 +01:00
|
|
|
logrus.Infof("removed share '%v' for environment '%v'", shr.Token, env.ZId)
|
2022-10-07 20:27:01 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-30 17:43:00 +01:00
|
|
|
func (h *disableHandler) removeEnvironment(envId int, tx *sqlx.Tx) error {
|
2023-01-04 19:13:50 +01:00
|
|
|
shrs, err := str.FindSharesForEnvironment(envId, tx)
|
2022-11-28 17:40:39 +01:00
|
|
|
if err != nil {
|
2023-01-04 20:21:23 +01:00
|
|
|
return errors.Wrapf(err, "error finding shares for environment '%d'", envId)
|
2022-11-28 17:40:39 +01:00
|
|
|
}
|
2023-01-04 19:13:50 +01:00
|
|
|
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)
|
2022-11-28 17:40:39 +01:00
|
|
|
}
|
|
|
|
}
|
2022-09-02 18:45:54 +02:00
|
|
|
if err := str.DeleteEnvironment(envId, tx); err != nil {
|
|
|
|
return errors.Wrapf(err, "error deleting environment '%d'", envId)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|