github.com/mweagle/Sparta@v1.15.0/aws/events/event.go (about)

     1  package events
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  )
     8  
     9  // APIGatewayIdentity is the API Gateway identity information
    10  type APIGatewayIdentity struct {
    11  	AccountID                     string `json:"accountId"`
    12  	APIKey                        string `json:"apiKey"`
    13  	Caller                        string `json:"caller"`
    14  	CognitoAuthenticationProvider string `json:"cognitoAuthenticationProvider"`
    15  	CognitoAuthenticationType     string `json:"cognitoAuthenticationType"`
    16  	CognitoIdentityID             string `json:"cognitoIdentityId"`
    17  	CognitoIdentityPoolID         string `json:"cognitoIdentityPoolId"`
    18  	SourceIP                      string `json:"sourceIp"`
    19  	User                          string `json:"user"`
    20  	UserAgent                     string `json:"userAgent"`
    21  	UserArn                       string `json:"userArn"`
    22  }
    23  
    24  // APIGatewayContext is the API-Gateway context information
    25  type APIGatewayContext struct {
    26  	AppID        string             `json:"appId"`
    27  	Method       string             `json:"method"`
    28  	RequestID    string             `json:"requestId"`
    29  	ResourceID   string             `json:"resourceId"`
    30  	ResourcePath string             `json:"resourcePath"`
    31  	Stage        string             `json:"stage"`
    32  	Identity     APIGatewayIdentity `json:"identity"`
    33  }
    34  
    35  // APIGatewayEnvelope is the type that maps to the VTL properties
    36  type APIGatewayEnvelope struct {
    37  	Method      string                 `json:"method"`
    38  	Headers     map[string]string      `json:"headers"`
    39  	QueryParams map[string]string      `json:"queryParams"`
    40  	PathParams  map[string]string      `json:"pathParams"`
    41  	Context     APIGatewayContext      `json:"context"`
    42  	Authorizer  map[string]interface{} `json:"authorizer"`
    43  }
    44  
    45  // APIGatewayRequest represents the API Gateway request that
    46  // is submitted to a Lambda function. This format matches the
    47  // inputmapping_default.VTL templates
    48  type APIGatewayRequest struct {
    49  	APIGatewayEnvelope
    50  	Body interface{} `json:"body"`
    51  }
    52  
    53  // NewAPIGatewayMockRequest creates a mock API Gateway request.
    54  // This request format mirrors the VTL templates in
    55  // github.com/mweagle/Sparta/resources/provision/apigateway
    56  func NewAPIGatewayMockRequest(lambdaName string,
    57  	httpMethod string,
    58  	whitelistParamValues map[string]string,
    59  	eventData interface{}) (*APIGatewayRequest, error) {
    60  
    61  	apiGatewayRequest := &APIGatewayRequest{
    62  		Body: eventData,
    63  		APIGatewayEnvelope: APIGatewayEnvelope{
    64  			Method:      httpMethod,
    65  			Headers:     make(map[string]string),
    66  			QueryParams: make(map[string]string),
    67  			PathParams:  make(map[string]string),
    68  		},
    69  	}
    70  	for eachWhitelistKey, eachWhitelistValue := range whitelistParamValues {
    71  		// Whitelisted params include their
    72  		// namespace as part of the whitelist expression:
    73  		// method.request.querystring.keyName
    74  		parts := strings.Split(eachWhitelistKey, ".")
    75  
    76  		// The string should have 4 parts...
    77  		if len(parts) != 4 {
    78  			return nil, fmt.Errorf("invalid whitelist param name: %s (MUST be: method.request.KEY_TYPE.KEY_NAME, ex: method.request.querystring.myQueryParam", eachWhitelistKey)
    79  		}
    80  		keyType := parts[2]
    81  		keyName := parts[3]
    82  		switch keyType {
    83  		case "header":
    84  			apiGatewayRequest.Headers[keyName] = eachWhitelistValue
    85  		case "querystring":
    86  			apiGatewayRequest.QueryParams[keyName] = eachWhitelistValue
    87  		case "path":
    88  			apiGatewayRequest.PathParams[keyName] = eachWhitelistValue
    89  		default:
    90  			return nil, fmt.Errorf("unsupported whitelist param type: %s", keyType)
    91  		}
    92  	}
    93  
    94  	apiGatewayRequest.Context.AppID = fmt.Sprintf("spartaApp%d", os.Getpid())
    95  	apiGatewayRequest.Context.Method = httpMethod
    96  	apiGatewayRequest.Context.RequestID = "12341234-1234-1234-1234-123412341234"
    97  	apiGatewayRequest.Context.ResourceID = "anon42"
    98  	apiGatewayRequest.Context.ResourcePath = "/mock"
    99  	apiGatewayRequest.Context.Stage = "mock"
   100  	apiGatewayRequest.Context.Identity = APIGatewayIdentity{
   101  		AccountID:                     "123412341234",
   102  		APIKey:                        "",
   103  		Caller:                        "",
   104  		CognitoAuthenticationProvider: "",
   105  		CognitoAuthenticationType:     "",
   106  		CognitoIdentityID:             "",
   107  		CognitoIdentityPoolID:         "",
   108  		SourceIP:                      "127.0.0.1",
   109  		User:                          "Unknown",
   110  		UserAgent:                     "Mozilla/Gecko",
   111  		UserArn:                       "",
   112  	}
   113  	return apiGatewayRequest, nil
   114  }