From 31333eecae172678c5ce2dd4e76c511bb6a19649 Mon Sep 17 00:00:00 2001 From: Sean Chittenden Date: Thu, 2 Feb 2017 16:24:32 -0800 Subject: [PATCH] Add better verification of a host's HostID. --- client/client.go | 3 ++- helper/funcs.go | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/client/client.go b/client/client.go index 3ad8f07bd..16b3ac401 100644 --- a/client/client.go +++ b/client/client.go @@ -27,6 +27,7 @@ import ( "github.com/hashicorp/nomad/client/stats" "github.com/hashicorp/nomad/client/vaultclient" "github.com/hashicorp/nomad/command/agent/consul" + "github.com/hashicorp/nomad/helper" "github.com/hashicorp/nomad/helper/tlsutil" "github.com/hashicorp/nomad/nomad" "github.com/hashicorp/nomad/nomad/structs" @@ -636,7 +637,7 @@ func (c *Client) getAllocRunners() map[string]*AllocRunner { func (c *Client) nodeID() (id, secret string, err error) { var hostID string hostInfo, err := host.Info() - if err == nil && hostInfo.HostID != "" { + if err == nil && helper.IsUUID(hostInfo.HostID) { hostID = hostInfo.HostID } else { // Generate a random hostID if no constant ID is available on diff --git a/helper/funcs.go b/helper/funcs.go index 653c3a100..89538f42c 100644 --- a/helper/funcs.go +++ b/helper/funcs.go @@ -1,5 +1,20 @@ package helper +import "regexp" + +// validUUID is used to check if a given string looks like a UUID +var validUUID = regexp.MustCompile(`(?i)^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$`) + +// IsUUID returns true if the given string is a valid UUID. +func IsUUID(str string) bool { + const uuidLen = 36 + if len(str) != uuidLen { + return false + } + + return validUUID.MatchString(str) +} + // boolToPtr returns the pointer to a boolean func BoolToPtr(b bool) *bool { return &b