Skip to content

Commit

Permalink
xlsx use customFields
Browse files Browse the repository at this point in the history
  • Loading branch information
Harshvardhan Karn committed Nov 20, 2023
1 parent e5cc1e3 commit 104884d
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 35 deletions.
22 changes: 12 additions & 10 deletions deepfence_server/handler/export_reports.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,11 @@ func (h *Handler) GenerateReport(w http.ResponseWriter, r *http.Request) {
// report task params
report_id := uuid.New().String()
params := utils.ReportParams{
ReportID: report_id,
ReportType: req.ReportType,
Duration: req.Duration,
Filters: req.Filters,
ReportID: report_id,
ReportType: req.ReportType,
Duration: req.Duration,
Filters: req.Filters,
CustomFields: req.CustomFields,
}

worker, err := directory.Worker(r.Context())
Expand Down Expand Up @@ -294,14 +295,15 @@ func (h *Handler) GenerateReport(w http.ResponseWriter, r *http.Request) {
defer tx.Close()

query := `
CREATE (n:Report{created_at:TIMESTAMP(), type:$type, report_id:$uid, status:$status, filters:$filters, duration:$duration})
CREATE (n:Report{created_at:TIMESTAMP(), type:$type, report_id:$uid, status:$status, filters:$filters, duration:$duration, custom_fields:$custom_fields})
RETURN n`
vars := map[string]interface{}{
"type": req.ReportType,
"uid": report_id,
"status": utils.SCAN_STATUS_STARTING,
"filters": req.Filters.String(),
"duration": req.Duration,
"type": req.ReportType,
"uid": report_id,
"status": utils.SCAN_STATUS_STARTING,
"filters": req.Filters.String(),
"duration": req.Duration,
"custom_fields": req.CustomFields,
}

_, err = tx.Run(query, vars)
Expand Down
7 changes: 4 additions & 3 deletions deepfence_server/model/reports.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
)

type GenerateReportReq struct {
ReportType string `json:"report_type" validate:"required" required:"true" enum:"pdf,xlsx"`
Duration int `json:"duration" enum:"0,1,7,30,60,90,180"`
Filters utils.ReportFilters `json:"filters"`
ReportType string `json:"report_type" validate:"required" required:"true" enum:"pdf,xlsx"`
Duration int `json:"duration" enum:"0,1,7,30,60,90,180"`
Filters utils.ReportFilters `json:"filters"`
CustomFields []string `json:"custom_fields"`
}

type GenerateReportResp struct {
Expand Down
9 changes: 5 additions & 4 deletions deepfence_utils/utils/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ type MalwareScanParameters struct {
}

type ReportParams struct {
ReportID string `json:"report_id"`
ReportType string `json:"report_type"`
Duration int `json:"duration"`
Filters ReportFilters `json:"filters"`
ReportID string `json:"report_id"`
ReportType string `json:"report_type"`
Duration int `json:"duration"`
Filters ReportFilters `json:"filters"`
CustomFields []string `json:"custom_fields"`
}

type ReportFilters struct {
Expand Down
104 changes: 86 additions & 18 deletions deepfence_worker/tasks/reports/xlsx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package reports
import (
"context"
"os"
"reflect"

"github.com/deepfence/ThreatMapper/deepfence_utils/log"
"github.com/deepfence/ThreatMapper/deepfence_utils/utils"
Expand Down Expand Up @@ -72,6 +73,20 @@ var (
}
)

func customFieldsToMap(customFields []string) map[string]string {
result := make(map[string]string)

for i, field := range customFields {
// Convert index to corresponding column letter
colLetter := string(rune('A' + i))

// Create map entry with the column letter as key and field as value
result[colLetter+"1"] = field
}

return result
}

func generateXLSX(ctx context.Context, params utils.ReportParams) (string, error) {

var (
Expand Down Expand Up @@ -126,6 +141,45 @@ func xlsxSetHeader(xlsx *excelize.File, sheet string, headers map[string]string)
}
}

func getMatchingValues(v interface{}, scanInfo interface{}, customFields []string) ([]interface{}, error) {
var matchingValues []interface{}

vValue := reflect.ValueOf(v)
scanInfoValue := reflect.ValueOf(scanInfo)

for _, field := range customFields {
// Get field values using reflection and JSON tags
vFieldValue := getFieldByJSONTag(vValue, field)
scanInfoFieldValue := getFieldByJSONTag(scanInfoValue, field)

// Check which field has valid value
if vFieldValue.IsValid() && !vFieldValue.IsZero() {
matchingValues = append(matchingValues, vFieldValue.Interface())
continue
}

if scanInfoFieldValue.IsValid() && !scanInfoFieldValue.IsZero() {
matchingValues = append(matchingValues, scanInfoFieldValue.Interface())
continue
}
}

return matchingValues, nil
}

// getFieldByJSONTag retrieves a field value by its JSON tag
func getFieldByJSONTag(value reflect.Value, jsonTag string) reflect.Value {
for i := 0; i < value.NumField(); i++ {
field := value.Type().Field(i)
tag := field.Tag.Get("json")

if tag == jsonTag {
return value.Field(i)
}
}
return reflect.Value{}
}

func vulnerabilityXLSX(ctx context.Context, params utils.ReportParams) (string, error) {
data, err := getVulnerabilityData(ctx, params)
if err != nil {
Expand All @@ -140,6 +194,12 @@ func vulnerabilityXLSX(ctx context.Context, params utils.ReportParams) (string,
}
}()

if len(params.CustomFields) > 0 {
// make vulnerabilityHeader according to custom fields
vulnerabilityHeader = customFieldsToMap(params.CustomFields)
}
log.Info().Msgf("vulnerabilityHeader: %+v", vulnerabilityHeader)

xlsxSetHeader(xlsx, "Sheet1", vulnerabilityHeader)

offset := 0
Expand All @@ -149,24 +209,32 @@ func vulnerabilityXLSX(ctx context.Context, params utils.ReportParams) (string,
if err != nil {
log.Error().Err(err).Msg("error generating cell name")
}
value := []interface{}{
nodeScanData.ScanInfo.UpdatedAt,
v.Cve_attack_vector,
v.Cve_caused_by_package,
nodeScanData.ScanInfo.NodeName,
nodeScanData.ScanInfo.ScanID,
nodeScanData.ScanInfo.NodeID,
v.Cve_cvss_score,
v.Cve_description,
v.Cve_fixed_in,
v.Cve_id,
v.Cve_link,
v.Cve_severity,
v.Cve_overall_score,
v.Cve_type,
nodeScanData.ScanInfo.HostName,
nodeScanData.ScanInfo.CloudAccountID,
v.Masked,
var value []interface{}
if len(params.CustomFields) > 0 {
value, err = getMatchingValues(v, nodeScanData.ScanInfo, params.CustomFields)
if err != nil {
log.Error().Err(err).Msg("error getting matching values")
}
} else {
value = []interface{}{
nodeScanData.ScanInfo.UpdatedAt,
v.Cve_attack_vector,
v.Cve_caused_by_package,
nodeScanData.ScanInfo.NodeName,
nodeScanData.ScanInfo.ScanID,
nodeScanData.ScanInfo.NodeID,
v.Cve_cvss_score,
v.Cve_description,
v.Cve_fixed_in,
v.Cve_id,
v.Cve_link,
v.Cve_severity,
v.Cve_overall_score,
v.Cve_type,
nodeScanData.ScanInfo.HostName,
nodeScanData.ScanInfo.CloudAccountID,
v.Masked,
}
}
err = xlsx.SetSheetRow("Sheet1", cellName, &value)
if err != nil {
Expand Down

0 comments on commit 104884d

Please sign in to comment.