Skip to content

Conversation

EmilienM
Copy link
Contributor

What this PR does / why we need it:

When port security is disabled on a port, don't add any security group
to the port options.

Skip the security groups when resolving the ports spec.
Report an error when a port tries to be created with both security
groups and disable port security to true.
Adds unit tests coverage for these scenarios.

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #2158

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Aug 15, 2024
@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Aug 15, 2024
Copy link

netlify bot commented Aug 15, 2024

Deploy Preview for kubernetes-sigs-cluster-api-openstack ready!

Name Link
🔨 Latest commit 4a7744a
🔍 Latest deploy log https://app.netlify.com/sites/kubernetes-sigs-cluster-api-openstack/deploys/66beb0e7e69fcd0008480cbf
😎 Deploy Preview https://deploy-preview-2159--kubernetes-sigs-cluster-api-openstack.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Contributor

@mdbooth mdbooth left a comment

Choose a reason for hiding this comment

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

Change looks sound. If my assumption about the error is correct, we should add API validation instead of runtime validation, though.

Comment on lines +173 to +175
if ptr.Deref(portSpec.DisablePortSecurity, false) {
return nil, errors.New("security groups cannot be set when port security is disabled")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

IIRC if you try to add a security group to a port with port security disabled you get an error, right? i.e. It's not just that it ignores the option, but it actually won't add the security groups?

If so, we should be able to safely add API validation for this instead, because we know there is no working configuration with it set.

i.e. We should write this as CEL instead. The tests would be in apivalidations.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IIRC if you try to add a security group to a port with port security disabled you get an error, right? i.e. It's not just that it ignores the option, but it actually won't add the security groups?

yeah right. And good points on API validations. I'll check that.

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've added tests in the webhook like other fields and tests in API validations. I've let this check on purpose because:

  1. it doesn't hurt, I think
  2. it's safe and it makes sense to report an error and not let it through anyway.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ack to leaving the check in.

I'd prefer we didn't add anything new to the webhooks, though, unless we absolutely have to. I take a look to see how easy the CEL is to write.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm going to withdraw this objection. While the CEL is simple enough to write, unfortunately because it affects both the SecurityGroups and DisablePortSecurity fields it needs to be implemented on the PortOpts struct rather than just one field. Unfortunately this struct is a monster, so I wasn't able to write the rule which doesn't exceed the complexity budget.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that was my thought when I looked at CEL and why I took the webhook way instead 😕

When port security is disabled on a port, don't add any security group
to the port options.

Skip the security groups when resolving the ports spec.
Report an error when a port tries to be created with both security
groups and disable port security to true.
Adds unit tests coverage for these scenarios.
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Aug 16, 2024
Comment on lines +68 to +72
for _, port := range newObj.Spec.Ports {
if ptr.Deref(port.DisablePortSecurity, false) && len(port.SecurityGroups) > 0 {
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "ports"), "cannot have security groups when DisablePortSecurity is set to true"))
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any reason we can't do this in CEL? We haven't added any new logic to the webhooks in a while and I'd like to phase them out.

@mdbooth
Copy link
Contributor

mdbooth commented Aug 16, 2024

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 16, 2024
@EmilienM
Copy link
Contributor Author

if you can /approve instead so I can ask @MaysaMacedo to take a look as well. Thanks

@mdbooth
Copy link
Contributor

mdbooth commented Aug 16, 2024

/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: mdbooth

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:

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

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 16, 2024
@k8s-ci-robot k8s-ci-robot merged commit 8973a4d into kubernetes-sigs:main Aug 16, 2024
9 checks passed
@EmilienM
Copy link
Contributor Author

@MaysaMacedo it merged by accident but when time permits, please have a look and let me know if anything is wrong, we'll fix afterwards. Thanks

@EmilienM EmilienM deleted the issue_1341 branch August 16, 2024 12:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Impossible to disable portSecurity on a port in OpenStackMachine
3 participants