Coverage Report

Created: 2026-10-20 22:38

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
#include <gen_cpp/cloud.pb.h>
27
#ifdef BE_TEST
28
#include <gtest/gtest_prod.h>
29
#endif
30
31
#include <functional>
32
#include <map>
33
#include <memory>
34
#include <mutex>
35
#include <string>
36
#include <unordered_map>
37
38
#include "common/status.h"
39
#include "core/string_ref.h"
40
#include "cpp/aws_common.h"
41
#include "cpp/obj-client/auth/aws_credential_factory.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::io {
54
struct S3AuthConfig;
55
} // namespace doris::io
56
57
namespace doris {
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
    // True when this client is bound to a Doris internal object storage bucket
82
    // (a storage vault in cloud mode). S3ClientFactory wraps such clients with the
83
    // shared rate limiter; external buckets (S3 load, TVF, external catalogs) are
84
    // returned bare in cloud mode.
85
    bool is_internal_bucket = false;
86
87
    // Full-field identity. get_hash() is only good for picking an unordered_map
88
    // bucket; distinct configurations can collide, so never treat hash equality as
89
    // configuration equality.
90
85
    bool operator==(const S3ClientConf&) const = default;
91
92
4
    uint64_t get_hash() const {
93
4
        uint64_t hash_code = 0;
94
        // Use crc32_hash(ak + sk) hash to prevent swapped AK/SK order from producing same result.
95
4
        hash_code ^= crc32_hash(ak + sk);
96
4
        hash_code ^= crc32_hash(token);
97
4
        hash_code ^= crc32_hash(endpoint);
98
4
        hash_code ^= crc32_hash(region);
99
4
        hash_code ^= crc32_hash(bucket);
100
4
        hash_code ^= max_connections;
101
4
        hash_code ^= request_timeout_ms;
102
4
        hash_code ^= connect_timeout_ms;
103
4
        hash_code ^= use_virtual_addressing;
104
4
        hash_code ^= static_cast<int>(provider);
105
106
4
        hash_code ^= static_cast<int>(cred_provider_type);
107
4
        hash_code ^= crc32_hash(role_arn);
108
4
        hash_code ^= crc32_hash(external_id);
109
4
        hash_code ^= is_internal_bucket;
110
4
        return hash_code;
111
4
    }
112
113
5
    std::string to_string() const {
114
5
        return fmt::format(
115
5
                "(ak={}, token={}, endpoint={}, region={}, bucket={}, max_connections={}, "
116
5
                "request_timeout_ms={}, connect_timeout_ms={}, use_virtual_addressing={}, "
117
5
                "cred_provider_type={},role_arn={}, external_id={}, is_internal_bucket={}",
118
5
                hide_access_key(ak), token.empty() ? "" : "******", endpoint, region, bucket,
119
5
                max_connections, request_timeout_ms, connect_timeout_ms, use_virtual_addressing,
120
5
                cred_provider_type, role_arn, external_id, is_internal_bucket);
121
5
    }
122
};
123
124
struct S3ClientConfHash {
125
0
    size_t operator()(const S3ClientConf& conf) const {
126
0
        return static_cast<size_t>(conf.get_hash());
127
0
    }
128
};
129
130
struct S3Conf {
131
    std::string bucket;
132
    std::string prefix;
133
    S3ClientConf client_conf;
134
135
    bool sse_enabled = false;
136
    static S3Conf get_s3_conf(const cloud::ObjectStoreInfoPB&);
137
    static S3Conf get_s3_conf(const TS3StorageParam&);
138
139
1
    std::string to_string() const {
140
1
        return fmt::format("(bucket={}, prefix={}, client_conf={}, sse_enabled={})", bucket, prefix,
141
1
                           client_conf.to_string(), sse_enabled);
142
1
    }
143
};
144
145
// Compatibility entry for the legacy S3ClientConf, and owner of the AWS SDK lifetime and
146
// the CA certificate path shared by every object storage client.
147
//
148
// create() validates the legacy conf, resolves it into an io::ObjClientConfig and
149
// delegates to io::ObjClientFactory, whose S3/Azure client providers build and
150
// cache the clients. No provider-specific construction or cache lives here any more.
151
class S3ClientFactory {
152
public:
153
    ~S3ClientFactory();
154
155
    static S3ClientFactory& instance();
156
157
    Result<std::shared_ptr<io::ObjStorageClient>> create(const S3ClientConf& s3_conf);
158
159
    static Status convert_properties_to_s3_conf(const std::map<std::string, std::string>& prop,
160
                                                const S3URI& s3_uri, S3Conf* s3_conf);
161
162
48
    static Aws::Client::ClientConfiguration& getClientConfiguration() {
163
        // The default constructor of ClientConfiguration will do some http call
164
        // such as Aws::Internal::GetEC2MetadataClient and other init operation,
165
        // which is unnecessary.
166
        // So here we use a static instance, and deep copy every time
167
        // to avoid unnecessary operations.
168
48
        static Aws::Client::ClientConfiguration instance;
169
48
        instance.requestTimeoutMs = config::aws_client_request_timeout_ms;
170
48
        return instance;
171
48
    }
172
173
    AwsCredentialResult create_aws_credentials_provider(const S3ClientConf& s3_conf);
174
175
    AwsCredentialResult create_aws_credentials_provider(const io::S3AuthConfig& auth);
176
177
    // The first valid path of config::ca_cert_file_paths, resolved lazily and cached; empty
178
    // when none is valid yet, in which case the next call resolves it again.
179
11
    std::string ca_cert_file_path() { return _get_ca_cert_file_path(); }
180
181
#ifdef BE_TEST
182
    void set_client_creator_for_test(
183
            std::function<std::shared_ptr<io::ObjStorageClient>(const S3ClientConf&)> creator);
184
185
    void clear_client_creator_for_test();
186
#endif
187
188
private:
189
#ifdef BE_TEST
190
    FRIEND_TEST(S3ClientFactoryTest, RefreshCaCertForCredentialsProvider);
191
#endif
192
    std::string _get_ca_cert_file_path();
193
    S3ClientFactory();
194
195
    Aws::SDKOptions _aws_options;
196
#ifdef BE_TEST
197
    std::mutex _lock;
198
#endif
199
    std::mutex _ca_cert_lock;
200
    std::string _ca_cert_file_path;
201
#ifdef BE_TEST
202
    std::function<std::shared_ptr<io::ObjStorageClient>(const S3ClientConf&)> _test_client_creator;
203
#endif
204
};
205
206
} // end namespace doris