Skip to content

Conversation

@yiraeChristineKim
Copy link
Contributor

@yiraeChristineKim yiraeChristineKim commented Jul 9, 2024

  • Add a new field of gatekeeperEnforcementAction
  • Default value of the field is unset and when unset, don't touch the constraint (existing behavior)
  • If set, then directly set spec.enforcementAction in the Gatekeeper constraint
  • Should be settable at the policyDefaults, policy, and manifest levels (i.e. like the ConfigurationPolicyOptions struct).
    Ref: https://issues.redhat.com/browse/ACM-11076
    Signed-off-by: yiraeChristineKim [email protected]

Comment on lines 202 to 204
# Optional. gatekeeperEnforcementAction("deny" | "warn" | "dryrun") will override gatekeeper.constraint.spec.enforcementAction
# This only applies to gatekeeper constraint
# The default is "deny"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Optional. gatekeeperEnforcementAction("deny" | "warn" | "dryrun") will override gatekeeper.constraint.spec.enforcementAction
# This only applies to gatekeeper constraint
# The default is "deny"
# Optional. Overrides the spec.enforcementAction field of a Gatekeeper constraint. This only applies to Gatekeeper constraints and is ignored by other manifests. If not set, the spec.enforcementAction field is not changed.


policyTemplateUnstructured.SetAnnotations(annotations)

err := setGatekeeperEnforcementAction(policyTemplate,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be cleaner to directly work with the manifest rather than after it is a policy template. You could do this right at the start of the for _, manifest := range manifestGroup { loop.

Name: "policy-gatekeeper",
Manifests: []types.Manifest{
{
Path: path.Join(tmpDir, "gatekeeper.yaml"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you reuse gatekeeperPath here and also in other places?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good!

policyDefaultEA: "",
expectedEA: `
spec:
enforcementAction: deny
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be cleaner if you just provided deny here and the test code can do the work of checking for spec and enforcementAction being in the generated string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh Great

@yiraeChristineKim yiraeChristineKim force-pushed the ACM-11076 branch 3 times, most recently from 53d65bb to 1215ea5 Compare July 10, 2024 15:18

// setGatekeeperEnforcementAction function override gatekeeper.constraint.enforcementAction
func setGatekeeperEnforcementAction(manifest map[string]interface{}, enforcementAction string) error {
apiVersion, found, err := unstructured.NestedString(manifest, "apiVersion")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you return right away if enforcementAction == ""?

Comment on lines 321 to 335
apiVersion, found, err := unstructured.NestedString(manifest, "apiVersion")
if err != nil {
return fmt.Errorf("getting apiVersion has an error %w", err)
}

if found && strings.HasPrefix(apiVersion, "constraints.gatekeeper.sh") && enforcementAction != "" {
err := unstructured.SetNestedField(manifest, enforcementAction, "spec", "enforcementAction")
if err != nil {
return err
}
}

return nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
apiVersion, found, err := unstructured.NestedString(manifest, "apiVersion")
if err != nil {
return fmt.Errorf("getting apiVersion has an error %w", err)
}
if found && strings.HasPrefix(apiVersion, "constraints.gatekeeper.sh") && enforcementAction != "" {
err := unstructured.SetNestedField(manifest, enforcementAction, "spec", "enforcementAction")
if err != nil {
return err
}
}
return nil
if enforcementAction == "" {
return nil
}
apiVersion, _, _ := unstructured.NestedString(manifest, "apiVersion")
if strings.HasPrefix(apiVersion, "constraints.gatekeeper.sh") {
err := unstructured.SetNestedField(manifest, enforcementAction, "spec", "enforcementAction")
if err != nil {
return err
}
}
return nil

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be more safe that we have error check here
apiVersion, _, err := unstructured.NestedString(manifest, "apiVersion") if err != nil { return err }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yiraeChristineKim that's fine but I kind of wanted to avoid validating the actual manifest in the Policy Generator and let the policy controllers handle it.

err := setGatekeeperEnforcementAction(manifest,
policyConf.Manifests[i].GatekeeperEnforcementAction)
if err != nil {
return nil, err
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you provide more context in the return error such as the manifest index and that this error occurred due to setting spec.enforcementAction?

Add a new field of `gatekeeperEnforcementAction`
Default value of the field is unset and when unset, don't touch the constraint (existing behavior)
If set, then directly set `spec.enforcementAction` in the Gatekeeper constraint
Should be settable at the policyDefaults, policy, and manifest levels (i.e. like the `ConfigurationPolicyOptions` struct).
Ref: https://issues.redhat.com/browse/ACM-11076
Signed-off-by: yiraeChristineKim <[email protected]>
@openshift-ci
Copy link

openshift-ci bot commented Jul 10, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: mprahl, yiraeChristineKim

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [mprahl,yiraeChristineKim]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants