Skip to content

Commit

Permalink
Bugfix: Limit length of error fields per AWS limits.
Browse files Browse the repository at this point in the history
AWS will return a validation error when trying to send a task failure with error or cause above
certain limits. sfncli should just defend itself against that by truncating.
  • Loading branch information
taylor-sutton committed Aug 18, 2023
1 parent f5d3f8b commit e6f7190
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions cmd/sfncli/error_names.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sfn"
Expand All @@ -26,11 +27,15 @@ type TaskFailureError interface {
func (t TaskRunner) sendTaskFailure(err TaskFailureError) error {
t.logger.ErrorD("send-task-failure", logger.M{"name": err.ErrorName(), "cause": err.ErrorCause()})

// Limits from https://docs.aws.amazon.com/step-functions/latest/apireference/API_SendTaskFailure.html
const maxErrorLength = 256
const maxCauseLength = 32768

// don't use SendTaskFailureWithContext, since the failure itself could be from the parent
// context being cancelled, but we still want to report to AWS the failure of the task.
_, sendErr := t.sfnapi.SendTaskFailure(&sfn.SendTaskFailureInput{
Error: aws.String(err.ErrorName()),
Cause: aws.String(err.ErrorCause()),
Error: aws.String(truncateString(err.ErrorName(), maxErrorLength, "[truncated]")),
Cause: aws.String(truncateString(err.ErrorCause(), maxCauseLength, "[truncated]")),
TaskToken: &t.taskToken,
})
if sendErr != nil {
Expand All @@ -39,6 +44,20 @@ func (t TaskRunner) sendTaskFailure(err TaskFailureError) error {
return err
}

// Returns its input truncated to maxLength, with the ability to replace the end to indicate truncation.
//
// For example, truncateString(s, l, "") just truncates to length l. But truncateString(s, l, "xy") will
// first truncate to length l, then replace the last two characters with "xy"
func truncateString(s string, maxLength int, truncationIndicatorSuffix string) string {
if len(s) <= maxLength {
return s
}
// when we cut out some number of bytes from the end, we may be cutting in the middle of a multi-byte unicode char
// if so, we can use ToValidUTF8 to trim it a teeny bit further to eliminate the whole char.
// (Note, this does mean invalid UTF8 inputs will see more changes than expected, but we won't worry about that)
return strings.ToValidUTF8(s[:maxLength-len(truncationIndicatorSuffix)], "") + truncationIndicatorSuffix
}

// TaskFailureUnknown is used for any error that is unexpected or not understood completely.
type TaskFailureUnknown struct {
error
Expand Down

0 comments on commit e6f7190

Please sign in to comment.