Skip to content

Commit 9ff9c3f

Browse files
committed
Add support for s3:// URIs
This adds support for s3:// URIs in all places where Nix allows URIs, e.g. in builtins.fetchurl, builtins.fetchTarball, <nix/fetchurl.nix> and NIX_PATH. It allows fetching resources from private S3 buckets, using credentials obtained from the standard places (i.e. AWS_* environment variables, ~/.aws/credentials and the EC2 metadata server). This may not be super-useful in general, but since we already depend on aws-sdk-cpp, it's a cheap feature to add.
1 parent 62ff5ad commit 9ff9c3f

File tree

5 files changed

+142
-63
lines changed

5 files changed

+142
-63
lines changed

src/libstore/download.cc

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "hash.hh"
55
#include "store-api.hh"
66
#include "archive.hh"
7+
#include "s3.hh"
78

89
#include <unistd.h>
910
#include <fcntl.h>
@@ -480,6 +481,31 @@ struct CurlDownloader : public Downloader
480481
std::function<void(const DownloadResult &)> success,
481482
std::function<void(std::exception_ptr exc)> failure) override
482483
{
484+
/* Ugly hack to support s3:// URIs. */
485+
if (hasPrefix(request.uri, "s3://")) {
486+
// FIXME: do this on a worker thread
487+
sync2async<DownloadResult>(success, failure, [&]() {
488+
#ifdef ENABLE_S3
489+
S3Helper s3Helper;
490+
auto slash = request.uri.find('/', 5);
491+
if (slash == std::string::npos)
492+
throw nix::Error("bad S3 URI ‘%s’", request.uri);
493+
std::string bucketName(request.uri, 5, slash - 5);
494+
std::string key(request.uri, slash + 1);
495+
// FIXME: implement ETag
496+
auto s3Res = s3Helper.getObject(bucketName, key);
497+
DownloadResult res;
498+
if (!s3Res.data)
499+
throw DownloadError(NotFound, fmt("S3 object ‘%s’ does not exist", request.uri));
500+
res.data = s3Res.data;
501+
return res;
502+
#else
503+
throw nix::Error("cannot download ‘%s’ because Nix is not built with S3 support", request.uri);
504+
#endif
505+
});
506+
return;
507+
}
508+
483509
auto item = std::make_shared<DownloadItem>(*this, request);
484510
item->success = success;
485511
item->failure = failure;
@@ -629,7 +655,7 @@ bool isUri(const string & s)
629655
size_t pos = s.find("://");
630656
if (pos == string::npos) return false;
631657
string scheme(s, 0, pos);
632-
return scheme == "http" || scheme == "https" || scheme == "file" || scheme == "channel" || scheme == "git";
658+
return scheme == "http" || scheme == "https" || scheme == "file" || scheme == "channel" || scheme == "git" || scheme == "s3";
633659
}
634660

635661

src/libstore/download.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct DownloadRequest
2323

2424
struct DownloadResult
2525
{
26-
bool cached;
26+
bool cached = false;
2727
std::string etag;
2828
std::string effectiveUrl;
2929
std::shared_ptr<std::string> data;

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

Lines changed: 80 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#if ENABLE_S3
2-
#if __linux__
32

3+
#include "s3.hh"
44
#include "s3-binary-cache-store.hh"
55
#include "nar-info.hh"
66
#include "nar-info-disk-cache.hh"
@@ -18,15 +18,6 @@
1818

1919
namespace nix {
2020

21-
struct istringstream_nocopy : public std::stringstream
22-
{
23-
istringstream_nocopy(const std::string & s)
24-
{
25-
rdbuf()->pubsetbuf(
26-
(char *) s.data(), s.size());
27-
}
28-
};
29-
3021
struct S3Error : public Error
3122
{
3223
Aws::S3::S3Errors err;
@@ -60,21 +51,81 @@ static void initAWS()
6051
});
6152
}
6253

54+
S3Helper::S3Helper()
55+
: config(makeConfig())
56+
, client(make_ref<Aws::S3::S3Client>(*config))
57+
{
58+
}
59+
60+
ref<Aws::Client::ClientConfiguration> S3Helper::makeConfig()
61+
{
62+
initAWS();
63+
auto res = make_ref<Aws::Client::ClientConfiguration>();
64+
res->region = Aws::Region::US_EAST_1; // FIXME: make configurable
65+
res->requestTimeoutMs = 600 * 1000;
66+
return res;
67+
}
68+
69+
S3Helper::DownloadResult S3Helper::getObject(
70+
const std::string & bucketName, const std::string & key)
71+
{
72+
debug("fetching ‘s3://%s/%s’...", bucketName, key);
73+
74+
auto request =
75+
Aws::S3::Model::GetObjectRequest()
76+
.WithBucket(bucketName)
77+
.WithKey(key);
78+
79+
request.SetResponseStreamFactory([&]() {
80+
return Aws::New<std::stringstream>("STRINGSTREAM");
81+
});
82+
83+
DownloadResult res;
84+
85+
auto now1 = std::chrono::steady_clock::now();
86+
87+
try {
88+
89+
auto result = checkAws(fmt("AWS error fetching ‘%s’", key),
90+
client->GetObject(request));
91+
92+
res.data = std::make_shared<std::string>(
93+
dynamic_cast<std::stringstream &>(result.GetBody()).str());
94+
95+
} catch (S3Error & e) {
96+
if (e.err != Aws::S3::S3Errors::NO_SUCH_KEY) throw;
97+
}
98+
99+
auto now2 = std::chrono::steady_clock::now();
100+
101+
res.durationMs = std::chrono::duration_cast<std::chrono::milliseconds>(now2 - now1).count();
102+
103+
return res;
104+
}
105+
106+
#if __linux__
107+
108+
struct istringstream_nocopy : public std::stringstream
109+
{
110+
istringstream_nocopy(const std::string & s)
111+
{
112+
rdbuf()->pubsetbuf(
113+
(char *) s.data(), s.size());
114+
}
115+
};
116+
63117
struct S3BinaryCacheStoreImpl : public S3BinaryCacheStore
64118
{
65119
std::string bucketName;
66120

67-
ref<Aws::Client::ClientConfiguration> config;
68-
ref<Aws::S3::S3Client> client;
69-
70121
Stats stats;
71122

123+
S3Helper s3Helper;
124+
72125
S3BinaryCacheStoreImpl(
73126
const Params & params, const std::string & bucketName)
74127
: S3BinaryCacheStore(params)
75128
, bucketName(bucketName)
76-
, config(makeConfig())
77-
, client(make_ref<Aws::S3::S3Client>(*config))
78129
{
79130
diskCache = getNarInfoDiskCache();
80131
}
@@ -84,31 +135,22 @@ struct S3BinaryCacheStoreImpl : public S3BinaryCacheStore
84135
return "s3://" + bucketName;
85136
}
86137

87-
ref<Aws::Client::ClientConfiguration> makeConfig()
88-
{
89-
initAWS();
90-
auto res = make_ref<Aws::Client::ClientConfiguration>();
91-
res->region = Aws::Region::US_EAST_1; // FIXME: make configurable
92-
res->requestTimeoutMs = 600 * 1000;
93-
return res;
94-
}
95-
96138
void init() override
97139
{
98140
if (!diskCache->cacheExists(getUri(), wantMassQuery_, priority)) {
99141

100142
/* Create the bucket if it doesn't already exists. */
101143
// FIXME: HeadBucket would be more appropriate, but doesn't return
102144
// an easily parsed 404 message.
103-
auto res = client->GetBucketLocation(
145+
auto res = s3Helper.client->GetBucketLocation(
104146
Aws::S3::Model::GetBucketLocationRequest().WithBucket(bucketName));
105147

106148
if (!res.IsSuccess()) {
107149
if (res.GetError().GetErrorType() != Aws::S3::S3Errors::NO_SUCH_BUCKET)
108150
throw Error(format("AWS error checking bucket ‘%s’: %s") % bucketName % res.GetError().GetMessage());
109151

110152
checkAws(format("AWS error creating bucket ‘%s’") % bucketName,
111-
client->CreateBucket(
153+
s3Helper.client->CreateBucket(
112154
Aws::S3::Model::CreateBucketRequest()
113155
.WithBucket(bucketName)
114156
.WithCreateBucketConfiguration(
@@ -146,7 +188,7 @@ struct S3BinaryCacheStoreImpl : public S3BinaryCacheStore
146188
{
147189
stats.head++;
148190

149-
auto res = client->HeadObject(
191+
auto res = s3Helper.client->HeadObject(
150192
Aws::S3::Model::HeadObjectRequest()
151193
.WithBucket(bucketName)
152194
.WithKey(path));
@@ -179,7 +221,7 @@ struct S3BinaryCacheStoreImpl : public S3BinaryCacheStore
179221
auto now1 = std::chrono::steady_clock::now();
180222

181223
auto result = checkAws(format("AWS error uploading ‘%s’") % path,
182-
client->PutObject(request));
224+
s3Helper.client->PutObject(request));
183225

184226
auto now2 = std::chrono::steady_clock::now();
185227

@@ -198,42 +240,18 @@ struct S3BinaryCacheStoreImpl : public S3BinaryCacheStore
198240
sync2async<std::shared_ptr<std::string>>(success, failure, [&]() {
199241
debug(format("fetching ‘s3://%1%/%2%’...") % bucketName % path);
200242

201-
auto request =
202-
Aws::S3::Model::GetObjectRequest()
203-
.WithBucket(bucketName)
204-
.WithKey(path);
205-
206-
request.SetResponseStreamFactory([&]() {
207-
return Aws::New<std::stringstream>("STRINGSTREAM");
208-
});
209-
210243
stats.get++;
211244

212-
try {
213-
214-
auto now1 = std::chrono::steady_clock::now();
215-
216-
auto result = checkAws(format("AWS error fetching ‘%s’") % path,
217-
client->GetObject(request));
218-
219-
auto now2 = std::chrono::steady_clock::now();
245+
auto res = s3Helper.getObject(bucketName, path);
220246

221-
auto res = dynamic_cast<std::stringstream &>(result.GetBody()).str();
247+
stats.getBytes += res.data ? res.data->size() : 0;
248+
stats.getTimeMs += res.durationMs;
222249

223-
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now2 - now1).count();
250+
if (res.data)
251+
printTalkative("downloaded ‘s3://%s/%s’ (%d bytes) in %d ms",
252+
bucketName, path, res.data->size(), res.durationMs);
224253

225-
printMsg(lvlTalkative, format("downloaded ‘s3://%1%/%2%’ (%3% bytes) in %4% ms")
226-
% bucketName % path % res.size() % duration);
227-
228-
stats.getBytes += res.size();
229-
stats.getTimeMs += duration;
230-
231-
return std::make_shared<std::string>(res);
232-
233-
} catch (S3Error & e) {
234-
if (e.err == Aws::S3::S3Errors::NO_SUCH_KEY) return std::shared_ptr<std::string>();
235-
throw;
236-
}
254+
return res.data;
237255
});
238256
}
239257

@@ -246,7 +264,7 @@ struct S3BinaryCacheStoreImpl : public S3BinaryCacheStore
246264
debug(format("listing bucket ‘s3://%s’ from key ‘%s’...") % bucketName % marker);
247265

248266
auto res = checkAws(format("AWS error listing bucket ‘%s’") % bucketName,
249-
client->ListObjects(
267+
s3Helper.client->ListObjects(
250268
Aws::S3::Model::ListObjectsRequest()
251269
.WithBucket(bucketName)
252270
.WithDelimiter("/")
@@ -281,7 +299,8 @@ static RegisterStoreImplementation regStore([](
281299
return store;
282300
});
283301

302+
#endif
303+
284304
}
285305

286306
#endif
287-
#endif

src/libstore/s3.hh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#pragma once
2+
3+
#if ENABLE_S3
4+
5+
#include "ref.hh"
6+
7+
namespace Aws { namespace Client { class ClientConfiguration; } }
8+
namespace Aws { namespace S3 { class S3Client; } }
9+
10+
namespace nix {
11+
12+
struct S3Helper
13+
{
14+
ref<Aws::Client::ClientConfiguration> config;
15+
ref<Aws::S3::S3Client> client;
16+
17+
S3Helper();
18+
19+
ref<Aws::Client::ClientConfiguration> makeConfig();
20+
21+
struct DownloadResult
22+
{
23+
std::shared_ptr<std::string> data;
24+
unsigned int durationMs;
25+
};
26+
27+
DownloadResult getObject(
28+
const std::string & bucketName, const std::string & key);
29+
};
30+
31+
}
32+
33+
#endif

src/libutil/logging.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ extern Verbosity verbosity; /* suppress msgs > this */
7878

7979
#define printError(args...) printMsg(lvlError, args)
8080
#define printInfo(args...) printMsg(lvlInfo, args)
81+
#define printTalkative(args...) printMsg(lvlTalkative, args)
8182
#define debug(args...) printMsg(lvlDebug, args)
8283
#define vomit(args...) printMsg(lvlVomit, args)
8384

0 commit comments

Comments
 (0)