Repository navigation
Share S3 credentials provider across Context and fix anonymous-fallback caching - #297
BrianMichell wants to merge 5 commits into
Conversation
- Only wrap leaf credential providers (Environment, Imds, Profile, EcsRole) in MakeCache in MakeAwsCredentialsProvider; MakeDefault already wraps its internal chain in aws_credentials_provider_new_cached, whereas wrapping the outer [MakeDefault, MakeAnonymous] chain in MakeCache permanently cached UINT64_MAX-expiration anonymous credentials after any initial fallback. - Guard AsProvider, MakeCache, and MakeDefaultWithAnonymous against null aws_credentials_provider pointers. - Add DefaultDoesNotCacheAnonymousFallback unit test.
- Change AwsCredentialsResource from config_only = true to own the live AwsCredentialsProvider in AwsCredentialsResource::Resource, created once per Context resource in AwsCredentialsResource::Create. - Use data_.aws_credentials->credentials_provider in S3KeyValueStoreSpec::DoOpen instead of creating a new provider per open. - Add unit tests verifying provider sharing within a Context and across parent/child Contexts.
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
4e409a1 to
f3e4980
Compare
…s-share-and-cache
748dd8e to
c8b88ff
Compare
| @@ -73,6 +74,7 @@ AwsCredentialsProvider MakeDefaultWithAnonymous( | |||
|
|
|||
| auto default_provider = MakeDefault(profile_name_override); | |||
There was a problem hiding this comment.
auto default_provider = MakeDefault(profile_name_override);
if (!default_provider) return nullptr;
auto anonymous_provider = MakeAnonymous();
if (!anonymous_provider) return nullptr;
| return (err == 0) | ||
| ? absl::InternalError("Failed to create credentials provider") | ||
| : absl::InternalError( | ||
| absl::StrCat("Failed to create credentials provider", |
There was a problem hiding this comment.
absl::StrCat("Failed to create credentials provider: ",
| } | ||
| }; | ||
|
|
||
| (void)internal_aws::GetAwsAllocator(); // Force AWS library initialization. |
There was a problem hiding this comment.
pre-existing: there may be existing errors that should be cleared.
After GetAwsAllocator() add:
aws_reset_error();
| EXPECT_EQ(creds->GetAccessKeyId(), "AKIDEXAMPLE"); | ||
| EXPECT_EQ(creds->GetSecretAccessKey(), "secretexample"); | ||
| } | ||
| } |
There was a problem hiding this comment.
Add
// Subsequent changes to the environment remain cached by the default chain.
SetEnv("AWS_ACCESS_KEY_ID", "AKIDSECOND");
SetEnv("AWS_SECRET_ACCESS_KEY", "secretsecond");
{
auto creds = GetAwsCredentials(provider.get()).result();
ASSERT_THAT(creds, IsOk());
EXPECT_FALSE(creds->IsAnonymous());
EXPECT_EQ(creds->GetAccessKeyId(), "AKIDEXAMPLE");
EXPECT_EQ(creds->GetSecretAccessKey(), "secretexample");
}
| S3KeyValueStoreSpec> { | ||
| public: | ||
| S3KeyValueStore(std::shared_ptr<HttpTransport> transport, | ||
| S3KeyValueStoreSpecData spec, AwsCredentialsProvider provider) |
There was a problem hiding this comment.
This is now provided by the Spec and can be removed from the constructor.
| {"credentials_file", "/nonexistent/credentials"}, | ||
| {"config_file", "/nonexistent/config"}})); | ||
| EXPECT_THAT(context.GetResource(profile_spec), | ||
| StatusIs(absl::StatusCode::kInternal)); |
There was a problem hiding this comment.
StatusIs(... , HasSubstr("Failed to create credentials provider"))
| EXPECT_THAT(r1->credentials_provider.get(), ::testing::NotNull()); | ||
| EXPECT_EQ(r1->credentials_provider.get(), r2->credentials_provider.get()); | ||
|
|
||
| auto child = Context(Context::Spec{}, parent); |
There was a problem hiding this comment.
IIRC, child may alias parent here.
TENSORSTORE_ASSERT_OK_AND_ASSIGN(
auto child, Context::FromJson(::nlohmann::json::object_t(), parent));
ASSERT_NE(child, parent);
Maybe add a negative case as well,
TENSORSTORE_ASSERT_OK_AND_ASSIGN(
auto child2,
Context::FromJson({{"aws_credentials", {{"type", "anonymous"}}}},
parent));
TENSORSTORE_ASSERT_OK_AND_ASSIGN(auto r4,
child2.GetResource(resource_spec));
EXPECT_NE(r1->credentials_provider.get(),
r4->credentials_provider.get());
// rename r4 below to r5. :)
Summary
Two related fixes to how the S3 kvstore resolves AWS credentials.
1. Don't cache the anonymous fallback of the default chain
MakeAwsCredentialsProviderwrapped every provider inaws_credentials_provider_new_cached. Fordefault, the chain falls back to anonymous credentials, which never expire, so the outer cache kept the anonymous result forever: credentials that became available later (env vars, IMDS, profile) were never picked up.MakeCacheis now applied per arm (environment, imds, profile, ecs_role).default(which already caches internally) andanonymousare no longer wrapped.AsProvider,MakeCache, andMakeDefaultWithAnonymousreturnnullptrwhen aws-c-auth fails to construct a provider, instead of dereferencing NULL.MakeAwsCredentialsProviderreports this as an error.2. Share one credentials provider per
aws_credentialscontext resourceBefore,
AwsCredentialsResourcewasconfig_onlyand eachS3KeyValueStoreopened its own provider, so every store re-ran credential resolution (for example IMDS and STS calls). The resource now creates the provider once and every store bound to it reuses it.Visible effects of removing
config_only:aws_credentialsresource is emitted as a context reference instead of inline. Explicit inline specs still round-trip inline.aws_credentialsresource instances.