-
Notifications
You must be signed in to change notification settings - Fork 596
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
Validate NodeNames, Address and Tags fields #612
Merged
Merged
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
73ccaa4
wip 1
e9d7462
wip validateMemberInfo
e40dcb9
minor updates
e91297a
more config update and questions
5fcfacf
add regex for ip:port add tests
dbbe2ca
appease go vet
ffd9c1e
make validatenodenames()
3f45222
fix regex, move test
47b6615
failing test? just remove it
00e98d9
cleanupcomments
ae7dc03
clean up comments and test func
3457667
set flag to default to false
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |
"net" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"sort" | ||
"strings" | ||
"time" | ||
|
@@ -227,6 +228,10 @@ type Config struct { | |
// 5 seconds. | ||
BroadcastTimeoutRaw string `mapstructure:"broadcast_timeout"` | ||
BroadcastTimeout time.Duration `mapstructure:"-"` | ||
|
||
//ValidateNodeNames specifies whether or not nodenames should | ||
// be alphanumeric and within 128 characters | ||
ValidateNodeNames bool `mapstructure:"validate_node_names"` | ||
} | ||
|
||
// BindAddrParts returns the parts of the BindAddr that should be | ||
|
@@ -344,6 +349,21 @@ func DecodeConfig(r io.Reader) (*Config, error) { | |
result.BroadcastTimeout = dur | ||
} | ||
|
||
if result.NodeName != "" && result.ValidateNodeNames { | ||
var InvalidNameRe = regexp.MustCompile(`[^A-Za-z0-9\\-]+`) | ||
if InvalidNameRe.MatchString(result.NodeName) { | ||
err = fmt.Errorf("NodeName contains invalid characters %v , Valid characters include "+ | ||
"all alpha-numerics and dashes.", result.NodeName) | ||
return nil, err | ||
} | ||
|
||
if len(result.NodeName) > serf.MaxNodeNameLength { | ||
err = fmt.Errorf("NodeName is %v characters. "+ | ||
"Valid length is between 1 and 128 characters", len(result.NodeName)) | ||
return nil, err | ||
} | ||
} | ||
|
||
return &result, nil | ||
} | ||
|
||
|
@@ -363,7 +383,9 @@ func containsKey(keys []string, key string) bool { | |
func MergeConfig(a, b *Config) *Config { | ||
var result Config = *a | ||
|
||
// Copy the strings if they're set | ||
//TODO(schristoff): do i need to check nodename | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can remove this todo (since we're verifying the final config that gets passed to Create() anyway) |
||
//validatity here? | ||
|
||
if b.NodeName != "" { | ||
result.NodeName = b.NodeName | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic for validating the name is getting repeated 3 times - we should pull it into a function for reuse. We can also avoid doing it in this
serf/command/agent
code at all and do it here instead: https://github.com/hashicorp/serf/blob/master/serf/serf.go#L235serf.Create
will get called on startup for Consul as well, where the code in this package is only used in the Serf binary.