common/cpp/obj-client/auth/aws_credential_factory.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_credential_factory.h" |
19 | | |
20 | | #include <aws/core/auth/AWSCredentials.h> |
21 | | #include <aws/core/auth/AWSCredentialsProvider.h> |
22 | | #include <aws/core/auth/AWSCredentialsProviderChain.h> |
23 | | #include <aws/core/auth/STSCredentialsProvider.h> |
24 | | #include <aws/core/platform/Environment.h> |
25 | | #include <aws/identity-management/auth/STSAssumeRoleCredentialsProvider.h> |
26 | | #include <aws/sts/STSClient.h> |
27 | | |
28 | | #include "cpp/custom_aws_credentials_provider_chain.h" |
29 | | |
30 | | namespace doris { |
31 | | namespace { |
32 | | |
33 | | using Provider = Aws::Auth::AWSCredentialsProvider; |
34 | | |
35 | 24 | std::shared_ptr<Provider> create_v2_base_provider(CredProviderType type) { |
36 | 24 | switch (type) { |
37 | 2 | case CredProviderType::Env: |
38 | 2 | return std::make_shared<Aws::Auth::EnvironmentAWSCredentialsProvider>(); |
39 | 2 | case CredProviderType::SystemProperties: |
40 | 2 | return std::make_shared<Aws::Auth::ProfileConfigFileAWSCredentialsProvider>(); |
41 | 3 | case CredProviderType::WebIdentity: |
42 | 3 | return std::make_shared<Aws::Auth::STSAssumeRoleWebIdentityCredentialsProvider>(); |
43 | 2 | case CredProviderType::Container: |
44 | 2 | return std::make_shared<Aws::Auth::TaskRoleCredentialsProvider>( |
45 | 2 | Aws::Environment::GetEnv("AWS_CONTAINER_CREDENTIALS_RELATIVE_URI").c_str()); |
46 | 4 | case CredProviderType::Anonymous: |
47 | 4 | return std::make_shared<Aws::Auth::AnonymousAWSCredentialsProvider>(); |
48 | 3 | case CredProviderType::Default: |
49 | 5 | case CredProviderType::Simple: |
50 | 5 | return std::make_shared<CustomAwsCredentialsProviderChain>(); |
51 | 6 | case CredProviderType::InstanceProfile: |
52 | 6 | return std::make_shared<Aws::Auth::InstanceProfileCredentialsProvider>(); |
53 | 24 | } |
54 | 0 | __builtin_unreachable(); |
55 | 24 | } |
56 | | |
57 | | AwsCredentialResult assume_role(const AwsCredentialOptions& options, |
58 | 12 | std::shared_ptr<Provider> base_provider) { |
59 | 12 | auto sts_client = |
60 | 12 | std::make_shared<Aws::STS::STSClient>(base_provider, options.sts_client_config); |
61 | 12 | return { |
62 | 12 | .provider = std::make_shared<Aws::Auth::STSAssumeRoleCredentialsProvider>( |
63 | 12 | options.role_arn, Aws::String(), options.external_id, |
64 | 12 | Aws::Auth::DEFAULT_CREDS_LOAD_FREQ_SECONDS, std::move(sts_client)), |
65 | 12 | }; |
66 | 12 | } |
67 | | |
68 | | } // namespace |
69 | | |
70 | 36 | AwsCredentialResult AwsCredentialFactory::create(const AwsCredentialOptions& options) { |
71 | 36 | const bool has_access_key = !options.access_key.empty(); |
72 | 36 | const bool has_secret_key = !options.secret_key.empty(); |
73 | | |
74 | 36 | if (has_access_key && has_secret_key) { |
75 | 6 | Aws::Auth::AWSCredentials credentials(options.access_key, options.secret_key); |
76 | 6 | if (!options.session_token.empty()) { |
77 | 0 | credentials.SetSessionToken(options.session_token); |
78 | 0 | } |
79 | 6 | return { |
80 | 6 | .provider = std::make_shared<Aws::Auth::SimpleAWSCredentialsProvider>( |
81 | 6 | std::move(credentials)), |
82 | 6 | }; |
83 | 6 | } |
84 | | |
85 | 30 | if (options.version == AwsCredentialProviderVersion::V1) { |
86 | 6 | if (options.provider_type == CredProviderType::InstanceProfile) { |
87 | 2 | auto base = std::make_shared<Aws::Auth::InstanceProfileCredentialsProvider>(); |
88 | 2 | return options.role_arn.empty() ? AwsCredentialResult {.provider = std::move(base)} |
89 | 2 | : assume_role(options, std::move(base)); |
90 | 2 | } |
91 | 4 | if (!has_access_key && !has_secret_key && |
92 | 4 | options.empty_credentials == EmptyCredentialsBehavior::ANONYMOUS) { |
93 | 2 | return { |
94 | 2 | .provider = std::make_shared<Aws::Auth::AnonymousAWSCredentialsProvider>(), |
95 | 2 | }; |
96 | 2 | } |
97 | 2 | return { |
98 | 2 | .provider = std::make_shared<Aws::Auth::DefaultAWSCredentialsProviderChain>(), |
99 | 2 | }; |
100 | 4 | } |
101 | | |
102 | 24 | auto base = create_v2_base_provider(options.provider_type); |
103 | 24 | return options.role_arn.empty() ? AwsCredentialResult {.provider = std::move(base)} |
104 | 24 | : assume_role(options, std::move(base)); |
105 | 30 | } |
106 | | |
107 | | } // namespace doris |