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

Adds functionality to rename fact names if there are duplicates #10

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
21 changes: 21 additions & 0 deletions internal/handler/fact_filters.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package handler

import (
"fmt"
)

func KeyFactsFilter(factsInput []LagoonFact) ([]LagoonFact, error) {
var filteredFacts []LagoonFact
for _, v := range factsInput {
Expand All @@ -9,3 +13,20 @@ func KeyFactsFilter(factsInput []LagoonFact) ([]LagoonFact, error) {
}
return filteredFacts, nil
}

func FactDuplicateHandler(factsInput []LagoonFact) ([]LagoonFact, error) {
var factOccurrenceTracker = map[string]int32{}

var filteredFacts []LagoonFact
for _, v := range factsInput {
if _, ok := factOccurrenceTracker[v.Name]; ok {
factOccurrenceTracker[v.Name] += 1
v.Name = fmt.Sprintf("%v (%v)", v.Name, v.OriginalFact.Name)
} else {
factOccurrenceTracker[v.Name] = 1
}
filteredFacts = append(filteredFacts, v)
}

return filteredFacts, nil
}
18 changes: 18 additions & 0 deletions internal/handler/fact_filters_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package handler

import (
"testing"
)

func TestFactDuplicateHandler(t *testing.T) {
dupfacts := []LagoonFact{
{Name: "duplicatename"},
{Name: "duplicatename"},
}

outfacts, _ := FactDuplicateHandler(dupfacts)

if outfacts[0].Name == outfacts[1].Name {
t.Errorf("Fact names should not be duplicated")
}
}
6 changes: 5 additions & 1 deletion internal/handler/imageInspectParserFilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ func processImageInspectInsightsData(h *Messaging, insights InsightsData, v stri
return nil, "", err
}

facts, err = FactDuplicateHandler(facts)
if err != nil {
return nil, "", err
}

return facts, source, nil
}
return []LagoonFact{}, "", nil
Expand Down Expand Up @@ -84,7 +89,6 @@ func processFactsFromImageInspect(imageInspectData ImageData, id int, source str
}

