Skip to content

Commit 9d7a2dd

Browse files
committed
refactor(libstore): move preResolveS3Credentials into aws-creds
1 parent 64a9cf2 commit 9d7a2dd

File tree

5 files changed

+33
-39
lines changed

5 files changed

+33
-39
lines changed

src/libstore/aws-creds.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
#if NIX_WITH_S3_SUPPORT
44

5+
# include "nix/store/s3-url.hh"
56
# include "nix/util/finally.hh"
67
# include "nix/util/logging.hh"
78
# include "nix/util/sync.hh"
9+
# include "nix/util/url.hh"
810
# include "nix/util/util.hh"
911

1012
# include <aws/crt/Api.h>
@@ -221,6 +223,28 @@ void clearAwsCredentialsCache()
221223
credentialProviderCache.clear();
222224
}
223225

226+
std::optional<AwsCredentials> preResolveS3Credentials(const std::string & url)
227+
{
228+
try {
229+
auto parsedUrl = parseURL(url);
230+
if (parsedUrl.scheme != "s3") {
231+
return std::nullopt;
232+
}
233+
234+
auto s3Url = ParsedS3URL::parse(parsedUrl);
235+
std::string profile = s3Url.profile.value_or("");
236+
237+
// Get credentials (automatically cached)
238+
return getAwsCredentials(profile);
239+
} catch (const AwsAuthError & e) {
240+
debug("Failed to pre-resolve S3 credentials: %s", e.what());
241+
return std::nullopt;
242+
} catch (const std::exception & e) {
243+
debug("Error pre-resolving S3 credentials: %s", e.what());
244+
return std::nullopt;
245+
}
246+
}
247+
224248
} // namespace nix
225249

226250
#endif // NIX_WITH_S3_SUPPORT

src/libstore/filetransfer.cc

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -911,30 +911,6 @@ struct curlFileTransfer : public FileTransfer
911911

912912
enqueueItem(std::make_shared<TransferItem>(*this, request, std::move(callback)));
913913
}
914-
915-
#if NIX_WITH_S3_SUPPORT
916-
std::optional<AwsCredentials> preResolveS3Credentials(const std::string & url) override
917-
{
918-
try {
919-
auto parsedUrl = parseURL(url);
920-
if (parsedUrl.scheme != "s3") {
921-
return std::nullopt;
922-
}
923-
924-
auto s3Url = ParsedS3URL::parse(parsedUrl);
925-
std::string profile = s3Url.profile.value_or("");
926-
927-
// Get credentials (automatically cached)
928-
return getAwsCredentials(profile);
929-
} catch (const AwsAuthError & e) {
930-
debug("Failed to pre-resolve S3 credentials: %s", e.what());
931-
return std::nullopt;
932-
} catch (const std::exception & e) {
933-
debug("Error pre-resolving S3 credentials: %s", e.what());
934-
return std::nullopt;
935-
}
936-
}
937-
#endif
938914
};
939915

940916
ref<curlFileTransfer> makeCurlFileTransfer()

src/libstore/include/nix/store/aws-creds.hh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ void invalidateAwsCredentials(const std::string & profile);
6262
*/
6363
void clearAwsCredentialsCache();
6464

65+
/**
66+
* Pre-resolve AWS credentials for S3 URLs.
67+
* Used to cache credentials in parent process before forking.
68+
* Returns credentials if URL is S3 and credentials are available.
69+
* Returns nullopt if URL is not S3 or credentials cannot be resolved.
70+
*/
71+
std::optional<AwsCredentials> preResolveS3Credentials(const std::string & url);
72+
6573
} // namespace nix
6674

6775
#endif // NIX_WITH_S3_SUPPORT

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,19 +165,6 @@ struct FileTransfer
165165
*/
166166
virtual void enqueueFileTransfer(const FileTransferRequest & request, Callback<FileTransferResult> callback) = 0;
167167

168-
#ifdef NIX_WITH_S3_SUPPORT
169-
/**
170-
* Pre-resolve AWS credentials for S3 URLs.
171-
* Used to cache credentials in parent process before forking.
172-
* Returns nullopt if URL is not S3 or credentials cannot be resolved.
173-
*/
174-
virtual std::optional<AwsCredentials> preResolveS3Credentials(const std::string & url)
175-
{
176-
// Default implementation returns nothing
177-
return std::nullopt;
178-
}
179-
#endif
180-
181168
std::future<FileTransferResult> enqueueFileTransfer(const FileTransferRequest & request);
182169

183170
/**

src/libstore/unix/build/derivation-builder.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,7 @@ class DerivationBuilderImpl : public DerivationBuilder, public DerivationBuilder
212212
auto s3Url = ParsedS3URL::parse(parsedUrl);
213213

214214
// Get credentials from the parent process's cached provider
215-
auto ft = getFileTransfer();
216-
auto creds = ft->preResolveS3Credentials(url->second);
215+
auto creds = preResolveS3Credentials(url->second);
217216

218217
if (creds) {
219218
preResolvedAwsCredentials = AwsCredentialsForBuilder{

0 commit comments

Comments
 (0)