-
Notifications
You must be signed in to change notification settings - Fork 1
/
settings.go
45 lines (36 loc) · 1.12 KB
/
settings.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
package main
import (
"encoding/json"
"fmt"
"log"
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
"k8s.io/apimachinery/pkg/util/yaml"
)
func validateSettings(input []byte) []byte {
var response SettingsValidationResponse
settings := &CliPolicySettings{}
if err := json.Unmarshal(input, &settings); err != nil {
response = RejectSettings(Message(fmt.Sprintf("cannot unmarshal settings: %v", err)))
} else {
response = validateCliSettings(settings)
}
responseBytes, err := json.Marshal(&response)
if err != nil {
log.Fatalf("cannot marshal validation response: %v", err)
}
return responseBytes
}
func validateCliSettings(settings *CliPolicySettings) SettingsValidationResponse {
// build policy
policyBytes, err := yaml.ToJSON([]byte(settings.Policy))
if err != nil {
return RejectSettings(Message(fmt.Sprintf("failed to convert policy to JSON: %v", err)))
}
policy := &kyvernov1.ClusterPolicy{}
err = json.Unmarshal(policyBytes, &policy)
if err != nil {
return RejectSettings(Message(fmt.Sprintf("cannot unmarshal policy: %v", err)))
}
//TODO: perform more validations
return AcceptSettings()
}