2023-03-27 17:53:18 +02:00
|
|
|
package limits
|
|
|
|
|
|
|
|
import (
|
2023-03-27 19:51:48 +02:00
|
|
|
"github.com/jmoiron/sqlx"
|
2024-05-30 19:46:15 +02:00
|
|
|
"github.com/openziti/edge-api/rest_management_api_client"
|
2023-03-27 17:53:18 +02:00
|
|
|
"github.com/openziti/zrok/controller/store"
|
2023-06-05 22:01:04 +02:00
|
|
|
"github.com/openziti/zrok/controller/zrokEdgeSdk"
|
2023-11-21 20:27:17 +01:00
|
|
|
"github.com/openziti/zrok/sdk/golang/sdk"
|
2023-03-27 20:06:57 +02:00
|
|
|
"github.com/pkg/errors"
|
2023-03-27 17:53:18 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2024-06-04 16:33:39 +02:00
|
|
|
type relaxAction struct {
|
2023-03-27 17:53:18 +02:00
|
|
|
str *store.Store
|
2023-06-05 22:01:04 +02:00
|
|
|
zCfg *zrokEdgeSdk.Config
|
2023-03-27 17:53:18 +02:00
|
|
|
}
|
|
|
|
|
2024-06-04 16:33:39 +02:00
|
|
|
func newRelaxAction(str *store.Store, zCfg *zrokEdgeSdk.Config) *relaxAction {
|
|
|
|
return &relaxAction{str, zCfg}
|
2023-03-27 17:53:18 +02:00
|
|
|
}
|
|
|
|
|
2024-06-04 16:33:39 +02:00
|
|
|
func (a *relaxAction) HandleAccount(acct *store.Account, _, _ int64, _ *BandwidthPerPeriod, trx *sqlx.Tx) error {
|
2023-03-27 17:53:18 +02:00
|
|
|
logrus.Infof("relaxing '%v'", acct.Email)
|
2023-03-27 20:06:57 +02:00
|
|
|
|
|
|
|
envs, err := a.str.FindEnvironmentsForAccount(acct.Id, trx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "error finding environments for account '%v'", acct.Email)
|
|
|
|
}
|
|
|
|
|
2023-06-05 22:01:04 +02:00
|
|
|
edge, err := zrokEdgeSdk.Client(a.zCfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-27 20:06:57 +02:00
|
|
|
for _, env := range envs {
|
|
|
|
shrs, err := a.str.FindSharesForEnvironment(env.Id, trx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "error finding shares for environment '%v'", env.ZId)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, shr := range shrs {
|
2023-03-29 21:08:04 +02:00
|
|
|
switch shr.ShareMode {
|
2023-07-17 22:21:29 +02:00
|
|
|
case string(sdk.PublicShareMode):
|
2023-06-05 22:01:04 +02:00
|
|
|
if err := relaxPublicShare(a.str, edge, shr, trx); err != nil {
|
2023-05-18 19:19:16 +02:00
|
|
|
return errors.Wrap(err, "error relaxing public share")
|
2023-03-29 21:08:04 +02:00
|
|
|
}
|
2023-07-17 22:21:29 +02:00
|
|
|
case string(sdk.PrivateShareMode):
|
2023-06-05 22:01:04 +02:00
|
|
|
if err := relaxPrivateShare(a.str, edge, shr, trx); err != nil {
|
2023-05-18 19:19:16 +02:00
|
|
|
return errors.Wrap(err, "error relaxing private share")
|
2023-03-27 20:06:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-27 17:53:18 +02:00
|
|
|
return nil
|
|
|
|
}
|
2024-05-30 19:46:15 +02:00
|
|
|
|
|
|
|
func relaxPublicShare(str *store.Store, edge *rest_management_api_client.ZitiEdgeManagement, shr *store.Share, trx *sqlx.Tx) error {
|
|
|
|
env, err := str.GetEnvironment(shr.EnvironmentId, trx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error finding environment")
|
|
|
|
}
|
|
|
|
|
|
|
|
fe, err := str.FindFrontendPubliclyNamed(*shr.FrontendSelection, trx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "error finding frontend name '%v' for '%v'", *shr.FrontendSelection, shr.Token)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := zrokEdgeSdk.CreateServicePolicyDial(env.ZId+"-"+shr.ZId+"-dial", shr.ZId, []string{fe.ZId}, zrokEdgeSdk.ZrokShareTags(shr.Token).SubTags, edge); err != nil {
|
|
|
|
return errors.Wrapf(err, "error creating dial service policy for '%v'", shr.Token)
|
|
|
|
}
|
|
|
|
logrus.Infof("added dial service policy for '%v'", shr.Token)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func relaxPrivateShare(str *store.Store, edge *rest_management_api_client.ZitiEdgeManagement, shr *store.Share, trx *sqlx.Tx) error {
|
|
|
|
fes, err := str.FindFrontendsForPrivateShare(shr.Id, trx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "error finding frontends for share '%v'", shr.Token)
|
|
|
|
}
|
|
|
|
for _, fe := range fes {
|
|
|
|
if fe.EnvironmentId != nil {
|
|
|
|
env, err := str.GetEnvironment(*fe.EnvironmentId, trx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "error getting environment for frontend '%v'", fe.Token)
|
|
|
|
}
|
|
|
|
|
|
|
|
addlTags := map[string]interface{}{
|
|
|
|
"zrokEnvironmentZId": env.ZId,
|
|
|
|
"zrokFrontendToken": fe.Token,
|
|
|
|
"zrokShareToken": shr.Token,
|
|
|
|
}
|
|
|
|
if err := zrokEdgeSdk.CreateServicePolicyDial(fe.Token+"-"+env.ZId+"-"+shr.ZId+"-dial", shr.ZId, []string{env.ZId}, addlTags, edge); err != nil {
|
|
|
|
return errors.Wrapf(err, "unable to create dial policy for frontend '%v'", fe.Token)
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Infof("added dial service policy for share '%v' to private frontend '%v'", shr.Token, fe.Token)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|