From 824fc4ee20b1ebe076f6e25bd8e6ecc89d584498 Mon Sep 17 00:00:00 2001 From: Preetha Appan Date: Thu, 3 Aug 2017 14:47:07 -0500 Subject: [PATCH] Unify regex used to identify invalid dns characters --- agent/agent.go | 8 ++------ agent/dns.go | 4 ++-- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/agent/agent.go b/agent/agent.go index ebb3228f58..99700f405c 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -13,7 +13,6 @@ import ( "net/http" "os" "path/filepath" - "regexp" "strconv" "strings" "sync" @@ -51,9 +50,6 @@ const ( "service, but no reason was provided. This is a default message." ) -// dnsNameRe checks if a name or tag is dns-compatible. -var dnsNameRe = regexp.MustCompile(`^[a-zA-Z0-9\-]+$`) - // delegate defines the interface shared by both // consul.Client and consul.Server. type delegate interface { @@ -1370,7 +1366,7 @@ func (a *Agent) AddService(service *structs.NodeService, chkTypes []*structs.Che } // Warn if the service name is incompatible with DNS - if !dnsNameRe.MatchString(service.Service) { + if InvalidDnsRe.MatchString(service.Service) { a.logger.Printf("[WARN] Service name %q will not be discoverable "+ "via DNS due to invalid characters. Valid characters include "+ "all alpha-numerics and dashes.", service.Service) @@ -1378,7 +1374,7 @@ func (a *Agent) AddService(service *structs.NodeService, chkTypes []*structs.Che // Warn if any tags are incompatible with DNS for _, tag := range service.Tags { - if !dnsNameRe.MatchString(tag) { + if InvalidDnsRe.MatchString(tag) { a.logger.Printf("[DEBUG] Service tag %q will not be discoverable "+ "via DNS due to invalid characters. Valid characters include "+ "all alpha-numerics and dashes.", tag) diff --git a/agent/dns.go b/agent/dns.go index 0678361706..8e458f6668 100644 --- a/agent/dns.go +++ b/agent/dns.go @@ -32,7 +32,7 @@ const ( defaultMaxUDPSize = 512 ) -var invalidCharsRe = regexp.MustCompile(`[^A-Za-z0-9\\-]+`) +var InvalidDnsRe = regexp.MustCompile(`[^A-Za-z0-9\\-]+`) // DNSServer is used to wrap an Agent and expose various // service discovery endpoints using a DNS interface. @@ -691,7 +691,7 @@ func (d *DNSServer) addAuthority(msg *dns.Msg) { serverAddrs := d.agent.delegate.ServerAddrs() for name, addr := range serverAddrs { ipAddrStr := strings.Split(addr, ":")[0] - sanitizedName := invalidCharsRe.ReplaceAllString(name, "-") // does some basic sanitization of the name + sanitizedName := InvalidDnsRe.ReplaceAllString(name, "-") // does some basic sanitization of the name nsName := "server-" + sanitizedName + "." + d.domain ip := net.ParseIP(ipAddrStr) if ip != nil {