| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/auth/credentials.h> |
| 7 |
#include <aws/auth/private/aws_profile.h> |
| 8 |
#include <aws/auth/private/credentials_utils.h> |
| 9 |
#include <aws/common/clock.h> |
| 10 |
#include <aws/common/date_time.h> |
| 11 |
#include <aws/common/environment.h> |
| 12 |
#include <aws/common/process.h> |
| 13 |
#include <aws/common/string.h> |
| 14 |
|
| 15 |
#if defined(_MSC_VER) |
| 16 |
# pragma warning(disable : 4204) |
| 17 |
#endif |
| 18 |
|
| 19 |
struct aws_credentials_provider_process_impl { |
| 20 |
struct aws_string *command; |
| 21 |
}; |
| 22 |
|
| 23 |
static int s_get_credentials_from_process( |
| 24 |
struct aws_credentials_provider *provider, |
| 25 |
aws_on_get_credentials_callback_fn callback, |
| 26 |
void *user_data) { |
| 27 |
|
| 28 |
struct aws_credentials_provider_process_impl *impl = provider->impl; |
| 29 |
struct aws_credentials *credentials = NULL; |
| 30 |
struct aws_run_command_options options = { |
| 31 |
.command = aws_string_c_str(impl->command), |
| 32 |
}; |
| 33 |
|
| 34 |
struct aws_run_command_result result; |
| 35 |
if (aws_run_command_result_init(provider->allocator, &result)) { |
| 36 |
goto on_finish; |
| 37 |
} |
| 38 |
|
| 39 |
if (aws_run_command(provider->allocator, &options, &result) || result.ret_code || !result.std_out) { |
| 40 |
AWS_LOGF_ERROR( |
| 41 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 42 |
"(id=%p) Failed to source credentials from running process credentials provider with command: %s, err:%s", |
| 43 |
(void *)provider, |
| 44 |
aws_string_c_str(impl->command), |
| 45 |
aws_error_str(aws_last_error())); |
| 46 |
goto on_finish; |
| 47 |
} |
| 48 |
|
| 49 |
struct aws_parse_credentials_from_json_doc_options parse_options = { |
| 50 |
.access_key_id_name = "AccessKeyId", |
| 51 |
.secret_access_key_name = "SecretAccessKey", |
| 52 |
.token_name = "SessionToken", |
| 53 |
.account_id_name = "AccountId", |
| 54 |
.expiration_name = "Expiration", |
| 55 |
.token_required = false, |
| 56 |
.expiration_required = false, |
| 57 |
}; |
| 58 |
|
| 59 |
credentials = aws_parse_credentials_from_json_document( |
| 60 |
provider->allocator, aws_byte_cursor_from_string(result.std_out), &parse_options); |
| 61 |
if (!credentials) { |
| 62 |
AWS_LOGF_INFO( |
| 63 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 64 |
"(id=%p) Process credentials provider failed to parse credentials from command output (output is not " |
| 65 |
"logged in case sensitive information).", |
| 66 |
(void *)provider); |
| 67 |
goto on_finish; |
| 68 |
} |
| 69 |
|
| 70 |
AWS_LOGF_INFO( |
| 71 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 72 |
"(id=%p) Process credentials provider successfully sourced credentials.", |
| 73 |
(void *)provider); |
| 74 |
|
| 75 |
on_finish: |
| 76 |
|
| 77 |
; |
| 78 |
int error_code = AWS_ERROR_SUCCESS; |
| 79 |
if (credentials == NULL) { |
| 80 |
error_code = aws_last_error(); |
| 81 |
if (error_code == AWS_ERROR_SUCCESS) { |
| 82 |
error_code = AWS_AUTH_CREDENTIALS_PROVIDER_PROCESS_SOURCE_FAILURE; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
callback(credentials, error_code, user_data); |
| 87 |
aws_run_command_result_cleanup(&result); |
| 88 |
aws_credentials_release(credentials); |
| 89 |
return AWS_OP_SUCCESS; |
| 90 |
} |
| 91 |
|
| 92 |
static void s_credentials_provider_process_destroy(struct aws_credentials_provider *provider) { |
| 93 |
struct aws_credentials_provider_process_impl *impl = provider->impl; |
| 94 |
if (impl) { |
| 95 |
aws_string_destroy_secure(impl->command); |
| 96 |
} |
| 97 |
aws_credentials_provider_invoke_shutdown_callback(provider); |
| 98 |
aws_mem_release(provider->allocator, provider); |
| 99 |
} |
| 100 |
|
| 101 |
AWS_STATIC_STRING_FROM_LITERAL(s_credentials_process, "credential_process"); |
| 102 |
|
| 103 |
static struct aws_profile_collection *s_load_profile(struct aws_allocator *allocator) { |
| 104 |
|
| 105 |
struct aws_profile_collection *config_profiles = NULL; |
| 106 |
struct aws_string *config_file_path = NULL; |
| 107 |
|
| 108 |
config_file_path = aws_get_config_file_path(allocator, NULL); |
| 109 |
if (!config_file_path) { |
| 110 |
AWS_LOGF_ERROR( |
| 111 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 112 |
"Failed to resolve config file path during process credentials provider initialization: %s", |
| 113 |
aws_error_str(aws_last_error())); |
| 114 |
goto on_done; |
| 115 |
} |
| 116 |
|
| 117 |
config_profiles = aws_profile_collection_new_from_file(allocator, config_file_path, AWS_PST_CONFIG); |
| 118 |
if (config_profiles != NULL) { |
| 119 |
AWS_LOGF_DEBUG( |
| 120 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 121 |
"Successfully built config profile collection from file at (%s)", |
| 122 |
aws_string_c_str(config_file_path)); |
| 123 |
} else { |
| 124 |
AWS_LOGF_ERROR( |
| 125 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 126 |
"Failed to build config profile collection from file at (%s) : %s", |
| 127 |
aws_string_c_str(config_file_path), |
| 128 |
aws_error_str(aws_last_error())); |
| 129 |
goto on_done; |
| 130 |
} |
| 131 |
|
| 132 |
on_done: |
| 133 |
aws_string_destroy(config_file_path); |
| 134 |
return config_profiles; |
| 135 |
} |
| 136 |
|
| 137 |
static void s_check_or_get_with_profile_config( |
| 138 |
struct aws_allocator *allocator, |
| 139 |
const struct aws_profile *profile, |
| 140 |
const struct aws_string *config_key, |
| 141 |
struct aws_byte_buf *target) { |
| 142 |
|
| 143 |
if (!allocator || !profile || !config_key || !target) { |
| 144 |
return; |
| 145 |
} |
| 146 |
if (!target->len) { |
| 147 |
aws_byte_buf_clean_up(target); |
| 148 |
const struct aws_profile_property *property = aws_profile_get_property(profile, config_key); |
| 149 |
if (property) { |
| 150 |
aws_byte_buf_init_copy_from_cursor( |
| 151 |
target, allocator, aws_byte_cursor_from_string(aws_profile_property_get_value(property))); |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
#ifdef _WIN32 |
| 167 |
static struct aws_byte_cursor s_stderr_redirect_to_devnull = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(" 2> nul"); |
| 168 |
#else |
| 169 |
static struct aws_byte_cursor s_stderr_redirect_to_devnull = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(" 2> /dev/null"); |
| 170 |
#endif |
| 171 |
|
| 172 |
static struct aws_string *s_get_command( |
| 173 |
struct aws_allocator *allocator, |
| 174 |
const struct aws_credentials_provider_process_options *options) { |
| 175 |
|
| 176 |
struct aws_byte_buf command_buf; |
| 177 |
AWS_ZERO_STRUCT(command_buf); |
| 178 |
struct aws_string *command = NULL; |
| 179 |
struct aws_profile_collection *config_profiles = NULL; |
| 180 |
struct aws_string *profile_name = NULL; |
| 181 |
const struct aws_profile *profile = NULL; |
| 182 |
if (options->config_profile_collection_cached) { |
| 183 |
config_profiles = aws_profile_collection_acquire(options->config_profile_collection_cached); |
| 184 |
} else { |
| 185 |
config_profiles = s_load_profile(allocator); |
| 186 |
} |
| 187 |
profile_name = aws_get_profile_name(allocator, &options->profile_to_use); |
| 188 |
|
| 189 |
if (config_profiles && profile_name) { |
| 190 |
profile = aws_profile_collection_get_profile(config_profiles, profile_name); |
| 191 |
} |
| 192 |
|
| 193 |
if (!profile) { |
| 194 |
AWS_LOGF_ERROR( |
| 195 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 196 |
"Failed to resolve config profile during process credentials provider initialization."); |
| 197 |
goto on_finish; |
| 198 |
} |
| 199 |
|
| 200 |
s_check_or_get_with_profile_config(allocator, profile, s_credentials_process, &command_buf); |
| 201 |
|
| 202 |
if (!command_buf.len) { |
| 203 |
AWS_LOGF_ERROR( |
| 204 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 205 |
"Failed to resolve credentials_process command during process credentials provider initialization."); |
| 206 |
goto on_finish; |
| 207 |
} |
| 208 |
|
| 209 |
if (aws_byte_buf_append_dynamic(&command_buf, &s_stderr_redirect_to_devnull)) { |
| 210 |
goto on_finish; |
| 211 |
} |
| 212 |
|
| 213 |
command = aws_string_new_from_array(allocator, command_buf.buffer, command_buf.len); |
| 214 |
if (!command) { |
| 215 |
goto on_finish; |
| 216 |
} |
| 217 |
|
| 218 |
AWS_LOGF_DEBUG( |
| 219 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 220 |
"Successfully loaded credentials_process command for process credentials provider."); |
| 221 |
|
| 222 |
on_finish: |
| 223 |
aws_string_destroy(profile_name); |
| 224 |
aws_profile_collection_release(config_profiles); |
| 225 |
aws_byte_buf_clean_up_secure(&command_buf); |
| 226 |
return command; |
| 227 |
} |
| 228 |
|
| 229 |
static struct aws_credentials_provider_vtable s_aws_credentials_provider_process_vtable = { |
| 230 |
.get_credentials = s_get_credentials_from_process, |
| 231 |
.destroy = s_credentials_provider_process_destroy, |
| 232 |
}; |
| 233 |
|
| 234 |
struct aws_credentials_provider *aws_credentials_provider_new_process( |
| 235 |
struct aws_allocator *allocator, |
| 236 |
const struct aws_credentials_provider_process_options *options) { |
| 237 |
|
| 238 |
struct aws_credentials_provider *provider = NULL; |
| 239 |
struct aws_credentials_provider_process_impl *impl = NULL; |
| 240 |
|
| 241 |
aws_mem_acquire_many( |
| 242 |
allocator, |
| 243 |
2, |
| 244 |
&provider, |
| 245 |
sizeof(struct aws_credentials_provider), |
| 246 |
&impl, |
| 247 |
sizeof(struct aws_credentials_provider_process_impl)); |
| 248 |
|
| 249 |
if (!provider) { |
| 250 |
goto on_error; |
| 251 |
} |
| 252 |
|
| 253 |
AWS_ZERO_STRUCT(*provider); |
| 254 |
AWS_ZERO_STRUCT(*impl); |
| 255 |
|
| 256 |
impl->command = s_get_command(allocator, options); |
| 257 |
if (!impl->command) { |
| 258 |
goto on_error; |
| 259 |
} |
| 260 |
|
| 261 |
aws_credentials_provider_init_base(provider, allocator, &s_aws_credentials_provider_process_vtable, impl); |
| 262 |
provider->shutdown_options = options->shutdown_options; |
| 263 |
AWS_LOGF_TRACE( |
| 264 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 265 |
"(id=%p): Successfully initializing a process credentials provider.", |
| 266 |
(void *)provider); |
| 267 |
|
| 268 |
return provider; |
| 269 |
|
| 270 |
on_error: |
| 271 |
aws_mem_release(allocator, provider); |
| 272 |
return NULL; |
| 273 |
} |