Skip to content

Commit 573f12c

Browse files
morningmanxylaaaaaclaude
committed
[fix](be) Keep legacy Azure endpoints literal in S3ClientFactory
Review follow-up for apache#68117. Legacy SharedKey producers (old FE property maps, ObjectStoreInfoPB, TS3StorageParam) hand the endpoint over literally and the old factory only defaulted the scheme. Two rewrites had leaked into that path: - normalize_azure_endpoint()'s bare account-name inference also ran for legacy endpoints, so an unschemed single-label host such as AWS_ENDPOINT=storage-proxy was redirected to https://storage-proxy.blob.core.windows.net. - import_legacy_azure_shared_key() ran normalize_http_uri(), collapsing repeated path separators in custom reverse-proxy base paths. The native protocol keeps its full normalization where it is parsed (convert_native_azure_properties). _create_azure_client() now only applies the transport rule the old factory had: default the scheme and normalize the endpoint/container join boundary. The persisted endpoint is no longer rewritten. Tests cover all three legacy producers with unschemed single-label hosts and exact custom base paths, asserting both the stored endpoint and the container base the SDK signs against. Issue: apache#68103 Related PR: apache#67470 Co-authored-by: xylaaaaa <[email protected]> Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 5f2f1c5 commit 573f12c

2 files changed

Lines changed: 152 additions & 5 deletions

File tree

‎be/src/util/s3_util.cpp‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ bool has_property(const StringCaseMap<std::string>& properties,
266266
return find_property(properties, names) != nullptr;
267267
}
268268

269+
// Native protocol only: resolves the documented AZURE_ENDPOINT shorthands
270+
// (bare account name, official DFS host) into the Blob transport origin. Legacy
271+
// SharedKey endpoints are literal and never pass through here.
269272
std::string normalize_azure_endpoint(std::string endpoint) {
270273
if (endpoint.empty()) {
271274
return endpoint;
@@ -320,12 +323,27 @@ std::string endpoint_authority(const std::string& endpoint) {
320323
return authority;
321324
}
322325

326+
// The endpoint handed to the SDK. Native endpoints were already normalized
327+
// while parsing the native protocol. Legacy SharedKey producers hand over an
328+
// endpoint literal that only ever received a default scheme, so keep that
329+
// contract here: no account-name inference, no DFS-to-Blob rewrite and no path
330+
// rewriting, which would silently redirect single-label proxy hosts or custom
331+
// reverse-proxy routes after an upgrade. Only the endpoint/container join
332+
// boundary is normalized.
333+
std::string azure_transport_endpoint(std::string endpoint) {
334+
if (endpoint.find("://") == std::string::npos) {
335+
endpoint = "https://" + endpoint;
336+
}
337+
while (endpoint.ends_with('/')) {
338+
endpoint.pop_back();
339+
}
340+
return endpoint;
341+
}
342+
323343
// Only established SharedKey wire producers use AWS fields for Azure. Once
324-
// translated here the native factory never inspects these fields again.
344+
// translated here the native factory never inspects these fields again. The
345+
// endpoint stays byte-for-byte as configured, exactly like the old factory.
325346
void import_legacy_azure_shared_key(S3ClientConf* conf) {
326-
// Keep the established SharedKey endpoint normalization at the legacy
327-
// boundary. Native endpoints and object keys retain their internal slashes.
328-
conf->endpoint = normalize_http_uri(conf->endpoint);
329347
conf->azure_credentials = {};
330348
conf->azure_credentials.type = AzureCredentialType::SHARED_KEY;
331349
conf->azure_credentials.account_name = std::move(conf->ak);
@@ -781,7 +799,7 @@ void S3ClientFactory::clear_client_creator_for_test() {
781799
Result<std::shared_ptr<io::ObjStorageClient>> S3ClientFactory::_create_azure_client(
782800
const S3ClientConf& s3_conf) {
783801
#ifdef USE_AZURE
784-
const std::string endpoint = normalize_azure_endpoint(s3_conf.endpoint);
802+
const std::string endpoint = azure_transport_endpoint(s3_conf.endpoint);
785803
const std::string container_name = s3_conf.bucket;
786804
std::string uri = fmt::format("{}/{}", endpoint, container_name);
787805

‎be/test/io/s3_client_factory_test.cpp‎

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <aws/core/client/ClientConfiguration.h>
2222
#include <aws/identity-management/auth/STSAssumeRoleCredentialsProvider.h>
2323
#include <aws/s3/model/HeadObjectResult.h>
24+
#include <gen_cpp/AgentService_types.h>
25+
#include <gen_cpp/cloud.pb.h>
2426
#include <gtest/gtest.h>
2527
#include <unistd.h>
2628

@@ -717,6 +719,133 @@ TEST_F(S3ClientFactoryTest, LegacyAzureSharedKeyRetainsCustomEndpointAndS3Uri) {
717719
EXPECT_TRUE(conf.client_conf.region.empty());
718720
}
719721

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+
720849
TEST_F(S3ClientFactoryTest, RequiresExplicitAzureProviderAndAuthentication) {
721850
S3URI uri("abfss://[email protected]/path/file");
722851
ASSERT_TRUE(uri.parse().ok());

0 commit comments

Comments
 (0)