Skip to content

fix: create security group rule with any protocol - #3157

Merged
pierreprinetti merged 7 commits into
gophercloud:masterfrom
kmlebedev:sec_group_rule_create_any_protocol
Jul 29, 2024
Merged

pierreprinetti merged 7 commits into
gophercloud:masterfrom
kmlebedev:sec_group_rule_create_any_protocol

Conversation

@kmlebedev

@kmlebedev kmlebedev commented Jul 24, 2024 •

Copy link
Copy Markdown

Fixes #2442 after #2309

@github-actions github-actions Bot added edit:networking This PR updates networking code semver:patch No API change labels Jul 24, 2024

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thank you for submitting your first PR! Be sure that we will be looking at it but keep in mind
this sometimes takes a while.
Please let the maintainers know if your PR has not got enough attention after a few days.
If any doubt, please consult our PR tutorial.

@pierreprinetti pierreprinetti self-assigned this Jul 24, 2024
@pierreprinetti

Copy link
Copy Markdown
Member

I'm afraid that I'm missing something here. What is the problem that this patch is solving?

A minimal reproducer in code would be great.

/hold

@kmlebedev

kmlebedev commented Jul 24, 2024 •

Copy link
Copy Markdown
Author

I'm afraid that I'm missing something here. What is the problem that this patch is solving?

A minimal reproducer in code would be great.

/hold

I did the same https://github.com/gophercloud/gophercloud/blob/master/openstack/networking/v2/extensions/fwaas_v2/rules/requests.go#L141

// ToRuleCreateMap casts a CreateOpts struct to a map.
func (opts CreateOpts) ToRuleCreateMap() (map[string]any, error) {
	b, err := gophercloud.BuildRequestBody(opts, "firewall_rule")
	if err != nil {
		return nil, err
	}

	if m := b["firewall_rule"].(map[string]any); m["protocol"] == "any" {
		m["protocol"] = nil
	}

	return b, nil
}

@kayrus

kayrus commented Jul 24, 2024 •

Copy link
Copy Markdown
Contributor

There is another issue. When you retrieve the rule, the protocol is a string, and for any it will be an empty string. External code would need to make some additional checks.

        if sgRule.Protocol == "" {
                sgRule.Protocol = string(rules.ProtocolAny)
        }

Maybe it makes sense to change this directly in gophercloud.

see an example terraform-provider-openstack/terraform-provider-openstack#1765 (comment)

@kmlebedev

kmlebedev commented Jul 24, 2024 •

Copy link
Copy Markdown
Author

There is another issue. When you retrieve the rule, the protocol is a string, and for any it will be an empty string. External code would need to make some additional checks.

        if sgRule.Protocol == "" {
                sgRule.Protocol = string(rules.ProtocolAny)
        }

Maybe it makes sense to change this directly in gophercloud.

see an example terraform-provider-openstack/terraform-provider-openstack#1765 (comment)

@kayrus Thank you for helping. Done

@kayrus

kayrus commented Jul 24, 2024

Copy link
Copy Markdown
Contributor

Terraform supports integer value for the protocol. What if an end user provides "0"? I think we also need to convert it to null.

@kmlebedev

Copy link
Copy Markdown
Author

Terraform supports integer value for the protocol. What if an end user provides "0"? I think we also need to convert it to null.

I didn't change the value 0.
Just made the register comparison a little easier.

len(s.SecGroupRule.Protocol) == 0

eq

s.SecGroupRule.Protocol) == ""

@kayrus

kayrus commented Jul 24, 2024 •

Copy link
Copy Markdown
Contributor

@kmlebedev currently I'm testing corner cases with terraform provider. Just give me some time to post the results of my findings.

@kayrus

kayrus commented Jul 24, 2024

Copy link
Copy Markdown
Contributor

@kmlebedev and @HappyFX you both opened a can of worms.

It's possible to set the protocol to "0" and this will not work. There is also a way to set it to 0 and I this will also cause "0" in POST response. I'd like a confirmation that the "0" protocol doesn't work and we need to ensure that any 0, "" or "0" are detected converted to null.

@kayrus

kayrus commented Jul 24, 2024 •

