forked from smothiki/builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish-release-controller.go
109 lines (88 loc) · 2.09 KB
/
publish-release-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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"strings"
)
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(1)
}
if *builderKey == "" {
fmt.Println("invalid builder key")
os.Exit(1)
}
bytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
fmt.Println("invalid json payload")
os.Exit(1)
}
postBody := strings.Replace(string(bytes), "'", "", -1)
// Check for a variable trying to exploit Shellshock.
potentialExploit := regexp.MustCompile(`\(\)\s+\{[^\}]+\};\s+(.*)`)
if potentialExploit.MatchString(postBody) {
fmt.Println("")
fmt.Println("ATTENTION: an environment variable in the app is trying to exploit Shellshock. Aborting...")
fmt.Println("")
os.Exit(1)
}
b := strings.NewReader(postBody)
client := &http.Client{}
req, err := http.NewRequest("POST", *url, b)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
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)
res, err := client.Do(req)
if err != nil {
log.Fatalln(err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if res.StatusCode == 503 {
log.Fatalln("check the controller. is it running?")
} else if res.StatusCode != 200 {
log.Fatalf("failed retrieving config from controller: %s\n", body)
}
var response map[string]interface{}
if err := json.Unmarshal(body, &response); err != nil {
fmt.Println("invalid controller json response")
fmt.Println(string(body))
os.Exit(1)
}
toString, _ := json.Marshal(response)
fmt.Println(string(toString))
os.Exit(0)
}