-
Notifications
You must be signed in to change notification settings - Fork 72
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 #160 from ycombinator/semantic-validations
Semantic validations
- Loading branch information
Showing
31 changed files
with
326 additions
and
33 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
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
2 changes: 1 addition & 1 deletion
2
code/go/internal/validator/errors_test.go → code/go/internal/errors/errors_test.go
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package validator | ||
package errors | ||
|
||
import ( | ||
"errors" | ||
|
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,83 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package pkgpath | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/PaesslerAG/jsonpath" | ||
"github.com/joeshaw/multierror" | ||
"github.com/pkg/errors" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
// File represents a file in the package. | ||
type File struct { | ||
path string | ||
os.FileInfo | ||
} | ||
|
||
// Files finds files for the given glob | ||
func Files(glob string) ([]File, error) { | ||
paths, err := filepath.Glob(glob) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var errs multierror.Errors | ||
var files = make([]File, 0) | ||
for _, path := range paths { | ||
info, err := os.Stat(path) | ||
if err != nil { | ||
errs = append(errs, err) | ||
continue | ||
} | ||
|
||
file := File{path, info} | ||
files = append(files, file) | ||
} | ||
|
||
return files, errs.Err() | ||
} | ||
|
||
// Values returns values within the file matching the given path. Paths | ||
// should be expressed using JSONPath syntax. This method is only supported | ||
// for YAML and JSON files. | ||
func (f File) Values(path string) (interface{}, error) { | ||
fileName := f.Name() | ||
fileExt := strings.TrimLeft(filepath.Ext(fileName), ".") | ||
|
||
if fileExt != "json" && fileExt != "yaml" && fileExt != "yml" { | ||
return nil, fmt.Errorf("cannot extract values from file type = %s", fileExt) | ||
} | ||
|
||
contents, err := ioutil.ReadFile(f.path) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "reading file content failed") | ||
} | ||
|
||
var v interface{} | ||
if fileExt == "yaml" || fileExt == "yml" { | ||
if err := yaml.Unmarshal(contents, &v); err != nil { | ||
return nil, errors.Wrapf(err, "unmarshalling YAML file failed (path: %s)", fileName) | ||
} | ||
} else if fileExt == "json" { | ||
if err := json.Unmarshal(contents, &v); err != nil { | ||
return nil, errors.Wrapf(err, "unmarshalling JSON file failed (path: %s)", fileName) | ||
} | ||
} | ||
|
||
return jsonpath.Get(path, v) | ||
} | ||
|
||
// Path returns the complete path to the file. | ||
func (f File) Path() string { | ||
return f.path | ||
} |
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
58 changes: 58 additions & 0 deletions
58
code/go/internal/validator/semantic/kibana_matching_object_ids.go
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,58 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package semantic | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"strings" | ||
|
||
ve "github.com/elastic/package-spec/code/go/internal/errors" | ||
|
||
"github.com/elastic/package-spec/code/go/internal/pkgpath" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// ValidateKibanaObjectIDs returns validation errors if there are any Kibana | ||
// object files that define IDs not matching the file's name. That is, it returns | ||
// validation errors if a Kibana object file, foo.json, in the package defines | ||
// an object ID other than foo inside it. | ||
func ValidateKibanaObjectIDs(pkgRoot string) ve.ValidationErrors { | ||
var errs ve.ValidationErrors | ||
|
||
filePaths := filepath.Join(pkgRoot, "kibana", "*", "*.json") | ||
objectFiles, err := pkgpath.Files(filePaths) | ||
if err != nil { | ||
errs = append(errs, errors.Wrap(err, "error finding Kibana object files")) | ||
return errs | ||
} | ||
|
||
for _, objectFile := range objectFiles { | ||
filePath := objectFile.Path() | ||
|
||
idPath := "$.id" | ||
// Special case: object is of type 'security_rule' | ||
if filepath.Base(filepath.Dir(filePath)) == "security_rule" { | ||
idPath = "$.rule_id" | ||
} | ||
|
||
objectID, err := objectFile.Values(idPath) | ||
if err != nil { | ||
errs = append(errs, errors.Wrapf(err, "unable to get Kibana object ID in file [%s]", filePath)) | ||
continue | ||
} | ||
|
||
// fileID == filename without the extension == expected ID of Kibana object defined inside file. | ||
fileName := filepath.Base(filePath) | ||
fileExt := filepath.Ext(filePath) | ||
fileID := strings.Replace(fileName, fileExt, "", -1) | ||
if fileID != objectID { | ||
err := fmt.Errorf("kibana object file [%s] defines non-matching ID [%s]", filePath, objectID) | ||
errs = append(errs, err) | ||
} | ||
} | ||
|
||
return errs | ||
} |
17 changes: 17 additions & 0 deletions
17
code/go/internal/validator/semantic/kibana_no_dangling_object_ids.go
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,17 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package semantic | ||
|
||
import "github.com/elastic/package-spec/code/go/internal/errors" | ||
|
||
// ValidateKibanaNoDanglingObjectIDs returns validation errors if there are any | ||
// dangling references to Kibana objects in any Kibana object files. That is, it | ||
// returns validation errors if a Kibana object file in the package references another | ||
// Kibana object with ID i, but no Kibana object file for object ID i is found in the | ||
// package. | ||
func ValidateKibanaNoDanglingObjectIDs(pkgRoot string) errors.ValidationErrors { | ||
// TODO: will be implemented in follow up PR | ||
return nil | ||
} |
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
package validator | ||
|
||
import ( | ||
"github.com/elastic/package-spec/code/go/internal/validator" | ||
"github.com/elastic/package-spec/code/go/internal/errors" | ||
) | ||
|
||
// ValidationErrors is an Error that contains a iterable collection of validation error messages. | ||
type ValidationErrors validator.ValidationErrors | ||
type ValidationErrors errors.ValidationErrors |
Oops, something went wrong.