Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement SSOCredentialsProvider #189

Merged
merged 96 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
73daee8
maybe
waahm7 Mar 2, 2023
d01bb05
cleanup
waahm7 Mar 2, 2023
bea4bf0
basic impl for parsing region and start url
waahm7 Mar 2, 2023
559370e
rename sso_token_provider
waahm7 Mar 2, 2023
bb0302d
test infra
waahm7 Mar 2, 2023
ec164b8
adds invalid tests
waahm7 Mar 2, 2023
3348681
fix valid test
waahm7 Mar 2, 2023
3aaaec2
more tests
waahm7 Mar 2, 2023
3c50ecc
token file parsing initial
waahm7 Mar 5, 2023
ff5836e
test date
waahm7 Mar 5, 2023
085f791
adds fetch token logic
waahm7 Mar 5, 2023
819a39e
split into two different token providers
waahm7 Mar 6, 2023
d606b79
cleanup
waahm7 Mar 6, 2023
f566454
cleanup
waahm7 Mar 6, 2023
9382505
initial sso provider
waahm7 Mar 6, 2023
20d5a3a
in progress
waahm7 Mar 7, 2023
0247b0e
working temp test
waahm7 Mar 8, 2023
eef6038
rename to token_provider
waahm7 Mar 8, 2023
e79186a
rename files
waahm7 Mar 8, 2023
e3944d2
fix memory leaks
waahm7 Mar 8, 2023
fc8fb5a
fix get token async
waahm7 Mar 8, 2023
b962d5c
refactor sso token profile
waahm7 Mar 8, 2023
5d4ebf1
refactor
waahm7 Mar 11, 2023
01db30c
first working test with mock in utils
waahm7 Mar 11, 2023
2d4271e
basic sanity tests
waahm7 Mar 11, 2023
f2876e3
verify_credentials
waahm7 Mar 11, 2023
963978e
sso token failure test
waahm7 Mar 11, 2023
86ef49b
more tests
waahm7 Mar 12, 2023
672f889
fix expiration
waahm7 Mar 12, 2023
46d8a26
adds invalid config examples
waahm7 Mar 12, 2023
828cf2c
cleanup tests
waahm7 Mar 12, 2023
65688b0
fix token provider tests
waahm7 Mar 12, 2023
66657d9
adds token provider tests
waahm7 Mar 12, 2023
bfe3e18
test token expiration
waahm7 Mar 12, 2023
3d0fc78
refactor
waahm7 Mar 13, 2023
3121c8e
rename
waahm7 Mar 13, 2023
1ae7910
reset builder file
waahm7 Mar 13, 2023
2054cce
cleanup
waahm7 Mar 13, 2023
3afa155
import cleanup
waahm7 Mar 13, 2023
a7449ea
import cleanup
waahm7 Mar 13, 2023
d6ea07e
add a test for expired token in sso
waahm7 Mar 13, 2023
302386d
Adds comments
waahm7 Mar 14, 2023
0a11f1d
rename
waahm7 Mar 14, 2023
3569522
Update to latest sdkutils
waahm7 Mar 14, 2023
d2a0473
move aws_create_directory to credentials_provider utils
waahm7 Mar 14, 2023
0886c91
add a log
waahm7 Mar 17, 2023
f037f79
fix tests
waahm7 Mar 17, 2023
f10ffc3
Merge branch 'main' into token-provider-profile
waahm7 Mar 17, 2023
a6b3573
Fix logs and return error or callback
waahm7 Mar 17, 2023
c969bff
explicit conversion
waahm7 Mar 17, 2023
27749df
explicit conversion
waahm7 Mar 17, 2023
8f69d0e
add log
waahm7 Mar 17, 2023
affe702
fix getenv
waahm7 Mar 17, 2023
3dfbe06
update log with time difference
waahm7 Mar 17, 2023
f80c6cf
void ctx fix
waahm7 Mar 17, 2023
4f9eb6c
update expiration time
waahm7 Mar 17, 2023
b55ff71
update log
waahm7 Mar 17, 2023
d45acfb
update log
waahm7 Mar 17, 2023
cc9c03f
mock system clock for tests
waahm7 Mar 17, 2023
54f2353
fix warning
waahm7 Mar 17, 2023
71d5736
remove path for windows
waahm7 Mar 17, 2023
370dfab
stop trying to reset home
waahm7 Mar 18, 2023
de8d83e
change to aws config
waahm7 Mar 20, 2023
acda1c8
Fix new token
waahm7 Mar 20, 2023
d194521
more tests
waahm7 Mar 20, 2023
e55d86c
refactor token provider tests
waahm7 Mar 20, 2023
4e3d45a
remove sso credentials provider to split PR
waahm7 Mar 20, 2023
38afb6d
remove default case
waahm7 Mar 20, 2023
da5bc12
remove ms stuff from this PR
waahm7 Mar 20, 2023
5950222
remove mocked http connection manager
waahm7 Mar 20, 2023
6ae2410
remove top level object change
waahm7 Mar 20, 2023
39fd115
fix add test_case
waahm7 Mar 21, 2023
d709f59
move token provider to private header file
waahm7 Mar 27, 2023
00ed28e
add header file in tests
waahm7 Mar 27, 2023
8c80d9e
PR feedback
waahm7 Mar 29, 2023
bd78bb5
deep copy token path for now
waahm7 Mar 30, 2023
d2c9fc1
remove async
waahm7 Mar 30, 2023
8cbe450
try constructing platform specific paths
waahm7 Mar 30, 2023
8578682
rename to access_token
waahm7 Mar 30, 2023
fd2b7f9
improve logs
waahm7 Mar 30, 2023
749a74d
remove comment
waahm7 Mar 30, 2023
c549d77
re add comment
waahm7 Mar 30, 2023
5afddc0
add blank line
waahm7 Mar 30, 2023
405a2c5
update logs
waahm7 Mar 30, 2023
d7677a1
update name to sso token
waahm7 Mar 30, 2023
b555c3d
use aws_normalize_directory_separator
waahm7 Mar 30, 2023
7223348
Add docs for legacy way
waahm7 Apr 3, 2023
288373b
move token api to sso_token_utils
waahm7 Apr 3, 2023
6663f96
remove clock.h
waahm7 Apr 3, 2023
f8a6b49
test including token utils
waahm7 Apr 3, 2023
72ada93
remove include from token utils
waahm7 Apr 3, 2023
4fe3fff
Fix comment
waahm7 Apr 4, 2023
b6d0a2f
Merge branch 'main' into token-provider-profile
waahm7 Apr 5, 2023
b761bdc
Implement SSO Credentials Provider Part 2 (#191)
waahm7 May 1, 2023
9c5275d
Merge branch 'main' into token-provider-profile
waahm7 May 1, 2023
56d18dd
update user agent value
waahm7 May 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/aws/auth/auth.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ enum aws_auth_errors {
AWS_AUTH_SIGV4A_SIGNATURE_VALIDATION_FAILURE,
AWS_AUTH_CREDENTIALS_PROVIDER_COGNITO_SOURCE_FAILURE,
AWS_AUTH_CREDENTIALS_PROVIDER_DELEGATE_FAILURE,
AWS_AUTH_SSO_TOKEN_PROVIDER_SOURCE_FAILURE,
AWS_AUTH_SSO_TOKEN_INVALID,
AWS_AUTH_SSO_TOKEN_EXPIRED,
AWS_AUTH_CREDENTIALS_PROVIDER_SSO_SOURCE_FAILURE,

AWS_AUTH_ERROR_END_RANGE = AWS_ERROR_ENUM_END_RANGE(AWS_C_AUTH_PACKAGE_ID)
};
Expand Down
64 changes: 60 additions & 4 deletions include/aws/auth/credentials.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ struct aws_credentials_provider_environment_options {
};

/**
* Configuration options for a provider that sources credentials from the aws profile and credentials files
* (by default ~/.aws/profile and ~/.aws/credentials)
* Configuration options for a provider that sources credentials from the aws config and credentials files
* (by default ~/.aws/config and ~/.aws/credentials)
*/
struct aws_credentials_provider_profile_options {
struct aws_credentials_provider_shutdown_options shutdown_options;
Expand All @@ -114,7 +114,7 @@ struct aws_credentials_provider_profile_options {
/**
* (Optional)
* Use a cached merged profile collection. A merge collection has both config file
* (~/.aws/profile) and credentials file based profile collection (~/.aws/credentials) using
* (~/.aws/config) and credentials file based profile collection (~/.aws/credentials) using
* `aws_profile_collection_new_from_merge`.
* If this option is provided, `config_file_name_override` and `credentials_file_name_override` will be ignored.
*/
Expand Down Expand Up @@ -355,6 +355,49 @@ struct aws_credentials_provider_sts_web_identity_options {
struct aws_auth_http_system_vtable *function_table;
};

/*
* Configuration for the SSOCredentialsProvider that sends a GetRoleCredentialsRequest to the AWS Single
* Sign-On Service to maintain short-lived sessions to use for authentication.
*
* https://docs.aws.amazon.com/sdkref/latest/guide/feature-sso-credentials.html
*/
struct aws_credentials_provider_sso_options {
struct aws_credentials_provider_shutdown_options shutdown_options;

/*
* Override of what profile to use to source credentials from ('default' by default)
*/
struct aws_byte_cursor profile_name_override;

/*
* Override path to the profile config file (~/.aws/config by default)
*/
struct aws_byte_cursor config_file_name_override;

/**
* (Optional)
* Use a cached config profile collection. You can also pass a merged collection.
* config_file_name_override will be ignored if this option is provided.
*/
struct aws_profile_collection *config_file_cached;

/*
* Connection bootstrap to use for any network connections made while sourcing credentials
* Required.
*/
struct aws_client_bootstrap *bootstrap;

/*
* Client TLS context to use when querying SSO provider.
* Required.
*/
struct aws_tls_ctx *tls_ctx;

/* For mocking, leave NULL otherwise */
struct aws_auth_http_system_vtable *function_table;
aws_io_clock_fn *system_clock_fn;
};

/**
* Configuration options for the STS credentials provider
*/
Expand Down Expand Up @@ -457,7 +500,7 @@ struct aws_credentials_provider_chain_default_options {
/**
* (Optional)
* Use a cached merged profile collection. A merge collection has both config file
* (~/.aws/profile) and credentials file based profile collection (~/.aws/credentials) using
* (~/.aws/config) and credentials file based profile collection (~/.aws/credentials) using
* `aws_profile_collection_new_from_merge`.
* If this option is provided, `config_file_name_override` and `credentials_file_name_override` will be ignored.
*/
Expand Down Expand Up @@ -927,6 +970,19 @@ struct aws_credentials_provider *aws_credentials_provider_new_sts_web_identity(
struct aws_allocator *allocator,
const struct aws_credentials_provider_sts_web_identity_options *options);

/**
* Creates a provider that sources credentials from SSO using a SSOToken.
*
* @param allocator memory allocator to use for all memory allocation
* @param options provider-specific configuration options
*
* @return the newly-constructed credentials provider, or NULL if an error occurred.
*/
AWS_AUTH_API
struct aws_credentials_provider *aws_credentials_provider_new_sso(
struct aws_allocator *allocator,
const struct aws_credentials_provider_sso_options *options);

/*
* Creates a provider that sources credentials from running an external command or process
*
Expand Down
10 changes: 10 additions & 0 deletions include/aws/auth/private/credentials_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,15 @@ struct aws_auth_http_system_vtable {
enum aws_parse_credentials_expiration_format {
AWS_PCEF_STRING_ISO_8601_DATE,
AWS_PCEF_NUMBER_UNIX_EPOCH,
AWS_PCEF_NUMBER_UNIX_EPOCH_MS,
};

struct aws_parse_credentials_from_json_doc_options {
const char *access_key_id_name;
const char *secret_access_key_name;
const char *token_name;
const char *expiration_name;
const char *top_level_object_name;
enum aws_parse_credentials_expiration_format expiration_format;
bool token_required;
bool expiration_required;
Expand Down Expand Up @@ -160,6 +162,14 @@ struct aws_credentials *aws_parse_credentials_from_json_document(
AWS_AUTH_API
enum aws_retry_error_type aws_credentials_provider_compute_retry_error_type(int response_code, int error_code);

/*
* Loads an aws config profile collection
*/
AWS_AUTH_API
struct aws_profile_collection *aws_load_profile_collection_from_config_file(
struct aws_allocator *allocator,
struct aws_byte_cursor config_file_name_override);

AWS_EXTERN_C_END

#endif /* AWS_AUTH_CREDENTIALS_PRIVATE_H */
112 changes: 112 additions & 0 deletions include/aws/auth/private/sso_token_providers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#ifndef AWS_AUTH_TOKEN_PROVIDERS_PRIVATE_H
#define AWS_AUTH_TOKEN_PROVIDERS_PRIVATE_H

/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

#include <aws/auth/auth.h>
#include <aws/auth/credentials.h>

/**
* Configuration options for a provider that sources sso token information from the aws profile (by default
* ~/.aws/config) and token from ~/.aws/sso/cache/<sha1 of start url>.json.
waahm7 marked this conversation as resolved.
Show resolved Hide resolved
*/
struct aws_token_provider_sso_profile_options {
struct aws_credentials_provider_shutdown_options shutdown_options;

/*
* Override of what profile to use to source credentials from ('default' by default)
*/
struct aws_byte_cursor profile_name_override;

/*
* Override path to the profile config file (~/.aws/config by default)
*/
struct aws_byte_cursor config_file_name_override;

/**
* (Optional)
* Use a cached config profile collection. You can also pass a merged collection.
* config_file_name_override will be ignored if this option is provided.
*/
struct aws_profile_collection *config_file_cached;

/* For mocking, leave NULL otherwise */
aws_io_clock_fn *system_clock_fn;
};

/**
* Configuration options for a provider that sources sso token information from the aws profile (by default
* ~/.aws/config) and token from ~/.aws/sso/cache/<sha1 of session name>.json.
*/
struct aws_token_provider_sso_session_options {
struct aws_credentials_provider_shutdown_options shutdown_options;

/*
* Override of what profile to use to source credentials from ('default' by default)
*/
struct aws_byte_cursor profile_name_override;

/*
* Override path to the profile config file (~/.aws/config by default)
*/
struct aws_byte_cursor config_file_name_override;

/**
* (Optional)
* Use a cached config profile collection. You can also pass a merged collection.
* config_file_name_override will be ignored if this option is provided.
*/
struct aws_profile_collection *config_file_cached;

/*
* Connection bootstrap to use for any network connections made
*/
struct aws_client_bootstrap *bootstrap;

/*
* Client TLS context to use for any network connections made.
*/
struct aws_tls_ctx *tls_ctx;

/* For mocking, leave NULL otherwise */
aws_io_clock_fn *system_clock_fn;
};

AWS_EXTERN_C_BEGIN

/**
* Creates a provider that sources sso token based credentials from key-value profiles loaded from the aws
* config("~/.aws/config" by default) and ~/.aws/sso/cache/<sha1 of start url>.json
* This is the legacy way which doesn't support refreshing credentials.
*
* @param allocator memory allocator to use for all memory allocation
* @param options provider-specific configuration options
*
* @return the newly-constructed credentials provider, or NULL if an error occurred.
*/
AWS_AUTH_API
struct aws_credentials_provider *aws_token_provider_new_sso_profile(
struct aws_allocator *allocator,
const struct aws_token_provider_sso_profile_options *options);

/**
* Creates a provider that sources sso token based credentials from key-value profiles loaded from the aws
* config("~/.aws/config" by default) and ~/.aws/sso/cache/<sha1 of session name>.json
* Note: Token refresh is not currently supported
*
* @param allocator memory allocator to use for all memory allocation
* @param options provider-specific configuration options
*
* @return the newly-constructed credentials provider, or NULL if an error occurred.
*/
AWS_AUTH_API
struct aws_credentials_provider *aws_token_provider_new_sso_session(
struct aws_allocator *allocator,
const struct aws_token_provider_sso_session_options *options);

AWS_EXTERN_C_END

#endif /* AWS_AUTH_TOKEN_PROVIDERS_PRIVATE_H */
58 changes: 58 additions & 0 deletions include/aws/auth/private/sso_token_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#ifndef AWS_AUTH_TOKEN_PRIVATE_H
#define AWS_AUTH_TOKEN_PRIVATE_H

/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

#include <aws/auth/auth.h>
#include <aws/common/date_time.h>

/* structure to represent a parsed sso token */
struct aws_sso_token {
struct aws_allocator *allocator;

struct aws_string *access_token;
struct aws_date_time expiration;
};

AWS_EXTERN_C_BEGIN

/* Construct token path which is ~/.aws/sso/cache/<hex encoded sha1 of input>.json */
AWS_AUTH_API
struct aws_string *aws_construct_sso_token_path(struct aws_allocator *allocator, const struct aws_string *input);

AWS_AUTH_API
void aws_sso_token_destroy(struct aws_sso_token *token);

/* Parse `aws_sso_token` from the give file path */
AWS_AUTH_API
struct aws_sso_token *aws_sso_token_new_from_file(struct aws_allocator *allocator, const struct aws_string *file_path);

/**
* Creates a set of AWS credentials based on a token with expiration.
*
* @param allocator memory allocator to use for all memory allocation
* @param token token for the credentials
* @param expiration_timepoint_in_seconds time at which these credentials expire
* @return a new pair of AWS credentials, or NULL
*/
AWS_AUTH_API
struct aws_credentials *aws_credentials_new_token(
struct aws_allocator *allocator,
struct aws_byte_cursor token,
uint64_t expiration_timepoint_in_seconds);

/**
* Get the token from a set of AWS credentials
*
* @param credentials credentials to get the token from
* @return a byte cursor to the token or an empty byte cursor if there is no token
*/
AWS_AUTH_API
struct aws_byte_cursor aws_credentials_get_token(const struct aws_credentials *credentials);

AWS_EXTERN_C_END

#endif /* AWS_AUTH_TOKEN_PRIVATE_H */
12 changes: 12 additions & 0 deletions source/auth.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ static struct aws_error_info s_errors[] = {
AWS_DEFINE_ERROR_INFO_AUTH(
AWS_AUTH_CREDENTIALS_PROVIDER_DELEGATE_FAILURE,
"Valid credentials could not be sourced by the delegate provider"),
AWS_DEFINE_ERROR_INFO_AUTH(
AWS_AUTH_SSO_TOKEN_PROVIDER_SOURCE_FAILURE,
"Valid token could not be sourced by the sso token provider"),
AWS_DEFINE_ERROR_INFO_AUTH(
AWS_AUTH_SSO_TOKEN_INVALID,
"Token sourced by the sso token provider is invalid."),
AWS_DEFINE_ERROR_INFO_AUTH(
AWS_AUTH_SSO_TOKEN_EXPIRED,
"Token sourced by the sso token provider is expired."),
AWS_DEFINE_ERROR_INFO_AUTH(
AWS_AUTH_CREDENTIALS_PROVIDER_SSO_SOURCE_FAILURE,
"Valid credentials could not be sourced by the sso credentials provider"),

};
/* clang-format on */
Expand Down
Loading