|
16 | 16 | // under the License. |
17 | 17 |
|
18 | 18 | #include <aws/core/auth/AWSCredentialsProviderChain.h> |
| 19 | +#include <aws/core/auth/GeneralHTTPCredentialsProvider.h> |
19 | 20 | #include <aws/core/auth/STSCredentialsProvider.h> |
20 | 21 | #include <aws/core/client/ClientConfiguration.h> |
21 | 22 | #include <aws/identity-management/auth/STSAssumeRoleCredentialsProvider.h> |
22 | 23 | #include <aws/s3/model/HeadObjectResult.h> |
23 | 24 | #include <gtest/gtest.h> |
24 | 25 | #include <unistd.h> |
25 | 26 |
|
| 27 | +#include <cstdio> |
26 | 28 | #include <cstdlib> |
27 | 29 | #include <filesystem> |
28 | 30 | #include <fstream> |
| 31 | +#include <memory> |
29 | 32 | #include <string> |
30 | 33 | #include <utility> |
31 | 34 | #include <vector> |
|
34 | 37 | #include "common/config.h" |
35 | 38 | #include "cpp/aws_common.h" |
36 | 39 | #include "cpp/custom_aws_credentials_provider_chain.h" |
| 40 | +#include "cpp/obj-client/auth/aws_credential_factory.h" |
37 | 41 | #include "cpp/obj-client/s3_obj_storage_client.h" |
38 | 42 | #include "cpp/sync_point.h" |
39 | 43 | #include "io/fs/s3_file_system.h" |
| 44 | +#include "testutil/container_credentials_endpoint.h" |
40 | 45 | #include "util/s3_rate_limiter_manager.h" |
41 | 46 | #include "util/s3_uri.h" |
42 | 47 | #include "util/s3_util.h" |
@@ -573,16 +578,15 @@ TEST_F(S3ClientFactoryTest, AwsCredentialsProviderV2ProviderTypeWithoutRoleArn) |
573 | 578 | provider), |
574 | 579 | nullptr); |
575 | 580 |
|
576 | | - const char* old_container_uri = std::getenv("AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"); |
577 | | - if (old_container_uri == nullptr) { |
578 | | - setenv("AWS_CONTAINER_CREDENTIALS_RELATIVE_URI", "/v2/credentials/mock", 1); |
579 | | - } |
580 | | - S3ClientConf container_conf; |
581 | | - container_conf.cred_provider_type = CredProviderType::Container; |
582 | | - provider = factory.create_aws_credentials_provider(container_conf).provider; |
583 | | - ASSERT_NE(std::dynamic_pointer_cast<Aws::Auth::TaskRoleCredentialsProvider>(provider), nullptr); |
584 | | - if (old_container_uri == nullptr) { |
585 | | - unsetenv("AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"); |
| 581 | + { |
| 582 | + // The environment is pinned rather than merely topped up, so that this case asserts the |
| 583 | + // same thing whether or not the machine running it happens to be inside a container. |
| 584 | + ContainerCredentialsEnvGuard env; |
| 585 | + env.set_ecs_task_role("/v2/credentials/mock"); |
| 586 | + S3ClientConf container_conf; |
| 587 | + container_conf.cred_provider_type = CredProviderType::Container; |
| 588 | + provider = factory.create_aws_credentials_provider(container_conf).provider; |
| 589 | + ASSERT_NE(as_valid_http_provider(provider), nullptr); |
586 | 590 | } |
587 | 591 |
|
588 | 592 | S3ClientConf instance_profile_conf; |
@@ -689,4 +693,187 @@ TEST_F(S3ClientFactoryTest, AwsCredentialsProviderV1PartialCredentialsUseDefault |
689 | 693 | config::aws_credentials_provider_version = "v2"; |
690 | 694 | } |
691 | 695 |
|
| 696 | +namespace { |
| 697 | + |
| 698 | +// The provider is looked up inside the chain rather than constructed directly, |
| 699 | +// because what is under test is how CustomAwsCredentialsProviderChain wires the |
| 700 | +// environment. |
| 701 | +Aws::Auth::GeneralHTTPCredentialsProvider* find_http_provider( |
| 702 | + const CustomAwsCredentialsProviderChain& chain) { |
| 703 | + for (const auto& provider : chain.GetProviders()) { |
| 704 | + auto* http_provider = |
| 705 | + dynamic_cast<Aws::Auth::GeneralHTTPCredentialsProvider*>(provider.get()); |
| 706 | + if (http_provider != nullptr) { |
| 707 | + return http_provider; |
| 708 | + } |
| 709 | + } |
| 710 | + return nullptr; |
| 711 | +} |
| 712 | + |
| 713 | +} // namespace |
| 714 | + |
| 715 | +// EKS Pod Identity supplies the credential-endpoint token as a file that the |
| 716 | +// kubelet rotates in place, and never as a plain environment variable. The chain |
| 717 | +// must therefore hand the provider the token path, so that every refresh picks |
| 718 | +// up the current contents. Passing the value read at construction time works |
| 719 | +// until the first rotation and then fails with AccessDenied. |
| 720 | +TEST_F(S3ClientFactoryTest, CustomChainReadsRotatedTokenFileForPodIdentity) { |
| 721 | + (void)S3ClientFactory::instance(); |
| 722 | + |
| 723 | + ContainerCredentialsEndpoint endpoint; |
| 724 | + ASSERT_TRUE(endpoint.start()); |
| 725 | + |
| 726 | + ContainerCredentialsEnvGuard env; |
| 727 | + const std::string token_path = env.token_file_path("custom_chain"); |
| 728 | + env.write_token_file(token_path, "token-one"); |
| 729 | + env.set_pod_identity(endpoint.url(), token_path); |
| 730 | + |
| 731 | + CustomAwsCredentialsProviderChain chain; |
| 732 | + auto* provider = find_http_provider(chain); |
| 733 | + ASSERT_NE(provider, nullptr) << "no GeneralHTTPCredentialsProvider was added to the chain"; |
| 734 | + |
| 735 | + // Each GetAWSCredentials() drives one HTTP GET to the endpoint: the provider re-reads the token |
| 736 | + // file, sends it as the Authorization header, and parses the credentials from the reply. The |
| 737 | + // handler records the header it saw, so auth_headers() reports what actually went over the wire. |
| 738 | + const auto first_credentials = provider->GetAWSCredentials(); |
| 739 | + EXPECT_EQ(first_credentials.GetAWSAccessKeyId(), "AKIDTEST"); |
| 740 | + EXPECT_EQ(first_credentials.GetSessionToken(), "SESSIONTEST"); |
| 741 | + |
| 742 | + // Rotate the file the way the kubelet does. The second call refetches rather than serving its |
| 743 | + // cache only because the handler reports an already-expired Expiration. |
| 744 | + env.write_token_file(token_path, "token-two"); |
| 745 | + provider->GetAWSCredentials(); |
| 746 | + |
| 747 | + // GE rather than EQ: the credentials client retries and the handler records every attempt, so an |
| 748 | + // exact count would make a retry look like a bug. front/back keep the assertion at full strength |
| 749 | + // because every request before the rewrite carries token-one and every one after carries token-two. |
| 750 | + const auto auth_headers = endpoint.auth_headers(); |
| 751 | + ASSERT_GE(auth_headers.size(), 2u); |
| 752 | + EXPECT_EQ(auth_headers.front(), "token-one"); |
| 753 | + EXPECT_EQ(auth_headers.back(), "token-two"); |
| 754 | +} |
| 755 | + |
| 756 | +// The same wiring has to hold when CONTAINER is asked for by name rather than reached through the |
| 757 | +// default chain, which is the whole point of a public provider mode. Reading only |
| 758 | +// AWS_CONTAINER_CREDENTIALS_RELATIVE_URI leaves this provider with an empty URI, no HTTP client and |
| 759 | +// no credentials on every standard EKS deployment. |
| 760 | +TEST_F(S3ClientFactoryTest, ContainerProviderTypeReadsRotatedTokenFileForPodIdentity) { |
| 761 | + S3ClientFactory& factory = S3ClientFactory::instance(); |
| 762 | + config::aws_credentials_provider_version = "v2"; |
| 763 | + |
| 764 | + ContainerCredentialsEndpoint endpoint; |
| 765 | + ASSERT_TRUE(endpoint.start()); |
| 766 | + |
| 767 | + ContainerCredentialsEnvGuard env; |
| 768 | + const std::string token_path = env.token_file_path("container_type"); |
| 769 | + env.write_token_file(token_path, "token-one"); |
| 770 | + env.set_pod_identity(endpoint.url(), token_path); |
| 771 | + |
| 772 | + S3ClientConf conf; |
| 773 | + conf.cred_provider_type = CredProviderType::Container; |
| 774 | + auto provider = as_valid_http_provider(factory.create_aws_credentials_provider(conf).provider); |
| 775 | + ASSERT_NE(provider, nullptr) << "CONTAINER did not yield a usable container credentials " |
| 776 | + "provider for AWS_CONTAINER_CREDENTIALS_FULL_URI"; |
| 777 | + |
| 778 | + const auto first_credentials = provider->GetAWSCredentials(); |
| 779 | + EXPECT_EQ(first_credentials.GetAWSAccessKeyId(), "AKIDTEST"); |
| 780 | + EXPECT_EQ(first_credentials.GetSessionToken(), "SESSIONTEST"); |
| 781 | + |
| 782 | + env.write_token_file(token_path, "token-two"); |
| 783 | + provider->GetAWSCredentials(); |
| 784 | + |
| 785 | + const auto auth_headers = endpoint.auth_headers(); |
| 786 | + ASSERT_GE(auth_headers.size(), 2u); |
| 787 | + EXPECT_EQ(auth_headers.front(), "token-one"); |
| 788 | + EXPECT_EQ(auth_headers.back(), "token-two"); |
| 789 | +} |
| 790 | + |
| 791 | +TEST_F(S3ClientFactoryTest, ContainerProviderTypeForwardsInlineAuthorizationToken) { |
| 792 | + S3ClientFactory& factory = S3ClientFactory::instance(); |
| 793 | + config::aws_credentials_provider_version = "v2"; |
| 794 | + |
| 795 | + ContainerCredentialsEndpoint endpoint; |
| 796 | + ASSERT_TRUE(endpoint.start()); |
| 797 | + |
| 798 | + ContainerCredentialsEnvGuard env; |
| 799 | + env.set_inline_token_endpoint(endpoint.url(), "inline-token"); |
| 800 | + |
| 801 | + S3ClientConf conf; |
| 802 | + conf.cred_provider_type = CredProviderType::Container; |
| 803 | + auto provider = as_valid_http_provider(factory.create_aws_credentials_provider(conf).provider); |
| 804 | + ASSERT_NE(provider, nullptr) << "CONTAINER did not yield a usable container credentials " |
| 805 | + "provider for an inline authorization token"; |
| 806 | + |
| 807 | + EXPECT_EQ(provider->GetAWSCredentials().GetAWSAccessKeyId(), "AKIDTEST"); |
| 808 | + |
| 809 | + const auto auth_headers = endpoint.auth_headers(); |
| 810 | + ASSERT_GE(auth_headers.size(), 1u); |
| 811 | + EXPECT_EQ(auth_headers.front(), "inline-token"); |
| 812 | +} |
| 813 | + |
| 814 | +TEST_F(S3ClientFactoryTest, ContainerProviderTypeStillHonoursEcsRelativeUri) { |
| 815 | + S3ClientFactory& factory = S3ClientFactory::instance(); |
| 816 | + config::aws_credentials_provider_version = "v2"; |
| 817 | + |
| 818 | + ContainerCredentialsEnvGuard env; |
| 819 | + env.set_ecs_task_role("/v2/credentials/mock"); |
| 820 | + |
| 821 | + S3ClientConf conf; |
| 822 | + conf.cred_provider_type = CredProviderType::Container; |
| 823 | + EXPECT_NE(as_valid_http_provider(factory.create_aws_credentials_provider(conf).provider), |
| 824 | + nullptr) |
| 825 | + << "CONTAINER did not yield a usable container credentials provider for " |
| 826 | + "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"; |
| 827 | +} |
| 828 | + |
| 829 | +// With neither URI exported there is nothing to talk to, and the provider says so. This pins the |
| 830 | +// discriminator the two tests above rely on: IsValid() really does distinguish a wired provider |
| 831 | +// from an inert one, so their assertions cannot pass vacuously. |
| 832 | +TEST_F(S3ClientFactoryTest, ContainerProviderTypeIsUnusableWithoutAnyUri) { |
| 833 | + S3ClientFactory& factory = S3ClientFactory::instance(); |
| 834 | + config::aws_credentials_provider_version = "v2"; |
| 835 | + |
| 836 | + // Constructing the guard is the whole setup: it clears all four AWS_CONTAINER_* variables, so |
| 837 | + // this runs as if on a host that is not a container at all. |
| 838 | + ContainerCredentialsEnvGuard env; |
| 839 | + |
| 840 | + S3ClientConf conf; |
| 841 | + conf.cred_provider_type = CredProviderType::Container; |
| 842 | + auto provider = factory.create_aws_credentials_provider(conf).provider; |
| 843 | + ASSERT_NE(std::dynamic_pointer_cast<Aws::Auth::GeneralHTTPCredentialsProvider>(provider), |
| 844 | + nullptr); |
| 845 | + EXPECT_EQ(as_valid_http_provider(provider), nullptr); |
| 846 | +} |
| 847 | + |
| 848 | +// With a role_arn the CONTAINER provider is not handed to the S3 client directly: it becomes the |
| 849 | +// base provider the STS client authenticates its AssumeRole call with. AwsCredentialFactory is |
| 850 | +// asked for that base provider here - a v2 request with no role_arn returns it unwrapped - so the |
| 851 | +// assertion is about the base itself rather than the STS wrapper around it. |
| 852 | +TEST_F(S3ClientFactoryTest, ContainerProviderTypeIsUsableAsStsBaseProvider) { |
| 853 | + (void)S3ClientFactory::instance(); |
| 854 | + |
| 855 | + ContainerCredentialsEndpoint endpoint; |
| 856 | + ASSERT_TRUE(endpoint.start()); |
| 857 | + |
| 858 | + ContainerCredentialsEnvGuard env; |
| 859 | + const std::string token_path = env.token_file_path("sts_base"); |
| 860 | + env.write_token_file(token_path, "token-one"); |
| 861 | + env.set_pod_identity(endpoint.url(), token_path); |
| 862 | + |
| 863 | + AwsCredentialOptions options; |
| 864 | + options.version = AwsCredentialProviderVersion::V2; |
| 865 | + options.provider_type = CredProviderType::Container; |
| 866 | + // role_arn stays empty on purpose: that is what makes create() hand back the base provider |
| 867 | + // itself instead of the STS wrapper built on top of it. |
| 868 | + auto base_provider = as_valid_http_provider(AwsCredentialFactory::create(options).provider); |
| 869 | + ASSERT_NE(base_provider, nullptr) |
| 870 | + << "CONTAINER did not yield a usable STS base credentials provider"; |
| 871 | + |
| 872 | + EXPECT_EQ(base_provider->GetAWSCredentials().GetAWSAccessKeyId(), "AKIDTEST"); |
| 873 | + const auto auth_headers = endpoint.auth_headers(); |
| 874 | + ASSERT_FALSE(auth_headers.empty()) |
| 875 | + << "the STS base provider never contacted the credentials endpoint"; |
| 876 | + EXPECT_EQ(auth_headers.back(), "token-one"); |
| 877 | +} |
| 878 | + |
692 | 879 | } // namespace doris |
0 commit comments