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
Compose validation #760
Merged
Merged
Compose validation #760
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4f5d946
Add external secrets rule
rumpl e1d4364
Add relative path rule
rumpl 08c99b7
Add the validator
rumpl 5d2b833
Call the validator in packager init and extract
rumpl fae5df6
Add e2e test for the init with an invalid file
rumpl 0be3c16
Remove useless check
rumpl 25bc6b4
Only test the number of errors
rumpl cac8c32
Change the external secret validation message
rumpl 582f377
Fix invalid volume definition
rumpl 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
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.
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.
I wonder if those checks could be removed, if we validate the compose file first using the json schema.
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.
Once we have schema validation then yeah, sure