-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Index Kyverno policies and policy reports (#327)
PolicyReport indexing was previously focused on ACM policy results for Insights. This now also accounts for PolicyReport and ClusterPolicyReport objects generated by Kyverno by having the correct numRuleViolations values and edges created between the report and the Kyverno policy. Relates: https://issues.redhat.com/browse/ACM-15127 Signed-off-by: mprahl <[email protected]>
- Loading branch information
Showing
9 changed files
with
635 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright Contributors to the Open Cluster Management project | ||
|
||
package transforms | ||
|
||
import ( | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
) | ||
|
||
type KyvernoPolicyResource struct { | ||
node Node | ||
} | ||
|
||
// KyvernoPolicyResourceBuilder handles Kyverno Policy and ClusterPolicy objects. See: | ||
// | ||
// https://github.com/kyverno/kyverno/blob/main/config/crds/kyverno/kyverno.io_policies.yaml | ||
// https://github.com/kyverno/kyverno/blob/main/config/crds/kyverno/kyverno.io_clusterpolicies.yaml | ||
func KyvernoPolicyResourceBuilder(p *unstructured.Unstructured) *KyvernoPolicyResource { | ||
node := transformCommon(p) // Start off with the common properties | ||
apiGroupVersion(metav1.TypeMeta{Kind: p.GetKind(), APIVersion: p.GetAPIVersion()}, &node) | ||
node.Properties["_isExternal"] = getIsPolicyExternal(p) | ||
if validationFailureAction, ok, _ := unstructured.NestedString(p.Object, "spec", "validationFailureAction"); ok { | ||
node.Properties["validationFailureAction"] = validationFailureAction | ||
} else { | ||
// Audit is the default value and this makes the indexing easier | ||
node.Properties["validationFailureAction"] = "Audit" | ||
} | ||
|
||
if background, ok, _ := unstructured.NestedBool(p.Object, "spec", "background"); ok { | ||
node.Properties["background"] = background | ||
} else { | ||
// true is the default value and this makes the indexing easier | ||
node.Properties["background"] = true | ||
} | ||
|
||
if admission, ok, _ := unstructured.NestedBool(p.Object, "spec", "admission"); ok { | ||
node.Properties["admission"] = admission | ||
} else { | ||
// true is the default value and this makes the indexing easier | ||
node.Properties["admission"] = true | ||
} | ||
|
||
node.Properties["severity"] = p.GetAnnotations()["policies.kyverno.io/severity"] | ||
|
||
return &KyvernoPolicyResource{node: node} | ||
} | ||
|
||
func (p KyvernoPolicyResource) BuildNode() Node { | ||
return p.node | ||
} | ||
|
||
func (p KyvernoPolicyResource) BuildEdges(ns NodeStore) []Edge { | ||
return []Edge{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright Contributors to the Open Cluster Management project | ||
|
||
package transforms | ||
|
||
import ( | ||
"testing" | ||
|
||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
) | ||
|
||
func TestTransformKyvernoPolicy(t *testing.T) { | ||
p := unstructured.Unstructured{ | ||
Object: map[string]interface{}{ | ||
"apiVersion": "kyverno.io/v1", | ||
"kind": "Policy", | ||
"metadata": map[string]interface{}{ | ||
"name": "my-policy", | ||
"namespace": "my-app", | ||
"annotations": map[string]interface{}{ | ||
"policies.kyverno.io/severity": "medium", | ||
}, | ||
}, | ||
"spec": map[string]interface{}{ | ||
"validationFailureAction": "Deny", | ||
"random": "value", | ||
}, | ||
}, | ||
} | ||
rv := KyvernoPolicyResourceBuilder(&p) | ||
node := rv.node | ||
|
||
AssertEqual("validationFailureAction", node.Properties["validationFailureAction"], "Deny", t) | ||
AssertEqual("background", node.Properties["background"], true, t) | ||
AssertEqual("admission", node.Properties["admission"], true, t) | ||
AssertEqual("severity", node.Properties["severity"], "medium", t) | ||
|
||
// Check the default value for spec.validationFailureAction | ||
unstructured.RemoveNestedField(p.Object, "spec", "validationFailureAction") | ||
|
||
rv = KyvernoPolicyResourceBuilder(&p) | ||
node = rv.node | ||
AssertEqual("validationFailureAction", node.Properties["validationFailureAction"], "Audit", t) | ||
} | ||
|
||
func TestTransformKyvernoClusterPolicy(t *testing.T) { | ||
p := unstructured.Unstructured{ | ||
Object: map[string]interface{}{ | ||
"apiVersion": "kyverno.io/v1", | ||
"kind": "ClusterPolicy", | ||
"metadata": map[string]interface{}{ | ||
"name": "my-policy", | ||
"annotations": map[string]interface{}{ | ||
"policies.kyverno.io/severity": "critical", | ||
}, | ||
}, | ||
"spec": map[string]interface{}{ | ||
"validationFailureAction": "Deny", | ||
"random": "value", | ||
"background": false, | ||
"admission": false, | ||
}, | ||
}, | ||
} | ||
rv := KyvernoPolicyResourceBuilder(&p) | ||
node := rv.node | ||
|
||
AssertEqual("validationFailureAction", node.Properties["validationFailureAction"], "Deny", t) | ||
AssertEqual("background", node.Properties["background"], false, t) | ||
AssertEqual("admission", node.Properties["admission"], false, t) | ||
AssertEqual("severity", node.Properties["severity"], "critical", t) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.