Skip to content
Snippets Groups Projects
Select Git revision
  • dbc1ae112070546db1159aedbb32ed82c92dc12c
  • main default protected
  • v1.298.0
  • v1.297.0
  • v1.296.0
  • v1.295.0
  • v1.294.0
  • v1.293.0
  • v1.292.0
  • v1.291.0
  • v1.290.0
  • v1.289.0
  • v1.288.0
  • v1.287.0
  • v1.286.0
  • v1.285.0
  • v1.284.0
  • v1.283.0
  • v1.282.0
  • v1.281.0
  • v1.280.0
  • v1.279.0
22 results

batch.go

Blame
  • request.go 2.05 KiB
    package handler_utils
    
    import (
    	"bytes"
    	"context"
    	"github.com/aws/aws-sdk-go/aws/credentials"
    	v4 "github.com/aws/aws-sdk-go/aws/signer/v4"
    	"github.com/go-resty/resty/v2"
    	"io"
    	"net/http"
    	"os"
    	"strings"
    	"time"
    
    	"github.com/aws/aws-lambda-go/lambdacontext"
    )
    
    func RequestIDFromLambdaContext(ctx context.Context) *string {
    	// Get request ID from context
    	if ctx != nil {
    		lambdaContext, _ := lambdacontext.FromContext(ctx)
    		if lambdaContext != nil {
    			return &lambdaContext.AwsRequestID
    		}
    	}
    	return nil
    }
    
    func RequestIDFromHeaders(headers map[string]string, requestIDHeaderKey string) *string {
    	// Get request ID from parent micro service
    	if requestID, ok := headers[requestIDHeaderKey]; ok {
    		return &requestID
    	}
    	return nil
    }
    
    func AddRequestIDToHeaders(requestID *string, headers map[string]string, requestIDHeaderKey string) {
    	if requestID != nil && headers != nil {
    		headers[requestIDHeaderKey] = *requestID
    	}
    }
    
    func SignAWSRestyClient(client *resty.Client, accessKeyID, secretAccessKey string, bodyBytes []byte) error {
    	var bodySeeker io.ReadSeeker = bytes.NewReader(bodyBytes)
    
    	// Add a hook to sign the request before sending
    	client.SetPreRequestHook(func(_ *resty.Client, r *http.Request) error {
    		err := SignAWSHttpRequest(r, accessKeyID, secretAccessKey, bodySeeker)
    		if err != nil {
    			return err
    		}
    		return nil
    	})
    
    	return nil
    }
    
    // SignAWSRequest wraps and executes http.NewRequest and adds a sig version 4 signature for AWS API Gateway
    func SignAWSHttpRequest(request *http.Request, accessKeyID, secretAccessKey string, bodySeeker io.ReadSeeker) error {
    	// Use AWS SDK to sign the request for API gateway,  i.e. execute-api, and the current region
    	_, err := v4.NewSigner(credentials.NewStaticCredentials(accessKeyID, secretAccessKey, "")).
    		Sign(request, bodySeeker, "execute-api", os.Getenv("AWS_REGION"), time.Now())
    	if err != nil {
    		return err
    	}
    
    	return nil
    }
    
    func FindHeaderValue(headers map[string]string, key string) string {