Skip to content
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

Hash host ID so its stable and well distributed #2541

Merged
merged 2 commits into from
Apr 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,9 +655,13 @@ func (c *Client) getAllocRunners() map[string]*AllocRunner {
func (c *Client) nodeID() (id, secret string, err error) {
var hostID string
hostInfo, err := host.Info()
if !c.config.NoHostUUID && err == nil && helper.IsUUID(hostInfo.HostID) {
hostID = hostInfo.HostID
} else {
if !c.config.NoHostUUID && err == nil {
if hashed, ok := helper.HashUUID(hostInfo.HostID); ok {
hostID = hashed
}
}

if hostID == "" {
// Generate a random hostID if no constant ID is available on
// this platform.
hostID = structs.GenerateUUID()
Expand Down
21 changes: 21 additions & 0 deletions helper/funcs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package helper

import (
"crypto/sha512"
"fmt"
"regexp"
"time"
)
Expand All @@ -18,6 +20,25 @@ func IsUUID(str string) bool {
return validUUID.MatchString(str)
}

// HashUUID takes an input UUID and returns a hashed version of the UUID to
// ensure it is well distributed.
func HashUUID(input string) (output string, hashed bool) {
if !IsUUID(input) {
Copy link
Member

Choose a reason for hiding this comment

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

Why do we only hash things that look like UUIDs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We want to ensure that the UUID reported by the host has a certain amount of entropy (not 1234, etc)

return "", false
}

// Hash the input
buf := sha512.Sum512([]byte(input))
output = fmt.Sprintf("%08x-%04x-%04x-%04x-%12x",
buf[0:4],
buf[4:6],
buf[6:8],
buf[8:10],
buf[10:16])

return output, true
}

// boolToPtr returns the pointer to a boolean
func BoolToPtr(b bool) *bool {
return &b
Expand Down