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

Switch for disabling validation #667

Merged
merged 6 commits into from
Jan 5, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
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 is disabled by default. [#667] (https://github.com/elastic/package-registry/pull/667)

### Deprecated

### Known Issues
Expand Down
5 changes: 5 additions & 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.EnablePackageValidation, "validate", false, "Validate package content")
}

type Config struct {
Expand All @@ -61,6 +62,10 @@ func main() {
log.Println("Package registry started.")
defer log.Println("Package registry stopped.")

if dryRun {
util.EnablePackageValidation = true // dry-run enables the package validation
}

config := mustLoadConfig()
packagesBasePaths := getPackagesBasePaths(config)
ensurePackagesAvailable(packagesBasePaths)
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 == "" {
Copy link
Member

Choose a reason for hiding this comment

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

This is kind of a mix between validation and building the data model. I wonder if we should clean it up later or keep it as is. It would probably mean doing parts of it twice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure if I understand your concerns. Any code that modifies a package or a datastream is placed in constructors (e.g. NewDataStream). Are you referring to this part of code:

if d.Elasticsearch != nil && d.Elasticsearch.IngestPipelineName == "" {
// Check that no ingest pipeline exists in the directory except default
for _, path := range paths {
if filepath.Base(path) == DefaultPipelineNameJSON || filepath.Base(path) == DefaultPipelineNameYAML {
d.Elasticsearch.IngestPipelineName = DefaultPipelineName
// TODO: remove because of legacy
d.IngestPipeline = DefaultPipelineName
break
}
}
// TODO: Remove, only here for legacy
} else if d.IngestPipeline == "" {
// Check that no ingest pipeline exists in the directory except default
for _, path := range paths {
if filepath.Base(path) == DefaultPipelineNameJSON || filepath.Base(path) == DefaultPipelineNameYAML {
d.IngestPipeline = DefaultPipelineName
break
}
}
}
if d.IngestPipeline == "" && len(paths) > 0 {
return nil, fmt.Errorf("unused pipelines in the package (dataset: %s): %s", d.Dataset, strings.Join(paths, ","))
}
? If so, then we should come back here and refactor, as the DataStream.IngestPipeline is marked as deprecated.

I suppose we can strip this part of at all, unless there is Kibana logic using it.

Copy link
Member

Choose a reason for hiding this comment

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

What I initially assume could be an issue does not seem to be one after quickly checking the code. I was worried that for example the pipeline checks do not happen if only Validate is run. But this is not the case as before validate is run, the constructor is used which runs all these additional checks always to build the actual package object. So all good.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I know the design is not really clear as we have to sneak the condition into the Validate function, but I didn't see any Option to disable validation at Unpack.

Copy link
Member

Choose a reason for hiding this comment

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

I think I know the person who built this initially and is to blame for the design 😆

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 !EnablePackageValidation {
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 !EnablePackageValidation {
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
1 change: 1 addition & 0 deletions util/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ var packageTests = []struct {
}

func TestValidate(t *testing.T) {
EnablePackageValidation = true
for _, tt := range packageTests {
t.Run(tt.description, func(t *testing.T) {
err := tt.p.Validate()
Expand Down
4 changes: 4 additions & 0 deletions util/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import (
"github.com/pkg/errors"
)

// EnablePackageValidation is a flag which can enable package content validation (package, data streams, assets, etc.).
// As the validation is relatively heavy process, it's disabled in runtime by default.
var EnablePackageValidation bool

var packageList Packages

type Packages []Package
Expand Down