zrok/controller/zrokEdgeSdk/config.go

138 lines
4.2 KiB
Go
Raw Normal View History

2022-12-14 20:40:45 +01:00
package zrokEdgeSdk
2022-12-14 20:09:54 +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/config"
"github.com/openziti/edge-api/rest_model"
2023-11-21 20:27:17 +01:00
"github.com/openziti/zrok/sdk/golang/sdk"
2022-12-14 20:09:54 +01:00
"github.com/sirupsen/logrus"
2024-09-05 19:34:07 +02:00
"reflect"
2023-07-18 20:03:43 +02:00
"time"
2022-12-14 20:09:54 +01:00
)
type FrontendOptions struct {
Interstitial bool
2023-09-25 20:15:19 +02:00
AuthScheme sdk.AuthScheme
BasicAuthUsers []*sdk.AuthUserConfig
Oauth *sdk.OauthConfig
2023-09-05 18:50:41 +02:00
}
func CreateConfig(cfgTypeZId, envZId, shrToken string, options *FrontendOptions, edge *rest_management_api_client.ZitiEdgeManagement) (cfgZId string, err error) {
cfg := &sdk.FrontendConfig{
Interstitial: options.Interstitial,
AuthScheme: options.AuthScheme,
2022-12-14 20:09:54 +01:00
}
if cfg.AuthScheme == sdk.Basic {
cfg.BasicAuth = &sdk.BasicAuthConfig{}
2023-09-25 20:15:19 +02:00
for _, authUser := range options.BasicAuthUsers {
cfg.BasicAuth.Users = append(cfg.BasicAuth.Users, &sdk.AuthUserConfig{Username: authUser.Username, Password: authUser.Password})
2022-12-14 20:09:54 +01:00
}
}
2023-09-25 20:06:14 +02:00
if cfg.AuthScheme == sdk.Oauth && options.Oauth != nil {
cfg.OauthAuth = &sdk.OauthConfig{
Provider: options.Oauth.Provider,
EmailDomains: options.Oauth.EmailDomains,
AuthorizationCheckInterval: options.Oauth.AuthorizationCheckInterval,
}
}
2022-12-14 20:09:54 +01:00
cfgCrt := &rest_model.ConfigCreate{
ConfigTypeID: &cfgTypeZId,
2022-12-14 20:09:54 +01:00
Data: cfg,
2023-01-04 20:21:23 +01:00
Name: &shrToken,
Tags: ZrokShareTags(shrToken),
2022-12-14 20:09:54 +01:00
}
cfgReq := &config.CreateConfigParams{
Config: cfgCrt,
Context: context.Background(),
}
cfgReq.SetTimeout(30 * time.Second)
cfgResp, err := edge.Config.CreateConfig(cfgReq, nil)
if err != nil {
return "", err
}
logrus.Infof("created config '%v' for environment '%v'", cfgResp.Payload.Data.ID, envZId)
return cfgResp.Payload.Data.ID, nil
}
2024-09-05 19:34:07 +02:00
func GetConfig(shrToken string, edge *rest_management_api_client.ZitiEdgeManagement) (string, *sdk.FrontendConfig, error) {
filter := fmt.Sprintf("tags.zrokShareToken=\"%v\"", shrToken)
limit := int64(0)
offset := int64(0)
listReq := &config.ListConfigsParams{
Filter: &filter,
Limit: &limit,
Offset: &offset,
Context: context.Background(),
}
listReq.SetTimeout(30 * time.Second)
listResp, err := edge.Config.ListConfigs(listReq, nil)
if err != nil {
return "", nil, err
}
if len(listResp.Payload.Data) != 1 {
return "", nil, fmt.Errorf("expected 1 configuration, found %v", len(listResp.Payload.Data))
}
if listResp.Payload.Data[0].ConfigType.Name != sdk.ZrokProxyConfig {
return "", nil, fmt.Errorf("expected '%v', found '%v'", sdk.ZrokProxyConfig, listResp.Payload.Data[0].ConfigType.Name)
}
if v, ok := listResp.Payload.Data[0].Data.(map[string]interface{}); ok {
fec, err := sdk.FrontendConfigFromMap(v)
if err != nil {
return "", nil, err
}
return *listResp.Payload.Data[0].ID, fec, nil
}
return "", nil, fmt.Errorf("unknown data type '%v' unmarshaling config for '%v'", reflect.TypeOf(listResp.Payload.Data[0].Data), shrToken)
}
2024-09-06 17:36:34 +02:00
func UpdateConfig(shrToken, cfgZId string, cfg *sdk.FrontendConfig, edge *rest_management_api_client.ZitiEdgeManagement) error {
logrus.Infof("updating config for '%v' (%v)", shrToken, cfgZId)
req := &config.UpdateConfigParams{
Config: &rest_model.ConfigUpdate{
Data: cfg,
Name: &shrToken,
Tags: ZrokShareTags(shrToken),
},
ID: cfgZId,
Context: context.Background(),
}
req.SetTimeout(30 * time.Second)
_, err := edge.Config.UpdateConfig(req, nil)
if err != nil {
return err
}
2024-09-05 19:34:07 +02:00
return nil
}
2023-01-04 20:21:23 +01:00
func DeleteConfig(envZId, shrToken string, edge *rest_management_api_client.ZitiEdgeManagement) error {
filter := fmt.Sprintf("tags.zrokShareToken=\"%v\"", shrToken)
2022-12-14 20:09:54 +01:00
limit := int64(0)
offset := int64(0)
listReq := &config.ListConfigsParams{
Filter: &filter,
Limit: &limit,
Offset: &offset,
Context: context.Background(),
}
listReq.SetTimeout(30 * time.Second)
listResp, err := edge.Config.ListConfigs(listReq, nil)
if err != nil {
return err
}
for _, cfg := range listResp.Payload.Data {
deleteReq := &config.DeleteConfigParams{
ID: *cfg.ID,
Context: context.Background(),
}
deleteReq.SetTimeout(30 * time.Second)
_, err := edge.Config.DeleteConfig(deleteReq, nil)
if err != nil {
return err
}
logrus.Infof("deleted config '%v' for '%v'", *cfg.ID, envZId)
}
return nil
}