Add client certificate verification for Gitaly and Praefect TLS listeners
Gitaly and Praefect can verify client certificates on their TLS listeners and present one on their outbound dials. Both are opt-in: client_auth defaults to none, so existing configurations behave as before.
client_auth takes none, verify_if_given or require, checked against client_ca_path. The outbound pair is client_certificate_path and client_key_path.
This is one MR because require on a node rejects Praefect and its peer nodes unless they present the outbound pair, so the listener and the pair only work together. Production code is about 576 lines and tests about 3,230. I can split the pair into a follow-up MR, keeping the listener at none by default, if you'd prefer.
This relates to gitlab#634055 and gitlab#634060 and closes neither. @pks-gitlab proposed client-side certificates to assert a client's identity.
flowchart LR
subgraph IN["Inbound: Gitaly and Praefect tls_listen_addr"]
C["Client"] -->|"TLS handshake"| L["Listener"]
L -->|"client_auth none"| T["Token check"]
L -->|"verify_if_given: a certificate, if sent, must chain to client_ca_path"| T
L -->|"require: a certificate that chains to client_ca_path"| T
T --> R["RPC"]
end
subgraph OUT["Outbound: the pair in [tls] client_certificate_path and client_key_path"]
G["Gitaly process"] -->|"connection pool, read at startup"| GG["Other Gitaly nodes and Praefect"]
G -->|"paths in GITALY_TLS_CLIENT_CERT_FILE and _KEY_FILE"| S["gitaly-ssh, per run"]
S --> GG
B["gitaly-backup, same variables per run"] --> GG
PF["Praefect: node dialer, health checks, subcommands"] --> GN["Gitaly nodes"]
end
X["Plaintext listen_addr and the unix socket"] -.->|"not covered: no client certificate is asked for"| RNot in this MR
From 634055:
- Rejections aren't logged or counted by Gitaly beyond gRPC's transport errors.
- The Gitaly TLS documentation page isn't updated.
- The CA isn't reloaded without a
hup.
From 634060:
- There's no client-side CA variable (
GITALY_CLIENT_CA_PATH); servers are verified against the system pool. - Workhorse and gitlab-shell aren't changed.
Divergences from the requirements
- Gitaly's environment variables are
GITALY_TLS_CLIENT_CERT_FILEandGITALY_TLS_CLIENT_KEY_FILE, which differ from the names in 634060. - The mode is spelled
verify_if_given, where 634055 writesverify-if-given, to match the other keys in[tls]. - Gitaly's outbound pair is its own and never falls back to
certificate_pathandkey_path, where 634060 defaults to the server pair. - Restart-free rotation is out of scope.
Claims, with the code and the test that backs each
Code citations are at 8f83d601. Each test runs a client with and without a certificate, and the none arm is the control for the others.
- Each mode maps to a Go verification mode, and
ClientCAsholds only the file named byclient_ca_path, never the system pool:internal/gitaly/config/config.go:480-501. Tests:TestTLS_ConfigureClientAuth,TestGitalyServerFactory_clientAuth(no certificate, wrong CA, trusted CA, for all three modes). - Expired, not-yet-valid and server-only certificates are refused; a certificate with no extended key usage is accepted:
TestGitalyServerFactory_clientAuthCertificates. - Gitaly applies this to its external listener at
internal/gitaly/server/server.go:108, and Praefect to its secure listener atinternal/praefect/server_factory.go:93. The token check still runs after the handshake (TestGitalyServerFactory_clientAuthWithToken), and sidechannel connections present the certificate (TestGitalyServerFactory_clientAuthSidechannel). - Startup refuses
requirewith a plaintextlisten_addr, a verifying mode withouttls_listen_addr, a missing or unreadable CA, and a half-set or mismatched pair:config.go:356-462, withTestTLS_Validate_clientCertificate,TestTLS_ValidateListeners,TestCfg_Validate_clientAuthListeners,TestCfg_ValidateV2_clientAuthListenersand, for Praefect,TestConfigValidation. - These checks run from
Cfg.Validateas well asValidateV2, because V2 failures only warn at startup (internal/cli/gitaly/serve.go:177) and these settings are new and opt-in, so failing closed breaks no existing configuration. - The dialer reads the certificate only in its secure branch, so
unix:dials are unchanged. Servers are verified against the system pool before the certificate is sent:internal/grpc/client/dial.go:144-162, 230-242. Tests:TestWithClientCertificateinclient/dial_test.goandinternal/grpc/client/dial_test.go. client.WithClientCertificateis exported for Workhorse and gitlab-shell, which dial through the public package. Once each is changed, it lets them present a certificate ontls://addresses without building their own credentials, whichWithTransportCredentialswould also replace forunix:addresses.- Gitaly presents the pair from its connection pool and its local subcommands, and passes it to gitaly-ssh as paths in
GITALY_TLS_CLIENT_CERT_FILEandGITALY_TLS_CLIENT_KEY_FILE. gitaly-backup reads the same two variables, which the operator sets where it runs (doc/gitaly-backup.md). Both fail closed on a half-set pair or an unreadable file, naming the variable, and receive paths, never key material:internal/grpc/client/client_certificate.go,internal/git/gitcmd/command_options.go:269,cmd/gitaly-ssh/main.go:145-152,internal/cli/gitalybackup/pool.go:15-28. Tests:TestCertificateFromEnvironment,TestClientCertificate(gitaly-ssh),TestCreateSubcommand_clientCertificate,TestRestoreSubcommand_clientCertificate. - Praefect presents its own pair from its node dialer, health checks and subcommands:
internal/praefect/nodes/manager.go:149,195,217,nodes/ping.go:87,247,internal/cli/praefect/serve.go:292. Tests:TestDial_clientCertificateand theinternal/cli/praefectclientCertificatetests. - On an Omnibus GitLab install,
requireon Praefect is refused at startup until the plaintexttcp://127.0.0.1:2305listener is removed. - On a FIPS-mode host,
TestFIPSTLSHandshakepasses 6 of 6 with a client certificate, and a non-FIPS sibling is the control. The host also rejects an RSA-1024 client certificate. - On a Gitaly Cluster with one Praefect and two Gitaly nodes, a client certificate under
requireadds about 201 µs to the handshake at p50, about 12%, against 4 to 11 µs between identical runs, and nothing to a warm RPC. - Every commit at 8f83d601 builds and passes
go vet, and theverifyjob, which runsgolangci-lint, passed on that tree. On a private copy of this branch, every Gitaly CI job that runs by default passes, including the Praefect, race, PgBouncer and SHA-256 jobs. The manual FIPS, nightly and macOS jobs were not run.
Operator steps and rotation
Rails (lib/gitlab/gitaly_client.rb:259 in gitlab-org/gitlab passes root CAs only), Workhorse and gitlab-shell present no client certificate yet, so each needs its own change. Today require fits Gitaly nodes that only Praefect and other Gitaly nodes reach. Leave Praefect's listener at none or verify_if_given until those clients can present a certificate. With require on Praefect, startup refuses a plaintext listen_addr such as Omnibus GitLab's tcp://127.0.0.1:2305.
client_certificate_path must hold the leaf and any intermediates up to a CA in the server's client_ca_path. A leaf-only file issued by an intermediate sends no certificate when client_ca_path holds only the root, and the server fails the handshake as "certificate required".
The CA file and the pool's pair are read at startup and on hup. gitaly-ssh and gitaly-backup read the pair on each run, so swapping in a pair the peer rejects fails CreateFork and the fetch step of ReplicateRepository at once, while the pool keeps presenting the old pair until hup. Replace both files together, then hup.
What this does not do
- It does not check revocation.
- It does not authorize an identity: any certificate that chains to
client_ca_pathis accepted. - It does not cover the plaintext listener or the unix socket.
- Rails, Workhorse and gitlab-shell do not present a client certificate.
- A misspelled
[tls]key is ignored, like any other unknown configuration key. - A node that is not set to
requireaccepts relayed requests, so arequirenode is only as strong as the nodes that reach it. - Outbound dials do not pin the server beyond the system pool.
- It does not reload certificates without a
hup.
An AI coding assistant wrote this change and ran the checks above on test VMs.