forked from G-Node/gin-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
268 lines (209 loc) · 5.61 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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"github.com/G-Node/gin-repo/auth"
"github.com/G-Node/gin-repo/store"
"github.com/dgrijalva/jwt-go"
"github.com/docopt/docopt-go"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)
type Server struct {
http.Server
Root *mux.Router
srvKey []byte
users store.UserStore
repos *store.RepoStore
}
type LogLevel int
const (
PANIC LogLevel = iota
ERROR
WARN
INFO
DEBUG
)
func (s *Server) log(lvl LogLevel, msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
}
type statusResponseWriter struct {
http.ResponseWriter
status int
headerWritten bool
}
func (w *statusResponseWriter) WriteHeader(status int) {
if !w.headerWritten {
w.status = status
}
w.ResponseWriter.WriteHeader(status)
}
func (s *Server) ServeHTTP(original http.ResponseWriter, req *http.Request) {
w := &statusResponseWriter{original, 200, false}
defer func() {
if r := recover(); r != nil {
w.WriteHeader(http.StatusInternalServerError)
buf := make([]byte, 1024*10)
n := runtime.Stack(buf, false)
st := string(buf[:n])
s.log(ERROR, "'%s %s' %d, Recovered from panic!", req.Method, req.URL.Path, w.status)
fmt.Fprintf(os.Stderr, "Panic on reqest: '%s %s': %s", req.Method, req.URL.Path, st)
} else {
s.log(INFO, "'%s %s' %d", req.Method, req.URL.Path, w.status)
}
}()
// this should most like be done differently, in a middleware maybe
// good enough for now though
if strings.HasPrefix(path.Clean(req.URL.Path), "/intern") {
token, err := s.getAuthToken(req)
if err != nil {
s.log(WARN, "Got invalid token: %v", err)
w.WriteHeader(http.StatusForbidden)
return
}
claims := token.Claims.(*auth.Claims)
if claims.TokenType != "service" {
s.log(WARN, "Got token without or non-service type")
w.WriteHeader(http.StatusUnauthorized)
return
}
}
s.Root.ServeHTTP(w, req)
}
func (s *Server) getAuthToken(r *http.Request) (*jwt.Token, error) {
header := r.Header.Get("Authorization")
if header == "" {
return nil, auth.ErrNoAuth
} else if !strings.HasPrefix(header, "Bearer ") {
return nil, fmt.Errorf("Invalid auth type: %q", header)
}
return jwt.ParseWithClaims(strings.Trim(header[6:], " "), &auth.Claims{}, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Wrong signing method: %v", token.Header["alg"])
}
return s.srvKey, nil
})
}
func (s *Server) SetupServiceSecret() error {
secret, err := auth.ReadSharedSecret()
if err != nil {
secret, err = auth.CreateSharedSecret()
if err != nil {
panic(fmt.Errorf("Could not create shared secret: %v", err))
}
path := "gin.secret"
err = ioutil.WriteFile(path, secret, 0600)
if err != nil {
s.log(PANIC, "Could not write to gin.secret: %v", err)
os.Exit(10)
}
if abspath, err := filepath.Abs(path); err == nil {
path = abspath
}
s.log(DEBUG, "Wrote shared secret to %q", path)
}
s.srvKey = secret
if val := os.Getenv("GIN_REPO_DEBUGTOKEN"); val != "" {
str, err := auth.MakeServiceToken(s.srvKey)
if err != nil {
s.log(PANIC, "Could not make debug auth token")
}
s.log(DEBUG, "Token: [%s]", str)
err = ioutil.WriteFile("token", []byte(str), 0600)
if err != nil {
s.log(WARN, "Could not write debug token: %v", err)
}
}
return nil
}
func (s *Server) SetupStores() {
var err error
dir := os.Getenv("GIN_REPO_DIR")
if dir == "" {
dir = "."
}
s.users, err = store.NewUserStore(dir)
if err != nil {
s.log(PANIC, "Could not setup user store: %v", err)
os.Exit(11)
}
s.repos, err = store.NewRepoStore(dir)
if err != nil {
s.log(PANIC, "Could not setup repo store: %v", err)
os.Exit(12)
}
repos, err := s.repos.ListRepos()
if err != nil {
s.log(PANIC, "Could not read repo store: %v", err)
os.Exit(13)
}
s.log(DEBUG, "repos detected:")
for _, repo := range repos {
s.log(DEBUG, "- [%s]", repo)
public, err := s.repos.GetRepoVisibility(repo)
if err != nil {
s.log(WARN, " - visibility error: %v", err)
} else {
s.log(DEBUG, " - public: %v", public)
}
access, err := s.repos.ListSharedAccess(repo)
if err != nil {
s.log(WARN, " - shared access error: %v", err)
} else if len(access) > 0 {
s.log(DEBUG, " - sharing:")
for k, v := range access {
s.log(DEBUG, " %q: %s", k, v)
}
}
}
}
func (s *Server) ListenAndServe() error {
s.log(INFO, "Listening on %s", s.Addr)
err := s.Server.ListenAndServe()
if err != nil {
s.log(ERROR, "ListenAndServe() error: %v", err)
}
return err
}
func NewServer(addr string) *Server {
s := &Server{Server: http.Server{Addr: addr}, Root: mux.NewRouter()}
s.Handler = s
return s
}
func main() {
usage := `gin repo daemon.
Usage:
gin-repod [--listen=<address>]
gin-repod make-token <user>
gin-repod -h | --help
gin-repod --version
Options:
-h --help Show this screen.
--version Show version.
--listen=<address> Address to listen on [default: :8082]
`
args, err := docopt.Parse(usage, nil, true, "gin repod 0.1a", false)
if err != nil {
fmt.Fprintf(os.Stderr, "Error while parsing command line: %v\n", err)
os.Exit(-1)
}
fmt.Println(args)
s := NewServer(args["--listen"].(string))
s.SetupRoutes()
s.SetupServiceSecret()
s.SetupStores()
s.Handler = handlers.CORS(
handlers.AllowedHeaders([]string{"Authorization", "Content-Type"}),
handlers.AllowedMethods([]string{"GET", "PUT", "POST", "DELETE", "PATCH"}),
)(s.Handler)
// this call might never return if there actually was
// a command line "command"
s.handleCommands(args)
s.ListenAndServe()
}