Skip to content

Commit 368352d

Browse files
ztzgMic92
authored andcommitted
http-binary-cache-store: Add 'ssl-cert' and 'ssl-key' settings
Those are set via the store's URI, e.g.: https://substituter.invalid?ssl-cert=/path/to/cert.pem&ssl-key=/path/to/key.pem
1 parent 041d237 commit 368352d

File tree

5 files changed

+46
-2
lines changed

5 files changed

+46
-2
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
synopsis: Support substituters using mTLS (client certificate) authentication
3+
issues: []
4+
prs: [13030]
5+
---
6+
7+
Added support for `ssl-cert` and `ssl-key` options in substituter URLs.
8+
9+
Example:
10+
11+
https://substituter.invalid?ssl-cert=/path/to/cert.pem&ssl-key=/path/to/key.pem
12+
13+
When these options are configured, Nix will use this certificate/private key pair to authenticate to the server.

src/libstore/filetransfer.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,12 @@ struct curlFileTransfer : public FileTransfer
410410
if (writtenToSink)
411411
curl_easy_setopt(req, CURLOPT_RESUME_FROM_LARGE, writtenToSink);
412412

413+
if (!request.sslCert.empty())
414+
curl_easy_setopt(req, CURLOPT_SSLCERT, request.sslCert.c_str());
415+
416+
if (!request.sslKey.empty())
417+
curl_easy_setopt(req, CURLOPT_SSLKEY, request.sslKey.c_str());
418+
413419
curl_easy_setopt(req, CURLOPT_ERRORBUFFER, errbuf);
414420
errbuf[0] = 0;
415421

src/libstore/http-binary-cache-store.cc

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,28 @@ class HttpBinaryCacheStore :
152152

153153
FileTransferRequest makeRequest(const std::string & path)
154154
{
155-
return FileTransferRequest(
156-
hasPrefix(path, "https://") || hasPrefix(path, "http://") || hasPrefix(path, "file://")
155+
bool absolute = hasPrefix(path, "https://") || hasPrefix(path, "http://") || hasPrefix(path, "file://");
156+
157+
FileTransferRequest request(
158+
absolute
157159
? path
158160
: config->cacheUri + "/" + path);
159161

162+
if (!absolute) {
163+
Path sslCert = config->sslCert.get();
164+
if (!sslCert.empty()) {
165+
debug("configuring SSL client certificate '%s' for '%s'", sslCert, request.uri);
166+
request.sslCert = sslCert;
167+
}
168+
169+
Path sslKey = config->sslKey.get();
170+
if (!sslKey.empty()) {
171+
debug("configuring SSL client certificate key '%s' for '%s'", sslKey, request.uri);
172+
request.sslKey = sslKey;
173+
}
174+
}
175+
176+
return request;
160177
}
161178

162179
void getFile(const std::string & path, Sink & sink) override

src/libstore/include/nix/store/filetransfer.hh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ struct FileTransferRequest
6565
std::string uri;
6666
Headers headers;
6767
std::string expectedETag;
68+
Path sslCert;
69+
Path sslKey;
6870
bool verifyTLS = true;
6971
bool head = false;
7072
bool post = false;

src/libstore/include/nix/store/http-binary-cache-store.hh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ struct HttpBinaryCacheStoreConfig : std::enable_shared_from_this<HttpBinaryCache
1313

1414
Path cacheUri;
1515

16+
const Setting<std::string> sslCert{
17+
this, "", "ssl-cert", "An optional SSL client certificate in PEM format; see CURLOPT_SSLCERT."};
18+
19+
const Setting<std::string> sslKey{
20+
this, "", "ssl-key", "The SSL client certificate key in PEM format; see CURLOPT_SSLKEY."};
21+
1622
static const std::string name()
1723
{
1824
return "HTTP Binary Cache Store";

0 commit comments

Comments
 (0)