From dff6164f1f3bef89ae64fcda02f4e75fa2966f82 Mon Sep 17 00:00:00 2001 From: Blaize M Kaye Date: Wed, 11 Oct 2023 10:16:30 +1300 Subject: [PATCH] interim work --- internal/handler/main.go | 51 ++++++++------------ internal/handler/messaging.go | 14 +++--- internal/handler/testassets/testSbom.gz.json | 4 +- main.go | 8 +++ 4 files changed, 38 insertions(+), 39 deletions(-) diff --git a/internal/handler/main.go b/internal/handler/main.go index 1303cb5..1062cc2 100644 --- a/internal/handler/main.go +++ b/internal/handler/main.go @@ -293,21 +293,7 @@ func parserFilterLoopForBinaryPayloads(insights InsightsData, p string, h *Messa log.Println(fmt.Errorf(err.Error())) } - for _, r := range result { - if fact, ok := r.(LagoonFact); ok { - // Handle single fact - err = h.sendFactsToLagoonAPI([]LagoonFact{fact}, apiClient, resource, source) - if err != nil { - fmt.Println(err) - } - } else if facts, ok := r.([]LagoonFact); ok { - // Handle slice of facts - h.sendFactsToLagoonAPI(facts, apiClient, resource, source) - } else { - // Unexpected type returned from filter() - log.Printf("unexpected type returned from filter(): %T\n", r) - } - } + processResultset(result, err, h, apiClient, resource, source) } } @@ -326,20 +312,25 @@ func parserFilterLoopForPayloads(insights InsightsData, p PayloadInput, h *Messa log.Println(fmt.Errorf(err.Error())) } - for _, r := range result { - if fact, ok := r.(LagoonFact); ok { - // Handle single fact - err = h.sendFactsToLagoonAPI([]LagoonFact{fact}, apiClient, resource, source) - if err != nil { - fmt.Println(err) - } - } else if facts, ok := r.([]LagoonFact); ok { - // Handle slice of facts - h.sendFactsToLagoonAPI(facts, apiClient, resource, source) - } else { - // Unexpected type returned from filter() - log.Printf("unexpected type returned from filter(): %T\n", r) + processResultset(result, err, h, apiClient, resource, source) + } +} + +// processResultset will send results as facts to the lagoon api after processing via a parser filter +func processResultset(result []interface{}, err error, h *Messaging, apiClient graphql.Client, resource ResourceDestination, source string) { + for _, r := range result { + if fact, ok := r.(LagoonFact); ok { + // Handle single fact + err = h.sendFactsToLagoonAPI([]LagoonFact{fact}, apiClient, resource, source) + if err != nil { + fmt.Println(err) } + } else if facts, ok := r.([]LagoonFact); ok { + // Handle slice of facts + h.sendFactsToLagoonAPI(facts, apiClient, resource, source) + } else { + // Unexpected type returned from filter() + log.Printf("unexpected type returned from filter(): %T\n", r) } } } @@ -390,11 +381,11 @@ func determineResourceFromLagoonAPI(apiClient graphql.Client, resource ResourceD // Get project data (we need the project ID to be able to utilise the environmentByName query) project, err := lagoonclient.GetProjectByName(context.TODO(), apiClient, resource.Project) if err != nil { - return lagoonclient.Project{}, lagoonclient.Environment{}, fmt.Errorf("error: unable to determine resource destination (does %s:%s exist?)", resource.Project, resource.Environment) + return lagoonclient.Project{}, lagoonclient.Environment{}, fmt.Errorf("error: unable to determine resource destination (does %s:%s exist?): %v", resource.Project, resource.Environment, err.Error()) } if project.Id == 0 || project.Name == "" { - return lagoonclient.Project{}, lagoonclient.Environment{}, fmt.Errorf("error: unable to determine resource destination (does %s:%s exist?)", resource.Project, resource.Environment) + return lagoonclient.Project{}, lagoonclient.Environment{}, fmt.Errorf("error: unable to determine resource destination (does %s:%s exist?): %v", resource.Project, resource.Environment, err.Error()) } environment, err := lagoonclient.GetEnvironmentFromName(context.TODO(), apiClient, resource.Environment, project.Id) diff --git a/internal/handler/messaging.go b/internal/handler/messaging.go index b35b57e..7e68b2e 100644 --- a/internal/handler/messaging.go +++ b/internal/handler/messaging.go @@ -42,13 +42,13 @@ func (h *Messaging) processMessageQueue(message mq.Message) { // set up defer to ack the message after we're done processing - defer func(message mq.Message) { - // Ack to remove from queue - err := message.Ack(false) - if err != nil { - fmt.Printf("Failed to acknowledge message: %s\n", err.Error()) - } - }(message) + //defer func(message mq.Message) { + // // Ack to remove from queue + // err := message.Ack(false) + // if err != nil { + // fmt.Printf("Failed to acknowledge message: %s\n", err.Error()) + // } + //}(message) acknowledgeMessage := func(message mq.Message) func() { return func() { diff --git a/internal/handler/testassets/testSbom.gz.json b/internal/handler/testassets/testSbom.gz.json index 55d172c..f3ecab5 100644 --- a/internal/handler/testassets/testSbom.gz.json +++ b/internal/handler/testassets/testSbom.gz.json @@ -5,8 +5,8 @@ }, "annotations": {}, "labels": { - "lagoon.sh/project": "high-cotton", - "lagoon.sh/environment": "Master", + "lagoon.sh/project": "organization-cotton", + "lagoon.sh/environment": "main", "lagoon.sh/service": "cli", "lagoon.sh/insightsType": "sbom-gz" } diff --git a/main.go b/main.go index 7227375..d01820c 100644 --- a/main.go +++ b/main.go @@ -102,6 +102,14 @@ func main() { problemsFromSBOM = getEnvBool("PROBLEMS_FROM_SBOM", problemsFromSBOM) trivyServerEndpoint = getEnv("TRIVY_SERVER_ENDPOINT", trivyServerEndpoint) + if problemsFromSBOM == true { + log.Println("PROBLEMS FROM SBOM - enabled") + if trivyServerEndpoint == "" { + log.Fatalf("NO TRIVY SERVER ENDPOINT SET - exiting") + os.Exit(1) + } + } + // configure the backup handler settings broker := handler.RabbitBroker{ Hostname: fmt.Sprintf("%s:%s", mqHost, mqPort),