-
Notifications
You must be signed in to change notification settings - Fork 5
/
signer.go
115 lines (94 loc) · 2.85 KB
/
signer.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package iopipe
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
)
// SignerRequest is a signer request
type SignerRequest struct {
ARN string `json:"arn"`
RequestID string `json:"requestId"`
Timestamp int `json:"timestamp"`
Extension string `json:"extension"`
}
// SignerResponse is a signer response
type SignerResponse struct {
JWTAccess string `json:"jwtAccess"`
SignedRequest string `json:"signedRequest"`
URL string `json:"url"`
}
// GetSignerURL returns the URL for the signer in the specified region
func GetSignerURL(region string) string {
supportedRegions := map[string]struct{}{
"ap-northeast-1": struct{}{},
"ap-southeast-2": struct{}{},
"eu-west-1": struct{}{},
"us-east-1": struct{}{},
"us-east-2": struct{}{},
"us-west-1": struct{}{},
"us-west-2": struct{}{},
}
if region == "mock" {
return os.Getenv("MOCK_SERVER")
}
if _, exists := supportedRegions[region]; exists {
return fmt.Sprintf("https://signer.%s.iopipe.com/", region)
}
return "https://signer.us-west-2.iopipe.com/"
}
// GetSignedRequest returns a signed request for uploading files to IOpipe
func GetSignedRequest(report *Report, extension string) (*SignerResponse, error) {
var (
err error
networkTimeout = 60 * time.Second
)
tr := &http.Transport{
DisableKeepAlives: false,
MaxIdleConns: 1, // is this equivalent to the maxCachedSessions in the js implementation
}
httpsClient := http.Client{Transport: tr, Timeout: networkTimeout}
signerRequest := &SignerRequest{
ARN: report.AWS.InvokedFunctionArn,
RequestID: report.AWS.AWSRequestID,
Timestamp: int(time.Now().UnixNano() / 1e6),
Extension: extension,
}
requestJSONBytes, _ := json.Marshal(signerRequest)
report.agent.log.Debug("Signer request: ", string(requestJSONBytes))
requestURL := GetSignerURL(os.Getenv("AWS_REGION"))
report.agent.log.Debug("Signer URL: ", requestURL)
req, err := http.NewRequest("POST", requestURL, bytes.NewReader(requestJSONBytes))
if err != nil {
report.agent.log.Debug(err)
return nil, err
}
req.Header.Set("Authorization", report.ClientID)
req.Header.Set("Content-Type", "application/json")
res, err := httpsClient.Do(req)
if err != nil {
report.agent.log.Debug(err)
return nil, err
}
defer res.Body.Close()
bodyBytes, err := ioutil.ReadAll(res.Body)
report.agent.log.Debug("Signer response: ", string(bodyBytes))
if err != nil {
report.agent.log.Debug(err)
return nil, err
}
if res.StatusCode > 299 {
report.agent.log.Debug("Response failed: %d %s", res.StatusCode, bodyBytes)
return nil, fmt.Errorf("Response failed: %d %s", res.StatusCode, bodyBytes)
}
var signerResponse *SignerResponse
err = json.Unmarshal(bodyBytes, &signerResponse)
if err != nil {
report.agent.log.Debug(err)
return nil, err
}
return signerResponse, nil
}