for _, f := range filteredFacts {

fact := LagoonFact{
Environment: id,
Name: f.Key,
Expand Down
16 changes: 13 additions & 3 deletions internal/handler/insightsParserFilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,18 @@ func processSbomInsightsData(h *Messaging, insights InsightsData, v string, apiC
source := fmt.Sprintf("insights:sbom:%s", resource.Service)

// Process SBOM into facts
facts := processFactsFromSBOM(bom.Components, environment.Id, source)
facts := processFactsFromSBOM(bom.Components, environment.Id, source, resource.Service)

facts, err = KeyFactsFilter(facts)
if err != nil {
return nil, "", err
}

facts, err = FactDuplicateHandler(facts)
if err != nil {
return nil, "", err
}

if len(facts) == 0 {
return nil, "", fmt.Errorf("no facts to process")
}
Expand All @@ -78,7 +83,7 @@ func processSbomInsightsData(h *Messaging, insights InsightsData, v string, apiC
return []LagoonFact{}, "", nil
}

func processFactsFromSBOM(facts *[]cdx.Component, environmentId int, source string) []LagoonFact {
func processFactsFromSBOM(facts *[]cdx.Component, environmentId int, source string, service string) []LagoonFact {
var factsInput []LagoonFact
if len(*facts) == 0 {
return factsInput
Expand All @@ -101,9 +106,14 @@ func processFactsFromSBOM(facts *[]cdx.Component, environmentId int, source stri
Name: f.Name,
Value: f.Version,
Source: source,
Service: service,
Description: f.PackageURL,
KeyFact: false,
Type: FactTypeText,
OriginalFact: PreTransformedFact{
Name: f.Name,
Value: f.Version,
},
Type: FactTypeText,
}
fmt.Println("Processing fact name " + f.Name)
fact, _ = ProcessLagoonFactAgainstRegisteredFilters(fact, f)
Expand Down
28 changes: 17 additions & 11 deletions internal/handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,22 @@ type InsightsData struct {

// LagoonFact Here we wrap outgoing facts before passing them to genqlient
type LagoonFact struct {
Id int `json:"id"`
Environment int `json:"environment"`
Name string `json:"name"`
Value string `json:"value"`
Source string `json:"source"`
Description string `json:"description"`
KeyFact bool `json:"keyFact"`
Type string `json:"type"`
Category string `json:"category"`
Id int `json:"id"`
Environment int `json:"environment"`
Name string `json:"name"`
Value string `json:"value"`
Source string `json:"source"`
Service string `json:"service"`
Description string `json:"description"`
KeyFact bool `json:"keyFact"`
Type string `json:"type"`
OriginalFact PreTransformedFact `json:"originalFact"`
Category string `json:"category"`
}

type PreTransformedFact struct {
Name string
Value string
}

const (
Expand Down Expand Up @@ -375,7 +382,6 @@ func (h *Messaging) sendToLagoonAPI(incoming *InsightsMessage, resource Resource
// Just wrapping this in a function to clean up the calls near the bottom of this function
// could potentially be moved into its own method
var processFactList = func(facts []LagoonFact, apiClient graphql.Client, resource ResourceDestination, source string, h *Messaging) error {

project, environment, apiErr := determineResourceFromLagoonAPI(apiClient, resource)
log.Printf("Matched %v number of facts for project:environment '%v:%v' from source '%v'", len(facts), project, environment, source)

Expand Down Expand Up @@ -416,7 +422,6 @@ func (h *Messaging) sendToLagoonAPI(incoming *InsightsMessage, resource Resource

if insights.InputPayload == BinaryPayload {
for _, v := range incoming.BinaryPayload {

for _, filter := range parserFilters {
facts, source, err = filter(h, insights, v, apiClient, resource)
if err != nil {
Expand Down Expand Up @@ -590,6 +595,7 @@ func (h *Messaging) pushFactsToLagoonApi(facts []LagoonFact, resource ResourceDe
Name: fact.Name,
Value: fact.Value,
Source: fact.Source,
Service: fact.Service,
Description: fact.Description,
KeyFact: fact.KeyFact,
Type: lagoonclient.FactType(fact.Type),
Expand Down
6 changes: 0 additions & 6 deletions internal/handler/registerFilters.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func ProcessLagoonFactAgainstRegisteredFilters(fact LagoonFact, insightsRawData
if !pfout.isFilteredOut() {
fact = pfout.getFact()
}

}
return fact, nil
}
Expand All @@ -68,15 +67,12 @@ func LoadTransformsFromDisk(filename string) ([]FactTransform, error) {
}

func GenerateFilterFromTransform(transform FactTransform) (func(filter parserFilter) parserFilter, error) {

return func(filter parserFilter) parserFilter {

if transform.Type != "" {
filter = filter.isOfType(transform.Type)
}

for i := range transform.Lookupvalue {

n := transform.Lookupvalue[i].Name
v := transform.Lookupvalue[i].Value
em := transform.Lookupvalue[i].ExactMatch
Expand All @@ -85,7 +81,6 @@ func GenerateFilterFromTransform(transform FactTransform) (func(filter parserFil
} else {
filter = filter.fieldContains(n, v)
}

}

for i := range transform.Transformations {
Expand All @@ -104,7 +99,6 @@ func GenerateFilterFromTransform(transform FactTransform) (func(filter parserFil
}

func RegisterFiltersFromJson(filename string) error {

transforms, err := LoadTransformsFromDisk(filename)
if err != nil {
return err
Expand Down
13 changes: 13 additions & 0 deletions internal/handler/testassets/testSbomDuplicateFacts.gz.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions internal/lagoonclient/facts.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
)

type Project struct {
Id int
Name string
Id int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
}

type Environment struct {
Id int
Name string
Id int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
}

type Fact struct {
Expand Down
1 change: 1 addition & 0 deletions internal/lagoonclient/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions internal/lagoonclient/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ input AddFactInput {
name: String!
value: String!
source: String!
service: String!
description: String!
keyFact: Boolean
type: FactType
Expand Down Expand Up @@ -865,6 +866,7 @@ type Fact {
name: String
value: String
source: String
service: String
description: String
keyFact: Boolean
type: FactType
Expand Down Expand Up @@ -1954,6 +1956,7 @@ input UpdateFactInputValue {
name: String!
value: String!
source: String!
service: String!
description: String
keyFact: Boolean
type: FactType
Expand Down