|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "net/http" |
| 6 | + "net/http/httputil" |
| 7 | + "net/url" |
| 8 | + "regexp" |
| 9 | +) |
| 10 | + |
| 11 | +// GitLabGateway acts as a proxy to Gitlab |
| 12 | +type GitLabGateway struct { |
| 13 | + proxy *httputil.ReverseProxy |
| 14 | +} |
| 15 | + |
| 16 | +var gitlabPathRegexp = regexp.MustCompile("^/gitlab/?") |
| 17 | +var gitlabAllowedRegexp = regexp.MustCompile("^/gitlab/repository/(files|commits|tree)/?") |
| 18 | + |
| 19 | +func NewGitLabGateway() *GitLabGateway { |
| 20 | + return &GitLabGateway{ |
| 21 | + proxy: &httputil.ReverseProxy{ |
| 22 | + Director: gitlabDirector, |
| 23 | + Transport: &GitLabTransport{}, |
| 24 | + }, |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +func gitlabDirector(r *http.Request) { |
| 29 | + ctx := r.Context() |
| 30 | + target := getProxyTarget(ctx) |
| 31 | + accessToken := getAccessToken(ctx) |
| 32 | + |
| 33 | + targetQuery := target.RawQuery |
| 34 | + r.Host = target.Host |
| 35 | + r.URL.Scheme = target.Scheme |
| 36 | + r.URL.Host = target.Host |
| 37 | + r.URL.Path = singleJoiningSlash(target.Path, gitlabPathRegexp.ReplaceAllString(r.URL.Path, "/")) |
| 38 | + if targetQuery == "" || r.URL.RawQuery == "" { |
| 39 | + r.URL.RawQuery = targetQuery + r.URL.RawQuery |
| 40 | + } else { |
| 41 | + r.URL.RawQuery = targetQuery + "&" + r.URL.RawQuery |
| 42 | + } |
| 43 | + if _, ok := r.Header["User-Agent"]; !ok { |
| 44 | + // explicitly disable User-Agent so it's not set to default value |
| 45 | + r.Header.Set("User-Agent", "") |
| 46 | + } |
| 47 | + if r.Method != http.MethodOptions { |
| 48 | + r.Header.Set("Authorization", "Bearer "+accessToken) |
| 49 | + } |
| 50 | + |
| 51 | + log := getLogEntry(r) |
| 52 | + log.Infof("Proxying to GitLab: %v", r.URL.String()) |
| 53 | +} |
| 54 | + |
| 55 | +func (gl *GitLabGateway) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 56 | + ctx := r.Context() |
| 57 | + config := getConfig(ctx) |
| 58 | + if config == nil || config.GitLab.AccessToken == "" { |
| 59 | + handleError(notFoundError("No GitLab Settings Configured"), w, r) |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + if err := gl.authenticate(w, r); err != nil { |
| 64 | + handleError(unauthorizedError(err.Error()), w, r) |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + endpoint := config.GitLab.Endpoint |
| 69 | + apiURL := singleJoiningSlash(endpoint, "/repos/"+config.GitLab.Repo) |
| 70 | + target, err := url.Parse(apiURL) |
| 71 | + if err != nil { |
| 72 | + handleError(internalServerError("Unable to process GitLab endpoint"), w, r) |
| 73 | + return |
| 74 | + } |
| 75 | + ctx = withProxyTarget(ctx, target) |
| 76 | + ctx = withAccessToken(ctx, config.GitLab.AccessToken) |
| 77 | + gl.proxy.ServeHTTP(w, r.WithContext(ctx)) |
| 78 | +} |
| 79 | + |
| 80 | +func (gl *GitLabGateway) authenticate(w http.ResponseWriter, r *http.Request) error { |
| 81 | + ctx := r.Context() |
| 82 | + claims := getClaims(ctx) |
| 83 | + config := getConfig(ctx) |
| 84 | + |
| 85 | + if claims == nil { |
| 86 | + return errors.New("Access to endpoint not allowed: no claims found in Bearer token") |
| 87 | + } |
| 88 | + |
| 89 | + if !gitlabAllowedRegexp.MatchString(r.URL.Path) { |
| 90 | + return errors.New("Access to endpoint not allowed: this part of GitLab's API has been restricted") |
| 91 | + } |
| 92 | + |
| 93 | + if len(config.Roles) == 0 { |
| 94 | + return nil |
| 95 | + } |
| 96 | + |
| 97 | + roles, ok := claims.AppMetaData["roles"] |
| 98 | + if ok { |
| 99 | + roleStrings, _ := roles.([]interface{}) |
| 100 | + for _, data := range roleStrings { |
| 101 | + role, _ := data.(string) |
| 102 | + for _, adminRole := range config.Roles { |
| 103 | + if role == adminRole { |
| 104 | + return nil |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return errors.New("Access to endpoint not allowed: your role doesn't allow access") |
| 111 | +} |
| 112 | + |
| 113 | +type GitLabTransport struct{} |
| 114 | + |
| 115 | +func (t *GitLabTransport) RoundTrip(r *http.Request) (*http.Response, error) { |
| 116 | + resp, err := http.DefaultTransport.RoundTrip(r) |
| 117 | + if err == nil { |
| 118 | + // remove CORS headers from GitHub and use our own |
| 119 | + resp.Header.Del("Access-Control-Allow-Origin") |
| 120 | + } |
| 121 | + return resp, err |
| 122 | +} |
0 commit comments