forked from gogap/string_keeper
/
main.go
369 lines (304 loc) · 8.89 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/go-martini/martini"
"github.com/gogap/env_strings"
"github.com/martini-contrib/auth"
"github.com/martini-contrib/cors"
"github.com/gogap/string_keeper/git"
)
var (
resDir string
keeperConf StringKeeperConfig
)
var (
gitDirList map[string]bool = make(map[string]bool)
revisionFileCache map[string][]byte = make(map[string][]byte)
synclocker sync.Mutex
)
func main() {
if conf, e := LoadConfig("conf/string_keeper.conf"); e != nil {
if os.IsNotExist(e) {
fmt.Println("'conf/string_keeper.conf' not exist, it will use default config.")
keeperConf = DefaultConfig()
} else {
fmt.Printf("load config file 'conf/string_keeper.conf' failed, err: %s\n", e.Error())
os.Exit(1)
}
} else {
keeperConf = conf
}
m := martini.Classic()
m.Post("/", GetBucketString)
m.Get("/ping", func() string {
return "pong"
})
if cwd, e := os.Getwd(); e != nil {
fmt.Printf("get current dir failed, err: %s", e.Error())
os.Exit(1)
} else if !filepath.IsAbs(cwd) {
if absPath, e := filepath.Abs(cwd); e != nil {
fmt.Printf("get current dir abs path failed, err: %s", e.Error())
os.Exit(1)
return
} else {
resDir = filepath.Join(absPath, "public")
}
} else {
resDir = filepath.Join(cwd, "public")
}
m.Use(cors.Allow(&cors.Options{
AllowOrigins: keeperConf.HTTP.CORS.AllowOrigins,
AllowMethods: keeperConf.HTTP.CORS.AllowMethods,
AllowHeaders: keeperConf.HTTP.CORS.AllowHeaders,
ExposeHeaders: keeperConf.HTTP.CORS.ExposeHeaders,
AllowCredentials: keeperConf.HTTP.CORS.AllowCerdentials,
}))
if keeperConf.ACL.AuthEnabled {
m.Use(auth.BasicFunc(AuthCheck))
} else {
m.Map(auth.User(""))
}
m.RunOnAddr(keeperConf.HTTP.Address)
}
type PostData struct {
Namespace string `json:"namespace"`
Bucket string `json:"bucket"`
Revision string `json:"revision,omitempty"`
File string `json:"file"`
Envs map[string]interface{} `json:"envs"`
RawData bool `json:"raw_data"`
}
func AuthCheck(userName, password string) bool {
if keeperConf.ACL.AuthEnabled {
if keeperConf.ACL.BasicAuth != nil {
if pwd, exist := keeperConf.ACL.BasicAuth[userName]; exist && pwd == password {
return true
}
}
return false
}
return true
}
func BucketAccessAuthCheck(user auth.User, namespace, bucket string) (err error) {
strUser := string(user)
correctUserName := strings.Join([]string{namespace, bucket}, "/")
if strUser == "" {
err = fmt.Errorf("no auth info found")
return
} else if strUser != correctUserName {
err = fmt.Errorf("did not have matched account of %s", correctUserName)
return
}
return
}
func IPCheck(remoteIP string) (err error) {
if keeperConf.ACL.IPWhiteList == nil {
err = fmt.Errorf("the ip of %s is not in white list", remoteIP)
return
}
isWhiteIP := false
for _, ip := range keeperConf.ACL.IPWhiteList {
if ip == remoteIP {
isWhiteIP = true
break
}
}
if !isWhiteIP {
err = fmt.Errorf("the ip of %s is not in white list", remoteIP)
return
}
return
}
func GetBucketString(
res http.ResponseWriter,
req *http.Request,
user auth.User,
params martini.Params) {
if keeperConf.ACL.IPACLEnabled {
remoteIP, _, _ := net.SplitHostPort(req.RemoteAddr)
if e := IPCheck(remoteIP); e != nil {
respErrorf(res, http.StatusForbidden, "%s", e.Error())
return
}
}
postBody, e := ioutil.ReadAll(req.Body)
if e != nil {
res.WriteHeader(http.StatusBadRequest)
return
}
data := PostData{}
if postBody != nil && string(postBody) != "" {
if e := json.Unmarshal(postBody, &data); e != nil {
respErrorf(res, http.StatusInternalServerError, "unmarshal post body to struct failed, err: %s", e.Error())
return
}
}
data.Namespace = strings.TrimSpace(data.Namespace)
data.Bucket = strings.TrimSpace(data.Bucket)
data.File = strings.TrimSpace(data.File)
if data.Namespace == "" ||
data.Bucket == "" ||
data.File == "" {
respErrorf(res, http.StatusBadRequest, "namespace/bucket/file could not empty.")
return
}
if keeperConf.ACL.AuthEnabled {
if e := BucketAccessAuthCheck(user, data.Namespace, data.Bucket); e != nil {
respErrorf(res, http.StatusForbidden, "%s", e.Error())
return
}
}
bucketRoot := filepath.Join("public", data.Namespace, data.Bucket)
originalStringFile := filepath.Join(bucketRoot, data.File)
stringFile := originalStringFile
if strings.Contains(stringFile, "..") || strings.Contains(stringFile, "./") {
respErrorf(res, http.StatusForbidden, "request data contains denied string")
return
}
if !filepath.IsAbs(stringFile) {
if absPath, e := filepath.Abs(stringFile); e != nil {
respErrorf(res, http.StatusInternalServerError, "get file of %s's abs path failed, err: %s", stringFile, e.Error())
return
} else {
if !filepath.HasPrefix(absPath, resDir) {
res.WriteHeader(http.StatusBadRequest)
return
}
}
}
if fi, e := os.Stat(stringFile); e != nil {
if os.IsNotExist(e) {
respErrorf(res, http.StatusNotFound, "file '%s' not exist", originalStringFile)
return
} else {
respErrorf(res, http.StatusInternalServerError, "get file stat of %s error, err: %s", originalStringFile, e.Error())
return
}
} else if fi.IsDir() {
respErrorf(res, http.StatusExpectationFailed, "the request file of %s is a dir", originalStringFile)
return
}
fileDir := filepath.Dir(stringFile)
readfileDirect := false
badRequest := false
gitFileRoot := ""
gitFilePath := ""
var err error
if data.Revision == "" {
readfileDirect = true
} else {
if fileDir == bucketRoot {
readfileDirect = true
badRequest = true
} else {
if gitFileRoot, err = getFileGitRoot(bucketRoot, fileDir); err != nil {
respErrorf(res, http.StatusExpectationFailed, "get file of %s git repo root error, err: %s", originalStringFile, err.Error())
return
}
gitFilePath, _ = filepath.Rel(gitFileRoot, stringFile)
if isGit, exist := gitDirList[gitFileRoot]; exist && isGit {
readfileDirect = false
} else if !exist {
if fi, e := os.Stat(filepath.Join(gitFileRoot, ".git")); e != nil {
synclocker.Lock()
gitDirList[gitFileRoot] = false
badRequest = true
synclocker.Unlock()
} else if fi.IsDir() {
synclocker.Lock()
gitDirList[gitFileRoot] = true
readfileDirect = false
go gitPuller(gitFileRoot)
synclocker.Unlock()
} else {
badRequest = true
}
} else {
readfileDirect = true
}
}
}
if badRequest {
respErrorf(res, http.StatusBadRequest, "read file of %s error, err: the file is not in git dir, could not use revision to pick file", originalStringFile)
return
}
var fileData []byte
if readfileDirect {
if fileData, err = ioutil.ReadFile(stringFile); err != nil {
respErrorf(res, http.StatusExpectationFailed, "read file of %s error, err: %s", originalStringFile, err.Error())
return
}
} else {
if fileData, err = GetRevisionFile(gitFileRoot, gitFilePath, data.Revision); err != nil {
respErrorf(res, http.StatusExpectationFailed, "read file of %s error, git root: %s, revision: %s , err: %s", gitFilePath, gitFileRoot, data.Revision, err.Error())
return
}
}
if !data.RawData {
if retStr, e := env_strings.ExecuteWith(string(fileData), data.Envs); e != nil {
respErrorf(res, http.StatusInternalServerError, "build file of %s error, err: %s", originalStringFile, e.Error())
return
} else {
respString(res, retStr)
return
}
} else {
respBytes(res, fileData)
return
}
}
func GetRevisionFile(baseDir, filePath, revision string) (data []byte, err error) {
revisionPath := revision + ":" + filePath
exist := false
if data, exist = revisionFileCache[revisionPath]; exist {
return
}
repoGit := git.NewGit(baseDir)
if data, err = repoGit.CatBlobFile(filePath, revision); err != nil {
err = fmt.Errorf("%s\n%s", string(data), err.Error())
return
}
revisionFileCache[revisionPath] = data
return
}
func getFileGitRoot(bucketDir string, fileDir string) (repoGitRoot string, err error) {
relPath := ""
if relPath, err = filepath.Rel(bucketDir, fileDir); err != nil {
return
}
dirs := strings.Split(relPath, "/")
repoGitRoot = filepath.Join(bucketDir, dirs[0])
return
}
func gitPuller(gitRoot string) {
repo := git.NewGit(gitRoot)
for {
if output, e := repo.Pull(); e != nil {
log.Printf("[%s]:%s, error: %s\n", gitRoot, string(output), e.Error())
} else {
log.Printf("[%s]:%s\n", gitRoot, string(output))
}
time.Sleep(time.Second * 30)
}
}
func respErrorf(res http.ResponseWriter, code int, format string, v ...interface{}) (int, error) {
res.WriteHeader(code)
return respString(res, fmt.Sprintf(format, v...))
}
func respString(res http.ResponseWriter, str string) (int, error) {
return res.Write([]byte(str))
}
func respBytes(res http.ResponseWriter, bytes []byte) (int, error) {
return res.Write(bytes)
}