Skip to content

Commit e8314e6

Browse files
authored
Merge pull request #13460 from m4dc4p/m4dc4p/handle-expired-tokens
Better Handling for Expired Credentials
2 parents 03bf582 + f786c0b commit e8314e6

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ namespace nix {
3737
struct S3Error : public Error
3838
{
3939
Aws::S3::S3Errors err;
40+
Aws::String exceptionName;
4041

4142
template<typename... Args>
42-
S3Error(Aws::S3::S3Errors err, const Args & ... args)
43-
: Error(args...), err(err) { };
43+
S3Error(Aws::S3::S3Errors err, Aws::String exceptionName, const Args & ... args)
44+
: Error(args...), err(err), exceptionName(exceptionName) { };
4445
};
4546

4647
/* Helper: given an Outcome<R, E>, return R in case of success, or
@@ -51,6 +52,7 @@ R && checkAws(std::string_view s, Aws::Utils::Outcome<R, E> && outcome)
5152
if (!outcome.IsSuccess())
5253
throw S3Error(
5354
outcome.GetError().GetErrorType(),
55+
outcome.GetError().GetExceptionName(),
5456
fmt(
5557
"%s: %s (request id: %s)",
5658
s,
@@ -226,7 +228,13 @@ S3Helper::FileTransferResult S3Helper::getObject(
226228

227229
} catch (S3Error & e) {
228230
if ((e.err != Aws::S3::S3Errors::NO_SUCH_KEY) &&
229-
(e.err != Aws::S3::S3Errors::ACCESS_DENIED)) throw;
231+
(e.err != Aws::S3::S3Errors::ACCESS_DENIED) &&
232+
// Expired tokens are not really an error, more of a caching problem. Should be treated same as 403.
233+
//
234+
// AWS unwilling to provide a specific error type for the situation (https://github.com/aws/aws-sdk-cpp/issues/1843)
235+
// so use this hack
236+
(e.exceptionName != "ExpiredToken")
237+
) throw;
230238
}
231239

232240
auto now2 = std::chrono::steady_clock::now();
@@ -334,6 +342,10 @@ struct S3BinaryCacheStoreImpl : virtual S3BinaryCacheStore
334342
auto & error = res.GetError();
335343
if (error.GetErrorType() == Aws::S3::S3Errors::RESOURCE_NOT_FOUND
336344
|| error.GetErrorType() == Aws::S3::S3Errors::NO_SUCH_KEY
345+
// Expired tokens are not really an error, more of a caching problem. Should be treated same as 403.
346+
// AWS unwilling to provide a specific error type for the situation (https://github.com/aws/aws-sdk-cpp/issues/1843)
347+
// so use this hack
348+
|| (error.GetErrorType() == Aws::S3::S3Errors::UNKNOWN && error.GetExceptionName() == "ExpiredToken")
337349
// If bucket listing is disabled, 404s turn into 403s
338350
|| error.GetErrorType() == Aws::S3::S3Errors::ACCESS_DENIED)
339351
return false;

0 commit comments

Comments
 (0)