forked from deis/builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
150 lines (125 loc) · 3.94 KB
/
controller.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package gitreceive
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"regexp"
"strings"
"github.com/deis/builder/pkg"
"github.com/deis/builder/pkg/gitreceive/log"
)
var (
potentialExploit = regexp.MustCompile(`\(\)\s+\{[^\}]+\};\s+(.*)`)
)
type unexpectedControllerStatusCode struct {
endpoint string
expected int
actual int
}
func newUnexpectedControllerStatusCode(endpoint string, expectedCode, actualCode int) unexpectedControllerStatusCode {
return unexpectedControllerStatusCode{endpoint: endpoint, expected: expectedCode, actual: actualCode}
}
func (u unexpectedControllerStatusCode) Error() string {
return fmt.Sprintf("Deis controller endpoint %s: expected status code %d, got %d", u.endpoint, u.expected, u.actual)
}
func controllerURLStr(conf *Config, additionalPath ...string) string {
return fmt.Sprintf("http://%s:%s/%s", conf.WorkflowHost, conf.WorkflowPort, strings.Join(additionalPath, "/"))
}
func setReqHeaders(builderKey string, req *http.Request) {
req.Header.Add("Content-Type", contentType)
req.Header.Add("Accept", contentType)
req.Header.Add("User-Agent", userAgent)
req.Header.Add("X-Deis-Builder-Auth", builderKey)
}
func getAppConfig(conf *Config, builderKey, userName, appName string) (*pkg.Config, error) {
url := controllerURLStr(conf, "v2", "hooks", "config")
data, err := json.Marshal(&pkg.ConfigHook{
ReceiveUser: userName,
ReceiveRepo: appName,
})
if err != nil {
return nil, err
}
b := bytes.NewReader(data)
req, err := http.NewRequest("POST", url, b)
if err != nil {
return nil, err
}
setReqHeaders(builderKey, req)
log.Debug("Workflow request POST /v2/hooks/config\n%s", string(data))
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, newUnexpectedControllerStatusCode(url, 200, res.StatusCode)
}
ret := &pkg.Config{}
if err := json.NewDecoder(res.Body).Decode(ret); err != nil {
return nil, err
}
return ret, nil
}
func publishRelease(conf *Config, builderKey string, buildHook *pkg.BuildHook) (*pkg.BuildHookResponse, error) {
var b bytes.Buffer
if err := json.NewEncoder(&b).Encode(buildHook); err != nil {
return nil, err
}
postBody := strings.Replace(string(b.Bytes()), "'", "", -1)
if potentialExploit.MatchString(postBody) {
return nil, fmt.Errorf("an environment variable in the app is trying to exploit Shellshock")
}
url := controllerURLStr(conf, "v2", "hooks", "build")
log.Debug("Workflow request POST /v2/hooks/build\n%s", postBody)
req, err := http.NewRequest("POST", url, strings.NewReader(postBody))
if err != nil {
return nil, err
}
setReqHeaders(builderKey, req)
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, newUnexpectedControllerStatusCode(url, 200, res.StatusCode)
}
ret := new(pkg.BuildHookResponse)
if err := json.NewDecoder(res.Body).Decode(ret); err != nil {
return nil, err
}
return ret, nil
}
func receive(conf *Config, builderKey, gitSha string) error {
urlStr := controllerURLStr(conf, "v2", "hooks", "push")
bodyMap := map[string]string{
"receive_user": conf.Username,
"receive_repo": conf.App(),
"sha": gitSha,
"fingerprint": conf.Fingerprint,
"ssh_connection": conf.SSHConnection,
"ssh_original_command": conf.SSHOriginalCommand,
}
var body bytes.Buffer
if err := json.NewEncoder(&body).Encode(bodyMap); err != nil {
return err
}
log.Debug("Workflow request /v2/hooks/push (body elided)")
req, err := http.NewRequest("POST", urlStr, &body)
if err != nil {
return err
}
setReqHeaders(builderKey, req)
// TODO: use ctxhttp here (https://godoc.org/golang.org/x/net/context/ctxhttp)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 201 {
return newUnexpectedControllerStatusCode(urlStr, 201, resp.StatusCode)
}
return nil
}