Skip to content

Commit

Permalink
Fixes merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
bomoko committed Nov 19, 2023
2 parents 402f3e9 + 6075b72 commit d0126c0
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 25 deletions.
2 changes: 1 addition & 1 deletion internal/handler/insightsParserFilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func processSbomInsightsData(h *Messaging, insights InsightsData, v string, apiC
logger.Debug("Trivy is reachable")
}
if isAlive {
err = SbomToProblems(apiClient, h.TrivyServerEndpoint, "/tmp/", environment.Id, "insights-handler", *bom)
err = SbomToProblems(apiClient, h.TrivyServerEndpoint, "/tmp/", environment.Id, resource.Service, *bom)
}
if err != nil {
return nil, "", err
Expand Down
7 changes: 7 additions & 0 deletions internal/handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ type DirectFacts struct {
InsightsType string `json:"insightsType"`
}

type DirectProblems struct {
EnvironmentId int `json:"environment"`
ProjectName string `json:"projectName"`
EnvironmentName string `json:"environmentName"`
Problems []lagoonclient.LagoonProblem `json:"problems"`
Type string `json:"type"`
}
type InsightsData struct {
InputType string
InputPayload PayloadType
Expand Down
6 changes: 3 additions & 3 deletions internal/handler/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,13 @@ func Test_processDirectFacts(t *testing.T) {

fmt.Println(string(message.Body()))

got := processItemsDirectly(tt.args.message, tt.args.h)
got := processFactsDirectly(tt.args.message, tt.args.h)
if (err != nil) != tt.wantErr {
t.Errorf("processItemsDirectly() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("processFactsDirectly() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("processItemsDirectly() got = %v, want %v", got, tt.want)
t.Errorf("processFactsDirectly() got = %v, want %v", got, tt.want)
}
})
}
Expand Down
53 changes: 36 additions & 17 deletions internal/handler/messaging.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package handler

import (
"encoding/json"
"fmt"
"github.com/cheshir/go-mq"
"log"
"log/slog"
"sort"
"strconv"
Expand Down Expand Up @@ -39,7 +39,6 @@ func NewMessaging(config mq.Config, lagoonAPI LagoonAPI, s3 S3, startupAttempts
func (h *Messaging) processMessageQueue(message mq.Message) {
var insights InsightsData
var resource ResourceDestination

acknowledgeMessage := func(message mq.Message) func() {
return func() {
// Ack to remove from queue
Expand All @@ -61,13 +60,30 @@ func (h *Messaging) processMessageQueue(message mq.Message) {
}(message)

incoming := &InsightsMessage{}
json.Unmarshal(message.Body(), incoming)
err := json.Unmarshal(message.Body(), incoming)

if err != nil {
fmt.Printf(err.Error())
acknowledgeMessage()
return
}

// if we have direct problems or facts, we process them differently - skipping all
// the extra processing below.
if incoming.Type == "direct.facts" || incoming.Type == "direct.problems" {
resp := processItemsDirectly(message, h)
slog.Info(resp)
if incoming.Type == "direct.facts" {
resp := processFactsDirectly(message, h)
slog.Debug(resp)
acknowledgeMessage()
return
}

if incoming.Type == "direct.problems" {
resp, _ := processProblemsDirectly(message, h)
if h.EnableDebug {
for _, d := range resp {
slog.Debug(d)
}
}
acknowledgeMessage()
return
}
Expand Down Expand Up @@ -133,9 +149,7 @@ func (h *Messaging) processMessageQueue(message mq.Message) {

// Determine incoming payload type
if incoming.Payload == nil && incoming.BinaryPayload == nil {
if h.EnableDebug {
log.Printf("[DEBUG] no payload was found")
}
slog.Debug("No payload was found - rejecting message and exiting")
rejectMessage(false)
return
}
Expand All @@ -147,18 +161,22 @@ func (h *Messaging) processMessageQueue(message mq.Message) {
}

// Debug
if h.EnableDebug {
log.Println("[DEBUG] insights:", insights)
log.Println("[DEBUG] target:", resource)
}
//if h.EnableDebug {
// log.Println("[DEBUG] insights:", insights)
// log.Println("[DEBUG] target:", resource)
//}
slog.Debug("Insights", "data", fmt.Sprint(insights))
slog.Debug("Target", "data", fmt.Sprint(resource))

// Process s3 upload
if !h.S3Config.Disabled {
if insights.InsightsType != Direct {
err := h.sendToLagoonS3(incoming, insights, resource)
if err != nil {
log.Printf("Unable to send to S3: %s", err.Error())
// TODO: do we reque here? Reject
//log.Printf("Unable to send to S3: %s", err.Error())
slog.Error("Unable to send to S3", "Error", err.Error())

// TODO: BETTER ERROR HANDLING
}
}
}
Expand All @@ -169,12 +187,13 @@ func (h *Messaging) processMessageQueue(message mq.Message) {
insights.InsightsType != Image &&
insights.InsightsType != Raw &&
insights.InsightsType != Direct {
log.Println("only 'sbom', 'direct', 'raw', and 'image' types are currently supported for api processing")
slog.Error("only 'sbom', 'direct', 'raw', and 'image' types are currently supported for api processing")
} else {
err := h.sendToLagoonAPI(incoming, resource, insights)

if err != nil {
log.Printf("Unable to send to the api: %s", err.Error())
//log.Printf("Unable to send to the api: %s", err.Error())
slog.Error("Unable to send to the API", "Error", err.Error())
rejectMessage(false)
return
}
Expand Down
60 changes: 57 additions & 3 deletions internal/handler/processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import (

// processing.go contains the functions that actually process the incoming messages

func processItemsDirectly(message mq.Message, h *Messaging) string {
func processFactsDirectly(message mq.Message, h *Messaging) string {
var directFacts DirectFacts
json.Unmarshal(message.Body(), &directFacts)
err := json.Unmarshal(message.Body(), &directFacts)
if err != nil {
log.Println("Error unmarshaling JSON:", err)
log.Println("Error unmarshaling JSON:", err.Error())
return "exciting, unable to process direct facts"
}

Expand Down Expand Up @@ -63,7 +63,7 @@ func processItemsDirectly(message mq.Message, h *Messaging) string {
if err != nil {
log.Println(err)
}
log.Printf("Deleted facts on '%v:%v' for source %v", directFacts.ProjectName, directFacts.EnvironmentName, s)
log.Printf("Deleted facts on '%v:%v' for source %v\n", directFacts.ProjectName, directFacts.EnvironmentName, s)
}

facts, err := lagoonclient.AddFacts(context.TODO(), apiClient, processedFacts)
Expand All @@ -73,3 +73,57 @@ func processItemsDirectly(message mq.Message, h *Messaging) string {

return facts
}

func processProblemsDirectly(message mq.Message, h *Messaging) ([]string, error) {
var directProblems DirectProblems
json.Unmarshal(message.Body(), &directProblems)
log.Println(directProblems)
err := json.Unmarshal(message.Body(), &directProblems)
if err != nil {
log.Println("Error unmarshaling JSON:", err)
return []string{}, err
}

if h.EnableDebug {
log.Print("[DEBUG] problems", directProblems)
}

apiClient := graphql.NewClient(h.LagoonAPI.Endpoint, &http.Client{Transport: &authedTransport{wrapped: http.DefaultTransport, h: h}})

// serviceSource just gives us simple structure to do the deletions
type serviceSource struct {
Source string
Service string
}
problemSources := map[string]serviceSource{}

for i, problem := range directProblems.Problems {

// We want to ensure that the incoming problems aren't malformed or trying to do anything dodgy with env ids

if problem.Environment != directProblems.EnvironmentId {
directProblems.Problems[i].Environment = directProblems.EnvironmentId
}

problemSources[problem.Service+problem.Source] = serviceSource{
Source: problem.Source,
Service: problem.Service,
}
}

for _, s := range problemSources {
_, err := lagoonclient.DeleteProblemsFromSource(context.TODO(), apiClient, directProblems.EnvironmentId, s.Service, s.Source)
if err != nil {
log.Println(err) //This could potentially mess up the state if we've already deleted source info, might
return []string{}, err
}
log.Printf("Deleted Problems on '%v:%v' for source %v\n", directProblems.ProjectName, directProblems.EnvironmentName, s)
}

resptext, err := lagoonclient.AddProblems(context.TODO(), apiClient, directProblems.Problems)
if err != nil {
log.Println(err)
}

return resptext, nil
}
22 changes: 22 additions & 0 deletions internal/handler/testassets/directProblemsPayload.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"projectName": "test6-drupal-example-simple",
"environment": 1236,
"environmentName": "test1copy",
"problems": [
{
"environment": 3,
"service": "cli",
"identifier": "testidentifier",
"version": "1.0.0",
"fixedVersion": "1.0.1",
"source": "insights:problems:cli",
"data": "Nothing to write home about",
"severityScore": 0.1,
"severity": "CRITICAL",
"description": "Test description"

}
],
"type": "direct.problems",
"source": "insights:problems:cli"
}
20 changes: 20 additions & 0 deletions internal/handler/testassets/directProblemsPayload2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"environment": 1236,
"projectName": "test6-drupal-example-simple",
"environmentName": "test1copy",
"problems": [
{
"environment": 1236,
"identifier": "1234",
"version": "1.0.1",
"fixedVersion": "1.0.2",
"source": "test",
"service": "cli",
"data": "{}",
"severity": "CRITICAL",
"severityScore": 0.5,
"description": "This is a test 1"
}
],
"type": "direct.problems"
}
2 changes: 1 addition & 1 deletion internal/lagoonclient/problems.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func AddProblems(ctx context.Context, client graphql.Client, problems []LagoonPr
}

func DeleteProblemsFromSource(ctx context.Context, client graphql.Client, environmentID int, service string, source string) (string, error) {
resp, err := deleteProblemsFromSource(ctx, client, environmentID, service, source)
resp, err := deleteProblemsFromSource(ctx, client, environmentID, source, service)
if err != nil {
return "", err
}
Expand Down

0 comments on commit d0126c0

Please sign in to comment.