Skip to content

Commit

Permalink
Fixes merge issues
Browse files Browse the repository at this point in the history
  • Loading branch information
bomoko committed Sep 17, 2023
2 parents 911eded + b91485a commit c1ca533
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 106 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
.env
/internal/handler/testassets/bin
internal/handler/testassets/bin/*

2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
95 changes: 0 additions & 95 deletions default_filter_transformers.json

This file was deleted.

49 changes: 49 additions & 0 deletions default_filter_transformers.yaml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 6 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions internal/handler/registerFilters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down
64 changes: 64 additions & 0 deletions internal/handler/registerFilters_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"transforms": [
{
"type": "test1"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
transforms:
- type: test2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
transforms:
- type: test3
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
transforms:
- type: test4
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit c1ca533

Please sign in to comment.