diff --git a/.gitignore b/.gitignore index f7fe5ec..6a18878 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea .env -/internal/handler/testassets/bin +internal/handler/testassets/bin/* + diff --git a/Dockerfile b/Dockerfile index 12b22fe..c4cf4bf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -25,7 +25,7 @@ RUN curl -sSfL https://raw.githubusercontent.com/anchore/grype/main/install.sh | WORKDIR / COPY --from=builder /go/src/github.com/uselagoon/lagoon/services/insights-handler/insights-handler . -COPY default_filter_transformers.json /default_filter_transformers.json +COPY default_filter_transformers.yaml /default_filter_transformers.yaml USER 65532:65532 ENTRYPOINT ["/insights-handler"] \ No newline at end of file diff --git a/default_filter_transformers.json b/default_filter_transformers.json deleted file mode 100644 index c1ce467..0000000 --- a/default_filter_transformers.json +++ /dev/null @@ -1,95 +0,0 @@ -{ - "transforms": [ - { - "type": "handler.EnvironmentVariable", - "lookupvalue": [ - { - "name": "Key", - "value": "PHP_VERSION", - "exactMatch": true - } - ], - "transformations": [ - { - "name": "Name", - "value": "PHP" - }, - { - "name": "Category", - "value": "Language" - }, - { - "name": "Description", - "value": "The currently installed PHP version" - } - ], - "keyfact": true - }, - { - "type": "cyclonedx.Component", - "lookupvalue": [ - { - "name": "Name", - "value": "drupal/core", - "exactMatch": true - } - ], - "transformations": [ - { - "name": "Name", - "value": "Drupal" - }, - { - "name": "Category", - "value": "Application" - } - ], - "keyfact": true - }, - { - "type": "cyclonedx.Component", - "lookupvalue": [ - { - "name": "Name", - "value": "alpine", - "exactMatch": true - } - ], - "transformations": [ - { - "name": "Name", - "value": "Alpine Linux" - }, - { - "name": "Category", - "value": "OS" - }, - { - "name": "Description", - "value": "Base image Alpine Linux version" - } - ], - "keyfact": true - }, - { - "type": "cyclonedx.Component", - "lookupvalue": [ - { - "name": "Name", - "value": "drush/drush" - } - ], - "transformations": [ - { - "name": "Name", - "value": "Drush" - }, - { - "name": "Category", - "value": "Helper" - } - ], - "keyfact": true - } - ] -} \ No newline at end of file diff --git a/default_filter_transformers.yaml b/default_filter_transformers.yaml new file mode 100644 index 0000000..360add3 --- /dev/null +++ b/default_filter_transformers.yaml @@ -0,0 +1,49 @@ +--- +transforms: + - type: handler.EnvironmentVariable + lookupvalue: + - name: Key + value: PHP_VERSION + exactMatch: true + transformations: + - name: Name + value: PHP + - name: Category + value: Language + - name: Description + value: The currently installed PHP version + keyfact: true + - type: cyclonedx.Component + lookupvalue: + - name: Name + value: drupal/core + exactMatch: true + transformations: + - name: Name + value: Drupal + - name: Category + value: Application + keyfact: true + - type: cyclonedx.Component + lookupvalue: + - name: Name + value: alpine + exactMatch: true + transformations: + - name: Name + value: Alpine Linux + - name: Category + value: OS + - name: Description + value: Base image Alpine Linux version + keyfact: true + - type: cyclonedx.Component + lookupvalue: + - name: Name + value: drush/drush + transformations: + - name: Name + value: Drush + - name: Category + value: Helper + keyfact: true \ No newline at end of file diff --git a/go.mod b/go.mod index 49f8496..1549693 100644 --- a/go.mod +++ b/go.mod @@ -11,11 +11,7 @@ require ( github.com/joho/godotenv v1.5.1 github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2 github.com/stretchr/testify v1.8.4 -) - -replace ( - github.com/docker/cli => github.com/docker/cli v20.10.19+incompatible - github.com/docker/docker => github.com/docker/docker v20.10.19+incompatible + gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0 ) require ( @@ -344,6 +340,11 @@ require ( sigs.k8s.io/yaml v1.3.0 // indirect ) +replace ( + github.com/docker/cli => github.com/docker/cli v20.10.19+incompatible + github.com/docker/docker => github.com/docker/docker v20.10.19+incompatible +) + require ( github.com/CycloneDX/cyclonedx-go v0.7.2-0.20230625092137-07e2f29defc3 github.com/cheekybits/is v0.0.0-20150225183255-68e9c0620927 // indirect diff --git a/internal/handler/registerFilters.go b/internal/handler/registerFilters.go index 3a56c2d..2cc9e98 100644 --- a/internal/handler/registerFilters.go +++ b/internal/handler/registerFilters.go @@ -2,7 +2,11 @@ package handler import ( "encoding/json" + "fmt" + "gopkg.in/yaml.v1" "io/ioutil" + "path/filepath" + "strings" ) var KeyFactFilters []func(filter parserFilter) parserFilter @@ -59,11 +63,22 @@ func LoadTransformsFromDisk(filename string) ([]FactTransform, error) { if err != nil { return ret.Transforms, err } - err = json.Unmarshal(file, &ret) + + ext := strings.ToLower(filepath.Ext(filename)) + + switch ext { + case ".json": + err = json.Unmarshal(file, &ret) + case ".yaml", ".yml": + err = yaml.Unmarshal(file, &ret) + default: + err = fmt.Errorf("Unsupported file type for default transforms: %v", ext) + } if err != nil { return ret.Transforms, err } + return ret.Transforms, nil } @@ -103,7 +118,7 @@ func GenerateFilterFromTransform(transform FactTransform) (func(filter parserFil } -func RegisterFiltersFromJson(filename string) error { +func RegisterFiltersFromDisk(filename string) error { transforms, err := LoadTransformsFromDisk(filename) if err != nil { diff --git a/internal/handler/registerFilters_test.go b/internal/handler/registerFilters_test.go new file mode 100644 index 0000000..2be7704 --- /dev/null +++ b/internal/handler/registerFilters_test.go @@ -0,0 +1,64 @@ +package handler + +import ( + "reflect" + "testing" +) + +func TestLoadTransformsFromDisk(t *testing.T) { + type args struct { + filename string + } + tests := []struct { + name string + args args + want []FactTransform + wantErr bool + }{ + { + name: "test1 - json", + args: args{filename: "testassets/testLoadTransformsFromDisk/test1.json"}, + want: []FactTransform{ + { + Type: "test1", + }, + }, + }, + { + name: "test2 - yaml", + args: args{filename: "testassets/testLoadTransformsFromDisk/test2.yaml"}, + want: []FactTransform{ + { + Type: "test2", + }, + }, + }, + { + name: "test3 - json", + args: args{filename: "testassets/testLoadTransformsFromDisk/test3.yml"}, + want: []FactTransform{ + { + Type: "test3", + }, + }, + }, + { + name: "test4 - unsupported file type", + args: args{filename: "testassets/testLoadTransformsFromDisk/test4.unsp"}, + want: []FactTransform{}, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := LoadTransformsFromDisk(tt.args.filename) + if (err != nil) != tt.wantErr { + t.Errorf("LoadTransformsFromDisk() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) && tt.wantErr == false { + t.Errorf("LoadTransformsFromDisk() got = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/internal/handler/testassets/testLoadTransformsFromDisk/test1.json b/internal/handler/testassets/testLoadTransformsFromDisk/test1.json new file mode 100644 index 0000000..f34bd9e --- /dev/null +++ b/internal/handler/testassets/testLoadTransformsFromDisk/test1.json @@ -0,0 +1,7 @@ +{ + "transforms": [ + { + "type": "test1" + } + ] +} \ No newline at end of file diff --git a/internal/handler/testassets/testLoadTransformsFromDisk/test2.yaml b/internal/handler/testassets/testLoadTransformsFromDisk/test2.yaml new file mode 100644 index 0000000..ebf747e --- /dev/null +++ b/internal/handler/testassets/testLoadTransformsFromDisk/test2.yaml @@ -0,0 +1,3 @@ +--- +transforms: + - type: test2 \ No newline at end of file diff --git a/internal/handler/testassets/testLoadTransformsFromDisk/test3.yml b/internal/handler/testassets/testLoadTransformsFromDisk/test3.yml new file mode 100644 index 0000000..7deda89 --- /dev/null +++ b/internal/handler/testassets/testLoadTransformsFromDisk/test3.yml @@ -0,0 +1,3 @@ +--- +transforms: + - type: test3 \ No newline at end of file diff --git a/internal/handler/testassets/testLoadTransformsFromDisk/test4.unsp b/internal/handler/testassets/testLoadTransformsFromDisk/test4.unsp new file mode 100644 index 0000000..d8f1150 --- /dev/null +++ b/internal/handler/testassets/testLoadTransformsFromDisk/test4.unsp @@ -0,0 +1,3 @@ +--- +transforms: + - type: test4 \ No newline at end of file diff --git a/main.go b/main.go index 0e07160..1c82287 100644 --- a/main.go +++ b/main.go @@ -67,7 +67,7 @@ func main() { flag.StringVar(&s3Bucket, "s3-bucket", "lagoon-insights", "The s3 bucket name.") flag.StringVar(&s3Region, "s3-region", "", "The s3 region.") flag.BoolVar(&s3useSSL, "s3-usessl", true, "Use SSL with S3") - flag.StringVar(&filterTransformerFile, "filter-transformer-file", "./default_filter_transformers.json", "The filter/transformers to load.") + flag.StringVar(&filterTransformerFile, "filter-transformer-file", "./default_filter_transformers.yaml", "The filter/transformers to load.") flag.BoolVar(&disableS3Upload, "disable-s3-upload", false, "Disable uploading insights data to an s3 s3Bucket") flag.BoolVar(&disableAPIIntegration, "disable-api-integration", false, "Disable insights data integration for the Lagoon API") flag.BoolVar(&enableDebug, "debug", false, "Enable debugging output") @@ -127,7 +127,7 @@ func main() { } log.Println("Registering Fact Filters/Transformer") - err := handler.RegisterFiltersFromJson(filterTransformerFile) + err := handler.RegisterFiltersFromDisk(filterTransformerFile) if err != nil { log.Println(err) }