Coverage Report

Created: 2026-11-03 14:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/s3_util.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <aws/core/Aws.h>
21
#include <aws/core/client/ClientConfiguration.h>
22
#include <aws/s3/S3Errors.h>
23
#include <bvar/bvar.h>
24
#include <fmt/format.h>
25
#include <gen_cpp/AgentService_types.h>
26
#ifdef BE_TEST
27
#include <gtest/gtest_prod.h>
28
#endif
29
30
#include <functional>
31
#include <map>
32
#include <memory>
33
#include <mutex>
34
#include <string>
35
#include <unordered_map>
36
37
#include "common/status.h"
38
#include "core/string_ref.h"
39
#include "cpp/aws_common.h"
40
#include "cpp/obj-client/auth/aws_credential_factory.h"
41
#include "cpp/obj-client/auth/obj_credential.h"
42
#include "cpp/obj-client/obj_storage_client.h"
43
44
namespace Aws::S3 {
45
class S3Client;
46
} // namespace Aws::S3
47
48
namespace bvar {
49
template <typename T>
50
class Adder;
51
}
52
53
namespace doris {
54
55
namespace cloud {
56
class ObjectStoreInfoPB;
57
}
58
59
std::string hide_access_key(const std::string& ak);
60
61
class S3URI;
62
struct S3ClientConf {
63
    std::string endpoint;
64
    std::string region;
65
    std::string ak;
66
    std::string sk;
67
    std::string token;
68
    // For azure we'd better support the bucket at the first time init azure blob container client
69
    std::string bucket;
70
    io::ObjStorageProvider provider = io::ObjStorageProvider::AWS;
71
    int max_connections = -1;
72
    int request_timeout_ms = -1;
73
    int connect_timeout_ms = -1;
74
    bool use_virtual_addressing = true;
75
    // For aws s3, no need to override endpoint
76
    bool need_override_endpoint = true;
77
78
    CredProviderType cred_provider_type = CredProviderType::Default;
79
    std::string role_arn;
80
    std::string external_id;
81
    CredentialConfig credential {};
82
    // True when this client is bound to a Doris internal object storage bucket
83
    // (a storage vault in cloud mode). S3ClientFactory wraps such clients with the
84
    // shared rate limiter; external buckets (S3 load, TVF, external catalogs) are
85
    // returned bare in cloud mode.
86
    bool is_internal_bucket = false;
87
88
    // Full-field identity. get_hash() is only good for picking an unordered_map
89
    // bucket; distinct configurations can collide, so never treat hash equality as
90
    // configuration equality.
91
51.4k
    bool operator==(const S3ClientConf&) const = default;
92
93
2.31k
    uint64_t get_hash() const {
94
2.31k
        uint64_t hash_code = 0;
95
        // Use crc32_hash(ak + sk) hash to prevent swapped AK/SK order from producing same result.
96
2.31k
        hash_code ^= crc32_hash(ak + sk);
97
2.31k
        hash_code ^= crc32_hash(token);
98
2.31k
        hash_code ^= crc32_hash(endpoint);
99
2.31k
        hash_code ^= crc32_hash(region);
100
2.31k
        hash_code ^= crc32_hash(bucket);
101
2.31k
        hash_code ^= max_connections;
102
2.31k
        hash_code ^= request_timeout_ms;
103
2.31k
        hash_code ^= connect_timeout_ms;
104
2.31k
        hash_code ^= use_virtual_addressing;
105
2.31k
        hash_code ^= static_cast<int>(provider);
106
107
2.31k
        hash_code ^= static_cast<int>(cred_provider_type);
108
2.31k
        hash_code ^= crc32_hash(role_arn);
109
2.31k
        hash_code ^= crc32_hash(external_id);
110
2.31k
        hash_combine_credential(&hash_code, credential);
111
2.31k
        hash_code ^= is_internal_bucket;
112
2.31k
        return hash_code;
113
2.31k
    }
114
115
67
    std::string to_string() const {
116
67
        return fmt::format(
117
67
                "(ak={}, token={}, endpoint={}, region={}, bucket={}, max_connections={}, "
118
67
                "request_timeout_ms={}, connect_timeout_ms={}, use_virtual_addressing={}, "
119
67
                "cred_provider_type={},role_arn={}, external_id={}, credential_type={}, "
120
67
                "is_internal_bucket={}",
121
67
                hide_access_key(ak), token.empty() ? "" : "******", endpoint, region, bucket,
122
67
                max_connections, request_timeout_ms, connect_timeout_ms, use_virtual_addressing,
123
67
                cred_provider_type, role_arn, external_id, credential_type_name(credential),
124
67
                is_internal_bucket);
125
67
    }
126
};
127
128
struct S3ClientConfHash {
129
2.31k
    size_t operator()(const S3ClientConf& conf) const {
130
2.31k
        return static_cast<size_t>(conf.get_hash());
131
2.31k
    }
132
};
133
134
struct S3Conf {
135
    std::string bucket;
136
    std::string prefix;
137
    S3ClientConf client_conf;
138
139
    bool sse_enabled = false;
140
    static S3Conf get_s3_conf(const cloud::ObjectStoreInfoPB&);
141
    static S3Conf get_s3_conf(const TS3StorageParam&);
142
143
34
    std::string to_string() const {
144
34
        return fmt::format("(bucket={}, prefix={}, client_conf={}, sse_enabled={})", bucket, prefix,
145
34
                           client_conf.to_string(), sse_enabled);
146
34
    }
147
};
148
149
class S3ClientFactory {
150
public:
151
    ~S3ClientFactory();
152
153
    static S3ClientFactory& instance();
154
155
    Result<std::shared_ptr<io::ObjStorageClient>> create(const S3ClientConf& s3_conf);
156
157
    static Status convert_properties_to_s3_conf(const std::map<std::string, std::string>& prop,
158
                                                const S3URI& s3_uri, S3Conf* s3_conf);
159
160
90
    static Aws::Client::ClientConfiguration& getClientConfiguration() {
161
        // The default constructor of ClientConfiguration will do some http call
162
        // such as Aws::Internal::GetEC2MetadataClient and other init operation,
163
        // which is unnecessary.
164
        // So here we use a static instance, and deep copy every time
165
        // to avoid unnecessary operations.
166
90
        static Aws::Client::ClientConfiguration instance;
167
90
        instance.requestTimeoutMs = config::aws_client_request_timeout_ms;
168
90
        return instance;
169
90
    }
170
171
    AwsCredentialResult create_aws_credentials_provider(const S3ClientConf& s3_conf);
172
173
#ifdef BE_TEST
174
    void set_client_creator_for_test(
175
            std::function<std::shared_ptr<io::ObjStorageClient>(const S3ClientConf&)> creator);
176
177
    void clear_client_creator_for_test();
178
#endif
179
180
private:
181
#ifdef BE_TEST
182
    FRIEND_TEST(S3ClientFactoryTest, RefreshCaCertForCredentialsProvider);
183
#endif
184
    Result<std::shared_ptr<io::ObjStorageClient>> _create_s3_client(const S3ClientConf& s3_conf);
185
    Result<std::shared_ptr<io::ObjStorageClient>> _create_azure_client(const S3ClientConf& s3_conf);
186
    std::string _get_ca_cert_file_path();
187
    S3ClientFactory();
188
189
    Aws::SDKOptions _aws_options;
190
    std::mutex _lock;
191
    std::unordered_map<S3ClientConf, std::shared_ptr<io::ObjStorageClient>, S3ClientConfHash>
192
            _cache;
193
    std::mutex _ca_cert_lock;
194
    std::string _ca_cert_file_path;
195
#ifdef BE_TEST
196
    std::function<std::shared_ptr<io::ObjStorageClient>(const S3ClientConf&)> _test_client_creator;
197
#endif
198
};
199
200
} // end namespace doris