Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions internal/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,11 @@ metadata:
},
}
p.Policies = append(p.Policies, policyConf)
p.applyDefaults(map[string]interface{}{})
p.applyDefaults(map[string]interface{}{
"policyDefaults": map[string]interface{}{
"informGatekeeperPolicies": false,
},
})

err = p.createPolicy(&p.Policies[0])
if err != nil {
Expand Down Expand Up @@ -1249,7 +1253,11 @@ metadata:
},
}
p.Policies = append(p.Policies, policyConf)
p.applyDefaults(map[string]interface{}{})
p.applyDefaults(map[string]interface{}{
"policyDefaults": map[string]interface{}{
"informGatekeeperPolicies": false,
},
})

err = p.createPolicy(&p.Policies[0])
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions internal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ func getPolicyTemplates(policyConf *types.PolicyConfig) ([]map[string]interface{
extraDeps := policyConf.Manifests[i].ExtraDependencies

for _, manifest := range manifestGroup {
isPolicyTypeManifest, isOcmPolicy, err := isPolicyTypeManifest(manifest)
isPolicyTypeManifest, isOcmPolicy, err := isPolicyTypeManifest(
manifest, policyConf.InformGatekeeperPolicies)
if err != nil {
return nil, fmt.Errorf(
"%w in manifest path: %s",
Expand Down Expand Up @@ -289,7 +290,7 @@ func setTemplateOptions(tmpl map[string]interface{}, ignorePending bool, extraDe
// - apiVersion and kind fields can't be determined
// - the manifest is a root policy manifest
// - the manifest is invalid because it is missing a name
func isPolicyTypeManifest(manifest map[string]interface{}) (bool, bool, error) {
func isPolicyTypeManifest(manifest map[string]interface{}, informGatekeeperPolicies bool) (bool, bool, error) {
apiVersion, found, err := unstructured.NestedString(manifest, "apiVersion")
if !found || err != nil {
return false, false, errors.New("invalid or not found apiVersion")
Expand All @@ -315,7 +316,7 @@ func isPolicyTypeManifest(manifest map[string]interface{}) (bool, bool, error) {
isGkConstraint := strings.HasPrefix(apiVersion, "constraints.gatekeeper.sh")
isGkObj := isGkConstraintTemplate || isGkConstraint

isPolicy := isOcmPolicy || isGkObj
isPolicy := isOcmPolicy || (isGkObj && !informGatekeeperPolicies)

if isPolicy {
// metadata.name is required on policy manifests
Expand Down
41 changes: 34 additions & 7 deletions internal/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -593,10 +593,11 @@ func TestIsPolicyTypeManifest(t *testing.T) {
t.Parallel()

tests := map[string]struct {
manifest map[string]interface{}
wantIsPolicy bool
wantIsOcmPolicy bool
wantErr string
manifest map[string]interface{}
informGatekeeperPolicies bool
wantIsPolicy bool
wantIsOcmPolicy bool
wantErr string
}{
"valid RandomPolicy": {
manifest: map[string]interface{}{
Expand All @@ -622,7 +623,33 @@ func TestIsPolicyTypeManifest(t *testing.T) {
wantIsOcmPolicy: true,
wantErr: "",
},
"valid Gatekeeper Constraint": {
"valid Gatekeeper Constraint with expander": {
manifest: map[string]interface{}{
"apiVersion": "constraints.gatekeeper.sh",
"kind": "Foo",
"metadata": map[string]interface{}{
"name": "foo",
},
},
informGatekeeperPolicies: true,
wantIsPolicy: false,
wantIsOcmPolicy: false,
wantErr: "",
},
"valid Gatekeeper ConstraintTemplate with expander": {
manifest: map[string]interface{}{
"apiVersion": "templates.gatekeeper.sh",
"kind": "ConstraintTemplate",
"metadata": map[string]interface{}{
"name": "foo",
},
},
informGatekeeperPolicies: true,
wantIsPolicy: false,
wantIsOcmPolicy: false,
wantErr: "",
},
"valid Gatekeeper Constraint without expander": {
manifest: map[string]interface{}{
"apiVersion": "constraints.gatekeeper.sh",
"kind": "Foo",
Expand All @@ -634,7 +661,7 @@ func TestIsPolicyTypeManifest(t *testing.T) {
wantIsOcmPolicy: false,
wantErr: "",
},
"valid Gatekeeper ConstraintTemplate": {
"valid Gatekeeper ConstraintTemplate without expander": {
manifest: map[string]interface{}{
"apiVersion": "templates.gatekeeper.sh",
"kind": "ConstraintTemplate",
Expand Down Expand Up @@ -738,7 +765,7 @@ func TestIsPolicyTypeManifest(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()

gotIsPolicy, gotIsOcmPolicy, gotErr := isPolicyTypeManifest(test.manifest)
gotIsPolicy, gotIsOcmPolicy, gotErr := isPolicyTypeManifest(test.manifest, test.informGatekeeperPolicies)
if gotErr != nil {
assertEqual(t, gotErr.Error(), test.wantErr)
}
Expand Down