Skip to content
Merged
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
4 changes: 4 additions & 0 deletions internal/handler/FactDataTransformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TestFactProcessor_TestMultipleFilters(t *testing.T) {
KeyFact: false,
Type: "",
Category: "",
Service: "",
}

fp1 := FactProcessor{
Expand Down Expand Up @@ -69,6 +70,7 @@ func TestFactProcessor_ProcessLagoonFactAgainstRegisteredFilters(t *testing.T) {
KeyFact: false,
Type: "",
Category: "",
Service: "",
}

poppedFactFilters := KeyFactFilters
Expand Down Expand Up @@ -181,6 +183,7 @@ func TestFactProcessor_TestSetFriendlyName(t *testing.T) {
KeyFact: false,
Type: "",
Category: "",
Service: "",
},
InsightsData: EnvironmentVariable{
Key: "testkey",
Expand Down Expand Up @@ -213,6 +216,7 @@ func TestFactProcessor_TestExactMatchLookup(t *testing.T) {
KeyFact: false,
Type: "",
Category: "",
Service: "",
},
InsightsData: EnvironmentVariable{
Key: "testkey",
Expand Down
10 changes: 6 additions & 4 deletions internal/handler/imageInspectParserFilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type ImageData struct {

func processImageInspectInsightsData(h *Messaging, insights InsightsData, v string, apiClient graphql.Client, resource ResourceDestination) ([]LagoonFact, string, error) {
source := fmt.Sprintf("insights:image:%s", resource.Service)
service := resource.Service
logger := slog.With("ProjectName", resource.Project, "EnvironmentName", resource.Environment, "Source", source)
if insights.InsightsType == Image {

Expand All @@ -34,12 +35,12 @@ func processImageInspectInsightsData(h *Messaging, insights InsightsData, v stri
}
environmentId := environment.Id

return ProcessImageInspectData(v, logger, environmentId, source)
return ProcessImageInspectData(v, logger, environmentId, source, service)
}
return []LagoonFact{}, "", nil
}

func ProcessImageInspectData(v string, logger *slog.Logger, environmentId int, source string) ([]LagoonFact, string, error) {
func ProcessImageInspectData(v string, logger *slog.Logger, environmentId int, source string, service string) ([]LagoonFact, string, error) {
decoded, err := decodeGzipString(v)
if err != nil {
return nil, "", err
Expand All @@ -53,7 +54,7 @@ func ProcessImageInspectData(v string, logger *slog.Logger, environmentId int, s
return nil, "", err
}

facts, err := processFactsFromImageInspect(logger, imageInspect, environmentId, source)
facts, err := processFactsFromImageInspect(logger, imageInspect, environmentId, source, service)
if err != nil {
return nil, "", err
}
Expand All @@ -66,7 +67,7 @@ func ProcessImageInspectData(v string, logger *slog.Logger, environmentId int, s
return facts, source, nil
}

func processFactsFromImageInspect(logger *slog.Logger, imageInspectData ImageData, id int, source string) ([]LagoonFact, error) {
func processFactsFromImageInspect(logger *slog.Logger, imageInspectData ImageData, id int, source string, service string) ([]LagoonFact, error) {

var factsInput []LagoonFact

Expand Down Expand Up @@ -101,6 +102,7 @@ func processFactsFromImageInspect(logger *slog.Logger, imageInspectData ImageDat
Description: "Environment Variable",
KeyFact: false,
Type: FactTypeText,
Service: service,
}

logger.Debug("Processing environment fact", "name", f.Key, "value", f.Value)
Expand Down
3 changes: 2 additions & 1 deletion internal/handler/imageInspectParserFilter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func Test_processFactsFromImageInspect(t *testing.T) {
imageInspectDataSource string
id int
source string
service string
}
tests := []struct {
name string
Expand Down Expand Up @@ -54,7 +55,7 @@ func Test_processFactsFromImageInspect(t *testing.T) {
panic(1)
}

got, err := processFactsFromImageInspect(tt.args.logger, imageInspectData, tt.args.id, tt.args.source)
got, err := processFactsFromImageInspect(tt.args.logger, imageInspectData, tt.args.id, tt.args.source, tt.args.service)
if (err != nil) != tt.wantErr {
t.Errorf("processFactsFromImageInspect() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
7 changes: 4 additions & 3 deletions internal/handler/insightsParserFilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func processSbomInsightsData(h *Messaging, insights InsightsData, v string, apiC

source := fmt.Sprintf("insights:sbom:%s", resource.Service)
logger := slog.With("ProjectName", resource.Project, "EnvironmentName", resource.Environment, "Source", source)

service := resource.Service
if insights.InsightsType != Sbom {
return []LagoonFact{}, "", nil
}
Expand Down Expand Up @@ -45,7 +45,7 @@ func processSbomInsightsData(h *Messaging, insights InsightsData, v string, apiC
}

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

facts, err = KeyFactsFilter(facts)
if err != nil {
Expand All @@ -66,7 +66,7 @@ func processSbomInsightsData(h *Messaging, insights InsightsData, v string, apiC
return facts, source, nil
}

func processFactsFromSBOM(logger *slog.Logger, facts *[]cdx.Component, environmentId int, source string) []LagoonFact {
func processFactsFromSBOM(logger *slog.Logger, facts *[]cdx.Component, environmentId int, source string, service string) []LagoonFact {
var factsInput []LagoonFact
if facts == nil || len(*facts) == 0 {
return factsInput
Expand All @@ -92,6 +92,7 @@ func processFactsFromSBOM(logger *slog.Logger, facts *[]cdx.Component, environme
Description: f.PackageURL,
KeyFact: false,
Type: FactTypeText,
Service: service,
}
//if EnableDebug {
// log.Println("[DEBUG] processing fact name " + f.Name)
Expand Down
5 changes: 5 additions & 0 deletions internal/handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ type InsightsMessage struct {
Annotations map[string]string `json:"annotations"`
Labels map[string]string `json:"labels"`
Type string `json:"type,omitempty"`
Environment string `json:"environment"`
Project string `json:"project"`
Service string `json:"service"`
}

type PayloadInput struct {
Expand Down Expand Up @@ -135,6 +138,7 @@ type LagoonFact struct {
KeyFact bool `json:"keyFact"`
Type string `json:"type"`
Category string `json:"category"`
Service string `json:"service"`
}

const (
Expand Down Expand Up @@ -573,6 +577,7 @@ func (h *Messaging) pushFactsToLagoonApi(facts []LagoonFact, resource ResourceDe
KeyFact: fact.KeyFact,
Type: lagoonclient.FactType(fact.Type),
Category: fact.Category,
Service: fact.Service,
}

}
Expand Down
6 changes: 4 additions & 2 deletions internal/handler/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func Test_processFactsFromSBOM(t *testing.T) {
bom *[]cdx.Component
environmentId int
source string
service string
}

testResponse, err := ioutil.ReadFile("./testassets/testSbomPayload.json")
Expand Down Expand Up @@ -201,7 +202,7 @@ func Test_processFactsFromSBOM(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := processFactsFromSBOM(slog.Default(), tt.args.bom, tt.args.environmentId, tt.args.source)
got := processFactsFromSBOM(slog.Default(), tt.args.bom, tt.args.environmentId, tt.args.source, tt.args.service)
if len(got) != len(tt.want) {
t.Errorf("processFactsFromSBOM() returned %d results, want %d", len(got), len(tt.want))
}
Expand All @@ -224,6 +225,7 @@ func Test_processFactsFromSBOMWithNoComponents(t *testing.T) {
bom *[]cdx.Component
environmentId int
source string
service string
}

testResponse, err := ioutil.ReadFile("./testassets/testSbomPayloadNoComponents.json")
Expand Down Expand Up @@ -266,7 +268,7 @@ func Test_processFactsFromSBOMWithNoComponents(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := processFactsFromSBOM(slog.Default(), tt.args.bom, tt.args.environmentId, tt.args.source)
got := processFactsFromSBOM(slog.Default(), tt.args.bom, tt.args.environmentId, tt.args.source, tt.args.service)
if len(got) != len(tt.want) {
t.Errorf("processFactsFromSBOM() returned %d results, want %d", len(got), len(tt.want))
}
Expand Down
39 changes: 28 additions & 11 deletions internal/handler/messaging.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,34 @@ func preprocessIncomingMessageData(incoming *InsightsMessage) (ResourceDestinati
OutputFileMIMEType: "application/json",
}

// Prioritize the insights data over labels
if incoming.Project != "" {
resource.Project = incoming.Project
} else if label, ok := incoming.Labels["lagoon.sh/project"]; ok {
resource.Project = label
}

if incoming.Environment != "" {
resource.Environment = incoming.Environment
} else if label, ok := incoming.Labels["lagoon.sh/environment"]; ok {
resource.Environment = label
}

if incoming.Service != "" {
resource.Service = incoming.Service
} else if label, ok := incoming.Labels["lagoon.sh/service"]; ok {
resource.Service = label
}

if incoming.Type != "" {
insights.InputType = incoming.Type
} else if label, ok := incoming.Labels["lagoon.sh/insightsType"]; ok {
insights.InputType = label
if label == "image-gz" {
insights.LagoonType = ImageFacts
}
}

// Check labels for insights data from message
if incoming.Labels != nil {
labelKeys := make([]string, 0, len(incoming.Labels))
Expand All @@ -181,17 +209,6 @@ func preprocessIncomingMessageData(incoming *InsightsMessage) (ResourceDestinati

for _, label := range labelKeys {
switch label {
case "lagoon.sh/project":
resource.Project = incoming.Labels["lagoon.sh/project"]
case "lagoon.sh/environment":
resource.Environment = incoming.Labels["lagoon.sh/environment"]
case "lagoon.sh/service":
resource.Service = incoming.Labels["lagoon.sh/service"]
case "lagoon.sh/insightsType":
insights.InputType = incoming.Labels["lagoon.sh/insightsType"]
if incoming.Labels["lagoon.sh/insightsType"] == "image-gz" {
insights.LagoonType = ImageFacts
}
case "lagoon.sh/insightsOutputCompressed":
compressed, _ := strconv.ParseBool(incoming.Labels["lagoon.sh/insightsOutputCompressed"])
insights.OutputCompressed = compressed
Expand Down
1 change: 1 addition & 0 deletions internal/handler/processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func processFactsDirectly(message mq.Message, h *Messaging) string {
KeyFact: false,
Type: lagoonclient.FactType(vartypeString),
Category: fact.Category,
Service: fact.Service,
}
factSources[fact.Source] = fact.Source
}
Expand Down
Loading