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 { |
54 | | |
55 | | std::string hide_access_key(const std::string& ak); |
56 | | |
57 | | class S3URI; |
58 | | struct S3ClientConf { |
59 | | std::string endpoint; |
60 | | std::string region; |
61 | | std::string ak; |
62 | | std::string sk; |
63 | | std::string token; |
64 | | // For azure we'd better support the bucket at the first time init azure blob container client |
65 | | std::string bucket; |
66 | | io::ObjStorageProvider provider = io::ObjStorageProvider::AWS; |
67 | | int max_connections = -1; |
68 | | int request_timeout_ms = -1; |
69 | | int connect_timeout_ms = -1; |
70 | | bool use_virtual_addressing = true; |
71 | | // For aws s3, no need to override endpoint |
72 | | bool need_override_endpoint = true; |
73 | | |
74 | | CredProviderType cred_provider_type = CredProviderType::Default; |
75 | | std::string role_arn; |
76 | | std::string external_id; |
77 | | // True when this client is bound to a Doris internal object storage bucket |
78 | | // (a storage vault in cloud mode). S3ClientFactory wraps such clients with the |
79 | | // shared rate limiter; external buckets (S3 load, TVF, external catalogs) are |
80 | | // returned bare in cloud mode. |
81 | | bool is_internal_bucket = false; |
82 | | |
83 | | // Full-field identity. get_hash() is only good for picking an unordered_map |
84 | | // bucket; distinct configurations can collide, so never treat hash equality as |
85 | | // configuration equality. |
86 | 225 | bool operator==(const S3ClientConf&) const = default; |
87 | | |
88 | 31 | uint64_t get_hash() const { |
89 | 31 | uint64_t hash_code = 0; |
90 | | // Use crc32_hash(ak + sk) hash to prevent swapped AK/SK order from producing same result. |
91 | 31 | hash_code ^= crc32_hash(ak + sk); |
92 | 31 | hash_code ^= crc32_hash(token); |
93 | 31 | hash_code ^= crc32_hash(endpoint); |
94 | 31 | hash_code ^= crc32_hash(region); |
95 | 31 | hash_code ^= crc32_hash(bucket); |
96 | 31 | hash_code ^= max_connections; |
97 | 31 | hash_code ^= request_timeout_ms; |
98 | 31 | hash_code ^= connect_timeout_ms; |
99 | 31 | hash_code ^= use_virtual_addressing; |
100 | 31 | hash_code ^= static_cast<int>(provider); |
101 | | |
102 | 31 | hash_code ^= static_cast<int>(cred_provider_type); |
103 | 31 | hash_code ^= crc32_hash(role_arn); |
104 | 31 | hash_code ^= crc32_hash(external_id); |
105 | 31 | hash_code ^= is_internal_bucket; |
106 | 31 | return hash_code; |
107 | 31 | } |
108 | | |
109 | 6 | std::string to_string() const { |
110 | 6 | return fmt::format( |
111 | 6 | "(ak={}, token={}, endpoint={}, region={}, bucket={}, max_connections={}, " |
112 | 6 | "request_timeout_ms={}, connect_timeout_ms={}, use_virtual_addressing={}, " |
113 | 6 | "cred_provider_type={},role_arn={}, external_id={}, is_internal_bucket={}", |
114 | 6 | hide_access_key(ak), token.empty() ? "" : "******", endpoint, region, bucket, |
115 | 6 | max_connections, request_timeout_ms, connect_timeout_ms, use_virtual_addressing, |
116 | 6 | cred_provider_type, role_arn, external_id, is_internal_bucket); |
117 | 6 | } |
118 | | }; |
119 | | |
120 | | struct S3ClientConfHash { |
121 | 27 | size_t operator()(const S3ClientConf& conf) const { |
122 | 27 | return static_cast<size_t>(conf.get_hash()); |
123 | 27 | } |
124 | | }; |
125 | | |
126 | | struct S3Conf { |
127 | | std::string bucket; |
128 | | std::string prefix; |
129 | | S3ClientConf client_conf; |
130 | | |
131 | | bool sse_enabled = false; |
132 | | static S3Conf get_s3_conf(const cloud::ObjectStoreInfoPB&); |
133 | | static S3Conf get_s3_conf(const TS3StorageParam&); |
134 | | |
135 | 1 | std::string to_string() const { |
136 | 1 | return fmt::format("(bucket={}, prefix={}, client_conf={}, sse_enabled={})", bucket, prefix, |
137 | 1 | client_conf.to_string(), sse_enabled); |
138 | 1 | } |
139 | | }; |
140 | | |
141 | | class S3ClientFactory { |
142 | | public: |
143 | | ~S3ClientFactory(); |
144 | | |
145 | | static S3ClientFactory& instance(); |
146 | | |
147 | | Result<std::shared_ptr<io::ObjStorageClient>> create(const S3ClientConf& s3_conf); |
148 | | |
149 | | static Status convert_properties_to_s3_conf(const std::map<std::string, std::string>& prop, |
150 | | const S3URI& s3_uri, S3Conf* s3_conf); |
151 | | |
152 | 40 | static Aws::Client::ClientConfiguration& getClientConfiguration() { |
153 | | // The default constructor of ClientConfiguration will do some http call |
154 | | // such as Aws::Internal::GetEC2MetadataClient and other init operation, |
155 | | // which is unnecessary. |
156 | | // So here we use a static instance, and deep copy every time |
157 | | // to avoid unnecessary operations. |
158 | 40 | static Aws::Client::ClientConfiguration instance; |
159 | 40 | instance.requestTimeoutMs = config::aws_client_request_timeout_ms; |
160 | 40 | return instance; |
161 | 40 | } |
162 | | |
163 | | AwsCredentialResult create_aws_credentials_provider(const S3ClientConf& s3_conf); |
164 | | |
165 | | #ifdef BE_TEST |
166 | | void set_client_creator_for_test( |
167 | | std::function<std::shared_ptr<io::ObjStorageClient>(const S3ClientConf&)> creator); |
168 | | |
169 | | void clear_client_creator_for_test(); |
170 | | #endif |
171 | | |
172 | | private: |
173 | | #ifdef BE_TEST |
174 | | FRIEND_TEST(S3ClientFactoryTest, RefreshCaCertForCredentialsProvider); |
175 | | #endif |
176 | | Result<std::shared_ptr<io::ObjStorageClient>> _create_s3_client(const S3ClientConf& s3_conf); |
177 | | Result<std::shared_ptr<io::ObjStorageClient>> _create_azure_client(const S3ClientConf& s3_conf); |
178 | | std::string _get_ca_cert_file_path(); |
179 | | S3ClientFactory(); |
180 | | |
181 | | Aws::SDKOptions _aws_options; |
182 | | std::mutex _lock; |
183 | | std::unordered_map<S3ClientConf, std::shared_ptr<io::ObjStorageClient>, S3ClientConfHash> |
184 | | _cache; |
185 | | std::mutex _ca_cert_lock; |
186 | | std::string _ca_cert_file_path; |
187 | | #ifdef BE_TEST |
188 | | std::function<std::shared_ptr<io::ObjStorageClient>(const S3ClientConf&)> _test_client_creator; |
189 | | #endif |
190 | | }; |
191 | | |
192 | | } // end namespace doris |