zrok/controller/zrokEdgeSdk/sp.go

122 lines
4.3 KiB
Go
Raw Normal View History

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"
"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
)
const (
servicePolicyDial = 1
servicePolicyBind = 2
)
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
identityRoles := []string{"@" + bindZId}
2023-01-04 20:21:23 +01:00
serviceRoles := []string{"@" + shrZId}
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
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}
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
}
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) {
var dialBindType rest_model.DialBind
switch dialBind {
case servicePolicyBind:
dialBindType = rest_model.DialBindBind
case servicePolicyDial:
dialBindType = rest_model.DialBindDial
default:
return "", errors.Errorf("invalid dial bind type")
2022-12-14 20:04:29 +01:00
}
spc := &rest_model.ServicePolicyCreate{
2022-12-14 20:04:29 +01:00
IdentityRoles: identityRoles,
Name: &name,
PostureCheckRoles: make([]string, 0),
2022-12-14 20:04:29 +01:00
Semantic: &semantic,
ServiceRoles: serviceRoles,
Tags: MergeTags(ZrokTags(), addlTags),
Type: &dialBindType,
2022-12-14 20:04:29 +01:00
}
2022-12-14 20:04:29 +01:00
req := &service_policy.CreateServicePolicyParams{
Policy: spc,
2022-12-14 20:04:29 +01:00
Context: context.Background(),
}
req.SetTimeout(30 * time.Second)
2022-12-14 20:04:29 +01:00
resp, err := edge.ServicePolicy.CreateServicePolicy(req, nil)
if err != nil {
return "", errors.Wrap(err, "error creating service policy")
2022-12-14 20:04:29 +01:00
}
return resp.Payload.Data.ID, nil
2022-12-14 20:04:29 +01: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
}
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
}
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
}
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)
}
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
}