-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
175 lines (156 loc) · 3.77 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"crypto/sha1"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"regexp"
"strings"
b64 "encoding/base64"
"github.com/ghodss/yaml"
"github.com/sethvargo/go-password/password"
"github.com/urfave/cli"
"golang.org/x/crypto/pbkdf2"
validator "gopkg.in/go-playground/validator.v9"
)
type Trigger struct {
Name string `json:"name"`
Value string `json:"value"`
Token string `json:"token"`
}
type Deployment struct {
Name string `json:"name"`
Command string `json:"command"`
Hash string `json:"hash"`
UseValue bool `json:"useValue"`
}
type Config struct {
Deployments []Deployment `json:"deployments"`
Salt string `json:"salt"`
}
var config *Config
func LoadConfig() (config *Config, err error) {
configPath := os.Getenv("CONFIG")
if len(configPath) < 1 {
configPath = "./config.yaml"
}
yamlFile, err := ioutil.ReadFile(configPath)
if err != nil {
return
}
err = yaml.Unmarshal(yamlFile, &config)
if err != nil {
return
}
validate := validator.New()
err = validate.Struct(config)
return config, nil
}
func handler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
var err error
config, err = LoadConfig()
if err != nil {
log.Println(err)
if strings.Contains(err.Error(), "no such file or directory") {
log.Println("Failed to load config file")
http.Error(w, "internal-server-error", http.StatusInternalServerError)
return
}
}
var t Trigger
err = json.NewDecoder(r.Body).Decode(&t)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
found := false
var deployment Deployment
dk := pbkdf2.Key([]byte(t.Token), []byte(os.Getenv("SALT")), 4096, 16, sha1.New)
sEnc := b64.StdEncoding.EncodeToString(dk)
for _, deploy := range config.Deployments {
if deploy.Name == t.Name && sEnc == deploy.Hash {
found = true
deployment = deploy
}
}
if !found {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
if !deployment.UseValue {
t.Value = ""
}
// Reject any value that has characters other than: alphanumeric, -, and _
re := regexp.MustCompile("^[a-zA-Z0-9_-]*$")
if !re.MatchString(t.Value) {
http.Error(w, "bad-request", http.StatusBadRequest)
return
}
if len(t.Value) > 0 {
deployment.Command += " " + t.Value
}
log.Println(t.Name, t.Value)
log.Println(deployment.Command)
output, err := exec.Command("bash", "-c", deployment.Command).Output()
if err != nil {
log.Println(err)
http.Error(w, "internal-server-error", http.StatusInternalServerError)
return
}
log.Println(string(output))
w.Write([]byte("OK"))
default:
fmt.Fprintf(w, "Tendang!")
}
}
func main() {
app := cli.NewApp()
app.Name = "tendang"
app.Author = "BlankOn Developer"
app.Email = "blankon-dev@googlegroups.com"
app.Commands = []cli.Command{
{
Name: "serve",
Usage: "Serving tendang proxy",
Action: func(c *cli.Context) (err error) {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
handler(w, r)
})
port := os.Getenv("PORT")
if len(port) < 1 {
port = "8000"
}
fmt.Printf("Starting tendang at port " + port + "\n")
if err := http.ListenAndServe("0.0.0.0:"+port, nil); err != nil {
return err
}
return nil
},
},
{
Name: "gen",
Usage: "Generate key pair",
Action: func(c *cli.Context) (err error) {
token, err := password.Generate(64, 10, 0, false, true)
if err != nil {
return
}
dk := pbkdf2.Key([]byte(token), []byte(os.Getenv("SALT")), 4096, 16, sha1.New)
sEnc := b64.StdEncoding.EncodeToString(dk)
log.Println("Token: ", token)
log.Println("Hash: ", sEnc)
return nil
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}