credentials_provider_static.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. static int s_static_credentials_provider_get_credentials_async(
  8. struct aws_credentials_provider *provider,
  9. aws_on_get_credentials_callback_fn callback,
  10. void *user_data) {
  11. struct aws_credentials *credentials = provider->impl;
  12. AWS_LOGF_INFO(
  13. AWS_LS_AUTH_CREDENTIALS_PROVIDER,
  14. "(id=%p) Static credentials provider successfully sourced credentials",
  15. (void *)provider);
  16. callback(credentials, AWS_ERROR_SUCCESS, user_data);
  17. return AWS_OP_SUCCESS;
  18. }
  19. static void s_static_credentials_provider_destroy(struct aws_credentials_provider *provider) {
  20. struct aws_credentials *credentials = provider->impl;
  21. aws_credentials_release(credentials);
  22. aws_credentials_provider_invoke_shutdown_callback(provider);
  23. aws_mem_release(provider->allocator, provider);
  24. }
  25. /*
  26. * shared across all providers that do not need to do anything special on shutdown
  27. */
  28. static struct aws_credentials_provider_vtable s_aws_credentials_provider_static_vtable = {
  29. .get_credentials = s_static_credentials_provider_get_credentials_async,
  30. .destroy = s_static_credentials_provider_destroy,
  31. };
  32. struct aws_credentials_provider *aws_credentials_provider_new_static(
  33. struct aws_allocator *allocator,
  34. const struct aws_credentials_provider_static_options *options) {
  35. struct aws_credentials_provider *provider = aws_mem_acquire(allocator, sizeof(struct aws_credentials_provider));
  36. if (provider == NULL) {
  37. return NULL;
  38. }
  39. AWS_ZERO_STRUCT(*provider);
  40. struct aws_credentials *credentials = aws_credentials_new(
  41. allocator, options->access_key_id, options->secret_access_key, options->session_token, UINT64_MAX);
  42. if (credentials == NULL) {
  43. goto on_new_credentials_failure;
  44. }
  45. aws_credentials_provider_init_base(provider, allocator, &s_aws_credentials_provider_static_vtable, credentials);
  46. provider->shutdown_options = options->shutdown_options;
  47. return provider;
  48. on_new_credentials_failure:
  49. aws_mem_release(allocator, provider);
  50. return NULL;
  51. }