From 6d3de3af727dcbcdfb4546b1865631f8c3c5c03c Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Tue, 5 Jan 2021 09:31:23 +0100 Subject: [PATCH] Switch for disabling validation (#667) * Add switch for enabling package validation * Fix: tests * Fix: tests * Update CHANGELOG * Address PR comments * Refactor to DisablePackageValidation --- CHANGELOG.md | 2 ++ main.go | 1 + util/data_stream.go | 39 ++++++++++++++++++++++----------------- util/package.go | 14 +++++++++----- util/packages.go | 3 +++ 5 files changed, 37 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b93ce5ca..7ddfa58e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/main.go b/main.go index 2ffd7191b..ee25450f1 100644 --- a/main.go +++ b/main.go @@ -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 { diff --git a/util/data_stream.go b/util/data_stream.go index f76d5077d..cedcf04db 100644 --- a/util/data_stream.go +++ b/util/data_stream.go @@ -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 == "" { @@ -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 @@ -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) } @@ -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) } @@ -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") } diff --git a/util/package.go b/util/package.go index 8e332bd3a..f30dfa69f 100644 --- a/util/package.go +++ b/util/package.go @@ -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) @@ -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) } @@ -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 { diff --git a/util/packages.go b/util/packages.go index b1f1e88f1..ad1daa65b 100644 --- a/util/packages.go +++ b/util/packages.go @@ -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