Skip to content

Commit

Permalink
WIP: zipped reports download
Browse files Browse the repository at this point in the history
  • Loading branch information
gnmahanth committed Nov 6, 2024
1 parent 213b39e commit 1bb1f16
Show file tree
Hide file tree
Showing 12 changed files with 948 additions and 389 deletions.
1 change: 1 addition & 0 deletions deepfence_server/handler/export_reports.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ func (h *Handler) GenerateReport(w http.ResponseWriter, r *http.Request) {
ToTimestamp: toTimestamp,
Filters: req.Filters,
Options: req.Options,
ZippedReport: req.ZippedReport,
}

// scan id can only be sent while downloading individual scans
Expand Down
1 change: 1 addition & 0 deletions deepfence_server/model/reports.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type GenerateReportReq struct {
ToTimestamp int64 `json:"to_timestamp"` // timestamp in milliseconds
Filters utils.ReportFilters `json:"filters"`
Options utils.ReportOptions `json:"options" validate:"omitempty"`
ZippedReport bool `json:"zipped_report"`
}

type GenerateReportResp struct {
Expand Down
1 change: 1 addition & 0 deletions deepfence_utils/utils/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ const (
ReportXLSX ReportType = "xlsx"
ReportPDF ReportType = "pdf"
ReportSBOM ReportType = "sbom"
ReportZIP ReportType = "zip"
)

// mask_global : This is to mask gobally. (same as previous mask_across_hosts_and_images flag)
Expand Down
1 change: 1 addition & 0 deletions deepfence_utils/utils/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ type ReportParams struct {
ToTimestamp time.Time `json:"to_timestamp"`
Filters ReportFilters `json:"filters"`
Options ReportOptions `json:"options,omitempty"`
ZippedReport bool `json:"zipped_report"`
}

type ReportOptions struct {
Expand Down
40 changes: 40 additions & 0 deletions deepfence_utils/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ var (

SBOMFormatReplacer = strings.NewReplacer("@", "_", ".", "_")

NodeNameReplacer = strings.NewReplacer("/", "_", " ", "")

matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
once1, once2 sync.Once
Expand Down Expand Up @@ -782,3 +784,41 @@ func ComputeChecksumForFile(filePath string) (string, error) {
cs := fmt.Sprintf("%x", h.Sum(nil))
return cs, nil
}

func ZipDir(sourceDir string, baseZipPath string, outputZip string) error {
archive, err := os.Create(outputZip)
if err != nil {
return err
}
defer archive.Close()

zw := zip.NewWriter(archive)
defer zw.Close()

return filepath.Walk(sourceDir,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()

f, err := zw.Create(filepath.Join(baseZipPath, info.Name()))
if err != nil {
return err
}

_, err = io.Copy(f, file)
if err != nil {
return err
}

return nil
})
}
19 changes: 2 additions & 17 deletions deepfence_worker/tasks/reports/pdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package reports
import (
"context"
_ "embed"
"os"
"strconv"
"time"

Expand Down Expand Up @@ -189,7 +188,7 @@ func generatePDF(ctx context.Context, params utils.ReportParams) (string, error)
log := log.WithCtx(ctx)

var (
document core.Document
document string
err error
)

Expand All @@ -213,19 +212,5 @@ func generatePDF(ctx context.Context, params utils.ReportParams) (string, error)
return "", err
}

// create a temp file to hold pdf report
temp, err := os.CreateTemp("", "report-*-"+reportFileName(params))
if err != nil {
return "", err
}
defer temp.Close()

if _, err := temp.Write(document.GetBytes()); err != nil {
return "", err
}

log.Info().Msgf("report id %s pdf generation metrics %s",
params.ReportID, document.GetReport())

return temp.Name(), nil
return document, nil
}
Loading

0 comments on commit 1bb1f16

Please sign in to comment.