Skip to content

Commit

Permalink
Cap report secrets at 1MB and improve tests (#4120)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgalsaleh authored Nov 6, 2023
1 parent f76b707 commit 61659b0
Show file tree
Hide file tree
Showing 5 changed files with 475 additions and 203 deletions.
6 changes: 3 additions & 3 deletions pkg/handlers/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func Test_validateCustomAppMetricsData(t *testing.T) {

func Test_SendCustomAppMetrics(t *testing.T) {
req := require.New(t)
customMetricsData := []byte(`{"data":{"key1_string":"val1","key2_int":5,"key3_float":1.5,"key4_numeric_string":"1.6"}}`)
customAppMetricsData := []byte(`{"data":{"key1_string":"val1","key2_int":5,"key3_float":1.5,"key4_numeric_string":"1.6"}}`)
appID := "app-id-123"

// Mock server side
Expand All @@ -89,7 +89,7 @@ func Test_SendCustomAppMetrics(t *testing.T) {
serverRouter.Methods("POST").Path("/application/custom-metrics").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
req.NoError(err)
req.Equal(string(customMetricsData), string(body))
req.Equal(string(customAppMetricsData), string(body))
req.Equal(appID, r.Header.Get("X-Replicated-InstanceID"))
w.WriteHeader(http.StatusOK)
})
Expand Down Expand Up @@ -117,7 +117,7 @@ spec:
handler := Handler{}
clientWriter := httptest.NewRecorder()
clientRequest := &http.Request{
Body: io.NopCloser(bytes.NewBuffer(customMetricsData)),
Body: io.NopCloser(bytes.NewBuffer(customAppMetricsData)),
}

// Validate
Expand Down
20 changes: 20 additions & 0 deletions pkg/reporting/instance_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,33 @@ func (r *InstanceReport) AppendEvents(report Report) error {
r.Events = r.Events[len(r.Events)-r.GetEventLimit():]
}

// remove one event at a time until the report is under the size limit
encoded, err := EncodeReport(r)
if err != nil {
return errors.Wrap(err, "failed to encode report")
}
for len(encoded) > r.GetSizeLimit() {
r.Events = r.Events[1:]
if len(r.Events) == 0 {
return errors.Errorf("size of latest event exceeds report size limit")
}
encoded, err = EncodeReport(r)
if err != nil {
return errors.Wrap(err, "failed to encode report")
}
}

return nil
}

func (r *InstanceReport) GetEventLimit() int {
return ReportEventLimit
}

func (r *InstanceReport) GetSizeLimit() int {
return ReportSizeLimit
}

func (r *InstanceReport) GetMtx() *sync.Mutex {
return &instanceReportMtx
}
20 changes: 20 additions & 0 deletions pkg/reporting/preflight_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,33 @@ func (r *PreflightReport) AppendEvents(report Report) error {
r.Events = r.Events[len(r.Events)-r.GetEventLimit():]
}

// remove one event at a time until the report is under the size limit
encoded, err := EncodeReport(r)
if err != nil {
return errors.Wrap(err, "failed to encode report")
}
for len(encoded) > r.GetSizeLimit() {
r.Events = r.Events[1:]
if len(r.Events) == 0 {
return errors.Errorf("size of latest event exceeds report size limit")
}
encoded, err = EncodeReport(r)
if err != nil {
return errors.Wrap(err, "failed to encode report")
}
}

return nil
}

func (r *PreflightReport) GetEventLimit() int {
return ReportEventLimit
}

func (r *PreflightReport) GetSizeLimit() int {
return ReportSizeLimit
}

func (r *PreflightReport) GetMtx() *sync.Mutex {
return &preflightReportMtx
}
1 change: 1 addition & 0 deletions pkg/reporting/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
ReportSecretNameFormat = "kotsadm-%s-report"
ReportSecretKey = "report"
ReportEventLimit = 4000
ReportSizeLimit = 1 * 1024 * 1024 // 1MB
)

type ReportType string
Expand Down
Loading

0 comments on commit 61659b0

Please sign in to comment.