Skip to content

Commit

Permalink
Switch for disabling validation (#667)
Browse files Browse the repository at this point in the history
* Add switch for enabling package validation

* Fix: tests

* Fix: tests

* Update CHANGELOG

* Address PR comments

* Refactor to DisablePackageValidation
  • Loading branch information
mtojek authored Jan 5, 2021
1 parent 26ba232 commit 6d3de3a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 22 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

* Package validation can be disabled via command line option. [#667] (https://github.com/elastic/package-registry/pull/667)

### Deprecated

### Known Issues
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func init() {
flag.StringVar(&address, "address", "localhost:8080", "Address of the package-registry service.")
// This flag is experimental and might be removed in the future or renamed
flag.BoolVar(&dryRun, "dry-run", false, "Runs a dry-run of the registry without starting the web service (experimental)")
flag.BoolVar(&util.PackageValidationDisabled, "disable-package-validation", false, "Disable package content validation")
}

type Config struct {
Expand Down
39 changes: 22 additions & 17 deletions util/data_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,22 +151,11 @@ func NewDataStream(basePath string, p *Package) (*DataStream, error) {
if !IsValidRelease(d.Release) {
return nil, fmt.Errorf("invalid release: %s", d.Release)
}
return d, nil
}

func (d *DataStream) Validate() error {
pipelineDir := filepath.Join(d.BasePath, "elasticsearch", DirIngestPipeline)
paths, err := filepath.Glob(filepath.Join(pipelineDir, "*"))
if err != nil {
return err
}

if strings.Contains(d.Dataset, "-") {
return fmt.Errorf("data stream name is not allowed to contain `-`: %s", d.Dataset)
}

if !d.validType() {
return fmt.Errorf("type is not valid: %s", d.Type)
return nil, err
}

if d.Elasticsearch != nil && d.Elasticsearch.IngestPipelineName == "" {
Expand All @@ -189,9 +178,25 @@ func (d *DataStream) Validate() error {
}
}
}

if d.IngestPipeline == "" && len(paths) > 0 {
return fmt.Errorf("unused pipelines in the package (dataset: %s): %s", d.Dataset, strings.Join(paths, ","))
return nil, fmt.Errorf("unused pipelines in the package (dataset: %s): %s", d.Dataset, strings.Join(paths, ","))
}
return d, nil
}

func (d *DataStream) Validate() error {
if PackageValidationDisabled {
return nil
}

pipelineDir := filepath.Join(d.BasePath, "elasticsearch", DirIngestPipeline)

if strings.Contains(d.Dataset, "-") {
return fmt.Errorf("data stream name is not allowed to contain `-`: %s", d.Dataset)
}

if !d.validType() {
return fmt.Errorf("type is not valid: %s", d.Type)
}

// In case an ingest pipeline is set, check if it is around
Expand All @@ -204,7 +209,7 @@ func (d *DataStream) Validate() error {
return errors.Wrapf(errJSON, "stat ingest pipeline JSON file failed (path: %s)", jsonPipelinePath)
}
if !os.IsNotExist(errJSON) {
err = validateIngestPipelineFile(jsonPipelinePath)
err := validateIngestPipelineFile(jsonPipelinePath)
if err != nil {
return errors.Wrapf(err, "validating ingest pipeline JSON file failed (path: %s)", jsonPipelinePath)
}
Expand All @@ -217,7 +222,7 @@ func (d *DataStream) Validate() error {
return errors.Wrapf(errYAML, "stat ingest pipeline YAML file failed (path: %s)", jsonPipelinePath)
}
if !os.IsNotExist(errYAML) {
err = validateIngestPipelineFile(yamlPipelinePath)
err := validateIngestPipelineFile(yamlPipelinePath)
if err != nil {
return errors.Wrapf(err, "validating ingest pipeline YAML file failed (path: %s)", jsonPipelinePath)
}
Expand All @@ -229,7 +234,7 @@ func (d *DataStream) Validate() error {
}
}

err = d.validateRequiredFields()
err := d.validateRequiredFields()
if err != nil {
return errors.Wrap(err, "validating required fields failed")
}
Expand Down
14 changes: 9 additions & 5 deletions util/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ func NewPackage(basePath string) (*Package, error) {
p.License = DefaultLicense
}

p.versionSemVer, err = semver.StrictNewVersion(p.Version)
if err != nil {
return nil, errors.Wrap(err, "invalid package version")
}

if p.Icons != nil {
for k, i := range p.Icons {
p.Icons[k].Path = i.getPath(p)
Expand Down Expand Up @@ -308,6 +313,10 @@ func collectAssets(pattern string) ([]string, error) {
// Validate is called during Unpack of the manifest.
// The validation here is only related to the fields directly specified in the manifest itself.
func (p *Package) Validate() error {
if PackageValidationDisabled {
return nil
}

if p.FormatVersion == "" {
return fmt.Errorf("no format_version set: %v", p)
}
Expand Down Expand Up @@ -336,11 +345,6 @@ func (p *Package) Validate() error {
}
}

p.versionSemVer, err = semver.StrictNewVersion(p.Version)
if err != nil {
return errors.Wrap(err, "invalid package version")
}

for _, i := range p.Icons {
_, err := os.Stat(filepath.Join(p.BasePath, i.Src))
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions util/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (
"github.com/pkg/errors"
)

// PackageValidationDisabled is a flag which can disable package content validation (package, data streams, assets, etc.).
var PackageValidationDisabled bool

var packageList Packages

type Packages []Package
Expand Down

0 comments on commit 6d3de3a

Please sign in to comment.