-
Notifications
You must be signed in to change notification settings - Fork 5
/
start.go
340 lines (277 loc) · 10.3 KB
/
start.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
package cmd
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"sync"
"time"
"github.com/Azure/mirrorcat"
"github.com/go-redis/redis"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// startCmd represents the start command
var startCmd = &cobra.Command{
Use: "start",
Short: "Starts the MirrorCat server on localhost",
Args: cobra.NoArgs,
// Long: `A longer description that spans multiple lines and likely contains examples
// and usage of using your command. For example:
// Cobra is a CLI library for Go that empowers applications.
// This application is a tool to generate the needed files
// to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
populateStaticMirrors()
var host string
if reportedHost, err := os.Hostname(); err == nil {
host = reportedHost
} else {
host = "Unknown Host"
}
log.Printf("Starting MirrorCat\n\tBuilt using commit %q", commit)
log.SetPrefix(fmt.Sprintf("[MirrorCat on %s]", host))
http.HandleFunc("/push/github", handleGitHubPushEvent)
port := viper.GetInt("port")
log.Printf("Listening on port %d\n", port)
if options, err := redis.ParseURL(viper.GetString("redis-connection")); err != nil {
log.Println("Unable to connect to Redis Because: ", err)
} else {
client := redis.NewClient(options)
go func() {
log.Print("Connecting to Redis at ", options.Addr)
allMirrors = append(allMirrors, mirrorcat.RedisFinder(*client))
_, err := client.Keys("*").Result()
if err != nil {
log.Print("Unable to connect to Redis because: ", err)
} else {
log.Print("Successfully connected to Redis.")
}
}()
}
if viper.IsSet("github-auth-token") && viper.GetString("github-auth-token") != "" {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
identity, err := FetchGitHubIdentity(ctx, viper.GetString("github-auth-token"))
if err == nil {
log.Print("Setting default GitHub identity: ", identity)
viper.Set("github-auth-username", identity)
} else {
log.Println("identity returned:")
log.Println("Unable to find identity to match access token because:", err)
}
}
if http.ListenAndServe(fmt.Sprintf(":%d", port), nil) != nil {
return
}
},
}
// DefaultPort is the port that will be used by default as MirrorCat is started.
const DefaultPort uint = 8080
// DefaultCloneDepth is the number of commits that will be checked out if one is
// not specified by the invoker of MirrorCat.
// Most MirrorCat functions will treat negative values as indicative of needing to
// clone the entire repository. However, at their discretion, they may try to use
// information local to it to optimize its performance.
// However, if this value is greater than zero, MirrorCat function implementers are
// advised to respect it.
const DefaultCloneDepth = -1
type WrittenTuple struct {
Original mirrorcat.RemoteRef `json:"original"`
Mirror mirrorcat.RemoteRef `json:"mirror"`
CommitID string `json:"commitID"`
}
func init() {
RootCmd.AddCommand(startCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// startCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// startCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
startCmd.Flags().UintP("port", "p", 0, "The port that should be used to serve the MirrorCat service on.")
viper.BindPFlag("port", startCmd.Flags().Lookup("port"))
startCmd.Flags().UintP("clone-depth", "c", 0, "The number of commits to checkout while cloning the original repository. (The default behavior is to clone all of the commits in the original repository.)")
viper.BindPFlag("clone-depth", startCmd.Flags().Lookup("clone-depth"))
startCmd.Flags().StringP("redis-connection", "r", "", "The host to contact Redis with, if it's relevant.")
viper.BindPFlag("redis-connection", startCmd.Flags().Lookup("redis-connection"))
startCmd.Flags().StringP("github-auth-token", "g", "", "The default identity to assume when communicating with GitHub. Mirror configuration overrides this setting.")
viper.BindPFlag("github-auth-token", startCmd.Flags().Lookup("github-auth-token"))
viper.SetDefault("port", DefaultPort)
viper.SetDefault("clone-depth", DefaultCloneDepth)
if prefixlessAuthToken := os.Getenv("GITHUB_AUTH_TOKEN"); !viper.IsSet("github-auth-token") && prefixlessAuthToken != "" {
viper.BindEnv("github-auth-token", "GITHUB_AUTH_TOKEN")
log.Print(`Using environment variable "GITHUB_AUTH_TOKEN" because "MIRRORCAT_GITHUB_AUTH_TOKEN" wasn't found.`)
}
}
func handleGitHubPushEvent(resp http.ResponseWriter, req *http.Request) {
// MaxPayloadSize is the largest payload that GitHub would transmit. It is defined by GitHub
// and was written here on 12/6/2017 after reading this page: https://developer.github.com/webhooks/#payloads
const MaxPayloadSize = 5 * 1024 * 1024 // 1024 * 1024 = 1 MB
// After spinning for 10 minutes, give up
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*10)
defer cancel()
log.Println("Request Received")
var pushed mirrorcat.PushEvent
// Limited reader decorates Body to prevent DOS attacks which open
// a request which will never be closed, or be closed after transmitting
// a huge amount of data.
payloadReader := &io.LimitedReader{
R: req.Body,
N: MaxPayloadSize,
}
payload, err := ioutil.ReadAll(payloadReader)
if err != nil {
fmt.Fprintln(resp, "Unable to read the request.")
return
}
err = json.Unmarshal(payload, &pushed)
if err != nil {
log.Println("Bad Request:\n", err.Error())
resp.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(resp, "Body of request didn't conform to expected pattern of GitHub v3 PushEvent. See https://developer.github.com/v3/activity/events/types/#pushevent for expected format.")
return
}
original := mirrorcat.RemoteRef{
Repository: pushed.Repository.CloneURL,
Ref: mirrorcat.NormalizeRef(pushed.Ref),
}
mirrors := make(chan mirrorcat.RemoteRef)
go allMirrors.FindMirrors(ctx, original, mirrors)
bodyWriter := json.NewEncoder(resp)
loop:
for {
select {
case entry, ok := <-mirrors:
if !ok {
break loop
}
var hasUser, hasPassword bool
repoURL, err := url.Parse(entry.Repository)
if err == nil && repoURL.User != nil {
hasUser = repoURL.User.Username() != ""
_, hasPassword = repoURL.User.Password()
}
if viper.IsSet("github-auth-token") && !hasUser && !hasPassword {
repoURL.User = url.UserPassword(viper.GetString("github-auth-username"), viper.GetString("github-auth-token"))
}
entry.Repository = repoURL.String()
err = mirrorcat.Push(ctx, original, entry, viper.GetInt("clone-depth"))
if err == nil {
// Strip password information before writing to logs.
repoURL.User = nil
entry.Repository = repoURL.String()
bodyWriter.Encode(WrittenTuple{
Original: original,
Mirror: entry,
CommitID: pushed.Head.ID,
})
log.Println("Pushed", pushed.Ref, "at", pushed.Head.ID, "from ", original, " to ", entry)
} else {
resp.WriteHeader(http.StatusInternalServerError)
log.Println("Unable to complete push:\n ", err.Error())
}
case <-ctx.Done():
resp.WriteHeader(http.StatusRequestTimeout)
log.Println(ctx.Err())
return
}
}
log.Println("Request Completed.")
}
var allMirrors = mirrorcat.MergeFinder{staticMirrors}
var staticMirrors = mirrorcat.NewDefaultMirrorFinder()
var populateStaticMirrors = func() func() error {
var populating sync.Mutex
return func() error {
populating.Lock()
defer populating.Unlock()
if !viper.InConfig("mirrors") {
return errors.New("no `mirrors` property found")
}
originalRepos, ok := viper.Get("mirrors").(map[string]interface{})
if !ok {
return errors.New("`mirrors` was in an unexpected format")
}
log.Println("Removing all Static Mirrors")
staticMirrors.ClearAll()
for origRepo, refs := range originalRepos {
originalRefs, ok := refs.(map[string]interface{})
if !ok {
log.Printf("skipping because key %q was in an unexpected format.", origRepo)
continue
}
for origRef, mirrors := range originalRefs {
original := mirrorcat.RemoteRef{
Repository: origRepo,
Ref: origRef,
}
remoteRepos, ok := mirrors.(map[string]interface{})
if !ok {
log.Printf("skipping because key %q was in an unexpected format.", origRef)
continue
}
for remote, branches := range remoteRepos {
remoteRefs, ok := branches.([]interface{})
if !ok {
log.Printf("skipping because key %q was in an unexpected format.", remote)
continue
}
for _, remoteRef := range remoteRefs {
mirror := mirrorcat.RemoteRef{
Repository: remote,
Ref: remoteRef.(string),
}
staticMirrors.AddMirrors(original, mirror)
log.Println("Adding Static Mirror:\n\t", original, "\n\t", mirror)
}
}
}
}
return nil
}
}()
// FetchGitHubIdentity uses the
func FetchGitHubIdentity(ctx context.Context, token string) (username string, err error) {
req, err := http.NewRequest(http.MethodGet, "https://api.github.com/user", &bytes.Buffer{})
if err != nil {
return
}
req = req.WithContext(ctx)
req.Header.Add("Accept", "application/vnd.github.jean-grey-preview+json")
req.SetBasicAuth("username", token) // The string "username" is arbitrary, and will be disregarded by the server when seeking an identity for a token.
resp, err := http.DefaultClient.Do(req)
if err != nil {
return
} else if expected := 200; resp.StatusCode != expected {
err = fmt.Errorf("response status code (%d) wasn't the expected (%d): %v", resp.StatusCode, expected, resp)
return
}
const maxBytes = 5 * 1024 * 1024
rawBody, err := ioutil.ReadAll(&io.LimitedReader{
R: resp.Body,
N: maxBytes,
})
if err != nil {
return
}
partial := map[string]json.RawMessage{}
err = json.Unmarshal(rawBody, &partial)
if err != nil {
return
}
if marshaledName, ok := partial["login"]; ok {
err = json.Unmarshal([]byte(marshaledName), &username)
} else {
err = fmt.Errorf("login filed wasn't presnent in GitHub repsonse, status code %d", resp.StatusCode)
}
return
}