Skip to content

Commit

Permalink
Clean up delegate credentials provider (#118)
Browse files Browse the repository at this point in the history
- rename .c file. had a typo and was inconsistent with others.
- don't have 2 different shutdown callbacks in options, just offer 1.
- don't force user to clean up memory allocated by new_delegate()
  • Loading branch information
graebm committed Jan 6, 2021
1 parent f3a8329 commit f58e202
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 29 deletions.
13 changes: 9 additions & 4 deletions include/aws/auth/credentials.h
Original file line number Diff line number Diff line change
Expand Up @@ -406,21 +406,26 @@ struct aws_credentials_provider_chain_default_options {
struct aws_client_bootstrap *bootstrap;
};

typedef int(aws_credentials_provider_delegate_get_credentials_fn)(
void *delegate_user_data,
aws_on_get_credentials_callback_fn callback,
void *callback_user_data);

/**
* Configuration options for the delegate credentials provider.
*/
struct aws_credentials_provider_delegate_options {
struct aws_credentials_provider_shutdown_options shutdown_options;

/**
* Provide the vtable of functions to get credentials.
* Delegated get_credentials() callback.
*/
struct aws_credentials_provider_vtable *provider_vtable;
aws_credentials_provider_delegate_get_credentials_fn *get_credentials;

/**
* Optional implementation.
* User data for delegated callbacks.
*/
void *impl;
void *delegate_user_data;
};

AWS_EXTERN_C_BEGIN
Expand Down
25 changes: 0 additions & 25 deletions source/crdential_provider_delegate.c

This file was deleted.

64 changes: 64 additions & 0 deletions source/credentials_provider_delegate.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

#include <aws/auth/private/credentials_utils.h>

struct aws_credentials_provider_delegate_impl {
aws_credentials_provider_delegate_get_credentials_fn *get_credentials;
void *user_data;
};

static int s_credentials_provider_delegate_get_credentials(
struct aws_credentials_provider *provider,
aws_on_get_credentials_callback_fn callback,
void *callback_user_data) {

struct aws_credentials_provider_delegate_impl *impl = provider->impl;
return impl->get_credentials(impl->user_data, callback, callback_user_data);
}

static void s_credentials_provider_delegate_destroy(struct aws_credentials_provider *provider) {
aws_credentials_provider_invoke_shutdown_callback(provider);
aws_mem_release(provider->allocator, provider);
}

static struct aws_credentials_provider_vtable s_credentials_provider_delegate_vtable = {
.get_credentials = s_credentials_provider_delegate_get_credentials,
.destroy = s_credentials_provider_delegate_destroy,
};

struct aws_credentials_provider *aws_credentials_provider_new_delegate(
struct aws_allocator *allocator,
struct aws_credentials_provider_delegate_options *options) {

AWS_ASSERT(options);
AWS_ASSERT(options->get_credentials);

struct aws_credentials_provider *provider = NULL;
struct aws_credentials_provider_delegate_impl *impl = NULL;

aws_mem_acquire_many(
allocator,
2,
&provider,
sizeof(struct aws_credentials_provider),
&impl,
sizeof(struct aws_credentials_provider_delegate_impl));

if (!provider) {
return NULL;
}

AWS_ZERO_STRUCT(*provider);
AWS_ZERO_STRUCT(*impl);

aws_credentials_provider_init_base(provider, allocator, &s_credentials_provider_delegate_vtable, impl);
provider->shutdown_options = options->shutdown_options;

impl->get_credentials = options->get_credentials;
impl->user_data = options->delegate_user_data;

return provider;
}

0 comments on commit f58e202

Please sign in to comment.