-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler_response.go
61 lines (56 loc) · 1.97 KB
/
handler_response.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package auth
import (
"context"
"encoding/json"
"fmt"
"github.com/aws/aws-sdk-go/service/sts"
"net/http"
)
// HandlerResponse the response format expected by Lambda
// see https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
type HandlerResponse struct {
IsBase64Encoded bool `json:"isBase64Encoded,omitempty"`
StatusCode int `json:"statusCode,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
Body string `json:"body,omitempty"`
}
// RespondError format a response with an error message
func RespondError(ctx context.Context, err error, statusCode int) (HandlerResponse, error) {
Logger(ctx).Errorf("error response of request %d, %s", statusCode, err.Error())
return HandlerResponse{
StatusCode: statusCode,
Body: err.Error(),
}, nil
}
// RespondShellscript format a response as a shellscript
func RespondShellscript(ctx context.Context, credentials *sts.Credentials) (HandlerResponse, error) {
data := fmt.Sprintf("export AWS_ACCESS_KEY_ID=\"%s\"\n"+
"export AWS_SECRET_ACCESS_KEY=\"%s\"\n"+
"export AWS_SESSION_TOKEN=\"%s\"\n",
*credentials.AccessKeyId,
*credentials.SecretAccessKey,
*credentials.SessionToken)
Logger(ctx).Debug("response successful - responding credentials as script")
return HandlerResponse{
StatusCode: http.StatusOK,
Headers: map[string]string{
"Content-Type": "text/x-shellscript",
},
Body: data,
}, nil
}
// RespondJSON format a response as json
func RespondJSON(ctx context.Context, credentials *sts.Credentials) (HandlerResponse, error) {
response, err := json.Marshal(&credentials)
if err != nil {
return RespondError(ctx, err, http.StatusInternalServerError)
}
Logger(ctx).Debug("response successful - responding credentials as json")
return HandlerResponse{
StatusCode: http.StatusOK,
Headers: map[string]string{
"Content-Type": "application/json",
},
Body: string(response),
}, nil
}