Files
artifactapi/internal/provider/rpm/github_auth_test.go
T
unkinben 109ba2ce27
ci/woodpecker/tag/docker Pipeline was successful
feat: server-level GitHub machine credential for authenticated requests (#109)
## Why

Anonymous GitHub is capped at 60 requests/hour and cannot read private repositories. A machine credential usable by a free (non-enterprise) account is needed to lift the request budget to ~5000/hr and to read private-repo release assets.

Builds on the background syncer (#108, now merged to `master`); this diff is the auth changes only.

## How

- Add `internal/githubauth`: a process-wide GitHub credential delivered via env/secret, applied by default to every outbound GitHub request (releases scan, ranged asset-header GETs, and the generic-github byte proxy for private assets).
- Support two modes:
  - **PAT** — `GITHUB_TOKEN` sent as `Authorization: Bearer <token>`.
  - **GitHub App** — `GITHUB_APP_ID` + `GITHUB_APP_INSTALLATION_ID` + private key (`GITHUB_APP_PRIVATE_KEY` inline PEM or `GITHUB_APP_PRIVATE_KEY_PATH`). Mint a short-lived RS256 JWT with stdlib `crypto/rsa` (no new dependency), exchange it at `POST /app/installations/{id}/access_tokens` for a ~1h installation token, cache it, and single-flight a refresh a few minutes before expiry.
- Inject at the two GitHub call paths: the rpm github provider header builder (releases + ranged fetches) and the generic provider `AuthHeaders` (byte proxy, github.com hosts only; the pre-signed `objects.githubusercontent.com` redirect deliberately gets no Authorization).
- Honor precedence: a remote's own `username`/`password` overrides the server credential; no credential configured stays anonymous (current behavior).
- Fail closed at startup on partial App configuration (e.g. App id without a private key); a token-and-App conflict is also rejected.
- Never persist the credential to the DB, return it from an API, or log it (token-exchange failures never echo the response body).
- Read config via the existing `getenv` convention; document PAT vs App setup, the free-account fine-grained PAT scopes (Contents:read + Metadata:read), precedence, and the rate-limit implication.

## Rate limit

Authenticated requests share the syncer's single global limiter — no second limiter is added. A token raises the effective GitHub ceiling (~5000/hr vs ~60/hr), so the limiter defaults stay safe.

## Tests

`internal/githubauth` and `internal/provider/{rpm,generic}`:
- PAT attaches the correct `Authorization` header to releases + asset-header requests.
- App mints a valid RS256 JWT (verified against the app public key), exchanges it at a mocked endpoint, reuses the cached token without re-exchanging, refreshes near expiry, and single-flights concurrent callers.
- Per-remote credential overrides the server credential (rpm + generic).
- No credential → no `Authorization` header, requests still succeed anonymously.
- ETag/304 flow still works with auth attached.
- The credential does not appear in a remote's serialized JSON.
- Config validation: no-config is anonymous; partial App config and token/App conflict both error.

Verified fail-before/pass-after for the injection tests. `gofmt -l`, `go build ./...`, `go vet ./...`, `go test ./...` all clean (26 packages).

Reviewed-on: #109
Co-authored-by: Ben Vincent <ben@unkin.net>
Co-committed-by: Ben Vincent <ben@unkin.net>
2026-08-10 21:42:39 +10:00

135 lines
4.3 KiB
Go

package rpm
import (
"context"
"encoding/json"
"strings"
"testing"
"git.unkin.net/unkin/artifactapi/internal/githubauth"
"git.unkin.net/unkin/artifactapi/pkg/models"
)
// staticCred is a test Credential yielding a fixed token.
type staticCred string
func (s staticCred) Token(context.Context) (string, error) { return string(s), nil }
func TestGitHubServerCredentialAttachedToReleasesAndAssets(t *testing.T) {
fx := newGitHubFixture(t, true)
p := newTestProvider()
p.serverCred = staticCred("ghp_server_secret")
store := newFakeStore()
if err := p.scan(context.Background(), fx.remote(), store); err != nil {
t.Fatalf("scan: %v", err)
}
if got := fx.releaseAuth; got != "Bearer ghp_server_secret" {
t.Fatalf("releases Authorization = %q, want Bearer ghp_server_secret", got)
}
if got := fx.assetAuth; got != "Bearer ghp_server_secret" {
t.Fatalf("asset Authorization = %q, want Bearer ghp_server_secret", got)
}
}
func TestGitHubPerRemoteCredentialOverridesServer(t *testing.T) {
fx := newGitHubFixture(t, true)
p := newTestProvider()
p.serverCred = staticCred("ghp_server_secret")
store := newFakeStore()
remote := fx.remote()
remote.Password = "ghp_remote_wins"
if err := p.scan(context.Background(), remote, store); err != nil {
t.Fatalf("scan: %v", err)
}
if got := fx.releaseAuth; got != "Bearer ghp_remote_wins" {
t.Fatalf("releases Authorization = %q, want per-remote token to win", got)
}
if got := fx.assetAuth; got != "Bearer ghp_remote_wins" {
t.Fatalf("asset Authorization = %q, want per-remote token to win", got)
}
}
func TestGitHubNoCredentialSendsNoAuthHeader(t *testing.T) {
fx := newGitHubFixture(t, true)
p := newTestProvider() // serverCred nil, package Server() unset in unit tests
store := newFakeStore()
if err := p.scan(context.Background(), fx.remote(), store); err != nil {
t.Fatalf("scan: %v", err)
}
if fx.releaseAuth != "" {
t.Fatalf("expected no Authorization header, got %q", fx.releaseAuth)
}
if fx.assetAuth != "" {
t.Fatalf("expected no asset Authorization header, got %q", fx.assetAuth)
}
// Requests still succeed anonymously.
if rows, _ := store.ListRPMMetadataEntries(context.Background(), "acme-rpm"); len(rows) != 1 {
t.Fatalf("anonymous scan should still derive metadata, got %d rows", len(rows))
}
}
func TestGitHubETag304FlowWithAuth(t *testing.T) {
fx := newGitHubFixture(t, true)
fx.etag = `"v1"`
p := newTestProvider()
p.serverCred = staticCred("ghp_server_secret")
store := newFakeStore()
etag, changed, err := p.scanWithState(context.Background(), fx.remote(), store, "")
if err != nil {
t.Fatalf("first scan: %v", err)
}
if !changed || etag != `"v1"` {
t.Fatalf("first scan changed=%v etag=%q, want true and \"v1\"", changed, etag)
}
// Re-scan with the captured ETag: a 304 means no change and no asset fetch.
etag2, changed2, err := p.scanWithState(context.Background(), fx.remote(), store, etag)
if err != nil {
t.Fatalf("second scan: %v", err)
}
if changed2 {
t.Fatal("expected no change on 304")
}
if etag2 != `"v1"` {
t.Fatalf("etag = %q, want preserved \"v1\"", etag2)
}
if fx.notModHit != 1 {
t.Fatalf("expected exactly one 304 response, got %d", fx.notModHit)
}
// The conditional request still carried the credential.
if fx.releaseAuth != "Bearer ghp_server_secret" {
t.Fatalf("conditional request Authorization = %q, want the server credential", fx.releaseAuth)
}
}
// TestGitHubCredentialAbsentFromRemoteJSON asserts the server credential never
// appears in a remote's serialized API representation, and per-remote secrets
// stay redacted by the models.Remote json:"-" tags.
func TestGitHubCredentialAbsentFromRemoteJSON(t *testing.T) {
githubauth.SetServer(staticCred("ghp_super_secret_server_token"))
t.Cleanup(func() { githubauth.SetServer(nil) })
remote := models.Remote{
Name: "acme-rpm",
PackageType: models.PackageGitHubRPM,
BaseURL: "https://api.github.com/repos/acme/tools",
Username: "per_remote_user",
Password: "per_remote_secret",
}
b, err := json.Marshal(remote)
if err != nil {
t.Fatalf("marshal remote: %v", err)
}
js := string(b)
for _, secret := range []string{"ghp_super_secret_server_token", "per_remote_secret", "per_remote_user"} {
if strings.Contains(js, secret) {
t.Fatalf("credential %q leaked into remote JSON: %s", secret, js)
}
}
}