Unify regex used to identify invalid dns characters

This commit is contained in:
Preetha Appan 2017-08-03 14:47:07 -05:00 committed by Frank Schroeder
parent 37f75a393e
commit 824fc4ee20
No known key found for this signature in database
GPG Key ID: 4D65C6EAEC87DECD
2 changed files with 4 additions and 8 deletions

View File

@ -13,7 +13,6 @@ import (
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -51,9 +50,6 @@ const (
"service, but no reason was provided. This is a default message." "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 // delegate defines the interface shared by both
// consul.Client and consul.Server. // consul.Client and consul.Server.
type delegate interface { 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 // 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 "+ a.logger.Printf("[WARN] Service name %q will not be discoverable "+
"via DNS due to invalid characters. Valid characters include "+ "via DNS due to invalid characters. Valid characters include "+
"all alpha-numerics and dashes.", service.Service) "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 // Warn if any tags are incompatible with DNS
for _, tag := range service.Tags { for _, tag := range service.Tags {
if !dnsNameRe.MatchString(tag) { if InvalidDnsRe.MatchString(tag) {
a.logger.Printf("[DEBUG] Service tag %q will not be discoverable "+ a.logger.Printf("[DEBUG] Service tag %q will not be discoverable "+
"via DNS due to invalid characters. Valid characters include "+ "via DNS due to invalid characters. Valid characters include "+
"all alpha-numerics and dashes.", tag) "all alpha-numerics and dashes.", tag)

View File

@ -32,7 +32,7 @@ const (
defaultMaxUDPSize = 512 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 // DNSServer is used to wrap an Agent and expose various
// service discovery endpoints using a DNS interface. // service discovery endpoints using a DNS interface.
@ -691,7 +691,7 @@ func (d *DNSServer) addAuthority(msg *dns.Msg) {
serverAddrs := d.agent.delegate.ServerAddrs() serverAddrs := d.agent.delegate.ServerAddrs()
for name, addr := range serverAddrs { for name, addr := range serverAddrs {
ipAddrStr := strings.Split(addr, ":")[0] 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 nsName := "server-" + sanitizedName + "." + d.domain
ip := net.ParseIP(ipAddrStr) ip := net.ParseIP(ipAddrStr)
if ip != nil { if ip != nil {