-
Notifications
You must be signed in to change notification settings - Fork 24
/
dynamic-config.go
73 lines (63 loc) · 2.03 KB
/
dynamic-config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package utils
import (
"context"
"errors"
"fmt"
"os"
"google.golang.org/protobuf/types/known/structpb"
languagev1 "github.com/fluxninja/aperture/api/v2/gen/proto/go/aperture/policy/language/v1"
"github.com/fluxninja/aperture/v2/pkg/log"
)
// GetDynamicConfigBytes returns the bytes of the dynamic config file.
func GetDynamicConfigBytes(policyName, dynamicConfigFile string) ([]byte, error) {
if policyName == "" {
return nil, errors.New("policy name is required")
}
if dynamicConfigFile == "" {
return nil, errors.New("dynamic config file is required")
}
// read the dynamic config file
return os.ReadFile(dynamicConfigFile)
}
// ApplyDynamicConfig applies the dynamic config.
func ApplyDynamicConfigUsingAPI(client SelfHostedPolicyClient, dynamicConfigYAML map[string]interface{}, policyName string) error {
var dynamicConfigStruct *structpb.Struct
var err error
dynamicConfigStruct, err = structpb.NewStruct(dynamicConfigYAML)
if err != nil {
return fmt.Errorf("failed to parse DynamicConfig Struct: %w", err)
}
request := languagev1.PostDynamicConfigRequest{
PolicyName: policyName,
DynamicConfig: dynamicConfigStruct,
}
_, err = client.PostDynamicConfig(context.Background(), &request)
if err != nil {
return fmt.Errorf("failed to update DynamicConfig: %w", err)
}
return nil
}
// GetDynamicConfigUsingAPI gets the dynamic config.
func GetDynamicConfigUsingAPI(client SelfHostedPolicyClient, policyName string) error {
request := languagev1.GetDynamicConfigRequest{
PolicyName: policyName,
}
resp, err := client.GetDynamicConfig(context.Background(), &request)
if err != nil {
return fmt.Errorf("failed to update DynamicConfig: %w", err)
}
if resp.DynamicConfig == nil {
log.Info().Str("policy-name", policyName).Msg("DynamicConfig is not set for the given Policy")
return nil
}
j, err := resp.DynamicConfig.MarshalJSON()
if err != nil {
return fmt.Errorf("failed to marshal response: %w", err)
}
yamlString, err := GetYAMLString(j)
if err != nil {
return err
}
fmt.Print(yamlString)
return nil
}