This repository has been archived by the owner on Jul 16, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
113 lines (94 loc) · 3.24 KB
/
main.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
package main
import (
"encoding/base64"
"encoding/json"
"errors"
"net/http"
"strings"
"time"
"github.com/Luzifer/gh-private-dl/privatehub"
"github.com/Sirupsen/logrus"
sparta "github.com/mweagle/Sparta"
)
const (
httpRequestTimeout = 5 * time.Second
)
func extractGithubToken(lambdaEvent sparta.APIGatewayLambdaJSONEvent) (string, error) {
if hdr, ok := lambdaEvent.Headers["Authorization"]; ok {
auth := strings.SplitN(hdr, " ", 2)
if len(auth) != 2 || auth[0] != "Basic" {
return "", errors.New("You need to provide HTTP basic auth")
}
payload, err := base64.StdEncoding.DecodeString(auth[1])
if err != nil {
return "", errors.New("You need to provide HTTP basic auth")
}
pair := strings.SplitN(string(payload), ":", 2)
if len(pair) != 2 {
return "", errors.New("You need to provide HTTP basic auth")
}
return pair[1], nil
}
return "", errors.New("You need to provide HTTP basic auth")
}
func handleLambdaGithubDownload(event *json.RawMessage, context *sparta.LambdaContext, w http.ResponseWriter, logger *logrus.Logger) {
var lambdaEvent sparta.APIGatewayLambdaJSONEvent
err := json.Unmarshal([]byte(*event), &lambdaEvent)
if err != nil {
logger.Error("Failed to unmarshal event data: ", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
githubToken, err := extractGithubToken(lambdaEvent)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
dlURL, err := privatehub.GetDownloadURL(
strings.Join([]string{lambdaEvent.PathParams["user"], lambdaEvent.PathParams["repo"]}, "/"),
lambdaEvent.PathParams["version"],
lambdaEvent.PathParams["binary"],
githubToken,
)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
response := sparta.ArbitraryJSONObject{
"location": dlURL,
}
w.Header().Set("content-type", "application/json")
w.WriteHeader(http.StatusFound)
json.NewEncoder(w).Encode(response)
}
func main() {
var lambdaFunctions []*sparta.LambdaAWSInfo
lambdaFn := sparta.NewLambda(sparta.IAMRoleDefinition{}, handleLambdaGithubDownload, &sparta.LambdaFunctionOptions{
Timeout: 30,
})
lambdaFunctions = append(lambdaFunctions, lambdaFn)
stage := sparta.NewStage("prod")
apiGateway := sparta.NewAPIGateway("GH-PrivateDL-API", stage)
// https://github.com/Luzifer/vault2env/releases/download/v0.6.1/vault2env_linux_amd64
apiGatewayResource, _ := apiGateway.NewResource("/{user}/{repo}/releases/download/{version}/{binary}", lambdaFn)
method, err := apiGatewayResource.NewMethod("GET", http.StatusFound)
if err != nil {
panic(err)
}
method.Parameters["method.request.path.user"] = true
method.Parameters["method.request.path.repo"] = true
method.Parameters["method.request.path.version"] = true
method.Parameters["method.request.path.binary"] = true
method.Parameters["method.request.header.authorization"] = true
method.Responses[http.StatusFound].Parameters = map[string]bool{
"method.response.header.Location": true,
}
method.Integration.Responses[http.StatusFound].Parameters["method.response.header.Location"] = "integration.response.body.location"
// Deploy it
sparta.MainEx("GH-PrivateDL",
"Lambda application for downloading private GitHub assets",
lambdaFunctions,
apiGateway,
nil,
nil)
}