update
This commit is contained in:
@@ -0,0 +1,520 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"compress/flate"
|
||||
"fmt"
|
||||
"hash/crc32"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
const maxAuditExportRows = 1_048_572
|
||||
|
||||
func (s *Server) handleAuditExport(w http.ResponseWriter, r *http.Request) {
|
||||
tech, err := s.currentTechnician(r)
|
||||
if err != nil {
|
||||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
filters := auditFiltersFromRequest(r)
|
||||
records, err := s.listAuditEvents(r, filters, maxAuditExportRows+1)
|
||||
if err != nil {
|
||||
http.Error(w, "unable to export audit events", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if len(records) > maxAuditExportRows {
|
||||
http.Error(
|
||||
w,
|
||||
"audit export is too large for a single Excel worksheet; narrow the filters and try again",
|
||||
http.StatusRequestEntityTooLarge,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
_ = s.audit(
|
||||
r.Context(),
|
||||
"audit_exported",
|
||||
tech.Login,
|
||||
nil,
|
||||
"",
|
||||
"",
|
||||
s.clientIP(r),
|
||||
fmt.Sprintf("format=xlsx rows=%d range=%s", len(records), filters.TimeRange),
|
||||
)
|
||||
|
||||
exportedAt := time.Now()
|
||||
filename := "tapm-audit-" + exportedAt.In(s.displayTimeZone()).Format("20060102-150405") + ".xlsx"
|
||||
w.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|
||||
w.Header().Set("Content-Disposition", `attachment; filename="`+filename+`"`)
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
|
||||
if err := writeAuditWorkbook(w, records, filters, exportedAt, s.displayTimeZone()); err != nil {
|
||||
log.Printf("write audit Excel export: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) displayTimeZone() *time.Location {
|
||||
if s.cfg.DisplayTimeZone != nil {
|
||||
return s.cfg.DisplayTimeZone
|
||||
}
|
||||
return time.UTC
|
||||
}
|
||||
|
||||
func writeAuditWorkbook(
|
||||
output io.Writer,
|
||||
records []auditRecord,
|
||||
filters auditFilters,
|
||||
exportedAt time.Time,
|
||||
displayTimeZone *time.Location,
|
||||
) error {
|
||||
if displayTimeZone == nil {
|
||||
displayTimeZone = time.UTC
|
||||
}
|
||||
|
||||
archive := zip.NewWriter(output)
|
||||
files := []struct {
|
||||
name string
|
||||
content func(io.Writer) error
|
||||
}{
|
||||
{"[Content_Types].xml", func(w io.Writer) error {
|
||||
return writeOnlyString(w, contentTypesXML)
|
||||
}},
|
||||
{"_rels/.rels", func(w io.Writer) error {
|
||||
return writeOnlyString(w, packageRelationshipsXML)
|
||||
}},
|
||||
{"docProps/app.xml", func(w io.Writer) error {
|
||||
return writeOnlyString(w, appPropertiesXML)
|
||||
}},
|
||||
{"docProps/core.xml", func(w io.Writer) error {
|
||||
return writeAuditCoreProperties(w, exportedAt)
|
||||
}},
|
||||
{"xl/workbook.xml", func(w io.Writer) error {
|
||||
return writeOnlyString(w, workbookXML)
|
||||
}},
|
||||
{"xl/_rels/workbook.xml.rels", func(w io.Writer) error {
|
||||
return writeOnlyString(w, workbookRelationshipsXML)
|
||||
}},
|
||||
{"xl/styles.xml", func(w io.Writer) error {
|
||||
return writeOnlyString(w, auditStylesXML)
|
||||
}},
|
||||
{"xl/worksheets/sheet1.xml", func(w io.Writer) error {
|
||||
return writeAuditWorksheet(w, records, filters, exportedAt, displayTimeZone)
|
||||
}},
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
if err := writeRawZipFile(archive, file.name, file.content); err != nil {
|
||||
_ = archive.Close()
|
||||
return err
|
||||
}
|
||||
}
|
||||
return archive.Close()
|
||||
}
|
||||
|
||||
func writeRawZipFile(
|
||||
archive *zip.Writer,
|
||||
name string,
|
||||
content func(io.Writer) error,
|
||||
) error {
|
||||
checksum := crc32.NewIEEE()
|
||||
var compressedSize byteCounter
|
||||
var uncompressedSize byteCounter
|
||||
sizeCompressor, err := flate.NewWriter(&compressedSize, flate.DefaultCompression)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := content(io.MultiWriter(checksum, &uncompressedSize, sizeCompressor)); err != nil {
|
||||
_ = sizeCompressor.Close()
|
||||
return err
|
||||
}
|
||||
if err := sizeCompressor.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
header := &zip.FileHeader{
|
||||
Name: name,
|
||||
Method: zip.Deflate,
|
||||
CRC32: checksum.Sum32(),
|
||||
CompressedSize64: uint64(compressedSize),
|
||||
UncompressedSize64: uint64(uncompressedSize),
|
||||
}
|
||||
entry, err := archive.CreateRaw(header)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
compressor, err := flate.NewWriter(entry, flate.DefaultCompression)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := content(compressor); err != nil {
|
||||
_ = compressor.Close()
|
||||
return err
|
||||
}
|
||||
return compressor.Close()
|
||||
}
|
||||
|
||||
func writeAuditWorksheet(
|
||||
w io.Writer,
|
||||
records []auditRecord,
|
||||
filters auditFilters,
|
||||
exportedAt time.Time,
|
||||
displayTimeZone *time.Location,
|
||||
) error {
|
||||
lastRow := len(records) + 4
|
||||
if lastRow < 4 {
|
||||
lastRow = 4
|
||||
}
|
||||
if _, err := fmt.Fprintf(
|
||||
w,
|
||||
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>`+
|
||||
`<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">`+
|
||||
`<dimension ref="A1:H%d"/>`+
|
||||
`<sheetViews><sheetView showGridLines="0" workbookViewId="0">`+
|
||||
`<pane ySplit="4" topLeftCell="A5" activePane="bottomLeft" state="frozen"/>`+
|
||||
`</sheetView></sheetViews>`+
|
||||
`<sheetFormatPr defaultRowHeight="15"/>`+
|
||||
`<cols>`+
|
||||
`<col min="1" max="1" width="22" customWidth="1"/>`+
|
||||
`<col min="2" max="2" width="28" customWidth="1"/>`+
|
||||
`<col min="3" max="3" width="30" customWidth="1"/>`+
|
||||
`<col min="4" max="4" width="22" customWidth="1"/>`+
|
||||
`<col min="5" max="5" width="24" customWidth="1"/>`+
|
||||
`<col min="6" max="6" width="24" customWidth="1"/>`+
|
||||
`<col min="7" max="7" width="18" customWidth="1"/>`+
|
||||
`<col min="8" max="8" width="54" customWidth="1"/>`+
|
||||
`</cols><sheetData>`,
|
||||
lastRow,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := writeMergedRow(w, 1, "TAPM Audit Log", 1, 30); err != nil {
|
||||
return err
|
||||
}
|
||||
metadata := fmt.Sprintf(
|
||||
"Exported %s · %s · %d event(s)",
|
||||
exportedAt.In(displayTimeZone).Format("Jan 2, 2006 3:04 PM MST"),
|
||||
auditFilterDescription(filters),
|
||||
len(records),
|
||||
)
|
||||
if err := writeMergedRow(w, 2, metadata, 2, 24); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := writeString(w, `<row r="3" ht="8" customHeight="1"/>`); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
headers := []string{
|
||||
"Timestamp",
|
||||
"Event",
|
||||
"Customer / Deployment",
|
||||
"User",
|
||||
"Hostname",
|
||||
"Package ID",
|
||||
"Source IP",
|
||||
"Details",
|
||||
}
|
||||
if _, err := writeString(w, `<row r="4" ht="24" customHeight="1">`); err != nil {
|
||||
return err
|
||||
}
|
||||
for index, header := range headers {
|
||||
if err := writeInlineStringCell(w, cellReference(index+1, 4), header, 3); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if _, err := writeString(w, `</row>`); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for index, record := range records {
|
||||
rowNumber := index + 5
|
||||
if _, err := fmt.Fprintf(w, `<row r="%d">`, rowNumber); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := writeDateCell(
|
||||
w,
|
||||
cellReference(1, rowNumber),
|
||||
record.CreatedAt,
|
||||
displayTimeZone,
|
||||
5,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
values := []string{
|
||||
record.EventType,
|
||||
record.CustomerLabel,
|
||||
record.User,
|
||||
record.Hostname,
|
||||
record.PackageSlug,
|
||||
record.SourceIP,
|
||||
record.Details,
|
||||
}
|
||||
for column, value := range values {
|
||||
style := 4
|
||||
if column == len(values)-1 {
|
||||
style = 6
|
||||
}
|
||||
if err := writeInlineStringCell(
|
||||
w,
|
||||
cellReference(column+2, rowNumber),
|
||||
value,
|
||||
style,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if _, err := writeString(w, `</row>`); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := fmt.Fprintf(
|
||||
w,
|
||||
`</sheetData><autoFilter ref="A4:H%d"/>`+
|
||||
`<mergeCells count="2"><mergeCell ref="A1:H1"/><mergeCell ref="A2:H2"/></mergeCells>`+
|
||||
`<pageMargins left="0.25" right="0.25" top="0.5" bottom="0.5" header="0.2" footer="0.2"/>`+
|
||||
`<pageSetup orientation="landscape" fitToWidth="1" fitToHeight="0"/>`+
|
||||
`</worksheet>`,
|
||||
lastRow,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func writeMergedRow(w io.Writer, row int, value string, style int, height int) error {
|
||||
if _, err := fmt.Fprintf(w, `<row r="%d" ht="%d" customHeight="1">`, row, height); err != nil {
|
||||
return err
|
||||
}
|
||||
for column := 1; column <= 8; column++ {
|
||||
cellValue := ""
|
||||
if column == 1 {
|
||||
cellValue = value
|
||||
}
|
||||
if err := writeInlineStringCell(w, cellReference(column, row), cellValue, style); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
_, err := writeString(w, `</row>`)
|
||||
return err
|
||||
}
|
||||
|
||||
func writeInlineStringCell(w io.Writer, reference string, value string, style int) error {
|
||||
if _, err := fmt.Fprintf(w, `<c r="%s" s="%d" t="inlineStr"><is><t`, reference, style); err != nil {
|
||||
return err
|
||||
}
|
||||
if strings.HasPrefix(value, " ") || strings.HasSuffix(value, " ") {
|
||||
if _, err := writeString(w, ` xml:space="preserve"`); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if _, err := writeString(w, `>`); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := writeEscapedXML(w, value); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := writeString(w, `</t></is></c>`)
|
||||
return err
|
||||
}
|
||||
|
||||
func writeDateCell(
|
||||
w io.Writer,
|
||||
reference string,
|
||||
value time.Time,
|
||||
displayTimeZone *time.Location,
|
||||
style int,
|
||||
) error {
|
||||
local := value.In(displayTimeZone)
|
||||
displayedTime := time.Date(
|
||||
local.Year(),
|
||||
local.Month(),
|
||||
local.Day(),
|
||||
local.Hour(),
|
||||
local.Minute(),
|
||||
local.Second(),
|
||||
local.Nanosecond(),
|
||||
time.UTC,
|
||||
)
|
||||
serial := float64(displayedTime.Unix())/86_400 + 25_569
|
||||
_, err := fmt.Fprintf(w, `<c r="%s" s="%d"><v>%.10f</v></c>`, reference, style, serial)
|
||||
return err
|
||||
}
|
||||
|
||||
func writeAuditCoreProperties(w io.Writer, exportedAt time.Time) error {
|
||||
if _, err := writeString(
|
||||
w,
|
||||
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>`+
|
||||
`<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" `+
|
||||
`xmlns:dc="http://purl.org/dc/elements/1.1/" `+
|
||||
`xmlns:dcterms="http://purl.org/dc/terms/" `+
|
||||
`xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">`+
|
||||
`<dc:title>TAPM Audit Log</dc:title><dc:creator>TAPM Broker</dc:creator>`+
|
||||
`<dcterms:created xsi:type="dcterms:W3CDTF">`,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := writeEscapedXML(w, exportedAt.UTC().Format(time.RFC3339)); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := writeString(w, `</dcterms:created></cp:coreProperties>`)
|
||||
return err
|
||||
}
|
||||
|
||||
func auditFilterDescription(filters auditFilters) string {
|
||||
rangeLabels := map[string]string{
|
||||
"24h": "Past 24 hours",
|
||||
"7d": "Past 7 days",
|
||||
"30d": "Past 30 days",
|
||||
"90d": "Past 90 days",
|
||||
"all": "All retained events",
|
||||
}
|
||||
parts := []string{rangeLabels[filters.TimeRange]}
|
||||
filterValues := []struct {
|
||||
label string
|
||||
value string
|
||||
}{
|
||||
{"event", filters.EventType},
|
||||
{"customer", filters.CustomerLabel},
|
||||
{"user", filters.User},
|
||||
{"hostname", filters.Hostname},
|
||||
{"package", filters.PackageSlug},
|
||||
{"IP", filters.SourceIP},
|
||||
{"details", filters.Details},
|
||||
}
|
||||
for _, filter := range filterValues {
|
||||
if filter.value != "" {
|
||||
parts = append(parts, filter.label+": "+filter.value)
|
||||
}
|
||||
}
|
||||
return strings.Join(parts, " · ")
|
||||
}
|
||||
|
||||
func cellReference(column int, row int) string {
|
||||
var letters [3]byte
|
||||
position := len(letters)
|
||||
for column > 0 {
|
||||
column--
|
||||
position--
|
||||
letters[position] = byte('A' + column%26)
|
||||
column /= 26
|
||||
}
|
||||
return string(letters[position:]) + fmt.Sprintf("%d", row)
|
||||
}
|
||||
|
||||
func writeEscapedXML(w io.Writer, value string) error {
|
||||
value = strings.ToValidUTF8(value, "\uFFFD")
|
||||
for _, character := range value {
|
||||
if character == '\t' || character == '\n' || character == '\r' ||
|
||||
(character >= 0x20 && character <= 0xD7FF) ||
|
||||
(character >= 0xE000 && character <= 0xFFFD) ||
|
||||
(character >= 0x10000 && character <= utf8.MaxRune) {
|
||||
switch character {
|
||||
case '&':
|
||||
if _, err := writeString(w, "&"); err != nil {
|
||||
return err
|
||||
}
|
||||
case '<':
|
||||
if _, err := writeString(w, "<"); err != nil {
|
||||
return err
|
||||
}
|
||||
case '>':
|
||||
if _, err := writeString(w, ">"); err != nil {
|
||||
return err
|
||||
}
|
||||
case '"':
|
||||
if _, err := writeString(w, """); err != nil {
|
||||
return err
|
||||
}
|
||||
case '\'':
|
||||
if _, err := writeString(w, "'"); err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
if _, err := fmt.Fprint(w, string(character)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func writeString(w io.Writer, value string) (int, error) {
|
||||
return io.WriteString(w, value)
|
||||
}
|
||||
|
||||
func writeOnlyString(w io.Writer, value string) error {
|
||||
_, err := io.WriteString(w, value)
|
||||
return err
|
||||
}
|
||||
|
||||
const contentTypesXML = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
|
||||
<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
|
||||
<Default Extension="xml" ContentType="application/xml"/>
|
||||
<Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>
|
||||
<Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>
|
||||
<Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/>
|
||||
<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>
|
||||
<Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/>
|
||||
</Types>`
|
||||
|
||||
const packageRelationshipsXML = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
|
||||
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>
|
||||
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>
|
||||
<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>
|
||||
</Relationships>`
|
||||
|
||||
const appPropertiesXML = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
|
||||
<Application>TAPM Broker</Application>
|
||||
</Properties>`
|
||||
|
||||
const workbookXML = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
|
||||
<sheets><sheet name="Audit Log" sheetId="1" r:id="rId1"/></sheets>
|
||||
</workbook>`
|
||||
|
||||
const workbookRelationshipsXML = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
|
||||
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/>
|
||||
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>
|
||||
</Relationships>`
|
||||
|
||||
const auditStylesXML = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
|
||||
<numFmts count="1"><numFmt numFmtId="164" formatCode="yyyy-mm-dd hh:mm:ss"/></numFmts>
|
||||
<fonts count="3">
|
||||
<font><sz val="11"/><color rgb="FF33404A"/><name val="Aptos"/></font>
|
||||
<font><b/><sz val="18"/><color rgb="FFFFFFFF"/><name val="Aptos Display"/></font>
|
||||
<font><b/><sz val="11"/><color rgb="FFFFFFFF"/><name val="Aptos"/></font>
|
||||
</fonts>
|
||||
<fills count="4">
|
||||
<fill><patternFill patternType="none"/></fill>
|
||||
<fill><patternFill patternType="gray125"/></fill>
|
||||
<fill><patternFill patternType="solid"><fgColor rgb="FF33404A"/><bgColor indexed="64"/></patternFill></fill>
|
||||
<fill><patternFill patternType="solid"><fgColor rgb="FFF4F7ED"/><bgColor indexed="64"/></patternFill></fill>
|
||||
</fills>
|
||||
<borders count="2">
|
||||
<border><left/><right/><top/><bottom/><diagonal/></border>
|
||||
<border><left/><right/><top/><bottom style="thin"><color rgb="FFD9DED4"/></bottom><diagonal/></border>
|
||||
</borders>
|
||||
<cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"/></cellStyleXfs>
|
||||
<cellXfs count="7">
|
||||
<xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/>
|
||||
<xf numFmtId="0" fontId="1" fillId="2" borderId="0" xfId="0" applyFont="1" applyFill="1"><alignment vertical="center"/></xf>
|
||||
<xf numFmtId="0" fontId="0" fillId="3" borderId="0" xfId="0" applyFill="1"><alignment vertical="center"/></xf>
|
||||
<xf numFmtId="0" fontId="2" fillId="2" borderId="0" xfId="0" applyFont="1" applyFill="1"><alignment vertical="center"/></xf>
|
||||
<xf numFmtId="0" fontId="0" fillId="0" borderId="1" xfId="0" applyBorder="1"><alignment vertical="top"/></xf>
|
||||
<xf numFmtId="164" fontId="0" fillId="0" borderId="1" xfId="0" applyNumberFormat="1" applyBorder="1"><alignment vertical="top"/></xf>
|
||||
<xf numFmtId="0" fontId="0" fillId="0" borderId="1" xfId="0" applyBorder="1"><alignment vertical="top" wrapText="1"/></xf>
|
||||
</cellXfs>
|
||||
<cellStyles count="1"><cellStyle name="Normal" xfId="0" builtinId="0"/></cellStyles>
|
||||
</styleSheet>`
|
||||
Reference in New Issue
Block a user