/
checker.go
143 lines (129 loc) 路 4.04 KB
/
checker.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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"strings"
"github.com/reviewdog/reviewdog/doghouse"
"github.com/reviewdog/reviewdog/doghouse/server"
"github.com/reviewdog/reviewdog/doghouse/server/ciutil"
"github.com/reviewdog/reviewdog/doghouse/server/storage"
)
type githubChecker struct {
privateKey []byte
integrationID int
ghInstStore storage.GitHubInstallationStore
ghRepoTokenStore storage.GitHubRepositoryTokenStore
}
func (gc *githubChecker) handleCheck(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
ctx := r.Context()
var req doghouse.CheckRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "failed to decode request: %v", err)
return
}
// Check authorization.
if !gc.validateCheckRequest(ctx, w, r, req.Owner, req.Repo) {
return
}
opt := &server.NewGitHubClientOption{
PrivateKey: gc.privateKey,
IntegrationID: gc.integrationID,
RepoOwner: req.Owner,
Client: &http.Client{},
InstallationStore: gc.ghInstStore,
}
gh, err := server.NewGitHubClient(ctx, opt)
if err != nil {
log.Printf("[ERROR] failed to create GitHub client: %v\n", err)
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(w, err)
return
}
res, err := server.NewChecker(&req, gh).Check(ctx)
if err != nil {
log.Printf("[ERROR] failed to run checker: %v\n", err)
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(w, err)
return
}
if err := json.NewEncoder(w).Encode(res); err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(w, err)
return
}
}
func (gc *githubChecker) validateCheckRequest(ctx context.Context, w http.ResponseWriter, r *http.Request, owner, repo string) bool {
if extractBearerToken(r) == "" {
// Update Travis IP Addresss before checking IP to reduce the # of
// flaky errors when token is not present.
if err := ciutil.UpdateTravisCIIPAddrs(&http.Client{}); err != nil {
log.Printf("[ERROR] failed to update travis CI IP addresses: %v\n", err)
}
}
log.Printf("[INFO] Remote Addr: %s\n", r.RemoteAddr)
if ciutil.IsFromCI(r) {
// Skip token validation if it's from trusted CI providers.
return true
}
return gc.validateCheckToken(ctx, w, r, owner, repo)
}
func (gc *githubChecker) validateCheckToken(ctx context.Context, w http.ResponseWriter, r *http.Request, owner, repo string) bool {
token := extractBearerToken(r)
if token == "" {
w.Header().Set("The WWW-Authenticate", `error="invalid_request", error_description="The access token not provided"`)
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "The access token not provided. Get token from %s", githubRepoURL(ctx, r, owner, repo))
return false
}
_, wantToken, err := gc.ghRepoTokenStore.Get(ctx, owner, repo)
if err != nil {
log.Printf("[ERROR] failed to get repository (%s/%s) token: %v\n", owner, repo, err)
}
if wantToken == nil {
w.WriteHeader(http.StatusNotFound)
return false
}
if token != wantToken.Token {
w.Header().Set("The WWW-Authenticate", `error="invalid_token", error_description="The access token is invalid"`)
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "The access token is invalid. Get valid token from %s", githubRepoURL(ctx, r, owner, repo))
return false
}
return true
}
func githubRepoURL(ctx context.Context, r *http.Request, owner, repo string) string {
u := doghouseBaseURL(ctx, r)
u.Path = fmt.Sprintf("/gh/%s/%s", owner, repo)
return u.String()
}
func doghouseBaseURL(ctx context.Context, r *http.Request) *url.URL {
scheme := ""
if r.URL != nil && r.URL.Scheme != "" {
scheme = r.URL.Scheme
}
if scheme == "" {
scheme = "https"
}
u, err := url.Parse(scheme + "://" + r.Host)
if err != nil {
log.Printf("[ERROR] %v\n", err)
}
return u
}
func extractBearerToken(r *http.Request) string {
auth := r.Header.Get("Authorization")
prefix := "bearer "
if strings.HasPrefix(strings.ToLower(auth), prefix) {
return auth[len(prefix):]
}
return ""
}