From 0e8b8ec589a3d476f14054c8ce3c17add1fc6420 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Fri, 13 Nov 2026 15:57:27 +0000 Subject: [PATCH] rogitproxy: add read-only GitHub API proxy Sandboxed VMs can clone via rogitproxy without credentials, but tooling in them also needs to read PR checks, Actions run status and failure logs, issues, and PR/review comments. Add an unauthenticated (from the client's view) GitHub API proxy that grants what a read-only collaborator on the caller's granted repos could see on the website. It is laid out like GitHub Enterprise Server (/api/v3/ for REST, /api/graphql for GraphQL) and is also served over HTTPS on :443 with the tailnet cert, so the gh CLI works with GH_HOST pointed at the proxy and no TLS MITM or custom root certs. gh even detects the repo from git:// remotes cloned from rogitproxy. Safety is layered: upstream requests use GitHub App installation tokens downscoped to read-only permissions and the caller's granted repos; REST is limited to GET/HEAD under /repos/{owner}/{repo}/ with a pull grant on that repo; GraphQL documents are parsed and anything but queries is rejected; client auth headers are dropped; and redirects (e.g. Actions log downloads to blob storage) are followed server-side without the token. API URLs in JSON bodies and Link headers are rewritten to point back at the proxy so pagination works. The GitHub App needs read permissions for actions, checks, issues, pull_requests, and statuses added for the scoped tokens to be minted. Updates tailscale/corp#51474 Signed-off-by: Brad Fitzpatrick Change-Id: Ic74e6727544f51b862ffea6719b70f1f04b0ffdf --- README.md | 50 +++++ github.go | 107 ++++++++++- github_test.go | 11 ++ githubapi.go | 442 +++++++++++++++++++++++++++++++++++++++++++++ githubapi_test.go | 386 +++++++++++++++++++++++++++++++++++++++ go.mod | 3 +- go.sum | 8 + metrics.go | 2 + rogitproxy.go | 61 +++++-- rogitproxy_test.go | 25 +-- 10 files changed, 1065 insertions(+), 30 deletions(-) create mode 100644 githubapi.go create mode 100644 githubapi_test.go diff --git a/README.md b/README.md index 6f2b5f5..c0436a0 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,56 @@ rogitproxy serves an HTTP status page on port 80 (tsnet) showing the requesting user's granted repos and usage examples. It also serves `/debug/` via tsweb.Debugger (metrics, pprof, etc.). +## Read-only GitHub API proxy + +When GitHub App auth is enabled, rogitproxy also proxies read-only GitHub +API requests with no client credentials, so sandboxed tools can read PRs, +issues, comments, reviews, checks, commit statuses, and GitHub Actions runs +and logs for the repos they have `pull` grants on. URLs are laid out like +GitHub Enterprise Server: + + REST: http://rogitproxy.your-tailnet.ts.net/api/v3/repos/org/repo/... + GraphQL: http://rogitproxy.your-tailnet.ts.net/api/graphql + +Both are served over plain HTTP on port 80 and over HTTPS on port 443 using +the tailnet's TLS certificate (no MITM or custom root certs needed). + +Safety is layered: + +* Each upstream request uses a GitHub App installation token downscoped to + read-only permissions (`actions`, `checks`, `contents`, `issues`, + `metadata`, `pull_requests`, `statuses`) and to the requested repo (REST) + or the caller's granted repos in one org (GraphQL). +* REST: only `GET` and `HEAD` under `/api/v3/repos/{owner}/{repo}/`, which + requires a `pull` grant on that repo. +* GraphQL: only `query` operations; mutations and subscriptions are rejected. + The token's org is taken from the `owner` variable if present. +* Client `Authorization` and `Cookie` headers are dropped. Redirects (e.g. + Actions log downloads) are followed server-side without the token. +* `https://api.github.com/` URLs in JSON bodies and `Link` headers are + rewritten to point back at the proxy, so pagination works. + +The GitHub App must have been granted the read permissions above, and each +org installation must accept them. + +### Clients + +The gh CLI only speaks HTTPS to Enterprise hosts. Point it at the proxy +(gh requires a token to be set, but its value is ignored): + + export GH_HOST=rogitproxy.your-tailnet.ts.net GH_ENTERPRISE_TOKEN=unused + gh pr checks 123 + gh run view 456 --log-failed + +Inside a checkout cloned from `git://rogitproxy.your-tailnet.ts.net/...`, +gh detects the repo from the remote. Elsewhere, pass +`-R rogitproxy.your-tailnet.ts.net/org/repo` or set `GH_REPO`. + +For go-github, use +`github.NewClient(nil).WithEnterpriseURLs("http://rogitproxy.your-tailnet.ts.net/api/v3/", "")`. +For curl and scripts, swap `https://api.github.com` for +`http://rogitproxy.your-tailnet.ts.net/api/v3`. + ## Only read-only operations Only `git-upload-pack` (the read-only git protocol command) is permitted. diff --git a/github.go b/github.go index 6516584..12bce74 100644 --- a/github.go +++ b/github.go @@ -11,11 +11,13 @@ import ( "net/http" "os" "path/filepath" + "slices" "strings" "sync" "time" "github.com/bradleyfalzon/ghinstallation/v2" + "github.com/google/go-github/v84/github" "github.com/tailscale/setec/client/setec" ) @@ -52,6 +54,8 @@ func GitHubAppTransport(ctx context.Context, setecURL string, setecDo func(*http base: http.DefaultTransport, appsTr: appsTr, orgTr: make(map[string]*ghinstallation.Transport), + orgID: make(map[string]int64), + scoped: make(map[string]*ghinstallation.Transport), } return mt, nil @@ -65,8 +69,10 @@ type multiOrgTransport struct { base http.RoundTripper appsTr *ghinstallation.AppsTransport - mu sync.RWMutex - orgTr map[string]*ghinstallation.Transport // lowercase org -> transport + mu sync.RWMutex + orgTr map[string]*ghinstallation.Transport // lowercase org -> transport + orgID map[string]int64 // lowercase org -> installation ID + scoped map[string]*ghinstallation.Transport // scopedKey -> read-only scoped transport } func (t *multiOrgTransport) RoundTrip(req *http.Request) (*http.Response, error) { @@ -119,6 +125,7 @@ func (t *multiOrgTransport) transportForOrg(ctx context.Context, org string) (*g } itr = ghinstallation.NewFromAppsTransport(t.appsTr, installID) t.mu.Lock() + t.orgID[org] = installID // Only set if we didn't lose the race if existing, ok := t.orgTr[org]; !ok { t.orgTr[org] = itr @@ -129,6 +136,101 @@ func (t *multiOrgTransport) transportForOrg(ctx context.Context, org string) (*g return itr, nil } +// installationIDForOrg returns the GitHub App installation ID for the +// given lowercase org, discovering it via the GitHub API on cache miss. +func (t *multiOrgTransport) installationIDForOrg(ctx context.Context, org string) (int64, error) { + t.mu.RLock() + id, ok := t.orgID[org] + t.mu.RUnlock() + if ok { + return id, nil + } + id, err := t.findInstallationForOrg(ctx, org) + if err != nil { + return 0, err + } + t.mu.Lock() + t.orgID[org] = id + t.mu.Unlock() + return id, nil +} + +// apiReadPermissions are the read-only permissions requested for +// downscoped installation tokens used by the GitHub API proxy. They +// cover what a read-only collaborator can see on the website: code, +// Actions runs and logs, checks, commit statuses, issues, and pull +// requests (including comments and reviews). +// +// The GitHub App itself must have been granted at least these +// permissions, or token creation fails. +func apiReadPermissions() *github.InstallationPermissions { + read := github.Ptr("read") + return &github.InstallationPermissions{ + Actions: read, + Checks: read, + Contents: read, + Issues: read, + Metadata: read, + PullRequests: read, + Statuses: read, + } +} + +// scopedToken returns a GitHub App installation token for the given +// lowercase org that has only read-only permissions (see +// apiReadPermissions) and, if repos is non-empty, can only access the +// named repos (bare repo names without the org prefix). +// +// Tokens are cached and refreshed by ghinstallation per distinct +// (installation, repo set) pair. +func (t *multiOrgTransport) scopedToken(ctx context.Context, org string, repos []string) (string, error) { + installID, err := t.installationIDForOrg(ctx, org) + if err != nil { + return "", err + } + repos = normalizeRepoNames(repos) + key := scopedKey(installID, repos) + + t.mu.RLock() + itr, ok := t.scoped[key] + t.mu.RUnlock() + if !ok { + itr = ghinstallation.NewFromAppsTransport(t.appsTr, installID) + itr.InstallationTokenOptions = &github.InstallationTokenOptions{ + Repositories: repos, + Permissions: apiReadPermissions(), + } + t.mu.Lock() + if existing, ok := t.scoped[key]; ok { + itr = existing + } else { + t.scoped[key] = itr + } + t.mu.Unlock() + } + return itr.Token(ctx) +} + +// normalizeRepoNames returns a sorted, deduplicated, lowercased copy +// of repos. +func normalizeRepoNames(repos []string) []string { + if len(repos) == 0 { + return nil + } + ret := make([]string, len(repos)) + for i, r := range repos { + ret[i] = strings.ToLower(r) + } + slices.Sort(ret) + return slices.Compact(ret) +} + +// scopedKey returns the scoped token cache key for the given +// installation ID and normalized repo names. +func scopedKey(installID int64, repos []string) string { + return fmt.Sprintf("%d:%s", installID, strings.Join(repos, ",")) +} + // findInstallationForOrg calls GET /orgs/{org}/installation using the // App JWT to discover the installation ID for the given org. func (t *multiOrgTransport) findInstallationForOrg(ctx context.Context, org string) (int64, error) { @@ -203,6 +305,7 @@ func (t *multiOrgTransport) discoverAllInstallations(ctx context.Context) (int, t.mu.Lock() t.orgTr[org] = itr + t.orgID[org] = inst.ID t.mu.Unlock() } return len(installations), nil diff --git a/github_test.go b/github_test.go index 6a1cf0a..7cd4a21 100644 --- a/github_test.go +++ b/github_test.go @@ -26,3 +26,14 @@ func TestOrgFromPath(t *testing.T) { } } } + +func TestScopedKey(t *testing.T) { + a := scopedKey(42, normalizeRepoNames([]string{"Corp", "tailscale", "corp"})) + b := scopedKey(42, normalizeRepoNames([]string{"tailscale", "corp"})) + if a != b || a != "42:corp,tailscale" { + t.Errorf("keys = %q, %q; want both %q", a, b, "42:corp,tailscale") + } + if got := scopedKey(42, normalizeRepoNames(nil)); got != "42:" { + t.Errorf("unscoped key = %q; want %q", got, "42:") + } +} diff --git a/githubapi.go b/githubapi.go new file mode 100644 index 0000000..ca4aa86 --- /dev/null +++ b/githubapi.go @@ -0,0 +1,442 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +package main + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "io" + "log" + "net/http" + "net/url" + "slices" + "strings" + + "github.com/vektah/gqlparser/v2/ast" + "github.com/vektah/gqlparser/v2/parser" + "tailscale.com/client/tailscale/apitype" +) + +// APIProxy is an HTTP handler that proxies read-only GitHub API +// requests on behalf of unauthenticated tailnet clients. It gives a +// caller the same read access to their granted repos that a read-only +// collaborator would have on the GitHub website: PRs, issues, +// comments, reviews, checks, statuses, and Actions runs and logs. +// +// It lays out its URLs like GitHub Enterprise Server so that the gh +// CLI and other GitHub clients can use it as a GHES host: REST at +// /api/v3/ and GraphQL at /api/graphql. +// +// Safety is layered. Only GET and HEAD under /api/v3/repos/{owner}/{repo}/ +// and GraphQL queries (not mutations) are accepted, the caller must +// have a pull grant on the repo, and the upstream request uses an +// installation token downscoped to read-only permissions and the +// caller's granted repos (see multiOrgTransport.scopedToken). +type APIProxy struct { + // Upstream is the GitHub API base URL. If empty, it defaults to + // "https://api.github.com". + Upstream string + + // Token returns a read-only GitHub token for the given lowercase + // org, restricted to the given bare repo names (or the whole + // installation if repos is empty). + Token func(ctx context.Context, org string, repos []string) (string, error) + + // WhoIs, if non-nil, identifies the tailnet peer at remoteAddr. + WhoIs func(ctx context.Context, remoteAddr string) (*apitype.WhoIsResponse, error) + + RequireGrants bool // if true, enforce tailnet grants for repo access + HTTPClient *http.Client // optional; defaults to http.DefaultClient's settings + Logf Logf // optional; defaults to log.Printf +} + +const ( + apiRESTPrefix = "/api/v3" + apiGraphQLPath = "/api/graphql" + + // maxRewriteBody is the largest JSON response body that is + // buffered to rewrite upstream API URLs. Larger bodies are + // streamed unmodified. + maxRewriteBody = 32 << 20 + + // maxGraphQLBody is the largest GraphQL request body accepted. + maxGraphQLBody = 1 << 20 +) + +// Register registers p's handlers on mux. +func (p *APIProxy) Register(mux *http.ServeMux) { + mux.HandleFunc(apiRESTPrefix+"/", p.serveREST) + mux.HandleFunc(apiRESTPrefix+"/meta", p.serveMeta) + mux.HandleFunc(apiGraphQLPath, p.serveGraphQL) +} + +func (p *APIProxy) logf(format string, args ...any) { + if p.Logf != nil { + p.Logf(format, args...) + } else { + log.Printf(format, args...) + } +} + +func (p *APIProxy) upstream() string { + if p.Upstream != "" { + return strings.TrimRight(p.Upstream, "/") + } + return "https://api.github.com" +} + +// httpClient returns the client for upstream requests. It follows +// redirects (Actions logs and artifacts redirect to blob storage that +// clients typically can't reach) but never sends the token to any +// host other than the upstream. +func (p *APIProxy) httpClient() *http.Client { + var c http.Client + if p.HTTPClient != nil { + c = *p.HTTPClient + } + up, _ := url.Parse(p.upstream()) + c.CheckRedirect = func(req *http.Request, via []*http.Request) error { + if len(via) >= 5 { + return errors.New("too many redirects") + } + if req.URL.Scheme != "https" && req.URL.Scheme != up.Scheme { + return fmt.Errorf("refusing redirect to %s URL", req.URL.Scheme) + } + if req.URL.Host != up.Host { + req.Header.Del("Authorization") + } + return nil + } + return &c +} + +// identify returns the caller's WhoIs (nil if unavailable) and a +// human-readable description for logs. +func (p *APIProxy) identify(r *http.Request) (*apitype.WhoIsResponse, string) { + if p.WhoIs == nil { + return nil, r.RemoteAddr + } + who, err := p.WhoIs(r.Context(), r.RemoteAddr) + if err != nil { + return nil, r.RemoteAddr + } + return who, formatWhoIs(r.RemoteAddr, who) +} + +func (p *APIProxy) deny(w http.ResponseWriter, r *http.Request, whoStr string, code int, msg string) { + counterAPIDenied.Add(1) + p.logf("ghapi: denied %s %s by %s: %s", r.Method, r.URL.Path, whoStr, msg) + apiError(w, code, msg) +} + +// apiError writes a GitHub-style JSON error response. +func apiError(w http.ResponseWriter, code int, msg string) { + w.Header().Set("Content-Type", "application/json; charset=utf-8") + w.WriteHeader(code) + json.NewEncoder(w).Encode(map[string]string{"message": msg}) +} + +// serveMeta serves a synthetic GHES /meta response. The gh CLI fetches +// it to learn the GHES version for feature detection; we claim a +// version new enough that gh uses the same features as on github.com. +func (p *APIProxy) serveMeta(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json; charset=utf-8") + json.NewEncoder(w).Encode(map[string]any{ + "installed_version": "3.99.0", + "verifiable_password_authentication": false, + }) +} + +// serveREST proxies GET and HEAD requests for +// /api/v3/repos/{owner}/{repo}/... to the upstream GitHub REST API. +func (p *APIProxy) serveREST(w http.ResponseWriter, r *http.Request) { + counterAPIRequests.Add(1) + who, whoStr := p.identify(r) + + if r.Method != "GET" && r.Method != "HEAD" { + p.deny(w, r, whoStr, http.StatusMethodNotAllowed, "rogitproxy: only GET and HEAD are allowed") + return + } + // Reject paths with escaped characters that decode differently + // (e.g. "%2F") so the path we check is exactly the path we forward. + if r.URL.RawPath != "" { + p.deny(w, r, whoStr, http.StatusBadRequest, "rogitproxy: escaped characters in path not supported") + return + } + rest := strings.TrimPrefix(r.URL.Path, apiRESTPrefix) + segs := strings.Split(strings.TrimPrefix(rest, "/"), "/") + if len(segs) < 3 || segs[0] != "repos" { + p.deny(w, r, whoStr, http.StatusNotFound, "rogitproxy: only /repos/{owner}/{repo}/... is supported") + return + } + for _, s := range segs { + if s == "." || s == ".." { + p.deny(w, r, whoStr, http.StatusBadRequest, "rogitproxy: invalid path") + return + } + } + owner, repo := segs[1], segs[2] + if !validRepoComponent(owner) || !validRepoComponent(repo) { + p.deny(w, r, whoStr, http.StatusBadRequest, "rogitproxy: invalid owner or repo") + return + } + if p.RequireGrants { + if who == nil { + p.deny(w, r, whoStr, http.StatusForbidden, "rogitproxy: unable to identify peer") + return + } + if !checkGrant(who, owner+"/"+repo, AccessPull) { + p.deny(w, r, whoStr, http.StatusForbidden, fmt.Sprintf("rogitproxy: no grant for %s/%s", owner, repo)) + return + } + } + + tok, err := p.Token(r.Context(), strings.ToLower(owner), []string{repo}) + if err != nil { + p.logf("ghapi: token for %s/%s: %v", owner, repo, err) + apiError(w, http.StatusBadGateway, "rogitproxy: failed to get GitHub token") + return + } + + target := p.upstream() + (&url.URL{Path: rest}).EscapedPath() + if r.URL.RawQuery != "" { + target += "?" + r.URL.RawQuery + } + req, err := http.NewRequestWithContext(r.Context(), r.Method, target, nil) + if err != nil { + apiError(w, http.StatusBadRequest, err.Error()) + return + } + copyHeaders(req.Header, r.Header, forwardRequestHeaders) + req.Header.Set("Authorization", "Bearer "+tok) + p.forward(w, r, req, whoStr) +} + +// serveGraphQL proxies GraphQL queries (but not mutations or +// subscriptions) to the upstream GitHub GraphQL API. +func (p *APIProxy) serveGraphQL(w http.ResponseWriter, r *http.Request) { + counterAPIRequests.Add(1) + who, whoStr := p.identify(r) + + if r.Method != "POST" { + p.deny(w, r, whoStr, http.StatusMethodNotAllowed, "rogitproxy: only POST is allowed") + return + } + body, err := io.ReadAll(io.LimitReader(r.Body, maxGraphQLBody+1)) + if err != nil { + apiError(w, http.StatusBadRequest, err.Error()) + return + } + if len(body) > maxGraphQLBody { + p.deny(w, r, whoStr, http.StatusRequestEntityTooLarge, "rogitproxy: request too large") + return + } + var gq struct { + Query string `json:"query"` + Variables map[string]any `json:"variables"` + } + if err := json.Unmarshal(body, &gq); err != nil { + p.deny(w, r, whoStr, http.StatusBadRequest, "rogitproxy: invalid JSON body") + return + } + if err := checkReadOnlyGraphQL(gq.Query); err != nil { + p.deny(w, r, whoStr, http.StatusForbidden, "rogitproxy: "+err.Error()) + return + } + + owner, _ := gq.Variables["owner"].(string) + org, repos, err := p.graphQLScope(who, strings.ToLower(owner)) + if err != nil { + p.deny(w, r, whoStr, http.StatusForbidden, "rogitproxy: "+err.Error()) + return + } + tok, err := p.Token(r.Context(), org, repos) + if err != nil { + p.logf("ghapi: graphql token for %s: %v", org, err) + apiError(w, http.StatusBadGateway, "rogitproxy: failed to get GitHub token") + return + } + + req, err := http.NewRequestWithContext(r.Context(), "POST", p.upstream()+"/graphql", bytes.NewReader(body)) + if err != nil { + apiError(w, http.StatusBadRequest, err.Error()) + return + } + copyHeaders(req.Header, r.Header, forwardRequestHeaders) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Authorization", "Bearer "+tok) + p.forward(w, r, req, whoStr) +} + +// graphQLScope returns the org and bare repo names that a GraphQL +// request's token should be scoped to. GraphQL requests don't name a +// single repo in their URL, so the token covers all of the caller's +// granted repos in one org: the "owner" variable's org if present +// (gh always sets it for repo queries), else the caller's first +// granted org. +func (p *APIProxy) graphQLScope(who *apitype.WhoIsResponse, owner string) (org string, repos []string, err error) { + if !p.RequireGrants { + if owner == "" { + return "", nil, errors.New(`GraphQL requests require an "owner" variable`) + } + return owner, nil, nil + } + if who == nil { + return "", nil, errors.New("unable to identify peer") + } + byOrg := map[string][]string{} + for _, full := range grantedRepos(who, AccessPull) { + o, r, ok := strings.Cut(strings.ToLower(full), "/") + if ok && o != "" && r != "" { + byOrg[o] = append(byOrg[o], r) + } + } + if len(byOrg) == 0 { + return "", nil, errors.New("no repo grants") + } + if owner == "" { + orgs := make([]string, 0, len(byOrg)) + for o := range byOrg { + orgs = append(orgs, o) + } + slices.Sort(orgs) + owner = orgs[0] + } + if len(byOrg[owner]) == 0 { + return "", nil, fmt.Errorf("no repo grants in org %q", owner) + } + return owner, byOrg[owner], nil +} + +// checkReadOnlyGraphQL returns an error if the GraphQL document q +// fails to parse or contains any operation other than a query. +func checkReadOnlyGraphQL(q string) error { + doc, err := parser.ParseQuery(&ast.Source{Input: q}) + if err != nil { + return fmt.Errorf("invalid GraphQL query: %v", err) + } + if len(doc.Operations) == 0 { + return errors.New("no GraphQL operations") + } + for _, op := range doc.Operations { + if op.Operation != ast.Query { + return fmt.Errorf("GraphQL %s operations are not allowed; only queries", op.Operation) + } + } + return nil +} + +// validRepoComponent reports whether s is a plausible GitHub owner or +// repo name. +func validRepoComponent(s string) bool { + if s == "" { + return false + } + for _, c := range s { + switch { + case 'a' <= c && c <= 'z', 'A' <= c && c <= 'Z', '0' <= c && c <= '9', c == '-', c == '_', c == '.': + default: + return false + } + } + return true +} + +// forwardRequestHeaders are the client request headers passed upstream. +// Notably, Authorization and Cookie are not. +var forwardRequestHeaders = []string{ + "Accept", + "If-Modified-Since", + "If-None-Match", + "Range", + "User-Agent", + "X-GitHub-Api-Version", +} + +// forwardResponseHeaders are the upstream response headers passed back +// to the client. Headers starting with "X-Ratelimit-" are also passed. +var forwardResponseHeaders = []string{ + "Cache-Control", + "Content-Disposition", + "Content-Range", + "Content-Type", + "Etag", + "Last-Modified", + "Link", + "Retry-After", + "X-Github-Media-Type", + "X-Github-Request-Id", +} + +func copyHeaders(dst, src http.Header, keys []string) { + for _, k := range keys { + if vv := src.Values(k); len(vv) > 0 { + dst[http.CanonicalHeaderKey(k)] = slices.Clone(vv) + } + } +} + +// forward sends req upstream and writes the response to w, rewriting +// upstream API URLs in the Link header and JSON bodies to point back +// at this proxy. +func (p *APIProxy) forward(w http.ResponseWriter, r *http.Request, req *http.Request, whoStr string) { + resp, err := p.httpClient().Do(req) + if err != nil { + p.logf("ghapi: %s %s by %s: %v", r.Method, r.URL.Path, whoStr, err) + apiError(w, http.StatusBadGateway, "rogitproxy: upstream error") + return + } + defer resp.Body.Close() + p.logf("ghapi: %s %s -> %d by %s", r.Method, r.URL.RequestURI(), resp.StatusCode, whoStr) + + scheme := "http" + if r.TLS != nil { + scheme = "https" + } + rw := strings.NewReplacer(p.upstream()+"/", scheme+"://"+r.Host+apiRESTPrefix+"/") + + copyHeaders(w.Header(), resp.Header, forwardResponseHeaders) + for k, vv := range resp.Header { + if strings.HasPrefix(k, "X-Ratelimit-") { + w.Header()[k] = slices.Clone(vv) + } + } + if link := w.Header().Get("Link"); link != "" { + w.Header().Set("Link", rw.Replace(link)) + } + + if !isJSON(resp.Header.Get("Content-Type")) || r.Method == "HEAD" { + if resp.ContentLength >= 0 { + w.Header().Set("Content-Length", fmt.Sprint(resp.ContentLength)) + } + w.WriteHeader(resp.StatusCode) + io.Copy(w, resp.Body) + return + } + + buf, err := io.ReadAll(io.LimitReader(resp.Body, maxRewriteBody+1)) + if err != nil { + apiError(w, http.StatusBadGateway, "rogitproxy: reading upstream response") + return + } + if len(buf) > maxRewriteBody { + // Too big to rewrite; stream it through unmodified. + w.WriteHeader(resp.StatusCode) + io.Copy(w, io.MultiReader(bytes.NewReader(buf), resp.Body)) + return + } + out := rw.Replace(string(buf)) + w.Header().Set("Content-Length", fmt.Sprint(len(out))) + w.WriteHeader(resp.StatusCode) + io.WriteString(w, out) +} + +func isJSON(contentType string) bool { + ct, _, _ := strings.Cut(contentType, ";") + ct = strings.TrimSpace(ct) + return ct == "application/json" || strings.HasSuffix(ct, "+json") +} diff --git a/githubapi_test.go b/githubapi_test.go new file mode 100644 index 0000000..29319dc --- /dev/null +++ b/githubapi_test.go @@ -0,0 +1,386 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +package main + +import ( + "context" + "encoding/json" + "fmt" + "io" + "net/http" + "net/http/httptest" + "slices" + "strings" + "sync" + "testing" + + "tailscale.com/client/tailscale/apitype" +) + +// tokenCall records a call to APIProxy.Token. +type tokenCall struct { + org string + repos []string +} + +// apiTestEnv is a test APIProxy in front of a fake upstream GitHub API. +type apiTestEnv struct { + proxy *httptest.Server + upstream *httptest.Server + blob *httptest.Server // redirect target, like Actions log storage + + mu sync.Mutex + tokenCalls []tokenCall + upReqs []*http.Request // requests seen by upstream + blobReqs []*http.Request // requests seen by blob +} + +func newAPITestEnv(t *testing.T, who *apitype.WhoIsResponse) *apiTestEnv { + t.Helper() + e := &apiTestEnv{} + + e.blob = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + e.mu.Lock() + e.blobReqs = append(e.blobReqs, r.Clone(context.Background())) + e.mu.Unlock() + w.Header().Set("Content-Type", "text/plain") + io.WriteString(w, "log line with "+e.upstream.URL+"/ untouched\n") + })) + t.Cleanup(e.blob.Close) + + e.upstream = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + e.mu.Lock() + e.upReqs = append(e.upReqs, r.Clone(context.Background())) + e.mu.Unlock() + base := "http://" + r.Host + switch { + case r.URL.Path == "/graphql": + body, _ := io.ReadAll(r.Body) + w.Header().Set("Content-Type", "application/json; charset=utf-8") + fmt.Fprintf(w, `{"data":{"echo":%q}}`, body) + case strings.HasSuffix(r.URL.Path, "/logs"): + http.Redirect(w, r, e.blob.URL+"/signed?sig=abc", http.StatusFound) + default: + w.Header().Set("Content-Type", "application/json; charset=utf-8") + w.Header().Set("Link", fmt.Sprintf(`<%s/repos/tailscale/corp/pulls?page=2>; rel="next"`, base)) + w.Header().Set("X-RateLimit-Remaining", "4999") + w.Header().Set("Set-Cookie", "secret=1") + fmt.Fprintf(w, `{"url":"%s/repos/tailscale/corp/pulls/1","html_url":"https://github.com/tailscale/corp/pull/1","path":%q,"query":%q}`, + base, r.URL.Path, r.URL.RawQuery) + } + })) + t.Cleanup(e.upstream.Close) + + p := &APIProxy{ + Upstream: e.upstream.URL, + Token: func(ctx context.Context, org string, repos []string) (string, error) { + e.mu.Lock() + defer e.mu.Unlock() + e.tokenCalls = append(e.tokenCalls, tokenCall{org, slices.Clone(repos)}) + return "scoped-token", nil + }, + Logf: t.Logf, + } + if who != nil { + p.RequireGrants = true + p.WhoIs = func(ctx context.Context, remoteAddr string) (*apitype.WhoIsResponse, error) { + return who, nil + } + } + mux := http.NewServeMux() + p.Register(mux) + e.proxy = httptest.NewServer(mux) + t.Cleanup(e.proxy.Close) + return e +} + +func (e *apiTestEnv) do(t *testing.T, method, path, body string) (*http.Response, string) { + t.Helper() + req, err := http.NewRequest(method, e.proxy.URL+path, strings.NewReader(body)) + if err != nil { + t.Fatal(err) + } + req.Header.Set("Authorization", "token client-token") + req.Header.Set("Cookie", "client=1") + req.Header.Set("Accept", "application/vnd.github+json") + res, err := http.DefaultClient.Do(req) + if err != nil { + t.Fatal(err) + } + defer res.Body.Close() + b, err := io.ReadAll(res.Body) + if err != nil { + t.Fatal(err) + } + return res, string(b) +} + +func corpWho() *apitype.WhoIsResponse { + return makeWho( + grantValue{Repos: []string{"tailscale/corp", "tailscale/tailscale"}, Access: AccessPull}, + grantValue{Repos: []string{"other/thing"}, Access: AccessPull}, + ) +} + +func TestAPIProxyREST(t *testing.T) { + e := newAPITestEnv(t, corpWho()) + + res, body := e.do(t, "GET", "/api/v3/repos/tailscale/corp/pulls?state=open&per_page=5", "") + if res.StatusCode != 200 { + t.Fatalf("status = %d; body: %s", res.StatusCode, body) + } + + var got struct { + URL string `json:"url"` + HTMLURL string `json:"html_url"` + Path string `json:"path"` + Query string `json:"query"` + } + if err := json.Unmarshal([]byte(body), &got); err != nil { + t.Fatalf("decoding %q: %v", body, err) + } + if want := e.proxy.URL + "/api/v3/repos/tailscale/corp/pulls/1"; got.URL != want { + t.Errorf("url = %q; want rewritten %q", got.URL, want) + } + if want := "https://github.com/tailscale/corp/pull/1"; got.HTMLURL != want { + t.Errorf("html_url = %q; want unchanged %q", got.HTMLURL, want) + } + if got.Path != "/repos/tailscale/corp/pulls" { + t.Errorf("upstream path = %q", got.Path) + } + if got.Query != "state=open&per_page=5" { + t.Errorf("upstream query = %q", got.Query) + } + if want := fmt.Sprintf(`<%s/api/v3/repos/tailscale/corp/pulls?page=2>; rel="next"`, e.proxy.URL); res.Header.Get("Link") != want { + t.Errorf("Link = %q; want %q", res.Header.Get("Link"), want) + } + if res.Header.Get("X-Ratelimit-Remaining") != "4999" { + t.Errorf("rate limit header not forwarded") + } + if res.Header.Get("Set-Cookie") != "" { + t.Errorf("Set-Cookie forwarded") + } + + up := e.upReqs[0] + if got, want := up.Header.Get("Authorization"), "Bearer scoped-token"; got != want { + t.Errorf("upstream Authorization = %q; want %q", got, want) + } + if up.Header.Get("Cookie") != "" { + t.Errorf("client Cookie forwarded upstream") + } + if up.Header.Get("Accept") != "application/vnd.github+json" { + t.Errorf("Accept not forwarded") + } + if want := []tokenCall{{"tailscale", []string{"corp"}}}; !slices.EqualFunc(e.tokenCalls, want, tokenCallEqual) { + t.Errorf("token calls = %v; want %v", e.tokenCalls, want) + } +} + +func tokenCallEqual(a, b tokenCall) bool { + return a.org == b.org && slices.Equal(a.repos, b.repos) +} + +func TestAPIProxyRESTDenied(t *testing.T) { + e := newAPITestEnv(t, corpWho()) + tests := []struct { + method, path string + want int + }{ + {"POST", "/api/v3/repos/tailscale/corp/issues/1/comments", 405}, + {"PATCH", "/api/v3/repos/tailscale/corp/pulls/1", 405}, + {"PUT", "/api/v3/repos/tailscale/corp/pulls/1/merge", 405}, + {"DELETE", "/api/v3/repos/tailscale/corp", 405}, + {"GET", "/api/v3/repos/tailscale/secret/pulls", 403}, + {"GET", "/api/v3/repos/tailscale", 404}, + {"GET", "/api/v3/user", 404}, + {"GET", "/api/v3/orgs/tailscale/members", 404}, + {"GET", "/api/v3/search/issues?q=foo", 404}, + {"GET", "/api/v3/repos/tailscale/corp%2F..%2Fsecret/pulls", 400}, + {"GET", "/api/v3/repos/tailscale/co%72p/pulls", 400}, + } + for _, tt := range tests { + res, body := e.do(t, tt.method, tt.path, "") + if res.StatusCode != tt.want { + t.Errorf("%s %s = %d; want %d; body: %s", tt.method, tt.path, res.StatusCode, tt.want, body) + } + } + if len(e.upReqs) != 0 { + t.Errorf("upstream saw %d requests; want 0", len(e.upReqs)) + } +} + +func TestAPIProxyRESTNoWhoIs(t *testing.T) { + e := newAPITestEnv(t, corpWho()) + // Simulate a WhoIs failure by making the proxy require grants with + // no WhoIs function configured. + p := &APIProxy{Upstream: e.upstream.URL, RequireGrants: true, Logf: t.Logf} + mux := http.NewServeMux() + p.Register(mux) + srv := httptest.NewServer(mux) + defer srv.Close() + res, err := http.Get(srv.URL + "/api/v3/repos/tailscale/corp/pulls") + if err != nil { + t.Fatal(err) + } + res.Body.Close() + if res.StatusCode != 403 { + t.Errorf("status = %d; want 403", res.StatusCode) + } +} + +func TestAPIProxyRedirectDropsToken(t *testing.T) { + e := newAPITestEnv(t, corpWho()) + res, body := e.do(t, "GET", "/api/v3/repos/tailscale/corp/actions/jobs/123/logs", "") + if res.StatusCode != 200 { + t.Fatalf("status = %d; body: %s", res.StatusCode, body) + } + // Non-JSON bodies are passed through without URL rewriting. + if want := "log line with " + e.upstream.URL + "/ untouched\n"; body != want { + t.Errorf("body = %q; want %q", body, want) + } + if len(e.blobReqs) != 1 { + t.Fatalf("blob saw %d requests; want 1", len(e.blobReqs)) + } + if got := e.blobReqs[0].Header.Get("Authorization"); got != "" { + t.Errorf("token leaked to redirect target: Authorization = %q", got) + } +} + +func TestAPIProxyMeta(t *testing.T) { + e := newAPITestEnv(t, corpWho()) + res, body := e.do(t, "GET", "/api/v3/meta", "") + if res.StatusCode != 200 { + t.Fatalf("status = %d", res.StatusCode) + } + var meta struct { + InstalledVersion string `json:"installed_version"` + } + if err := json.Unmarshal([]byte(body), &meta); err != nil || meta.InstalledVersion == "" { + t.Errorf("bad meta %q: %v", body, err) + } +} + +func graphQLBody(query string, vars map[string]any) string { + b, _ := json.Marshal(map[string]any{"query": query, "variables": vars}) + return string(b) +} + +func TestAPIProxyGraphQL(t *testing.T) { + const prQuery = `query PullRequestStatusChecks($owner: String!, $repo: String!, $number: Int!) { + repository(owner: $owner, name: $repo) { pullRequest(number: $number) { title } } + }` + + tests := []struct { + name string + body string + want int + wantToken *tokenCall + }{ + { + name: "query with owner", + body: graphQLBody(prQuery, map[string]any{"owner": "Tailscale", "repo": "corp", "number": 1}), + want: 200, + wantToken: &tokenCall{"tailscale", []string{"corp", "tailscale"}}, + }, + { + name: "query with other owner", + body: graphQLBody(prQuery, map[string]any{"owner": "other", "repo": "thing", "number": 1}), + want: 200, + wantToken: &tokenCall{"other", []string{"thing"}}, + }, + { + name: "query without owner uses first granted org", + body: graphQLBody(`query { Issue: __type(name: "Issue") { fields { name } } }`, nil), + want: 200, + wantToken: &tokenCall{"other", []string{"thing"}}, + }, + { + name: "query for ungranted org", + body: graphQLBody(prQuery, map[string]any{"owner": "evil", "repo": "x", "number": 1}), + want: 403, + }, + { + name: "mutation", + body: graphQLBody(`mutation { addComment(input: {subjectId: "x", body: "hi"}) { clientMutationId } }`, nil), + want: 403, + }, + { + name: "query then mutation", + body: graphQLBody(`query A { viewer { login } } mutation B { addStar(input: {starrableId: "x"}) { clientMutationId } }`, nil), + want: 403, + }, + { + name: "subscription", + body: graphQLBody(`subscription { foo }`, nil), + want: 403, + }, + { + name: "garbage", + body: graphQLBody(`not graphql {{{`, nil), + want: 403, + }, + { + name: "invalid json", + body: `{`, + want: 400, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + e := newAPITestEnv(t, corpWho()) + res, body := e.do(t, "POST", "/api/graphql", tt.body) + if res.StatusCode != tt.want { + t.Fatalf("status = %d; want %d; body: %s", res.StatusCode, tt.want, body) + } + if tt.wantToken == nil { + if len(e.upReqs) != 0 || len(e.tokenCalls) != 0 { + t.Errorf("upstream requests = %d, token calls = %d; want 0", len(e.upReqs), len(e.tokenCalls)) + } + return + } + if !slices.EqualFunc(e.tokenCalls, []tokenCall{*tt.wantToken}, tokenCallEqual) { + t.Errorf("token calls = %v; want %v", e.tokenCalls, *tt.wantToken) + } + if !strings.Contains(body, `"echo"`) { + t.Errorf("unexpected body %q", body) + } + if got := e.upReqs[0].Header.Get("Authorization"); got != "Bearer scoped-token" { + t.Errorf("upstream Authorization = %q", got) + } + }) + } + + t.Run("GET", func(t *testing.T) { + e := newAPITestEnv(t, corpWho()) + res, _ := e.do(t, "GET", "/api/graphql", "") + if res.StatusCode != 405 { + t.Errorf("status = %d; want 405", res.StatusCode) + } + }) +} + +func TestAPIProxyDevMode(t *testing.T) { + e := newAPITestEnv(t, nil) // no grants enforced + + res, body := e.do(t, "GET", "/api/v3/repos/tailscale/anything/pulls", "") + if res.StatusCode != 200 { + t.Fatalf("REST status = %d; body: %s", res.StatusCode, body) + } + res, body = e.do(t, "POST", "/api/graphql", graphQLBody(`query { viewer { login } }`, map[string]any{"owner": "tailscale"})) + if res.StatusCode != 200 { + t.Fatalf("GraphQL status = %d; body: %s", res.StatusCode, body) + } + res, _ = e.do(t, "POST", "/api/graphql", graphQLBody(`query { viewer { login } }`, nil)) + if res.StatusCode != 403 { + t.Errorf("GraphQL without owner status = %d; want 403", res.StatusCode) + } + want := []tokenCall{{"tailscale", []string{"anything"}}, {"tailscale", nil}} + if !slices.EqualFunc(e.tokenCalls, want, tokenCallEqual) { + t.Errorf("token calls = %v; want %v", e.tokenCalls, want) + } + res, _ = e.do(t, "POST", "/api/v3/repos/tailscale/anything/issues", "{}") + if res.StatusCode != 405 { + t.Errorf("POST in dev mode status = %d; want 405", res.StatusCode) + } +} diff --git a/go.mod b/go.mod index fb6115e..eb9d207 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,9 @@ go 1.26.1 require ( github.com/bradleyfalzon/ghinstallation/v2 v2.18.0 + github.com/google/go-github/v84 v84.0.0 github.com/tailscale/setec v0.0.0-20260310221408-dcd97e42f251 + github.com/vektah/gqlparser/v2 v2.5.64 golang.org/x/crypto v0.46.0 tailscale.com v1.96.1 ) @@ -38,7 +40,6 @@ require ( github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect github.com/google/btree v1.1.3 // indirect github.com/google/go-cmp v0.7.0 // indirect - github.com/google/go-github/v84 v84.0.0 // indirect github.com/google/go-querystring v1.2.0 // indirect github.com/google/uuid v1.6.0 // indirect github.com/hdevalence/ed25519consensus v0.2.0 // indirect diff --git a/go.sum b/go.sum index b8de602..a4f3d2b 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ filippo.io/mkcert v1.4.4 h1:8eVbbwfVlaqUM7OwuftKc2nuYOoTDQWqsoXmzoXZdbc= filippo.io/mkcert v1.4.4/go.mod h1:VyvOchVuAye3BoUsPUOOofKygVwLV2KQMVFJNRq+1dA= github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg= github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= +github.com/agnivade/levenshtein v1.2.2 h1:fI8RgiztYX1Nn5pBHHqQ9EMCCklhrmXqPRIedFmx5M4= +github.com/agnivade/levenshtein v1.2.2/go.mod h1:QVVI16kDrtSuwcpd0p1+xMC6Z/VfhtCyDIjcwga4/DU= github.com/akutz/memconn v0.1.0 h1:NawI0TORU4hcOMsMr11g7vwlCdkYeLKXBcxWu2W/P8A= github.com/akutz/memconn v0.1.0/go.mod h1:Jo8rI7m0NieZyLI5e2CDlRdRqRRB4S7Xp77ukDjH+Fw= github.com/alexbrainman/sspi v0.0.0-20231016080023-1a75b4708caa h1:LHTHcTQiSGT7VVbI0o4wBRNQIgn917usHWOd6VAffYI= @@ -169,6 +171,8 @@ github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0t github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc= github.com/safchain/ethtool v0.3.0 h1:gimQJpsI6sc1yIqP/y8GYgiXn/NjgvpM0RNoWLVVmP0= github.com/safchain/ethtool v0.3.0/go.mod h1:SA9BwrgyAqNo7M+uaL6IYbxpm5wk3L7Mm6ocLW+CJUs= +github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE= +github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg= github.com/tailscale/certstore v0.1.1-0.20231202035212-d3fa0460f47e h1:PtWT87weP5LWHEY//SWsYkSO3RWRZo4OSWagh3YD2vQ= github.com/tailscale/certstore v0.1.1-0.20231202035212-d3fa0460f47e/go.mod h1:XrBNfAFN+pwoWuksbFS9Ccxnopa15zJGgXRFN90l3K4= github.com/tailscale/go-winio v0.0.0-20231025203758-c4f33415bf55 h1:Gzfnfk2TWrk8Jj4P4c1a3CtQyMaTVCznlkLZI++hok4= @@ -199,10 +203,14 @@ github.com/u-root/u-root v0.14.0 h1:Ka4T10EEML7dQ5XDvO9c3MBN8z4nuSnGjcd1jmU2ivg= github.com/u-root/u-root v0.14.0/go.mod h1:hAyZorapJe4qzbLWlAkmSVCJGbfoU9Pu4jpJ1WMluqE= github.com/u-root/uio v0.0.0-20240224005618-d2acac8f3701 h1:pyC9PaHYZFgEKFdlp3G8RaCKgVpHZnecvArXvPXcFkM= github.com/u-root/uio v0.0.0-20240224005618-d2acac8f3701/go.mod h1:P3a5rG4X7tI17Nn3aOIAYr5HbIMukwXG0urG0WuL8OA= +github.com/vektah/gqlparser/v2 v2.5.64 h1:N32yM6hQ6Kd5RdjCCEJsUrbCebVoA9yLLMOIa88Mkk0= +github.com/vektah/gqlparser/v2 v2.5.64/go.mod h1:nILRSUbsWDTuf8hSmAgFxuYh0SaMZdkvl6JSEbkJX8g= github.com/vishvananda/netns v0.0.5 h1:DfiHV+j8bA32MFM7bfEunvT8IAqQ/NzSJHtcmW5zdEY= github.com/vishvananda/netns v0.0.5/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM= github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM= github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg= +go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw= +go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg= go4.org/mem v0.0.0-20240501181205-ae6ca9944745 h1:Tl++JLUCe4sxGu8cTpDzRLd3tN7US4hOxG5YpKCzkek= go4.org/mem v0.0.0-20240501181205-ae6ca9944745/go.mod h1:reUoABIJ9ikfM5sgtSF3Wushcza7+WeD01VB9Lirh3g= go4.org/netipx v0.0.0-20231129151722-fdeea329fbba h1:0b9z3AuHCjxk0x/opv64kcgZLBseWJUpBw5I82+2U4M= diff --git a/metrics.go b/metrics.go index a42a373..49b9adb 100644 --- a/metrics.go +++ b/metrics.go @@ -8,4 +8,6 @@ import "expvar" var ( counterNumRequests = expvar.NewInt("counter_num_requests") counterNumDenied = expvar.NewInt("counter_num_denied") + counterAPIRequests = expvar.NewInt("counter_api_requests") + counterAPIDenied = expvar.NewInt("counter_api_denied") ) diff --git a/rogitproxy.go b/rogitproxy.go index 5eec274..b3a1aaa 100644 --- a/rogitproxy.go +++ b/rogitproxy.go @@ -97,6 +97,7 @@ func main() { _, keyOnDisk := getGitHubAppPrivateKeyFromDisk() useGitHubTokenToBackend := *backend == "https://github.com" && (keyOnDisk || !*dev) + var apiProxy *APIProxy if useGitHubTokenToBackend { var setecDo func(*http.Request) (*http.Response, error) if srv != nil { @@ -114,8 +115,11 @@ func main() { log.Fatalf("no GitHub app installations found") } proxy.HTTPClient = &http.Client{Transport: tr} + apiProxy = &APIProxy{Token: tr.scopedToken} if srv != nil { proxy.RequireGrants = true + apiProxy.RequireGrants = true + apiProxy.WhoIs = proxy.LocalClient.WhoIs } log.Printf("github app auth enabled (app %d, %d org installations)", *gitHubAppID, n) } @@ -124,6 +128,9 @@ func main() { mux := http.NewServeMux() tsweb.Debugger(mux) mux.HandleFunc("/", proxy.serveIndex) + if apiProxy != nil { + apiProxy.Register(mux) + } if *dev { httpLn, err := net.Listen("tcp", "localhost:0") @@ -138,6 +145,14 @@ func main() { log.Fatalf("listen :80: %v", err) } go http.Serve(httpLn, mux) + + // Also serve HTTPS with the tailnet's TLS cert, as the gh CLI + // only speaks HTTPS to GitHub Enterprise Server hosts. + httpsLn, err := srv.ListenTLS("tcp", ":443") + if err != nil { + log.Fatalf("listen :443: %v", err) + } + go http.Serve(httpsLn, mux) } log.Fatal(proxy.Serve(ln)) @@ -326,7 +341,7 @@ func (p *GitProxy) handleConn(conn net.Conn) { counterNumRequests.Add(1) who := p.whoIs(conn) - whoStr := formatWhoIs(conn, who) + whoStr := formatWhoIs(conn.RemoteAddr().String(), who) if p.RequireGrants { if who == nil { @@ -377,9 +392,8 @@ func (p *GitProxy) whoIs(conn net.Conn) *apitype.WhoIsResponse { return who } -// formatWhoIs returns a human-readable string for the peer. -func formatWhoIs(conn net.Conn, who *apitype.WhoIsResponse) string { - addr := conn.RemoteAddr().String() +// formatWhoIs returns a human-readable string for the peer at addr. +func formatWhoIs(addr string, who *apitype.WhoIsResponse) string { if who == nil { return addr } @@ -419,6 +433,23 @@ func checkGrant(who *apitype.WhoIsResponse, repo string, access AccessType) bool return false } +// grantedRepos returns all repos (of the form "org/repo") that the +// peer identified by who has been granted the given access type to. +func grantedRepos(who *apitype.WhoIsResponse, access AccessType) []string { + var repos []string + for _, raw := range who.CapMap[grantCap] { + var g grantValue + if err := json.Unmarshal([]byte(raw), &g); err != nil { + continue + } + if g.Access != access { + continue + } + repos = append(repos, g.Repos...) + } + return repos +} + // handleSSH proxies via SSH. Since SSH runs git-upload-pack on the remote, // the wire protocol is identical to git:// — we just pipe the bidirectional // stream. No capability stripping or shallow workarounds needed. @@ -768,17 +799,7 @@ func (p *GitProxy) serveIndex(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, %s\n\n", who.UserProfile.LoginName) // Collect all repos the user has pull access to. - var repos []string - for _, raw := range who.CapMap[grantCap] { - var g grantValue - if err := json.Unmarshal([]byte(raw), &g); err != nil { - continue - } - if g.Access != AccessPull { - continue - } - repos = append(repos, g.Repos...) - } + repos := grantedRepos(who, AccessPull) if len(repos) == 0 { fmt.Fprintf(w, "You do not currently have access to any repositories through this proxy.\n") @@ -798,6 +819,16 @@ func (p *GitProxy) serveIndex(w http.ResponseWriter, r *http.Request) { if len(repos) > 0 { fmt.Fprintf(w, " git clone git://%s/%s.git\n", nodeName, repos[0]) } + + fmt.Fprintf(w, "\nRead-only GitHub API access (PRs, issues, checks, Actions logs) for\n") + fmt.Fprintf(w, "these repos is available with no credentials, laid out like GitHub\n") + fmt.Fprintf(w, "Enterprise Server:\n\n") + fmt.Fprintf(w, " REST: http://%s/api/v3/repos/%s/...\n", nodeName, repos[0]) + fmt.Fprintf(w, " GraphQL: http://%s/api/graphql (queries only)\n", nodeName) + fmt.Fprintf(w, "\nTo use the gh CLI (HTTPS; the token value is ignored):\n\n") + fmt.Fprintf(w, " export GH_HOST=%s GH_ENTERPRISE_TOKEN=unused\n", nodeName) + fmt.Fprintf(w, " gh pr checks -R %s/%s 123\n", nodeName, repos[0]) + fmt.Fprintf(w, "\nInside a git:// checkout from this proxy, -R is not needed.\n") } // --- pkt-line encoding/decoding --- diff --git a/rogitproxy_test.go b/rogitproxy_test.go index 124d500..703b797 100644 --- a/rogitproxy_test.go +++ b/rogitproxy_test.go @@ -422,20 +422,21 @@ func TestSSHRejectPush(t *testing.T) { t.Logf("ssh push rejection: %s", resp) } -func TestCheckGrant(t *testing.T) { - makeWho := func(grants ...grantValue) *apitype.WhoIsResponse { - var raw []tailcfg.RawMessage - for _, g := range grants { - b, _ := json.Marshal(g) - raw = append(raw, tailcfg.RawMessage(b)) - } - return &apitype.WhoIsResponse{ - Node: &tailcfg.Node{}, - UserProfile: &tailcfg.UserProfile{}, - CapMap: tailcfg.PeerCapMap{grantCap: raw}, - } +// makeWho returns a WhoIsResponse with the given rogitproxy grants. +func makeWho(grants ...grantValue) *apitype.WhoIsResponse { + var raw []tailcfg.RawMessage + for _, g := range grants { + b, _ := json.Marshal(g) + raw = append(raw, tailcfg.RawMessage(b)) + } + return &apitype.WhoIsResponse{ + Node: &tailcfg.Node{}, + UserProfile: &tailcfg.UserProfile{}, + CapMap: tailcfg.PeerCapMap{grantCap: raw}, } +} +func TestCheckGrant(t *testing.T) { tests := []struct { name string who *apitype.WhoIsResponse