2022-12-14 20:40:45 +01:00
|
|
|
package zrokEdgeSdk
|
2022-12-14 20:04:29 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2023-05-25 17:50:38 +02:00
|
|
|
"github.com/openziti/edge-api/rest_management_api_client"
|
|
|
|
"github.com/openziti/edge-api/rest_management_api_client/service_policy"
|
|
|
|
"github.com/openziti/edge-api/rest_model"
|
2022-12-14 23:17:19 +01:00
|
|
|
"github.com/pkg/errors"
|
2022-12-14 20:04:29 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
2023-05-25 20:59:39 +02:00
|
|
|
"time"
|
2022-12-14 20:04:29 +01:00
|
|
|
)
|
|
|
|
|
2022-12-14 21:47:14 +01:00
|
|
|
const (
|
2022-12-15 17:50:30 +01:00
|
|
|
servicePolicyDial = 1
|
|
|
|
servicePolicyBind = 2
|
2022-12-14 21:47:14 +01:00
|
|
|
)
|
|
|
|
|
2023-01-04 20:21:23 +01:00
|
|
|
func CreateServicePolicyBind(name, shrZId, bindZId string, addlTags map[string]interface{}, edge *rest_management_api_client.ZitiEdgeManagement) error {
|
2022-12-14 20:04:29 +01:00
|
|
|
semantic := rest_model.SemanticAllOf
|
2022-12-14 23:17:19 +01:00
|
|
|
identityRoles := []string{"@" + bindZId}
|
2023-01-04 20:21:23 +01:00
|
|
|
serviceRoles := []string{"@" + shrZId}
|
2022-12-15 17:50:30 +01:00
|
|
|
spZId, err := createServicePolicy(name, semantic, identityRoles, serviceRoles, addlTags, servicePolicyBind, edge)
|
2022-12-14 20:04:29 +01:00
|
|
|
if err != nil {
|
2023-01-04 20:21:23 +01:00
|
|
|
return errors.Wrapf(err, "error creating bind service policy for service '%v' for identity '%v'", shrZId, bindZId)
|
2022-12-14 20:04:29 +01:00
|
|
|
}
|
2023-01-04 20:21:23 +01:00
|
|
|
logrus.Infof("created bind service policy '%v' for service '%v' for identity '%v'", spZId, shrZId, bindZId)
|
2022-12-14 20:04:29 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:21:23 +01:00
|
|
|
func CreateServicePolicyDial(name, shrZId string, dialZIds []string, addlTags map[string]interface{}, edge *rest_management_api_client.ZitiEdgeManagement) error {
|
2022-12-14 20:04:29 +01:00
|
|
|
semantic := rest_model.SemanticAllOf
|
2022-12-14 23:17:19 +01:00
|
|
|
var identityRoles []string
|
|
|
|
for _, zId := range dialZIds {
|
|
|
|
identityRoles = append(identityRoles, "@"+zId)
|
2022-12-14 20:04:29 +01:00
|
|
|
}
|
2023-01-04 20:21:23 +01:00
|
|
|
serviceRoles := []string{"@" + shrZId}
|
2022-12-15 17:50:30 +01:00
|
|
|
spZId, err := createServicePolicy(name, semantic, identityRoles, serviceRoles, addlTags, servicePolicyDial, edge)
|
2022-12-14 20:04:29 +01:00
|
|
|
if err != nil {
|
2023-01-04 20:21:23 +01:00
|
|
|
return errors.Wrapf(err, "error creating dial service policy for service '%v' for identities '%v'", shrZId, dialZIds)
|
2022-12-14 20:04:29 +01:00
|
|
|
}
|
2023-01-04 20:21:23 +01:00
|
|
|
logrus.Infof("created dial service policy '%v' for service '%v' for identities '%v'", spZId, shrZId, dialZIds)
|
2022-12-14 20:04:29 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-14 23:21:56 +01:00
|
|
|
func createServicePolicy(name string, semantic rest_model.Semantic, identityRoles, serviceRoles []string, addlTags map[string]interface{}, dialBind int, edge *rest_management_api_client.ZitiEdgeManagement) (spZId string, err error) {
|
2022-12-14 23:17:19 +01:00
|
|
|
var dialBindType rest_model.DialBind
|
|
|
|
switch dialBind {
|
2022-12-15 17:50:30 +01:00
|
|
|
case servicePolicyBind:
|
2022-12-14 23:17:19 +01:00
|
|
|
dialBindType = rest_model.DialBindBind
|
2022-12-15 17:50:30 +01:00
|
|
|
case servicePolicyDial:
|
2022-12-14 23:17:19 +01:00
|
|
|
dialBindType = rest_model.DialBindDial
|
|
|
|
default:
|
|
|
|
return "", errors.Errorf("invalid dial bind type")
|
2022-12-14 20:04:29 +01:00
|
|
|
}
|
|
|
|
|
2022-12-14 23:17:19 +01:00
|
|
|
spc := &rest_model.ServicePolicyCreate{
|
2022-12-14 20:04:29 +01:00
|
|
|
IdentityRoles: identityRoles,
|
|
|
|
Name: &name,
|
2022-12-14 23:17:19 +01:00
|
|
|
PostureCheckRoles: make([]string, 0),
|
2022-12-14 20:04:29 +01:00
|
|
|
Semantic: &semantic,
|
|
|
|
ServiceRoles: serviceRoles,
|
2022-12-14 23:17:19 +01:00
|
|
|
Tags: MergeTags(ZrokTags(), addlTags),
|
|
|
|
Type: &dialBindType,
|
2022-12-14 20:04:29 +01:00
|
|
|
}
|
2022-12-14 23:17:19 +01:00
|
|
|
|
2022-12-14 20:04:29 +01:00
|
|
|
req := &service_policy.CreateServicePolicyParams{
|
2022-12-15 17:50:30 +01:00
|
|
|
Policy: spc,
|
2022-12-14 20:04:29 +01:00
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
req.SetTimeout(30 * time.Second)
|
2022-12-14 23:17:19 +01:00
|
|
|
|
2022-12-14 20:04:29 +01:00
|
|
|
resp, err := edge.ServicePolicy.CreateServicePolicy(req, nil)
|
|
|
|
if err != nil {
|
2022-12-14 23:17:19 +01:00
|
|
|
return "", errors.Wrap(err, "error creating service policy")
|
2022-12-14 20:04:29 +01:00
|
|
|
}
|
2022-12-14 23:17:19 +01:00
|
|
|
|
|
|
|
return resp.Payload.Data.ID, nil
|
2022-12-14 20:04:29 +01:00
|
|
|
}
|
|
|
|
|
2023-05-18 20:25:53 +02:00
|
|
|
func DeleteServicePoliciesBind(envZId, shrToken string, edge *rest_management_api_client.ZitiEdgeManagement) error {
|
|
|
|
return DeleteServicePolicies(envZId, fmt.Sprintf("tags.zrokShareToken=\"%v\" and type=%d", shrToken, servicePolicyBind), edge)
|
2022-12-14 20:04:29 +01:00
|
|
|
}
|
|
|
|
|
2023-05-18 20:25:53 +02:00
|
|
|
func DeleteServicePoliciesDial(envZId, shrToken string, edge *rest_management_api_client.ZitiEdgeManagement) error {
|
|
|
|
return DeleteServicePolicies(envZId, fmt.Sprintf("tags.zrokShareToken=\"%v\" and type=%d", shrToken, servicePolicyDial), edge)
|
2022-12-14 20:04:29 +01:00
|
|
|
}
|
|
|
|
|
2023-05-18 20:25:53 +02:00
|
|
|
func DeleteServicePolicies(envZId, filter string, edge *rest_management_api_client.ZitiEdgeManagement) error {
|
|
|
|
limit := int64(0)
|
2022-12-14 20:04:29 +01:00
|
|
|
offset := int64(0)
|
|
|
|
listReq := &service_policy.ListServicePoliciesParams{
|
|
|
|
Filter: &filter,
|
|
|
|
Limit: &limit,
|
|
|
|
Offset: &offset,
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
listReq.SetTimeout(30 * time.Second)
|
|
|
|
listResp, err := edge.ServicePolicy.ListServicePolicies(listReq, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-18 20:25:53 +02:00
|
|
|
logrus.Infof("found %d service policies to delete for '%v'", len(listResp.Payload.Data), filter)
|
|
|
|
for i := range listResp.Payload.Data {
|
|
|
|
spId := *(listResp.Payload.Data[i].ID)
|
2022-12-14 20:04:29 +01:00
|
|
|
req := &service_policy.DeleteServicePolicyParams{
|
|
|
|
ID: spId,
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
req.SetTimeout(30 * time.Second)
|
|
|
|
_, err := edge.ServicePolicy.DeleteServicePolicy(req, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logrus.Infof("deleted service policy '%v' for environment '%v'", spId, envZId)
|
2023-05-18 20:25:53 +02:00
|
|
|
}
|
|
|
|
if len(listResp.Payload.Data) < 1 {
|
|
|
|
logrus.Warnf("did not find any service policies to delete for '%v'", filter)
|
2022-12-14 20:04:29 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|