This repository has been archived by the owner on Jun 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #760 from rumpl/compose-validation
Compose validation
- Loading branch information
Showing
14 changed files
with
500 additions
and
95 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Compose file validation failed: | ||
* can't use relative path as volume source ("./src:/src") in service "api" | ||
* can't use relative path as volume source ("./static:/opt/${static_subdir}") in service "web" | ||
* secret "my_secret" must be external |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
version: "3.6" | ||
services: | ||
api: | ||
image: python:3.6 | ||
volumes: | ||
- ./src:/src | ||
web: | ||
image: nginx | ||
networks: | ||
- front | ||
volumes: | ||
- ./static:/opt/${static_subdir} | ||
secrets: | ||
my_secret: | ||
first: ./path/to/secret.txt |
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
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package rules | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type externalSecretsValidator struct { | ||
} | ||
|
||
func NewExternalSecretsRule() Rule { | ||
return &externalSecretsValidator{} | ||
} | ||
|
||
func (s *externalSecretsValidator) Collect(parent string, key string, value interface{}) { | ||
} | ||
|
||
func (s *externalSecretsValidator) Accept(parent string, key string) bool { | ||
return key == "secrets" | ||
} | ||
|
||
func (s *externalSecretsValidator) Validate(cfgMap interface{}) []error { | ||
errs := []error{} | ||
if value, ok := cfgMap.(map[string]interface{}); ok { | ||
for secretName, secret := range value { | ||
if secretMap, ok := secret.(map[string]interface{}); ok { | ||
var hasExternal = false | ||
for key := range secretMap { | ||
if key == "external" { | ||
hasExternal = true | ||
} | ||
} | ||
if !hasExternal { | ||
errs = append(errs, errors.Errorf(`secret %q must be external`, secretName)) | ||
} | ||
} | ||
} | ||
} | ||
return errs | ||
} |
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package rules | ||
|
||
import ( | ||
"testing" | ||
|
||
"gotest.tools/assert" | ||
) | ||
|
||
func TestExternalSecrets(t *testing.T) { | ||
s := NewExternalSecretsRule() | ||
|
||
t.Run("should accept secrets", func(t *testing.T) { | ||
// The secrets key is on the root path, that's why it doesn't | ||
// have a parent | ||
assert.Equal(t, s.Accept("", "secrets"), true) | ||
}) | ||
|
||
t.Run("should return nil if all secrets are external", func(t *testing.T) { | ||
input := map[string]interface{}{ | ||
"my_secret": map[string]interface{}{ | ||
"external": "true", | ||
}, | ||
} | ||
|
||
errs := s.Validate(input) | ||
assert.Equal(t, len(errs), 0) | ||
}) | ||
|
||
t.Run("should return error if no external secrets", func(t *testing.T) { | ||
input := map[string]interface{}{ | ||
"my_secret": map[string]interface{}{ | ||
"file": "./my_secret.txt", | ||
}, | ||
} | ||
|
||
errs := s.Validate(input) | ||
assert.Equal(t, len(errs), 1) | ||
assert.ErrorContains(t, errs[0], `secret "my_secret" must be external`) | ||
}) | ||
|
||
t.Run("should return all errors", func(t *testing.T) { | ||
input := map[string]interface{}{ | ||
"my_secret": map[string]interface{}{ | ||
"file": "./my_secret.txt", | ||
}, | ||
"my_other_secret": map[string]interface{}{ | ||
"file": "./my_secret.txt", | ||
}, | ||
} | ||
|
||
errs := s.Validate(input) | ||
assert.Equal(t, len(errs), 2) | ||
}) | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package rules | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
type relativePathRule struct { | ||
volumes map[string]interface{} | ||
service string | ||
} | ||
|
||
func NewRelativePathRule() Rule { | ||
return &relativePathRule{ | ||
volumes: map[string]interface{}{}, | ||
} | ||
} | ||
|
||
func (s *relativePathRule) Collect(parent string, key string, value interface{}) { | ||
if parent == "volumes" { | ||
s.volumes[key] = value | ||
} | ||
} | ||
|
||
func (s *relativePathRule) Accept(parent string, key string) bool { | ||
if parent == "services" { | ||
s.service = key | ||
} | ||
return regexp.MustCompile("services.(.*).volumes").MatchString(parent + "." + key) | ||
} | ||
|
||
func (s *relativePathRule) Validate(value interface{}) []error { | ||
if m, ok := value.(map[string]interface{}); ok { | ||
src, ok := m["source"] | ||
if !ok { | ||
return []error{fmt.Errorf("invalid volume in service %q", s.service)} | ||
} | ||
_, volumeExists := s.volumes[src.(string)] | ||
if !filepath.IsAbs(src.(string)) && !volumeExists { | ||
return []error{fmt.Errorf("can't use relative path as volume source (%q) in service %q", src, s.service)} | ||
} | ||
} | ||
|
||
if m, ok := value.([]interface{}); ok { | ||
errs := []error{} | ||
for _, p := range m { | ||
str, ok := p.(string) | ||
if !ok { | ||
errs = append(errs, fmt.Errorf("invalid volume in service %q", s.service)) | ||
continue | ||
} | ||
|
||
parts := strings.Split(str, ":") | ||
if len(parts) <= 1 { | ||
errs = append(errs, fmt.Errorf("invalid volume definition (%q) in service %q", str, s.service)) | ||
continue | ||
} | ||
|
||
volumeName := parts[0] | ||
_, volumeExists := s.volumes[volumeName] | ||
if !filepath.IsAbs(volumeName) && !volumeExists { | ||
errs = append(errs, fmt.Errorf("can't use relative path as volume source (%q) in service %q", str, s.service)) | ||
continue | ||
} | ||
} | ||
|
||
if len(errs) > 0 { | ||
return errs | ||
} | ||
} | ||
return nil | ||
} |
Oops, something went wrong.