Skip to content

Commit

Permalink
Add a "profile" option to S3BinaryCacheStore
Browse files Browse the repository at this point in the history
This allows specifying the AWS configuration profile to use. E.g.

  nix copy --from s3://my-cache?profile=aws-dev-account /nix/store/cf3isrlqavvd5w7rpky1fa8j9lcnlggm-...
  • Loading branch information
edolstra committed Nov 15, 2017
1 parent 897ca33 commit 8956ae1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/libstore/download.cc
Expand Up @@ -533,7 +533,7 @@ struct CurlDownloader : public Downloader
// FIXME: do this on a worker thread
sync2async<DownloadResult>(success, failure, [&]() -> DownloadResult {
#ifdef ENABLE_S3
S3Helper s3Helper(Aws::Region::US_EAST_1); // FIXME: make configurable
S3Helper s3Helper("", Aws::Region::US_EAST_1); // FIXME: make configurable
auto slash = request.uri.find('/', 5);
if (slash == std::string::npos)
throw nix::Error("bad S3 URI '%s'", request.uri);
Expand Down
15 changes: 12 additions & 3 deletions src/libstore/s3-binary-cache-store.cc
Expand Up @@ -10,6 +10,8 @@
#include "istringstream_nocopy.hh"

#include <aws/core/Aws.h>
#include <aws/core/auth/AWSCredentialsProvider.h>
#include <aws/core/auth/AWSCredentialsProviderChain.h>
#include <aws/core/client/ClientConfiguration.h>
#include <aws/core/client/DefaultRetryStrategy.h>
#include <aws/core/utils/logging/FormattedLogSystem.h>
Expand Down Expand Up @@ -77,9 +79,15 @@ static void initAWS()
});
}

S3Helper::S3Helper(const string & region)
S3Helper::S3Helper(const std::string & profile, const std::string & region)
: config(makeConfig(region))
, client(make_ref<Aws::S3::S3Client>(*config, true, false))
, client(make_ref<Aws::S3::S3Client>(
profile == ""
? std::dynamic_pointer_cast<Aws::Auth::AWSCredentialsProvider>(
std::make_shared<Aws::Auth::DefaultAWSCredentialsProviderChain>())
: std::dynamic_pointer_cast<Aws::Auth::AWSCredentialsProvider>(
std::make_shared<Aws::Auth::ProfileConfigFileAWSCredentialsProvider>(profile.c_str())),
*config, true, false))
{
}

Expand Down Expand Up @@ -148,6 +156,7 @@ S3Helper::DownloadResult S3Helper::getObject(

struct S3BinaryCacheStoreImpl : public S3BinaryCacheStore
{
const Setting<std::string> profile{this, "", "profile", "The name of the AWS configuration profile to use."};
const Setting<std::string> region{this, Aws::Region::US_EAST_1, "region", {"aws-region"}};
const Setting<std::string> narinfoCompression{this, "", "narinfo-compression", "compression method for .narinfo files"};
const Setting<std::string> lsCompression{this, "", "ls-compression", "compression method for .ls files"};
Expand All @@ -163,7 +172,7 @@ struct S3BinaryCacheStoreImpl : public S3BinaryCacheStore
const Params & params, const std::string & bucketName)
: S3BinaryCacheStore(params)
, bucketName(bucketName)
, s3Helper(region)
, s3Helper(profile, region)
{
diskCache = getNarInfoDiskCache();
}
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/s3.hh
Expand Up @@ -14,7 +14,7 @@ struct S3Helper
ref<Aws::Client::ClientConfiguration> config;
ref<Aws::S3::S3Client> client;

S3Helper(const std::string & region);
S3Helper(const std::string & profile, const std::string & region);

ref<Aws::Client::ClientConfiguration> makeConfig(const std::string & region);

Expand Down

0 comments on commit 8956ae1

Please sign in to comment.