Skip to content

Feature: Add option to delete default SG rule created upon VPC creation #212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 13, 2024

Conversation

nnbu
Copy link
Contributor

@nnbu nnbu commented Aug 19, 2024

Issue #, if available:

Description of changes:
When a VPC is created, it also creates 'default' security group. The 'default' security group has widely open egress rules. We need to have an option to delete this autocreated rules from 'default' security group. The 'default' security group itself can not be deleted.

Discussion:
https://kubernetes.slack.com/archives/C0402D8JJS1/p1720560499642019

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@ack-prow ack-prow bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Aug 19, 2024
Copy link

ack-prow bot commented Aug 19, 2024

Hi @nnbu. Thanks for your PR.

I'm waiting for a aws-controllers-k8s member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@ack-prow ack-prow bot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Aug 19, 2024
@nnbu nnbu force-pushed the delDefaultSG branch 5 times, most recently from b316b1e to b8f2c63 Compare August 19, 2024 18:40
@nnbu nnbu changed the title Fix VPC default SG rule Fix: Add option to delete default SG rule created upon VPC creation Aug 19, 2024
@nnbu nnbu marked this pull request as ready for review August 19, 2024 18:50
@ack-prow ack-prow bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Aug 19, 2024
@a-hilaly
Copy link
Member

/ok-to-test

@ack-prow ack-prow bot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Aug 27, 2024
Copy link
Member

@a-hilaly a-hilaly left a comment

Choose a reason for hiding this comment

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

Good stuff, thank you so much @nnbu for your contribution! :)
i left few comments in-line

@@ -207,6 +207,15 @@ func (rm *resourceManager) sdkFind(
ko.Spec.EnableDNSSupport = dnsAttrs.EnableSupport
ko.Spec.EnableDNSHostnames = dnsAttrs.EnableHostnames
}
defaultSGRuleExist, err := rm.hasDefaultSecurityGroupRule(ctx, &resource{ko})
Copy link
Member

Choose a reason for hiding this comment

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

We need to handle this error

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

return nil, err
}

return resp.SecurityGroups[0].GroupId, nil
Copy link
Member

Choose a reason for hiding this comment

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

I guess it's safe to access [0] here, since default SG can't be deleted?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, default SG has to be present. But I guess, it does not hurt checking for the access. So, I added it now

Comment on lines 525 to 569
func toStrPtr(str string) *string {
return &str
}

func toInt64Ptr(integer int64) *int64 {
return &integer
}
Copy link
Member

Choose a reason for hiding this comment

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

nit:

func ptr[t T](target T) *T { return &t }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done. Thanks

Comment on lines +433 to +455
if rule.CidrIpv4 == nil || rule.FromPort == nil || rule.ToPort == nil || rule.IpProtocol == nil || rule.IsEgress == nil {
return false
}

if *rule.CidrIpv4 == "0.0.0.0/0" &&
*rule.FromPort == -1 &&
*rule.ToPort == -1 &&
*rule.IpProtocol == "-1" &&
*rule.IsEgress {
return true
}
Copy link
Member

@a-hilaly a-hilaly Aug 27, 2024

Choose a reason for hiding this comment

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

i wonder if non default security groups can have these attributes? i guess no, just making sure we're getting it right

Copy link
Contributor Author

Choose a reason for hiding this comment

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

isDefaultSGEgressRule is only checking whether the passed rule is same as auto populated egress rule or not. Non-default security groups can intentionally add this rule, if they need. However, isDefaultSGEgressRule function gets called from hasDefaultEgressRule which gets security group ID of 'default' security group. So, we are never going to touch this rule for non-default security groups.

Comment on lines 297 to 310
if desired.ko.Spec.DeleteDefaultSecurityGroupRule != nil && *desired.ko.Spec.DeleteDefaultSecurityGroupRule {
if err = rm.deleteDefaultSecurityGroupRule(ctx, desired); err != nil {
// if deleteDefaultSecurityGroupRule fails, assume that the rule
// still exists and update the status accordingly.
exist := true
updated.ko.Status.DefaultSecurityGroupRuleExist = &exist
return nil, err
}
exist := false
updated.ko.Status.DefaultSecurityGroupRuleExist = &exist
}
Copy link
Member

Choose a reason for hiding this comment

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

Do we want to wrap this with if delta.DifferentAt("Spec.DeleteDefaultSecurityGroupRule") ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Comment on lines 268 to 282
# Set deleteDefaultSecurityGroupRule to delete default security group
# rule
updates = {
"spec": {"deleteDefaultSecurityGroupRule": True}
}
Copy link
Member

Choose a reason for hiding this comment

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

This feels a little like an imperative call, instead of a declarative approach.. especially when switching back to False is a no-op. Just thinking out-loud, i'm not against this approach.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My intentions was to test two scenarios.

  1. In test_delete_default_security_group_rule : set deleteDefaultSecurityGroupRule to true upfront. This tests create path
  2. In test_update_delete_default_security_group_rule : set deleteDefaultSecurityGroupRule to true as part of Update. This tests update path

@nnbu nnbu force-pushed the delDefaultSG branch 5 times, most recently from 9b7e4c3 to b7a60e4 Compare August 30, 2024 18:32
Copy link
Contributor

@TiberiuGC TiberiuGC left a comment

Choose a reason for hiding this comment

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

Outstanding work @nnbu! 🔥 Thanks a lot for updating the PR as discussed.
Just a few nits inline.

DisallowSecurityGroupDefaultRules:
type: bool
is_required: false
DefaultSecurityGroupRuleExist:
Copy link
Contributor

@TiberiuGC TiberiuGC Sep 11, 2024

Choose a reason for hiding this comment

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

Suggested change
DefaultSecurityGroupRuleExist:
SecurityGroupDefaultRulesExist:

should this be plural? how about the name change suggestion, wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

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

It's really annoying that ideally we'd use the word default twice. As both the SG is the default one for VPC and the rules are the default ones for SG.

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 agree. I have now renamed everything to on the lines of SecurityGroupDefaultRules

Copy link

ack-prow bot commented Sep 12, 2024

@nnbu: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
ec2-verify-attribution 48d668a link false /test ec2-verify-attribution

Full PR test history. Your PR dashboard.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

Copy link
Member

@a-hilaly a-hilaly left a comment

Choose a reason for hiding this comment

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

This is awesome! Thank you @nnbu @TiberiuGC !!

@a-hilaly
Copy link
Member

/lgtm

@ack-prow ack-prow bot added the approved label Sep 13, 2024
@ack-prow ack-prow bot added the lgtm Indicates that a PR is ready to be merged. label Sep 13, 2024
Copy link

ack-prow bot commented Sep 13, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: a-hilaly, nnbu, TiberiuGC

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

@ack-prow ack-prow bot merged commit b18aa39 into aws-controllers-k8s:main Sep 13, 2024
6 of 7 checks passed
@nnbu nnbu changed the title Fix: Add option to delete default SG rule created upon VPC creation Feature: Add option to delete default SG rule created upon VPC creation Sep 16, 2024
nnbu added a commit to nnbu/ack-ec2-controller that referenced this pull request Sep 18, 2024
…ws-controllers-k8s#212)

Issue #, if available:

Description of changes:
When a VPC is created, it also creates 'default' security group. The 'default' security group has widely open egress rules. We need to have an option to delete this autocreated rules from 'default' security group. The 'default' security group itself can not be deleted.

Discussion:
https://kubernetes.slack.com/archives/C0402D8JJS1/p1720560499642019

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved lgtm Indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants