v0.22.0 4c072b1

api.go RawHistoryFolder

1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3// Package rpc serves the ermite API contract (bnema.dev/ermite-api) with
4// ConnectRPC: handlers that map the messages of the contract to the use cases
5// of usecase and back, the interceptor that authenticates callers, and the error
6// mapping.
7package rpc
8
9import (
10	"context"
11	"net/http"
12	"net/netip"
13	"slices"
14
15	"connectrpc.com/connect"
16	"github.com/bnema/zerowrap"
17
18	"bnema.dev/ermite-api/gen/ermite/ermiteconnect"
19	"bnema.dev/ermite/internal/clientlimit"
20	"bnema.dev/ermite/internal/domain"
21	"bnema.dev/ermite/internal/ports"
22)
23
24// MaxRequestBytes bounds the size of a request message. Responses are not
25// bounded: a diff has its own limits and blobs are streamed.
26const MaxRequestBytes = 1 << 20
27
28// RunnerMaxRequestBytes bounds the size of a request message of RunnerService
29// and JobArtifactService: a job summary of exactly 1 MiB (UploadSummary) and a
30// block message with 1 MiB of data must be accepted, so the limit leaves room
31// for the envelope (job id, step number, block id, framing).
32const RunnerMaxRequestBytes = MaxRequestBytes + 64<<10
33
34// Features are the optional capabilities of the contract that this server
35// always implements, as GetVersion lists them. FeaturePasskeys comes on top
36// when Config.Passkeys is set, and FeatureCISecrets when the settings services
37// are.
38var Features = []string{"repo.update", "events.resume", "merge_requests", "merge_requests.delete_branch", "signing_key", "session", "git.list_paths", "token.branch_prefixes", "ci.runs", "ci.runs.filters", "ci.runners", "ci.actions", "ci.annotations", "ci.summaries", "ci.concurrency", "ci.dispatch", "ci.cache"}
39
40// FeaturePasskeys is listed by GetVersion when passkeys are available.
41// FeatureCIArtifacts (artifact.go) when the artifact services are served.
42const FeaturePasskeys = "passkeys"
43
44// FeatureCIOIDC is listed by GetVersion when the server issues OIDC ID tokens
45// to CI jobs (Job.id_token_url, ServerService.RotateOIDCKey).
46const FeatureCIOIDC = "ci.oidc"
47
48// Config configures the API.
49type Config struct {
50	Auth   ports.AuthService
51	Repos  ports.RepoService
52	Tokens ports.TokenService
53	Browse ports.BrowseService
54	Events ports.EventService
55	// MergeRequests serves MergeRequestService, SigningKey GetSigningKey.
56	MergeRequests ports.MergeRequestService
57	SigningKey    ports.SigningKeyService
58	Version       string
59	// TrustedProxies are the networks allowed to set X-Forwarded-For.
60	TrustedProxies []netip.Prefix
61	// Limiter counts failed authentications; nil builds a default one. Share
62	// it with the other adapters that authenticate, so that a client has one
63	// budget of failures. Leave it unset rather than assigning a typed nil
64	// pointer (a nil *T in the interface is not nil, and would be called).
65	Limiter ports.FailureLimiter
66	// Runner serves RunnerService and Runs serves RunService; without them the
67	// CI services are not served. Leave them unset rather than assigning a
68	// typed nil pointer.
69	Runner ports.RunnerService
70	Runs   ports.RunService
71	// Sessions are the owner's web sessions; nil serves no SessionService.
72	// Leave it unset rather than assigning a typed nil pointer (a nil *T in
73	// the interface is not nil, and the service would be served and called).
74	Sessions ports.SessionService
75	// Passkeys serves ListPasskeys and RemovePasskey; nil means passkeys are
76	// not available (the RPCs answer failed_precondition and GetVersion does
77	// not list "passkeys"). Same rule as Sessions about typed nil pointers.
78	Passkeys ports.PasskeyService
79	// Secrets, Variables and Environments serve SecretService, VariableService
80	// and EnvironmentService, and GetVersion lists "ci.secrets", when all three
81	// are set (they are one use case). Same rule as Sessions about typed nil
82	// pointers.
83	Secrets      ports.SecretService
84	Variables    ports.VariableService
85	Environments ports.EnvironmentService
86	// OIDCKeys rotates the key of the ID tokens of CI jobs; nil when the server
87	// issues none (GetVersion then does not list "ci.oidc"). Same rule as
88	// Sessions about typed nil pointers.
89	OIDCKeys ports.OIDCKeyService
90	// JobArtifacts and Artifacts serve JobArtifactService and ArtifactService,
91	// and GetVersion lists "ci.artifacts", when both are set and Runner is.
92	// Same rule as Sessions about typed nil pointers.
93	JobArtifacts ports.JobArtifactService
94	Artifacts    ports.ArtifactService
95}
96
97// API is the set of Connect handlers of the contract.
98type API struct {
99	handler http.Handler
100}
101
102// New builds the API.
103func New(cfg Config) *API {
104	if cfg.Limiter == nil {
105		cfg.Limiter = clientlimit.NewLimiter(nil)
106	}
107	opts := []connect.HandlerOption{
108		connect.WithInterceptors(newInterceptor(cfg)),
109		connect.WithReadMaxBytes(MaxRequestBytes),
110		connect.WithRecover(func(ctx context.Context, spec connect.Spec, _ http.Header, panicValue any) error {
111			log := zerowrap.FromCtx(ctx)
112			log.Error().Interface("panic", panicValue).Str("procedure", spec.Procedure).Msg("api handler panicked")
113			return connect.NewError(connect.CodeInternal, errInternal)
114		}),
115	}
116	mux := http.NewServeMux()
117	mux.Handle(ermiteconnect.NewRepoServiceHandler(&repoService{cfg.Repos}, opts...))
118	mux.Handle(ermiteconnect.NewGitServiceHandler(&gitService{cfg.Browse}, opts...))
119	mux.Handle(ermiteconnect.NewTokenServiceHandler(&tokenService{cfg.Tokens}, opts...))
120	mux.Handle(ermiteconnect.NewEventServiceHandler(&eventService{cfg.Events}, opts...))
121	features := slices.Clone(Features)
122	if cfg.Passkeys != nil {
123		features = append(features, FeaturePasskeys)
124	}
125	settings := cfg.Secrets != nil && cfg.Variables != nil && cfg.Environments != nil
126	if settings {
127		features = append(features, FeatureCISecrets)
128	}
129	artifacts := cfg.JobArtifacts != nil && cfg.Artifacts != nil && cfg.Runner != nil
130	if artifacts {
131		features = append(features, FeatureCIArtifacts)
132	}
133	if cfg.OIDCKeys != nil {
134		features = append(features, FeatureCIOIDC)
135	}
136	mux.Handle(ermiteconnect.NewServerServiceHandler(&serverService{version: cfg.Version, features: features, key: cfg.SigningKey, oidc: cfg.OIDCKeys}, opts...))
137	mux.Handle(ermiteconnect.NewMergeRequestServiceHandler(&mergeRequestService{cfg.MergeRequests}, opts...))
138	if cfg.Sessions != nil {
139		mux.Handle(ermiteconnect.NewSessionServiceHandler(&sessionService{sessions: cfg.Sessions, passkeys: cfg.Passkeys}, opts...))
140	}
141	if settings {
142		mux.Handle(ermiteconnect.NewSecretServiceHandler(&secretService{cfg.Secrets}, opts...))
143		mux.Handle(ermiteconnect.NewVariableServiceHandler(&variableService{cfg.Variables}, opts...))
144		mux.Handle(ermiteconnect.NewEnvironmentServiceHandler(&environmentService{cfg.Environments}, opts...))
145	}
146	if cfg.Runner != nil {
147		runnerOpts := append(slices.Clone(opts), connect.WithReadMaxBytes(RunnerMaxRequestBytes))
148		runners := &runnerService{runners: cfg.Runner, limiter: cfg.Limiter}
149		mux.Handle(ermiteconnect.NewRunnerServiceHandler(runners, runnerOpts...))
150		if artifacts {
151			// A block message carries up to 1 MiB of data, like a summary.
152			mux.Handle(ermiteconnect.NewJobArtifactServiceHandler(&jobArtifactService{artifacts: cfg.JobArtifacts, runner: runners}, runnerOpts...))
153			mux.Handle(ermiteconnect.NewArtifactServiceHandler(&artifactService{artifacts: cfg.Artifacts}, opts...))
154		}
155	}
156	if cfg.Runs != nil {
157		mux.Handle(ermiteconnect.NewRunServiceHandler(&runService{runs: cfg.Runs}, opts...))
158	}
159	return &API{handler: mux}
160}
161
162// Handler returns the handler of all services. Its paths are the procedure
163// paths of the contract ("/ermite.RepoService/ListRepos"); httpserver mounts
164// it under /-/api/ and strips the prefix.
165func (a *API) Handler() http.Handler { return a.handler }
166
167// caller returns the principal the interceptor put in ctx; anonymous when
168// there is none.
169func caller(ctx context.Context) domain.Principal {
170	p, _ := domain.PrincipalFromContext(ctx)
171	return p
172}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 2

Tags 45

↑↓ moveenter openesc close