From 8c460f58dea5cf500972037405c6c352922cc0b1 Mon Sep 17 00:00:00 2001 From: Derek Horton Date: Fri, 6 Sep 2024 14:31:05 -0500 Subject: [PATCH 1/2] Simple attempt. Just start truncating after a fixed number of lines --- internal/validator/handler.go | 60 ++++++++--------------------------- 1 file changed, 14 insertions(+), 46 deletions(-) diff --git a/internal/validator/handler.go b/internal/validator/handler.go index 184ec08a..8eb69593 100644 --- a/internal/validator/handler.go +++ b/internal/validator/handler.go @@ -165,23 +165,22 @@ func (this *handler) validateContent(ctx context.Context, requestType string, da log := utils.GetLogFromContext(ctx) + maxMessageSize := 1 * 1024 * 1024 + // FIXME: make this configurable - truncateData := len(data) >= 1*1024*1024 + truncateData := len(data) >= maxMessageSize if truncateData { log.Debug("Payload too big. Truncating payload.") } - eventTypeCount := make(map[string]int) - hostRunningPlaybook := make(map[string]int) - lines := strings.Split(string(data), "\n") - for _, line := range lines { + for i, line := range lines { if len(strings.TrimSpace(line)) == 0 { continue } - if requestType == playbookSatPayloadHeaderValue { + if requestType == "playbookSatPayloadHeaderValue" { validatedEvent, err := validateSatRunResponseWithSchema(ctx, this.schemas[1], line) if err == nil { @@ -192,34 +191,14 @@ func (this *handler) validateContent(ctx context.Context, requestType string, da return nil, err } - var storeEvent bool = true - - if truncateData { - - storeEvent = false - - if validatedEvent.Type == "playbook_run_completed" || validatedEvent.Type == "playbook_run_finished" { - storeEvent = true - log.Debug("storing run complete/finished for host " /*, *validatedEvent.Host*/) - } - - if validatedEvent.Type == "playbook_run_update" { - - if validatedEvent.Host != nil { - if _, ok := hostRunningPlaybook[*validatedEvent.Host]; !ok { - storeEvent = true - log.Debugf("storing run update for host %s", *validatedEvent.Host) - } - - hostRunningPlaybook[*validatedEvent.Host]++ - } + if i > 500 && truncateData { + if validatedEvent.Console != nil || *validatedEvent.Console != "" { + var truncated string = "Truncated..." + validatedEvent.Console = &truncated } } - if storeEvent { - log.Debugf("storing event %s ", validatedEvent.Type) - events.PlaybookSat = append(events.PlaybookSat, *validatedEvent) - } + events.PlaybookSat = append(events.PlaybookSat, *validatedEvent) } else { validatedEvent, err := validateRunResponseWithSchema(ctx, this.schemas[0], line) @@ -227,24 +206,13 @@ func (this *handler) validateContent(ctx context.Context, requestType string, da return nil, err } - var storeEvent bool = true - - if truncateData { - storeEvent = false - - // FIXME: hardcoded :( - if validatedEvent.Event == "executor_on_start" || validatedEvent.Event == "playbook_on_stats" || validatedEvent.Event == "runner_on_failed" || validatedEvent.Event == "executor_on_failed" { - eventTypeCount[validatedEvent.Event]++ - storeEvent = eventTypeCount[validatedEvent.Event] <= 1 - } + if i > 500 && i < len(lines)-2 && truncateData { + var truncated string = "Truncated..." + validatedEvent.Stdout = &truncated } - if storeEvent { - log.Debugf("storing event %s", validatedEvent.Event) - events.Playbook = append(events.Playbook, *validatedEvent) - } + events.Playbook = append(events.Playbook, *validatedEvent) } - } if len(events.PlaybookSat) == 0 && len(events.Playbook) == 0 { From b2ac4ca33130c068a6262f5f4897fe2dfdc66bc4 Mon Sep 17 00:00:00 2001 From: Derek Horton Date: Fri, 6 Sep 2024 14:43:17 -0500 Subject: [PATCH 2/2] Used a string for testing. Replaced string with correct constant --- internal/validator/handler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/validator/handler.go b/internal/validator/handler.go index 8eb69593..311668a8 100644 --- a/internal/validator/handler.go +++ b/internal/validator/handler.go @@ -180,7 +180,7 @@ func (this *handler) validateContent(ctx context.Context, requestType string, da continue } - if requestType == "playbookSatPayloadHeaderValue" { + if requestType == playbookSatPayloadHeaderValue { validatedEvent, err := validateSatRunResponseWithSchema(ctx, this.schemas[1], line) if err == nil {