credentials_provider_environment.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #include <aws/common/environment.h>
  8. #include <aws/common/string.h>
  9. AWS_STATIC_STRING_FROM_LITERAL(s_access_key_id_env_var, "AWS_ACCESS_KEY_ID");
  10. AWS_STATIC_STRING_FROM_LITERAL(s_secret_access_key_env_var, "AWS_SECRET_ACCESS_KEY");
  11. AWS_STATIC_STRING_FROM_LITERAL(s_session_token_env_var, "AWS_SESSION_TOKEN");
  12. static int s_credentials_provider_environment_get_credentials_async(
  13. struct aws_credentials_provider *provider,
  14. aws_on_get_credentials_callback_fn callback,
  15. void *user_data) {
  16. struct aws_allocator *allocator = provider->allocator;
  17. struct aws_string *access_key_id = NULL;
  18. struct aws_string *secret_access_key = NULL;
  19. struct aws_string *session_token = NULL;
  20. struct aws_credentials *credentials = NULL;
  21. int error_code = AWS_ERROR_SUCCESS;
  22. aws_get_environment_value(allocator, s_access_key_id_env_var, &access_key_id);
  23. aws_get_environment_value(allocator, s_secret_access_key_env_var, &secret_access_key);
  24. aws_get_environment_value(allocator, s_session_token_env_var, &session_token);
  25. if (access_key_id != NULL && secret_access_key != NULL) {
  26. credentials =
  27. aws_credentials_new_from_string(allocator, access_key_id, secret_access_key, session_token, UINT64_MAX);
  28. if (credentials == NULL) {
  29. error_code = aws_last_error();
  30. }
  31. } else {
  32. error_code = AWS_AUTH_CREDENTIALS_PROVIDER_INVALID_ENVIRONMENT;
  33. }
  34. callback(credentials, error_code, user_data);
  35. aws_credentials_release(credentials);
  36. aws_string_destroy(session_token);
  37. aws_string_destroy(secret_access_key);
  38. aws_string_destroy(access_key_id);
  39. return AWS_OP_SUCCESS;
  40. }
  41. static void s_credentials_provider_environment_destroy(struct aws_credentials_provider *provider) {
  42. aws_credentials_provider_invoke_shutdown_callback(provider);
  43. aws_mem_release(provider->allocator, provider);
  44. }
  45. static struct aws_credentials_provider_vtable s_aws_credentials_provider_environment_vtable = {
  46. .get_credentials = s_credentials_provider_environment_get_credentials_async,
  47. .destroy = s_credentials_provider_environment_destroy,
  48. };
  49. struct aws_credentials_provider *aws_credentials_provider_new_environment(
  50. struct aws_allocator *allocator,
  51. const struct aws_credentials_provider_environment_options *options) {
  52. struct aws_credentials_provider *provider = aws_mem_acquire(allocator, sizeof(struct aws_credentials_provider));
  53. if (provider == NULL) {
  54. return NULL;
  55. }
  56. AWS_ZERO_STRUCT(*provider);
  57. aws_credentials_provider_init_base(provider, allocator, &s_aws_credentials_provider_environment_vtable, NULL);
  58. provider->shutdown_options = options->shutdown_options;
  59. return provider;
  60. }