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