/root/doris/common/cpp/aws_common.cpp
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 | | #include "aws_common.h" |
19 | | |
20 | | #include <aws/core/auth/GeneralHTTPCredentialsProvider.h> |
21 | | #include <aws/core/platform/Environment.h> |
22 | | #include <aws/core/utils/memory/AWSMemory.h> |
23 | | #include <glog/logging.h> |
24 | | |
25 | | namespace doris { |
26 | | |
27 | | namespace { |
28 | | const char CONTAINER_CREDENTIALS_PROVIDER_TAG[] = "ContainerCredentialsProvider"; |
29 | | } // namespace |
30 | | |
31 | 0 | CredProviderType cred_provider_type_from_pb(cloud::CredProviderTypePB cred_provider_type) { |
32 | 0 | switch (cred_provider_type) { |
33 | 0 | case cloud::CredProviderTypePB::DEFAULT: |
34 | 0 | return CredProviderType::Default; |
35 | 0 | case cloud::CredProviderTypePB::SIMPLE: |
36 | 0 | return CredProviderType::Simple; |
37 | 0 | case cloud::CredProviderTypePB::INSTANCE_PROFILE: |
38 | 0 | return CredProviderType::InstanceProfile; |
39 | 0 | case cloud::CredProviderTypePB::ENV: |
40 | 0 | return CredProviderType::Env; |
41 | 0 | case cloud::CredProviderTypePB::SYSTEM_PROPERTIES: |
42 | 0 | return CredProviderType::SystemProperties; |
43 | 0 | case cloud::CredProviderTypePB::WEB_IDENTITY: |
44 | 0 | return CredProviderType::WebIdentity; |
45 | 0 | case cloud::CredProviderTypePB::CONTAINER: |
46 | 0 | return CredProviderType::Container; |
47 | 0 | case cloud::CredProviderTypePB::ANONYMOUS: |
48 | 0 | return CredProviderType::Anonymous; |
49 | 0 | default: |
50 | 0 | __builtin_unreachable(); |
51 | 0 | LOG(WARNING) << "Invalid CredProviderTypePB value: " << cred_provider_type |
52 | 0 | << ", use default instead."; |
53 | 0 | return CredProviderType::Default; |
54 | 0 | } |
55 | 0 | } |
56 | | |
57 | 8 | CredProviderType cred_provider_type_from_string(const std::string& type) { |
58 | 8 | if (type.empty() || type == "DEFAULT") { |
59 | 1 | return CredProviderType::Default; |
60 | 1 | } |
61 | 7 | if (type == "SIMPLE") { |
62 | 0 | return CredProviderType::Simple; |
63 | 0 | } |
64 | 7 | if (type == "INSTANCE_PROFILE") { |
65 | 1 | return CredProviderType::InstanceProfile; |
66 | 1 | } |
67 | 6 | if (type == "ENV") { |
68 | 1 | return CredProviderType::Env; |
69 | 1 | } |
70 | 5 | if (type == "SYSTEM_PROPERTIES") { |
71 | 1 | return CredProviderType::SystemProperties; |
72 | 1 | } |
73 | 4 | if (type == "WEB_IDENTITY") { |
74 | 2 | return CredProviderType::WebIdentity; |
75 | 2 | } |
76 | 2 | if (type == "CONTAINER") { |
77 | 1 | return CredProviderType::Container; |
78 | 1 | } |
79 | 1 | if (type == "ANONYMOUS") { |
80 | 1 | return CredProviderType::Anonymous; |
81 | 1 | } |
82 | 1 | LOG(WARNING) << "Unknown credentials provider type: " << type << ", use default instead."; |
83 | 0 | return CredProviderType::Default; |
84 | 1 | } |
85 | | |
86 | 4 | bool container_credentials_available() { |
87 | 4 | return !Aws::Environment::GetEnv(AWS_CONTAINER_CREDENTIALS_RELATIVE_URI).empty() || |
88 | 4 | !Aws::Environment::GetEnv(AWS_CONTAINER_CREDENTIALS_FULL_URI).empty(); |
89 | 4 | } |
90 | | |
91 | 9 | std::shared_ptr<Aws::Auth::AWSCredentialsProvider> create_container_credentials_provider() { |
92 | 9 | const auto relative_uri = Aws::Environment::GetEnv(AWS_CONTAINER_CREDENTIALS_RELATIVE_URI); |
93 | 9 | const auto absolute_uri = Aws::Environment::GetEnv(AWS_CONTAINER_CREDENTIALS_FULL_URI); |
94 | 9 | const auto token = Aws::Environment::GetEnv(AWS_CONTAINER_AUTHORIZATION_TOKEN); |
95 | 9 | const auto token_path = Aws::Environment::GetEnv(AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE); |
96 | | |
97 | | // Both URIs are forwarded and the provider decides between them: a non-empty relative URI wins |
98 | | // and is resolved against the ECS agent's address, otherwise the full URI is used as-is. This |
99 | | // is the same precedence the AWS SDK's own default chain applies. |
100 | | // |
101 | | // Both token forms are forwarded for the same reason. The endpoint authenticates every fetch |
102 | | // with a bearer token, which the provider takes either inline or as a file path, and given a |
103 | | // path it re-reads the file before each fetch. ECS sets only the inline variable, EKS Pod |
104 | | // Identity sets only the file one - so forwarding the path is what makes the Authorization header |
105 | | // non-empty under Pod Identity, and what keeps it valid once the kubelet rotates the file. |
106 | | // |
107 | | // NOTE: The header file names its third parameter authTokenFilePath and its fourth authToken, |
108 | | // but the implementation binds them the other way round. The header is the side that is wrong, |
109 | | // not the definition. This is reported as aws/aws-sdk-cpp#3143, fixed by |
110 | | // aws/aws-sdk-cpp#3162. |
111 | 9 | auto provider = Aws::MakeShared<Aws::Auth::GeneralHTTPCredentialsProvider>( |
112 | 9 | CONTAINER_CREDENTIALS_PROVIDER_TAG, relative_uri, absolute_uri, token, token_path); |
113 | | |
114 | 9 | const bool uses_relative_uri = !relative_uri.empty(); |
115 | 9 | const char* const uri_var = uses_relative_uri ? AWS_CONTAINER_CREDENTIALS_RELATIVE_URI |
116 | 9 | : AWS_CONTAINER_CREDENTIALS_FULL_URI; |
117 | 9 | const auto& uri = uses_relative_uri ? relative_uri : absolute_uri; |
118 | | |
119 | 9 | if (relative_uri.empty() && absolute_uri.empty()) { |
120 | 2 | LOG(WARNING) << "Container credentials provider has no endpoint to call and will return no " |
121 | 2 | "credentials: neither " |
122 | 2 | << AWS_CONTAINER_CREDENTIALS_RELATIVE_URI << " nor " |
123 | 2 | << AWS_CONTAINER_CREDENTIALS_FULL_URI << " is set."; |
124 | 7 | } else { |
125 | 7 | LOG(INFO) |
126 | 7 | << "Created container credentials provider from " << uri_var << ": [" << uri |
127 | 7 | << "] with a" << (token.empty() ? "n empty" : " non-empty") |
128 | 7 | << " inline authorization token and a" |
129 | 7 | << (token_path.empty() ? "n empty" : " non-empty") |
130 | 7 | << " authorization token file path: [" << token_path |
131 | 7 | << "]. If credentials come back empty, raise aws_log_level to 3 or higher for the " |
132 | 7 | "SDK's own reason."; |
133 | 7 | } |
134 | 9 | return provider; |
135 | 9 | } |
136 | | |
137 | 2 | std::string get_valid_ca_cert_path(const std::vector<std::string>& ca_cert_file_paths) { |
138 | 2 | for (const auto& path : ca_cert_file_paths) { |
139 | 2 | if (std::filesystem::exists(path)) { |
140 | 2 | return path; |
141 | 2 | } |
142 | 2 | } |
143 | 0 | return ""; |
144 | 2 | } |
145 | | } |