genericapi

package
v1.3.0-testing Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 20, 2020 License: AGPL-3.0 Imports: 15 Imported by: 0

Documentation

Overview

Package genericapi provides a generic Router for API style Lambda functions.

Index

Constants

View Source
const HTMLCharacterSet = `'<>&"`

HTMLCharacterSet is the same set of characters replaced by the built-in html.EscapeString.

Variables

View Source
var ErrContainsHTML = func() error {
	var chars []string
	for _, x := range HTMLCharacterSet {
		chars = append(chars, string(x))
	}
	return errors.New("cannot contain any of: " + strings.Join(chars, " "))
}()

ErrContainsHTML defines a standard error message if a field contains HTML characters.

Functions

func ContainsHTML added in v1.2.0

func ContainsHTML(s string) bool

ContainsHTML is true if the string contains any of HTMLCharacterSet

Such strings should be rejected for user-defined names and labels to prevent injection attacks.

func Invoke

func Invoke(
	client lambdaiface.LambdaAPI, function string, input interface{}, output interface{}) error

Invoke a Lambda function, taking care of error checking and json marshaling.

Arguments:

  • client: initialized Lambda client
  • function: function name (optionally qualified), e.g. "panther-rules-api"
  • input: (pointer to struct) payload for Lambda function, processed via jsoniter.Marshal()
  • output: (optional - pointer to struct) Lambda response is jsoniter.Unmarshal()ed here
  • If nil, the Lambda response is ignored

Use a type assertion on the returned error to distinguish different error conditions:

  • AWSError: lambda invocation failed (e.g. permission error)
  • InternalError: json marshal/unmarshal failed
  • LambdaError: lambda function returned an error, directly or indirectly (timeout, panic, etc)

Example:

client := lambda.New(session.Must(session.NewSession())) input := models.LambdaInput{AddRule: ...} var output models.AddRuleOutput err := Invoke(client, "panther-rules-api", &input, &output)

Types

type AWSError

type AWSError struct {
	Route  string
	Method string // name of the function that triggered the exception (e.g. "dynamodb.PutItem")
	Err    error  // error returned by the AWS SDK
}

AWSError is raised if an AWS API call (e.g. to Dynamo/S3) failed.

func (*AWSError) Error

func (e *AWSError) Error() string

type AlreadyExistsError

type AlreadyExistsError struct {
	Route   string
	Message string
}

AlreadyExistsError is raised if the item being created already exists.

func (*AlreadyExistsError) Error

func (e *AlreadyExistsError) Error() string

type DoesNotExistError

type DoesNotExistError struct {
	Route   string
	Message string
}

DoesNotExistError is raised if the item being retrieved or modified does not exist.

func (*DoesNotExistError) Error

func (e *DoesNotExistError) Error() string

type InUseError

type InUseError struct {
	Route   string
	Message string
}

InUseError is raised if the item cannot be modified or deleted because it's in use.

func (*InUseError) Error

func (e *InUseError) Error() string

type InternalError

type InternalError struct {
	Route   string
	Message string
}

InternalError is raised if there is an internal inconsistency in the code.

For example, a failure marshaling a struct to JSON.

func (*InternalError) Error

func (e *InternalError) Error() string

type InvalidInputError

type InvalidInputError struct {
	Route   string
	Message string
}

InvalidInputError is raised if the request is invalid.

For example, the request is missing an action or has invalid or missing fields. This is typically raised by the Validator, and indicates an error on the client side.

func (*InvalidInputError) Error

func (e *InvalidInputError) Error() string

type LambdaError

type LambdaError struct {
	// Route is the name of the API route if this error is ultimately returned by an API function.
	Route string

	// FunctionName is the qualified name of the function invoked (set by Invoke()).
	FunctionName string

	// ErrorMessage is always defined and contains the error string.
	ErrorMessage *string `json:"errorMessage"`

	// ErrorType is the name of the error class if applicable.
	// Some unhandled errors (e.g. task timed out) will not have an error type.
	// When panicking, the ErrorType is either "string" or the error type that caused the panic.
	ErrorType *string `json:"errorType"`

	// StackTrace is included only when the function panicked.
	StackTrace []*messages.InvokeResponse_Error_StackFrame `json:"stackTrace"`
}

LambdaError wraps the error structure returned by a Golang Lambda function.

This applies to all errors - returned errors, panics, time outs, etc. This format is set by the AWS SDK: see messages.InvokeResponse_Error. (For some reason, the open-source Lambda SDK doesn't define a struct with the json tags that can be used to unmarshal the returned error.)

func (*LambdaError) Error

func (e *LambdaError) Error() string

type Router

type Router struct {
	// contains filtered or unexported fields
}

Router is a generic API router for golang Lambda functions.

func NewRouter

func NewRouter(namespace, component string, validate *validator.Validate, routes interface{}) *Router

NewRouter initializes a Router with the handler functions and validator.

validate is an optional custom validator routes is a struct pointer, whose receiver methods are handler functions (e.g. AddRule)

func (*Router) Handle

func (r *Router) Handle(input interface{}) (output interface{}, err error)

Handle validates the Lambda input and invokes the appropriate handler.

For the sake of efficiency, no attempt is made to validate the routes or function signatures. As a result, this function will panic if a handler does not exist or is invalid. Be sure to VerifyHandlers as part of the unit tests for your function!

func (*Router) VerifyHandlers

func (r *Router) VerifyHandlers(lambdaInput interface{}) error

VerifyHandlers returns an error if the route handlers don't match the Lambda input struct.

This should be part of the unit tests for your Lambda function.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL