-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpublish-release-controller.go
More file actions
103 lines (83 loc) · 1.98 KB
/
publish-release-controller.go
File metadata and controls
103 lines (83 loc) · 1.98 KB
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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"regexp"
"strings"
"github.com/franela/goreq"
)
const (
contentType string = "application/json"
userAgent string = "deis-builder"
)
func init() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: [options]\n\n")
flag.PrintDefaults()
}
}
func main() {
url := flag.String("url", "", "Controller hook URL")
builderKey := flag.String("key", "", "Builder Key")
flag.Parse()
if flag.NFlag() < 2 {
flag.Usage()
os.Exit(1)
}
if *url == "" {
fmt.Println("invalid url")
os.Exit(64)
}
if *builderKey == "" {
fmt.Println("invalid builder key")
os.Exit(64)
}
bytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
fmt.Println("invalid json payload")
os.Exit(64)
}
postBody := strings.Replace(string(bytes), "'", "", -1)
// Check for a variable trying to exploit Shellshock.
potencialExploit := regexp.MustCompile(`\(\)\s+\{[^\}]+\};\s+(.*)`)
if potencialExploit.MatchString(postBody) {
fmt.Println("")
fmt.Println("ATTENTION: an environment variable in the app is trying to exploit Shellshock. Aborting...")
fmt.Println("")
os.Exit(1)
}
req := goreq.Request{
Method: "POST",
Uri: *url,
Body: postBody,
ContentType: contentType,
Accept: contentType,
UserAgent: userAgent,
}
req.AddHeader("X-Deis-Builder-Auth", *builderKey)
res, err := req.Do()
// Read json response from body
body, _ := res.Body.ToString()
var response map[string]interface{}
jsonErr := json.Unmarshal([]byte(body), &response)
if jsonErr != nil {
fmt.Println("invalid controller json response")
fmt.Println(body)
os.Exit(1)
}
if err != nil || res.StatusCode != 200 {
fmt.Println("failed retrieving config from controller")
fmt.Println(response["detail"].(string))
os.Exit(1)
}
if res.StatusCode == 404 {
fmt.Println("check the deis-controller. Is not running")
os.Exit(2)
}
toSring, _ := json.Marshal(response)
fmt.Println(string(toSring))
os.Exit(0)
}