|
21 | 21 | #include <aws/core/client/ClientConfiguration.h> |
22 | 22 | #include <aws/identity-management/auth/STSAssumeRoleCredentialsProvider.h> |
23 | 23 | #include <aws/s3/model/HeadObjectResult.h> |
| 24 | +#include <gen_cpp/AgentService_types.h> |
| 25 | +#include <gen_cpp/cloud.pb.h> |
24 | 26 | #include <gtest/gtest.h> |
25 | 27 | #include <unistd.h> |
26 | 28 |
|
@@ -717,6 +719,133 @@ TEST_F(S3ClientFactoryTest, LegacyAzureSharedKeyRetainsCustomEndpointAndS3Uri) { |
717 | 719 | EXPECT_TRUE(conf.client_conf.region.empty()); |
718 | 720 | } |
719 | 721 |
|
| 722 | +namespace { |
| 723 | + |
| 724 | +// Base64 test key: a real SharedKey client can sign with it, and the signed |
| 725 | +// blob URL exposes exactly which container base the SDK was built with. |
| 726 | +constexpr const char* LEGACY_AZURE_ACCOUNT_KEY {"MDEyMzQ1Njc4OWFiY2RlZg=="}; |
| 727 | + |
| 728 | +// The three established SharedKey producers hand the endpoint over literally: |
| 729 | +// old FE property maps, storage vault ObjectStoreInfoPB and TS3StorageParam. |
| 730 | +S3Conf legacy_azure_property_conf(const std::string& endpoint) { |
| 731 | + std::map<std::string, std::string> properties {{"provider", "azure"}, |
| 732 | + {"AWS_ENDPOINT", endpoint}, |
| 733 | + {"AWS_ACCESS_KEY", "account"}, |
| 734 | + {"AWS_SECRET_KEY", LEGACY_AZURE_ACCOUNT_KEY}, |
| 735 | + {"AWS_REGION", "legacy-region"}}; |
| 736 | + S3URI uri("s3://container/path/file"); |
| 737 | + EXPECT_TRUE(uri.parse().ok()); |
| 738 | + S3Conf conf; |
| 739 | + auto status = S3ClientFactory::convert_properties_to_s3_conf(properties, uri, &conf); |
| 740 | + EXPECT_TRUE(status.ok()) << status; |
| 741 | + return conf; |
| 742 | +} |
| 743 | + |
| 744 | +S3Conf legacy_azure_pb_conf(const std::string& endpoint) { |
| 745 | + cloud::ObjectStoreInfoPB info; |
| 746 | + info.set_endpoint(endpoint); |
| 747 | + info.set_ak("account"); |
| 748 | + info.set_sk(LEGACY_AZURE_ACCOUNT_KEY); |
| 749 | + info.set_bucket("container"); |
| 750 | + info.set_prefix("vault-prefix"); |
| 751 | + info.set_provider(cloud::ObjectStoreInfoPB::AZURE); |
| 752 | + return S3Conf::get_s3_conf(info); |
| 753 | +} |
| 754 | + |
| 755 | +S3Conf legacy_azure_thrift_conf(const std::string& endpoint) { |
| 756 | + TS3StorageParam param; |
| 757 | + param.__set_endpoint(endpoint); |
| 758 | + param.__set_ak("account"); |
| 759 | + param.__set_sk(LEGACY_AZURE_ACCOUNT_KEY); |
| 760 | + param.__set_bucket("container"); |
| 761 | + param.__set_root_path("resource-prefix"); |
| 762 | + param.__set_provider(TObjStorageType::AZURE); |
| 763 | + return S3Conf::get_s3_conf(param); |
| 764 | +} |
| 765 | + |
| 766 | +using LegacyAzureConfBuilder = S3Conf (*)(const std::string&); |
| 767 | + |
| 768 | +const std::vector<std::pair<const char*, LegacyAzureConfBuilder>>& legacy_azure_producers() { |
| 769 | + static const std::vector<std::pair<const char*, LegacyAzureConfBuilder>> producers { |
| 770 | + {"properties", legacy_azure_property_conf}, |
| 771 | + {"ObjectStoreInfoPB", legacy_azure_pb_conf}, |
| 772 | + {"TS3StorageParam", legacy_azure_thrift_conf}}; |
| 773 | + return producers; |
| 774 | +} |
| 775 | + |
| 776 | +#ifdef USE_AZURE |
| 777 | +// The blob URL the SDK signs is built from the container client, so it shows |
| 778 | +// the exact transport base the factory handed to the SDK. |
| 779 | +std::string azure_signed_blob_base(const S3ClientConf& conf) { |
| 780 | + auto client = S3ClientFactory::instance().create(conf); |
| 781 | + if (!client.has_value()) { |
| 782 | + ADD_FAILURE() << client.error(); |
| 783 | + return {}; |
| 784 | + } |
| 785 | + const auto url = |
| 786 | + client.value()->generate_presigned_url({.bucket = conf.bucket, .key = "dir/file"}, 60); |
| 787 | + return url.substr(0, url.find('?')); |
| 788 | +} |
| 789 | +#endif |
| 790 | + |
| 791 | +} // namespace |
| 792 | + |
| 793 | +TEST_F(S3ClientFactoryTest, LegacyAzureEndpointsKeepSingleLabelHosts) { |
| 794 | + // The legacy contract never required a scheme or a dotted host, and the old |
| 795 | + // factory only defaulted the scheme. An internal proxy or emulator with a |
| 796 | + // one-label DNS name must not be redirected to the public Blob origin. |
| 797 | + for (const auto& [producer, build] : legacy_azure_producers()) { |
| 798 | + SCOPED_TRACE(producer); |
| 799 | + for (const auto& [endpoint, transport_base] : |
| 800 | + std::vector<std::pair<std::string, std::string>> { |
| 801 | + {"storage-proxy", "https://storage-proxy/container"}, |
| 802 | + {"storage-proxy:10000", "https://storage-proxy:10000/container"}, |
| 803 | + {"http://storage-proxy", "http://storage-proxy/container"}, |
| 804 | + {"account.dfs.core.windows.net", |
| 805 | + "https://account.dfs.core.windows.net/container"}}) { |
| 806 | + SCOPED_TRACE(endpoint); |
| 807 | + const auto conf = build(endpoint); |
| 808 | + EXPECT_EQ(conf.client_conf.provider, io::ObjStorageProvider::AZURE); |
| 809 | + EXPECT_EQ(conf.client_conf.endpoint, endpoint); |
| 810 | + EXPECT_EQ(conf.client_conf.azure_credentials.type, AzureCredentialType::SHARED_KEY); |
| 811 | + EXPECT_EQ(conf.client_conf.azure_credentials.account_name, "account"); |
| 812 | + EXPECT_EQ(conf.client_conf.azure_credentials.account_key, LEGACY_AZURE_ACCOUNT_KEY); |
| 813 | + EXPECT_EQ(conf.bucket, "container"); |
| 814 | + EXPECT_EQ(conf.client_conf.bucket, "container"); |
| 815 | + EXPECT_TRUE(conf.client_conf.ak.empty()); |
| 816 | + EXPECT_TRUE(conf.client_conf.sk.empty()); |
| 817 | +#ifdef USE_AZURE |
| 818 | + EXPECT_EQ(azure_signed_blob_base(conf.client_conf), transport_base + "/dir/file"); |
| 819 | +#endif |
| 820 | + } |
| 821 | + } |
| 822 | +} |
| 823 | + |
| 824 | +TEST_F(S3ClientFactoryTest, LegacyAzureEndpointsKeepCustomBasePathsBytePreserving) { |
| 825 | + // Repeated separators can be meaningful reverse-proxy routes. Only the |
| 826 | + // endpoint/container join boundary is normalized; the persisted endpoint |
| 827 | + // itself is never rewritten. |
| 828 | + for (const auto& [producer, build] : legacy_azure_producers()) { |
| 829 | + SCOPED_TRACE(producer); |
| 830 | + for (const auto& [endpoint, transport_base] : |
| 831 | + std::vector<std::pair<std::string, std::string>> { |
| 832 | + {"https://proxy.example/gateway//tenant", |
| 833 | + "https://proxy.example/gateway//tenant/container"}, |
| 834 | + {"https://proxy.example:8443/gateway/tenant/", |
| 835 | + "https://proxy.example:8443/gateway/tenant/container"}, |
| 836 | + {"proxy.example/base%2Fpath", |
| 837 | + "https://proxy.example/base%2Fpath/container"}}) { |
| 838 | + SCOPED_TRACE(endpoint); |
| 839 | + const auto conf = build(endpoint); |
| 840 | + EXPECT_EQ(conf.client_conf.endpoint, endpoint); |
| 841 | + EXPECT_EQ(conf.client_conf.azure_credentials.account_name, "account"); |
| 842 | +#ifdef USE_AZURE |
| 843 | + EXPECT_EQ(azure_signed_blob_base(conf.client_conf), transport_base + "/dir/file"); |
| 844 | +#endif |
| 845 | + } |
| 846 | + } |
| 847 | +} |
| 848 | + |
720 | 849 | TEST_F(S3ClientFactoryTest, RequiresExplicitAzureProviderAndAuthentication) { |
721 | 850 | S3URI uri( "abfss://[email protected]/path/file"); |
722 | 851 | ASSERT_TRUE(uri.parse().ok()); |
|
0 commit comments