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

fix bug #414 where the jira consumer would not create issues with zer… #415

Merged
merged 3 commits into from
Oct 14, 2024
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
5 changes: 0 additions & 5 deletions components/consumers/jira/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ func ProcessMessages(allowDuplicates, allowFP bool, sevThreshold int) ([]documen
return nil, 0, err
}
messages, discarded := ProcessRawMessages(responses, sevThreshold)
if err != nil {
log.Print("Could not Process Raw Messages: ", err)
return nil, 0, err
}

return messages, discarded, nil
}
log.Print("Parsing Enriched results")
Expand Down
2 changes: 1 addition & 1 deletion pkg/jira/jira/apiutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package jira
import (
"testing"

jira "github.com/andygrunwald/go-jira"
"github.com/andygrunwald/go-jira"
"github.com/stretchr/testify/require"
"github.com/trivago/tgo/tcontainer"
)
Expand Down
64 changes: 36 additions & 28 deletions pkg/templating/template_description.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ const (
defaultRawFindingTemplate = "Dracon found '{{.Title}}' at '{{.Target}}', severity '{{.Severity}}', rule id: '{{.Type}}', CVSS '{{.Cvss}}' Confidence '{{.Confidence}}' Original Description: {{.Description}}, Cve {{.Cve}}"
)

type (
enrichedIssue struct {
*v1.EnrichedIssue
ToolName string
ScanStartTime string
ScanID string
ConfidenceText string
SeverityText string
Count uint
FirstFound string
}

enrichedIssueOption func(*enrichedIssue) error
)

