zrok/controller/zrokEdgeSdk/sp.go

120 lines
4.1 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"
"github.com/openziti/edge/rest_management_api_client"
"github.com/openziti/edge/rest_management_api_client/service_policy"
"github.com/openziti/edge/rest_model"
"github.com/pkg/errors"
2022-12-14 20:04:29 +01:00
"github.com/sirupsen/logrus"
"time"
)
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
}
2023-01-04 20:21:23 +01:00
func DeleteServicePolicyBind(envZId, shrToken string, edge *rest_management_api_client.ZitiEdgeManagement) error {
return DeleteServicePolicy(envZId, fmt.Sprintf("tags.zrokShareToken=\"%v\" and type=%d", shrToken, servicePolicyBind), edge)
2022-12-14 20:04:29 +01:00
}
2023-01-04 20:21:23 +01:00
func DeleteServicePolicyDial(envZId, shrToken string, edge *rest_management_api_client.ZitiEdgeManagement) error {
return DeleteServicePolicy(envZId, fmt.Sprintf("tags.zrokShareToken=\"%v\" and type=%d", shrToken, servicePolicyDial), edge)
2022-12-14 20:04:29 +01:00
}
func DeleteServicePolicy(envZId, filter string, edge *rest_management_api_client.ZitiEdgeManagement) error {
limit := int64(1)
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
}
if len(listResp.Payload.Data) == 1 {
spId := *(listResp.Payload.Data[0].ID)
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)
} else {
logrus.Infof("did not find a service policy")
}
return nil
}