Skip to content

lang/funcs: update cidrsubnet and cidrhost for 64-bit systems#25517

Merged
mildwonkey merged 3 commits intomasterfrom
mildwonkey/embiggen-cidr
Aug 11, 2020
Merged

lang/funcs: update cidrsubnet and cidrhost for 64-bit systems#25517
mildwonkey merged 3 commits intomasterfrom
mildwonkey/embiggen-cidr

Conversation

@mildwonkey
Copy link
Copy Markdown
Contributor

The cidrsubnet and cidrhost functions were limited to supporting 32-bit systems. This PR:

  • updates the "github.com/apparentlymart/go-cidr" library, which was recently extended with SubnetBig and HostBig functions
  • switches to the new go-cidr functions, including passing appropriate values as big.Ints
  • modifies terraform's validation check in cidrsubnet to check for bits > 64 instead of 32

Fixes #25360

There's a new test, but for good measure, have some terraform console output:

> cidrsubnet("fe08::/64", 63, 0)
fe08::/127

@mildwonkey mildwonkey requested a review from a team July 8, 2020 15:04
@mildwonkey mildwonkey changed the title Mildwonkey/embiggen cidr lang/funcs: update cidrsubnet and cidrhost for 64-bit systems Jul 8, 2020
@codecov
Copy link
Copy Markdown

codecov bot commented Jul 8, 2020

Codecov Report

Merging #25517 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted Files Coverage Δ
lang/funcs/cidr.go 84.61% <100.00%> (+7.11%) ⬆️
states/statefile/version4.go 57.87% <0.00%> (-0.29%) ⬇️

Type: function.StaticReturnType(cty.String),
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
var hostNum int
var hostNum *big.Int
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.

I had honestly completely forgotten that gocty knows how to decode into big.Int! I'm glad to remember now that it does, cause I was originally expecting this change to be a bit more invasive.

One thing I notice looking back at the test cases for these functions is that they aren't testing the situation where the newbits, hostnum, netnum arguments are fractional numbers. I'm expecting that all of those would be properly rejected as invalid by the gocty conversions here (it should say something like "value must be a whole number") but might be worth adding some "expected error" test cases as insurance to future changes that might inadvertently make those panic otherwise.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done and done!

// (Of course, this is significant only for IPv6.)
if newbits > 32 {
return cty.UnknownVal(cty.String), fmt.Errorf("may not extend prefix by more than 32 bits")
if newbits > 64 {
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.

I don't think we need to have this check at all anymore, because the underlying CIDR library will already check to make sure that there's enough room to extend by newbits bits. I had originally put the > 32 check here just to make sure this function wouldn't behave differently on 32-bit vs. 64-bit builds of Terraform, but using *big.Int now means that the behavior of SubnetBig should be the same for all architectures and so we shouldn't need to apply additional constraints in the Terraform layer here.

(When using IPv6, it would also be "valid" to start off with a very short prefix like e.g. 1::/1 and extend that by up to 127 new bits, which SubnetBig should accept just fine now and might be worth including a test for just to exercise the boundary cases a bit more.)

@mildwonkey mildwonkey requested a review from apparentlymart July 8, 2020 17:14
@mildwonkey mildwonkey added this to the v0.13.1 milestone Jul 8, 2020
@mildwonkey mildwonkey force-pushed the mildwonkey/embiggen-cidr branch from 9acb90b to e639d81 Compare July 8, 2020 17:54
Copy link
Copy Markdown
Contributor

@apparentlymart apparentlymart left a comment

Choose a reason for hiding this comment

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

Looks great! 🎉

@mildwonkey mildwonkey merged commit 4729f7f into master Aug 11, 2020
@mildwonkey mildwonkey deleted the mildwonkey/embiggen-cidr branch August 11, 2020 15:51
@ghost
Copy link
Copy Markdown

ghost commented Sep 11, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Sep 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cidrsubnet doesn't work with ipv6 subnets

2 participants