Copy link
Copy Markdown
Contributor

There is also missing hopopt, ip which represent the 0 protocol and icmpv6 protocol support
After a quick analysis of the https://github.com/openstack/neutron-lib/blob/90dcbe1f6be04aba5ac90201045be3721c06625e/neutron_lib/constants.py#L474 I found out that ETHERTYPE_ARP = 0x0806 and ETHERTYPE_RARP = 0x8035 are also missing.

@pierreprinetti

Copy link
Copy Markdown
Member

I know I may sound like a broken record, but... what is the problem that this patch is solving?

@pierreprinetti

pierreprinetti commented Jul 25, 2024 •

Copy link
Copy Markdown
Member

For anyone following the conversation:

  • this is the problem that this patch is attempting to solve
  • this is why we might not need this patch (please advise)

@pierreprinetti

pierreprinetti commented Jul 25, 2024 •

Copy link
Copy Markdown
Member

After some discussion in #2442, I now believe that the fix here should be to change ProtocolAny to "" , so that Gophercloud omits the protocol property from the payload and Neutron correctly sets None.

I think that this patch should really be:

diff --git a/openstack/networking/v2/extensions/security/rules/requests.go b/openstack/networking/v2/extensions/security/rules/requests.go
index 78b67b0d..f2ea9f2d 100644
--- a/openstack/networking/v2/extensions/security/rules/requests.go
+++ b/openstack/networking/v2/extensions/security/rules/requests.go
@@ -77,7 +77,7 @@ const (
 	ProtocolUDP       RuleProtocol  = "udp"
 	ProtocolUDPLite   RuleProtocol  = "udplite"
 	ProtocolVRRP      RuleProtocol  = "vrrp"
-	ProtocolAny       RuleProtocol  = "any"
+	ProtocolAny       RuleProtocol  = ""
 )
 
 // CreateOptsBuilder allows extensions to add additional parameters to the

What do you think?

@pierreprinetti

Copy link
Copy Markdown
Member

Or maybe that should be "0" instead of "", so that the same value can also be used as a filter in List.

@HappyFX

HappyFX commented Jul 25, 2024

Copy link
Copy Markdown
Contributor

What do you think?

Empty looks correct if it will omitted, still neutron API will response as "protocol": null and how handle it?

Or maybe that should be "0"

"0" is act as protocol set with "0x00" HOPOPT - it's will be not expecting filter (not "what ever protocol") security group in openstack.

@pierreprinetti

Copy link
Copy Markdown
Member

@kmlebedev Are you OK with changing this PR to be something like this? Would that fit your use-case?

@kmlebedev

Copy link
Copy Markdown
Author

@kmlebedev Are you OK with changing this PR to be something like this? Would that fit your use-case?

@pierreprinetti Done

@github-actions github-actions Bot removed the semver:patch No API change label Jul 26, 2024
@github-actions github-actions Bot added the semver:major Breaking change label Jul 26, 2024
@kayrus

kayrus commented Jul 26, 2024

Copy link
Copy Markdown
Contributor

I updated a corresponding terraform-provider-openstack/terraform-provider-openstack#1765

@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 78.738%. remained the same
when pulling 97ea7e1 on kmlebedev:sec_group_rule_create_any_protocol
into 0fc78e6 on gophercloud:master.

@kayrus kayrus left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, but I'm confused with the semver:major label.

@pierreprinetti

Copy link
Copy Markdown
Member

label:semver is technically correct because we are changing an exported value. However, no valid behaviour is going to be impacted by this change since "any" has never worked to the best of my knowledge.

Thank you

@pierreprinetti pierreprinetti added backport-v1 This PR will be backported to v1 semver:patch No API change backport-v2 This PR will be backported to v2 and removed semver:major Breaking change labels Jul 29, 2024
@pierreprinetti
pierreprinetti merged commit a43e70b into gophercloud:master Jul 29, 2024
@github-actions

Copy link
Copy Markdown

Failed to backport PR to v1 branch. See logs for details.

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

Labels

backport-v1 This PR will be backported to v1 backport-v2 This PR will be backported to v2 edit:networking This PR updates networking code semver:patch No API change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix name in protocolAny

5 participants