hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io
Other
43.14k stars 9.58k forks source link

uuid() generates UUID with incorrect version fields. Wrong V4 format #29493

Open filol opened 3 years ago

filol commented 3 years ago

Terraform Version

Terraform v1.0.5

Terraform Configuration Files

output "uuid" {
  value = "${uuid()}"
}

Expected Behavior

As write here : https://www.terraform.io/docs/language/functions/uuid.html

The id is a generated and formatted as required by RFC 4122 section 4.4, producing a Version 4 UUID. The result is a UUID generated only from pseudo-random numbers.

So the uuid should respect the RFC

Actual Behavior

Terraform outputs a UUID which doesn't encode the correct RFC 4122 v4 version information, for example, 800e82e5-b042-9ebd-944c-831673b7fc5d

References

It's a duplicate of this issue #18808 but the doc is still wrong

apparentlymart commented 3 years ago

Thanks for reporting this, @filol!

Given that even we seem to keep confusing ourselves about having a function called uuid that doesn't actually generate a UUID, my instinct here is to reverse our previous position and switch this function to generating a standards-compliant version 4 UUID.

Something that's changed since our original discussion about this is that we ended up adding a standards-compliant UUID library to Terraform's dependencies anyway, to implement the uuidv5 function, and so making the uuid function also use that library (uuid.NewRandom, specifically) wouldn't increase our dependency burden at all.

This does mean that the result of uuid will, as a practical matter, be slightly less random than it was before, due to including the fixed version portions you're talking about here, but that shouldn't be a material difference for any reasonable existing use of this function, and our documentation has never promised that the result would be exactly 128 bits of random number. I think it's more intuitive to match the standard definition of what a UUID is, and thus allow folks to bring the same expectations they might make about version 4 UUIDs generated in other systems (122 bits of random).

leventyalcin commented 5 months ago

I'd still appreciate if uuid was UUIDv4 compliant