// TemplateStringRaw applies the provided go template to the Raw Issue provided and returns the resulting str
func TemplateStringRaw(inputTemplate string, issue *v1.Issue) (*string, error) {
if inputTemplate == "" {
Expand All @@ -36,18 +51,7 @@ func TemplateStringRaw(inputTemplate string, issue *v1.Issue) (*string, error) {
return &res, nil
}

type enrichedIssue struct {
*v1.EnrichedIssue
ToolName string
ScanStartTime string
ScanID string
ConfidenceText string
SeverityText string
Count uint
FirstFound string
}
type enrichedIssueOption func(*enrichedIssue) error

// EnrichedIssueWithToolName allows customising the Enriched Issue's ToolName.
func EnrichedIssueWithToolName(toolname string) enrichedIssueOption {
return func(ei *enrichedIssue) error {
if toolname == "" {
Expand All @@ -58,16 +62,7 @@ func EnrichedIssueWithToolName(toolname string) enrichedIssueOption {
}
}

func EnrichedIssueWithScanStartTime(startTime time.Time) enrichedIssueOption {
return func(ei *enrichedIssue) error {
if time.Time.IsZero(startTime) {
return errors.New("invalid startTime zero")
}
ei.ScanStartTime = startTime.Format(time.RFC3339)
return nil
}
}

// EnrichedIssueWithConfidenceText allows customising the Enriched Issue's ConfidenceText.
func EnrichedIssueWithConfidenceText(confidence string) enrichedIssueOption {
return func(ei *enrichedIssue) error {
if confidence == "" {
Expand All @@ -78,6 +73,7 @@ func EnrichedIssueWithConfidenceText(confidence string) enrichedIssueOption {
}
}

// EnrichedIssueWithSeverityText allows customising the Enriched Issue's SeverityText.
func EnrichedIssueWithSeverityText(severity string) enrichedIssueOption {
return func(ei *enrichedIssue) error {
if severity == "" {
Expand All @@ -88,16 +84,15 @@ func EnrichedIssueWithSeverityText(severity string) enrichedIssueOption {
}
}

// EnrichedIssueWithCount allows customising the Enriched Issue's Count.
func EnrichedIssueWithCount(count uint) enrichedIssueOption {
return func(ei *enrichedIssue) error {
if count <= 0 {
return errors.Errorf("invalid count %d", count)
}
ei.Count = count
return nil
}
}

// EnrichedIssueWithScanID allows customising the Enriched Issue's Scan ID.
func EnrichedIssueWithScanID(scanID string) enrichedIssueOption {
return func(ei *enrichedIssue) error {
if scanID == "" {
Expand All @@ -108,12 +103,25 @@ func EnrichedIssueWithScanID(scanID string) enrichedIssueOption {
}
}

// EnrichedIssueWithScanStartTime allows customising the Enriched Issue's Scan start time.
func EnrichedIssueWithScanStartTime(startTime time.Time) enrichedIssueOption {
return func(ei *enrichedIssue) error {
if startTime.IsZero() {
andream16 marked this conversation as resolved.
Show resolved Hide resolved
return errors.Errorf("invalid startTime zero: %s", startTime.String())
}
ei.ScanStartTime = startTime.Format(time.RFC3339)
return nil
}
}

// EnrichedIssueWithFirstFound allows customising the Enriched Issue's Scan first found time.
func EnrichedIssueWithFirstFound(firstFound time.Time) enrichedIssueOption {
return func(ei *enrichedIssue) error {
if time.Time.IsZero(firstFound) {
return errors.New("invalid firstFound zero")
if firstFound.IsZero() {
return errors.Errorf("invalid firstFound zero %s", firstFound.String())
}
ei.FirstFound = firstFound.Format(time.RFC3339)
ff := firstFound.Format(time.RFC3339)
ei.FirstFound = ff
return nil
}
}
Expand Down
119 changes: 117 additions & 2 deletions pkg/templating/template_description_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import (
"testing"
"time"

"google.golang.org/protobuf/types/known/timestamppb"

"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/timestamppb"

v1 "github.com/ocurity/dracon/api/proto/v1"
)
Expand Down Expand Up @@ -138,3 +137,119 @@ func Test_TemplateStringEnriched(t *testing.T) {
})
}
}

func TestDescriptionOptions(t *testing.T) {
acceptableTime, err := time.Parse(time.RFC3339, "2024-10-10T20:06:33Z")
require.NoError(t, err)

for _, tt := range []struct {
name string
option enrichedIssueOption
wantErr bool
expectedEnrichedIssue *enrichedIssue
}{
{
name: "zero startTime returns err",
option: EnrichedIssueWithScanStartTime(time.Time{}),
wantErr: true,
},
{
name: "non zero startTime returns no err",
option: EnrichedIssueWithScanStartTime(acceptableTime),
wantErr: false,
expectedEnrichedIssue: &enrichedIssue{
ScanStartTime: acceptableTime.Format(time.RFC3339),
},
},
{
name: "zero firstFound returns err",
option: EnrichedIssueWithFirstFound(time.Time{}),
wantErr: true,
},
{
name: "non zero firstFound returns no err",
option: EnrichedIssueWithFirstFound(acceptableTime),
wantErr: false,
expectedEnrichedIssue: &enrichedIssue{
FirstFound: acceptableTime.Format(time.RFC3339),
},
},
{
name: "empty tool name returns err",
option: EnrichedIssueWithToolName(""),
wantErr: true,
},
{
name: "valid tool name returns no err",
option: EnrichedIssueWithToolName("some-tool"),
wantErr: false,
expectedEnrichedIssue: &enrichedIssue{
ToolName: "some-tool",
},
},
{
name: "empty confidence text returns err",
option: EnrichedIssueWithConfidenceText(""),
wantErr: true,
},
{
name: "valid confidence text returns no err",
option: EnrichedIssueWithConfidenceText("conf-text-1"),
wantErr: false,
expectedEnrichedIssue: &enrichedIssue{
ConfidenceText: "conf-text-1",
},
},
{
name: "empty severity text returns err",
option: EnrichedIssueWithSeverityText(""),
wantErr: true,
},
{
name: "valid severity text returns no err",
option: EnrichedIssueWithSeverityText("severity-text-1"),
wantErr: false,
expectedEnrichedIssue: &enrichedIssue{
SeverityText: "severity-text-1",
},
},
{
name: "0 count returns no err",
option: EnrichedIssueWithCount(0),
wantErr: false,
expectedEnrichedIssue: &enrichedIssue{
Count: 0,
},
},
{
name: "positive count returns no err",
option: EnrichedIssueWithCount(420),
wantErr: false,
expectedEnrichedIssue: &enrichedIssue{
Count: 420,
},
},
{
name: "empty scan ID returns err",
option: EnrichedIssueWithScanID(""),
wantErr: true,
},
{
name: "valid scan id returns no err",
option: EnrichedIssueWithScanID("scan-1"),
wantErr: false,
expectedEnrichedIssue: &enrichedIssue{
ScanID: "scan-1",
},
},
} {
t.Run(tt.name, func(t *testing.T) {
var ei enrichedIssue
if err := tt.option(&ei); tt.wantErr {
require.Error(t, err)
return
}
require.Equal(t, tt.expectedEnrichedIssue, &ei)
})
}
}