2022-09-06 17:16:44 +02:00
|
|
|
package yaml
|
2020-09-01 09:11:20 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
2022-05-17 13:12:43 +02:00
|
|
|
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
2022-10-02 20:45:03 +01:00
|
|
|
log "github.com/kyverno/kyverno/pkg/logging"
|
2022-11-11 16:18:17 +01:00
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2020-09-01 09:11:20 -07:00
|
|
|
"k8s.io/apimachinery/pkg/util/yaml"
|
|
|
|
)
|
|
|
|
|
2022-09-06 17:16:44 +02:00
|
|
|
// GetPolicy extracts policies from YAML bytes
|
2022-05-17 13:12:43 +02:00
|
|
|
func GetPolicy(bytes []byte) (policies []kyvernov1.PolicyInterface, err error) {
|
2022-09-06 17:16:44 +02:00
|
|
|
documents, err := SplitDocuments(bytes)
|
2020-09-01 09:11:20 -07:00
|
|
|
if err != nil {
|
2020-12-07 11:26:04 -08:00
|
|
|
return nil, err
|
2020-09-01 09:11:20 -07:00
|
|
|
}
|
2022-04-01 11:56:16 +02:00
|
|
|
for _, thisPolicyBytes := range documents {
|
2020-09-01 09:11:20 -07:00
|
|
|
policyBytes, err := yaml.ToJSON(thisPolicyBytes)
|
|
|
|
if err != nil {
|
2020-12-07 11:26:04 -08:00
|
|
|
return nil, fmt.Errorf("failed to convert to JSON: %v", err)
|
2020-09-01 09:11:20 -07:00
|
|
|
}
|
2022-11-11 16:18:17 +01:00
|
|
|
us := &unstructured.Unstructured{}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(policyBytes, us); err != nil {
|
2020-12-07 11:26:04 -08:00
|
|
|
return nil, fmt.Errorf("failed to decode policy: %v", err)
|
2020-09-01 09:11:20 -07:00
|
|
|
}
|
2022-11-11 16:18:17 +01:00
|
|
|
if us.IsList() {
|
|
|
|
list, err := us.ToList()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to decode policy list: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range list.Items {
|
|
|
|
item := list.Items[i]
|
|
|
|
if policies, err = addPolicy(policies, &item); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-01 14:16:33 +05:30
|
|
|
}
|
2022-09-06 17:16:44 +02:00
|
|
|
} else {
|
2022-11-11 16:18:17 +01:00
|
|
|
if policies, err = addPolicy(policies, us); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return policies, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func addPolicy(policies []kyvernov1.PolicyInterface, us *unstructured.Unstructured) ([]kyvernov1.PolicyInterface, error) {
|
2023-04-24 00:22:29 +08:00
|
|
|
var policy kyvernov1.PolicyInterface
|
|
|
|
if us.GetKind() == "ClusterPolicy" {
|
|
|
|
policy = &kyvernov1.ClusterPolicy{}
|
|
|
|
} else {
|
|
|
|
policy = &kyvernov1.Policy{}
|
|
|
|
}
|
2022-11-11 16:18:17 +01:00
|
|
|
|
|
|
|
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(us.Object, policy); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to decode policy: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-04-24 00:22:29 +08:00
|
|
|
if policy.GetKind() == "" {
|
2022-11-11 16:18:17 +01:00
|
|
|
log.V(3).Info("skipping file as policy.TypeMeta.Kind not found")
|
|
|
|
return policies, nil
|
|
|
|
}
|
2023-04-24 00:22:29 +08:00
|
|
|
if policy.GetKind() != "ClusterPolicy" && policy.GetKind() != "Policy" {
|
|
|
|
return nil, fmt.Errorf("resource %s/%s is not a Policy or a ClusterPolicy", policy.GetKind(), policy.GetName())
|
2022-11-11 16:18:17 +01:00
|
|
|
}
|
|
|
|
|
2023-04-24 00:22:29 +08:00
|
|
|
if policy.GetKind() == "Policy" {
|
|
|
|
if policy.GetNamespace() == "" {
|
|
|
|
policy.SetNamespace("default")
|
2021-10-01 14:16:33 +05:30
|
|
|
}
|
2022-11-11 16:18:17 +01:00
|
|
|
} else {
|
2023-04-24 00:22:29 +08:00
|
|
|
policy.SetNamespace("")
|
2020-09-01 09:11:20 -07:00
|
|
|
}
|
2022-11-11 16:18:17 +01:00
|
|
|
policies = append(policies, policy)
|
2020-09-01 09:11:20 -07:00
|
|
|
return policies, nil
|
|
|
|
}
|