Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
lhitchon
committed
Sep 29, 2018
1 parent
4866ded
commit b9f8d95
Showing
11 changed files
with
131 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package linter | ||
|
||
import ( | ||
"github.com/stelligent/config-lint/assertion" | ||
"path/filepath" | ||
) | ||
|
||
// CSVResourceLoader loads a list of Resource objects based on the list of ResourceConfig objects | ||
type CSVResourceLoader struct { | ||
Columns []assertion.ColumnConfig | ||
} | ||
|
||
func extractCSVResourceID(expression string, properties interface{}) string { | ||
resourceID := "None" | ||
result, err := assertion.SearchData(expression, properties) | ||
if err == nil { | ||
resourceID, _ = result.(string) | ||
} | ||
return resourceID | ||
} | ||
|
||
// Load converts a text file into a collection of Resource objects | ||
func (l CSVResourceLoader) Load(filename string) (FileResources, error) { | ||
loaded := FileResources{ | ||
Resources: make([]assertion.Resource, 0), | ||
} | ||
csvRows, err := loadCSV(filename) | ||
if err != nil { | ||
return loaded, err | ||
} | ||
for rowNumber, row := range csvRows { | ||
properties := map[string]interface{}{} | ||
properties["__file__"] = filename | ||
properties["__dir__"] = filepath.Dir(filename) | ||
for columnNumber, columnConfig := range l.Columns { | ||
properties[columnConfig.Name] = row[columnNumber] | ||
} | ||
resource := assertion.Resource{ | ||
ID: string(rowNumber), | ||
Type: "row", | ||
Properties: properties, | ||
Filename: filename, | ||
} | ||
loaded.Resources = append(loaded.Resources, resource) | ||
} | ||
return loaded, nil | ||
} | ||
|
||
// PostLoad does no additional processing fro a CSVResourceLoader | ||
func (l CSVResourceLoader) PostLoad(r FileResources) ([]assertion.Resource, error) { | ||
return r.Resources, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package linter | ||
|
||
import ( | ||
"bytes" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestCSVLinterValidate(t *testing.T) { | ||
options := Options{ | ||
Tags: []string{}, | ||
RuleIDs: []string{}, | ||
} | ||
ruleSet := loadRulesForTest("./testdata/rules/generic-csv.yml", t) | ||
filenames := []string{"./testdata/resources/users.csv"} | ||
loader := CSVResourceLoader{Columns: ruleSet.Columns} | ||
linter := FileLinter{Filenames: filenames, ValueSource: TestingValueSource{}, Loader: loader} | ||
report, err := linter.Validate(ruleSet, options) | ||
assert.Nil(t, err, "Expecting Validate to run without error") | ||
assert.Equal(t, 3, len(report.ResourcesScanned), "Expecting Validate to scan 3 resources") | ||
assert.Equal(t, 1, len(report.Violations), "Expecting Validate to find 1 violation") | ||
} | ||
|
||
func TestCSVLinterSearch(t *testing.T) { | ||
ruleSet := loadRulesForTest("./testdata/rules/generic-csv.yml", t) | ||
filenames := []string{"./testdata/resources/users.csv"} | ||
loader := CSVResourceLoader{Columns: ruleSet.Columns} | ||
linter := FileLinter{Filenames: filenames, ValueSource: TestingValueSource{}, Loader: loader} | ||
var b bytes.Buffer | ||
linter.Search(ruleSet, "Department", &b) | ||
assert.Contains(t, b.String(), "Audit", "Expecting TestCSVLinterSearch to find string in output") | ||
} |
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,3 @@ | ||
admin,Admin | ||
readonly,Audit | ||
user1, |
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 @@ | ||
version: 1 | ||
description: Rules for users in CSV file | ||
type: CSV | ||
files: | ||
- "*.csv" | ||
|
||
columns: | ||
- name: User | ||
- name: Department | ||
|
||
rules: | ||
- id: DEPARTMENT_REQUIRED | ||
message: User must have a department | ||
resource: row | ||
assertions: | ||
- key: Department | ||
op: not-empty |
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