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

Make validate command ignore environment variables in service name #3558

Merged
merged 8 commits into from
Nov 17, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions helper/args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ func ReplaceEnv(arg string, environments ...map[string]string) string {
return arg
})
}

// ReplaceEnvWithPlaceHolder replaces all occurrences of environment variables with the placeholder string.
func ReplaceEnvWithPlaceHolder(arg string, placeholder string) string {
return envRe.ReplaceAllString(arg, placeholder)
}
13 changes: 6 additions & 7 deletions nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3074,13 +3074,12 @@ func (s *Service) Validate() error {
var mErr multierror.Error

// Ensure the service name is valid per the below RFCs but make an exception
// for our interpolation syntax
// RFC-952 §1 (https://tools.ietf.org/html/rfc952), RFC-1123 §2.1
// (https://tools.ietf.org/html/rfc1123), and RFC-2782
// (https://tools.ietf.org/html/rfc2782).
re := regexp.MustCompile(`^(?i:[a-z0-9]|[a-z0-9\$][a-zA-Z0-9\-\$\{\}\_\.]*[a-z0-9\}])$`)
if !re.MatchString(s.Name) {
mErr.Errors = append(mErr.Errors, fmt.Errorf("service name must be valid per RFC 1123 and can contain only alphanumeric characters or dashes: %q", s.Name))
// for our interpolation syntax by first stripping any environment variables from the name

serviceNameStripped := args.ReplaceEnvWithPlaceHolder(s.Name, "ENV-VAR")

if err := s.ValidateName(serviceNameStripped); err != nil {
mErr.Errors = append(mErr.Errors, err)
}

switch s.AddressMode {
Expand Down
8 changes: 8 additions & 0 deletions nomad/structs/structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1681,6 +1681,14 @@ func TestInvalidServiceCheck(t *testing.T) {
t.Fatalf("Service should be valid: %v", err)
}

s = Service{
Name: "my_service-${NOMAD_META_FOO}",
PortLabel: "bar",
}
if err := s.Validate(); err == nil {
t.Fatalf("Service should be invalid (contains underscore but not in a variable name): %v", err)
}

s = Service{
Name: "abcdef0123456789-abcdef0123456789-abcdef0123456789-abcdef0123456",
PortLabel: "bar",
Expand Down
8 changes: 8 additions & 0 deletions website/source/docs/commands/validate.html.md.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ and supports `go-getter` syntax.
On successful validation, exit code 0 will be returned, otherwise an exit code
of 1 indicates an error.

Service names are validated according to [RFC-1123](https://tools.ietf.org/html/rfc1123).
Copy link
Contributor

Choose a reason for hiding this comment

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

I would remove from here and instead add to: https://www.nomadproject.io/docs/job-specification/service.html#name.

Copy link
Contributor

Choose a reason for hiding this comment

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

I am not a fan of any specific validation details on this page since then we will end up documenting validation details here and on the specific stanzas.

Any environment variables in the name are ignored, because some environment variables can only
be resolved at run time. Invalid characters outside of environment variable names will not pass
validation.
For example, if the service name is set to `my_service${NOMAD_NODE_NAME}` it will fail
validation. However, if a service name is set to `myservice{NOMAD_NODE_NAME}` and the node name
Copy link
Contributor

Choose a reason for hiding this comment

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

myservice${NOMAD_NODE_NAME} ?

contains an invalid character, it will only be caught at run time and nomad validate will succeed.

## Examples

Validate a job with invalid syntax:
Expand Down