credentials_provider_chain.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/auth/credentials.h>
  6. #include <aws/auth/private/credentials_utils.h>
  7. struct aws_credentials_provider_chain_impl {
  8. struct aws_array_list providers;
  9. };
  10. struct aws_credentials_provider_chain_user_data {
  11. struct aws_allocator *allocator;
  12. struct aws_credentials_provider *provider_chain;
  13. size_t current_provider_index;
  14. aws_on_get_credentials_callback_fn *original_callback;
  15. void *original_user_data;
  16. };
  17. static void s_aws_provider_chain_member_callback(struct aws_credentials *credentials, int error_code, void *user_data) {
  18. struct aws_credentials_provider_chain_user_data *wrapped_user_data = user_data;
  19. struct aws_credentials_provider *provider = wrapped_user_data->provider_chain;
  20. struct aws_credentials_provider_chain_impl *impl = provider->impl;
  21. size_t provider_count = aws_array_list_length(&impl->providers);
  22. if (credentials != NULL || wrapped_user_data->current_provider_index + 1 >= provider_count) {
  23. AWS_LOGF_INFO(
  24. AWS_LS_AUTH_CREDENTIALS_PROVIDER,
  25. "(id=%p) Credentials provider chain callback terminating on index %zu, with %s credentials and error code "
  26. "%d",
  27. (void *)provider,
  28. wrapped_user_data->current_provider_index + 1,
  29. (credentials != NULL) ? "valid" : "invalid",
  30. error_code);
  31. goto on_terminate_chain;
  32. }
  33. AWS_LOGF_DEBUG(
  34. AWS_LS_AUTH_CREDENTIALS_PROVIDER,
  35. "(id=%p) Credentials provider chain callback %zu invoked with %s credentials and error code %d",
  36. (void *)provider,
  37. wrapped_user_data->current_provider_index + 1,
  38. (credentials != NULL) ? "valid" : "invalid",
  39. error_code);
  40. wrapped_user_data->current_provider_index++;
  41. /*
  42. * TODO: Immutable data, shouldn't need a lock, but we might need a fence and we don't have one atm
  43. */
  44. struct aws_credentials_provider *next_provider = NULL;
  45. if (aws_array_list_get_at(&impl->providers, &next_provider, wrapped_user_data->current_provider_index)) {
  46. goto on_terminate_chain;
  47. }
  48. AWS_LOGF_DEBUG(
  49. AWS_LS_AUTH_CREDENTIALS_PROVIDER,
  50. "(id=%p) Credentials provider chain invoking chain member #%zu",
  51. (void *)provider,
  52. wrapped_user_data->current_provider_index);
  53. aws_credentials_provider_get_credentials(next_provider, s_aws_provider_chain_member_callback, wrapped_user_data);
  54. return;
  55. on_terminate_chain:
  56. wrapped_user_data->original_callback(credentials, error_code, wrapped_user_data->original_user_data);
  57. aws_credentials_provider_release(provider);
  58. aws_mem_release(wrapped_user_data->allocator, wrapped_user_data);
  59. }
  60. static int s_credentials_provider_chain_get_credentials_async(
  61. struct aws_credentials_provider *provider,
  62. aws_on_get_credentials_callback_fn callback,
  63. void *user_data) {
  64. struct aws_credentials_provider_chain_impl *impl = provider->impl;
  65. struct aws_credentials_provider *first_provider = NULL;
  66. if (aws_array_list_get_at(&impl->providers, &first_provider, 0)) {
  67. return AWS_OP_ERR;
  68. }
  69. struct aws_credentials_provider_chain_user_data *wrapped_user_data =
  70. aws_mem_acquire(provider->allocator, sizeof(struct aws_credentials_provider_chain_user_data));
  71. if (wrapped_user_data == NULL) {
  72. return AWS_OP_ERR;
  73. }
  74. AWS_ZERO_STRUCT(*wrapped_user_data);
  75. wrapped_user_data->allocator = provider->allocator;
  76. wrapped_user_data->provider_chain = provider;
  77. wrapped_user_data->current_provider_index = 0;
  78. wrapped_user_data->original_user_data = user_data;
  79. wrapped_user_data->original_callback = callback;
  80. aws_credentials_provider_acquire(provider);
  81. AWS_LOGF_DEBUG(
  82. AWS_LS_AUTH_CREDENTIALS_PROVIDER,
  83. "(id=%p) Credentials provider chain get credentials dispatch",
  84. (void *)provider);
  85. aws_credentials_provider_get_credentials(first_provider, s_aws_provider_chain_member_callback, wrapped_user_data);
  86. return AWS_OP_SUCCESS;
  87. }
  88. static void s_credentials_provider_chain_destroy(struct aws_credentials_provider *provider) {
  89. struct aws_credentials_provider_chain_impl *impl = provider->impl;
  90. if (impl == NULL) {
  91. return;
  92. }
  93. size_t provider_count = aws_array_list_length(&impl->providers);
  94. for (size_t i = 0; i < provider_count; ++i) {
  95. struct aws_credentials_provider *chain_member = NULL;
  96. if (aws_array_list_get_at(&impl->providers, &chain_member, i)) {
  97. continue;
  98. }
  99. aws_credentials_provider_release(chain_member);
  100. }
  101. /* Invoke our own shutdown callback */
  102. aws_credentials_provider_invoke_shutdown_callback(provider);
  103. aws_array_list_clean_up(&impl->providers);
  104. aws_mem_release(provider->allocator, provider);
  105. }
  106. static struct aws_credentials_provider_vtable s_aws_credentials_provider_chain_vtable = {
  107. .get_credentials = s_credentials_provider_chain_get_credentials_async,
  108. .destroy = s_credentials_provider_chain_destroy,
  109. };
  110. struct aws_credentials_provider *aws_credentials_provider_new_chain(
  111. struct aws_allocator *allocator,
  112. const struct aws_credentials_provider_chain_options *options) {
  113. if (options->provider_count == 0) {
  114. return NULL;
  115. }
  116. struct aws_credentials_provider *provider = NULL;
  117. struct aws_credentials_provider_chain_impl *impl = NULL;
  118. aws_mem_acquire_many(
  119. allocator,
  120. 2,
  121. &provider,
  122. sizeof(struct aws_credentials_provider),
  123. &impl,
  124. sizeof(struct aws_credentials_provider_chain_impl));
  125. if (!provider) {
  126. return NULL;
  127. }
  128. AWS_ZERO_STRUCT(*provider);
  129. AWS_ZERO_STRUCT(*impl);
  130. aws_credentials_provider_init_base(provider, allocator, &s_aws_credentials_provider_chain_vtable, impl);
  131. if (aws_array_list_init_dynamic(
  132. &impl->providers, allocator, options->provider_count, sizeof(struct aws_credentials_provider *))) {
  133. goto on_error;
  134. }
  135. for (size_t i = 0; i < options->provider_count; ++i) {
  136. struct aws_credentials_provider *sub_provider = options->providers[i];
  137. if (aws_array_list_push_back(&impl->providers, &sub_provider)) {
  138. goto on_error;
  139. }
  140. aws_credentials_provider_acquire(sub_provider);
  141. }
  142. provider->shutdown_options = options->shutdown_options;
  143. return provider;
  144. on_error:
  145. aws_credentials_provider_destroy(provider);
  146. return NULL;
  147. }