diff --git a/Makefile.frag b/Makefile.frag index dae5778..dadf724 100644 --- a/Makefile.frag +++ b/Makefile.frag @@ -10,7 +10,7 @@ ifeq (, $(shell which cmake3)) CMAKE = cmake endif -CMAKE_CONFIGURE = $(CMAKE) -DCMAKE_INSTALL_PREFIX=$(INT_DIR) -DCMAKE_PREFIX_PATH=$(INT_DIR) -DBUILD_TESTING=OFF +CMAKE_CONFIGURE = $(CMAKE) -DCMAKE_INSTALL_PREFIX=$(INT_DIR) -DCMAKE_PREFIX_PATH=$(INT_DIR) -DBUILD_TESTING=OFF -DCMAKE_BUILD_TYPE=$(CMAKE_BUILD_TYPE) CMAKE_BUILD = $(CMAKE) --build CMAKE_BUILD_TYPE ?= RelWithDebInfo CMAKE_TARGET = --config $(CMAKE_BUILD_TYPE) --target install @@ -63,14 +63,7 @@ ffi: src/libaws-crt-ffi.so src/libaws-crt-ffi.so: $(BUILD_DIR)/aws-crt-ffi-shared/libaws-crt-ffi.so src/api.h cp -v $(BUILD_DIR)/aws-crt-ffi-shared/libaws-crt-ffi.so src/libaws-crt-ffi.so -# Test the FFI interface on PHP7+ -ifeq ($(AT_LEAST_PHP7), 1) -test: src/libaws-crt-ffi.so - composer update - AWS_CRT_PHP_EXTENSION=1 composer run test - AWS_CRT_PHP_FFI=1 composer run test -else +# Use PHPUnit to run tests test: ext/api.h ext/awscrt_arginfo.h ext/crt.lo composer update composer run test -endif diff --git a/crt/aws-crt-ffi b/crt/aws-crt-ffi index 93a2f64..6337aab 160000 --- a/crt/aws-crt-ffi +++ b/crt/aws-crt-ffi @@ -1 +1 @@ -Subproject commit 93a2f64b7edf4ca39651657e4bee673d55da24a4 +Subproject commit 6337aabdfb48145387a0d05a1b5c58a7edf7bb88 diff --git a/ext/awscrt.stub.php b/ext/awscrt.stub.php index bab5cc1..ac6423b 100644 --- a/ext/awscrt.stub.php +++ b/ext/awscrt.stub.php @@ -10,12 +10,42 @@ function aws_crt_error_str(int $error_code): string {} function aws_crt_error_debug_str(int $error_code): string {} -function aws_crt_event_loop_group_options_new(): object {} +function aws_crt_event_loop_group_options_new(): int {} -function aws_crt_event_loop_group_options_release(object $elg_options): void {} +function aws_crt_event_loop_group_options_release(int $elg_options): void {} -function aws_crt_event_loop_group_options_set_max_threads(object $elg_options, int $max_threads): void {} +function aws_crt_event_loop_group_options_set_max_threads(int $elg_options, int $max_threads): void {} -function aws_crt_event_loop_group_new(object $options): object {} +function aws_crt_event_loop_group_new(int $options): int {} -function aws_crt_event_loop_group_release(object $event_loop_group): void {} +function aws_crt_event_loop_group_release(int $event_loop_group): void {} + +function aws_crt_credentials_options_new(): int {} + +function aws_crt_credentials_options_release(int $options): void {} + +function aws_crt_credentials_options_set_access_key_id(int $options, string $access_key_id): void {} + +function aws_crt_credentials_options_set_secret_access_key(int $options, string $secret_access_key): void {} + +function aws_crt_credentials_options_set_session_token(int $options, string $session_token): void {} + +function aws_crt_credentials_options_set_expiration_timepoint_seconds(int $options, int $expiration_timepoint_seconds): void {} + +function aws_crt_credentials_new(int $options): int {} + +function aws_crt_credentials_release(int $credentials): void {} + +function aws_crt_credentials_provider_release(int $credentials): void {} + +function aws_crt_credentials_provider_static_options_new(): int {} + +function aws_crt_credentials_provider_static_options_release(int $options): void {} + +function aws_crt_credentials_provider_static_options_set_access_key_id(int $options, string $access_key_id): void {} + +function aws_crt_credentials_provider_static_options_set_secret_access_key(int $options, string $secret_access_key): void {} + +function aws_crt_credentials_provider_static_options_set_session_token(int $options, string $session_token): void {} + +function aws_crt_credentials_provider_static_new(int $options): int {} diff --git a/ext/awscrt_arginfo.h b/ext/awscrt_arginfo.h index b4d6fa3..aa636fa 100644 --- a/ext/awscrt_arginfo.h +++ b/ext/awscrt_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: a2ad6edf64d0416356ba2a9a4c8b184615054a7c */ + * Stub hash: c681786667e5b7755b2874e4862ea7f162ef61ee */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_last_error, 0, 0, IS_LONG, 0) ZEND_END_ARG_INFO() @@ -12,26 +12,71 @@ ZEND_END_ARG_INFO() #define arginfo_aws_crt_error_debug_str arginfo_aws_crt_error_name -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_event_loop_group_options_new, 0, 0, IS_OBJECT, 0) -ZEND_END_ARG_INFO() +#define arginfo_aws_crt_event_loop_group_options_new arginfo_aws_crt_last_error ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_event_loop_group_options_release, 0, 1, IS_VOID, 0) - ZEND_ARG_TYPE_INFO(0, elg_options, IS_OBJECT, 0) + ZEND_ARG_TYPE_INFO(0, elg_options, IS_LONG, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_event_loop_group_options_set_max_threads, 0, 2, IS_VOID, 0) - ZEND_ARG_TYPE_INFO(0, elg_options, IS_OBJECT, 0) + ZEND_ARG_TYPE_INFO(0, elg_options, IS_LONG, 0) ZEND_ARG_TYPE_INFO(0, max_threads, IS_LONG, 0) ZEND_END_ARG_INFO() -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_event_loop_group_new, 0, 1, IS_OBJECT, 0) - ZEND_ARG_TYPE_INFO(0, options, IS_OBJECT, 0) +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_event_loop_group_new, 0, 1, IS_LONG, 0) + ZEND_ARG_TYPE_INFO(0, options, IS_LONG, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_event_loop_group_release, 0, 1, IS_VOID, 0) - ZEND_ARG_TYPE_INFO(0, event_loop_group, IS_OBJECT, 0) + ZEND_ARG_TYPE_INFO(0, event_loop_group, IS_LONG, 0) ZEND_END_ARG_INFO() +#define arginfo_aws_crt_credentials_options_new arginfo_aws_crt_last_error + +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_credentials_options_release, 0, 1, IS_VOID, 0) + ZEND_ARG_TYPE_INFO(0, options, IS_LONG, 0) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_credentials_options_set_access_key_id, 0, 2, IS_VOID, 0) + ZEND_ARG_TYPE_INFO(0, options, IS_LONG, 0) + ZEND_ARG_TYPE_INFO(0, access_key_id, IS_STRING, 0) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_credentials_options_set_secret_access_key, 0, 2, IS_VOID, 0) + ZEND_ARG_TYPE_INFO(0, options, IS_LONG, 0) + ZEND_ARG_TYPE_INFO(0, secret_access_key, IS_STRING, 0) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_credentials_options_set_session_token, 0, 2, IS_VOID, 0) + ZEND_ARG_TYPE_INFO(0, options, IS_LONG, 0) + ZEND_ARG_TYPE_INFO(0, session_token, IS_STRING, 0) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_credentials_options_set_expiration_timepoint_seconds, 0, 2, IS_VOID, 0) + ZEND_ARG_TYPE_INFO(0, options, IS_LONG, 0) + ZEND_ARG_TYPE_INFO(0, expiration_timepoint_seconds, IS_LONG, 0) +ZEND_END_ARG_INFO() + +#define arginfo_aws_crt_credentials_new arginfo_aws_crt_event_loop_group_new + +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_credentials_release, 0, 1, IS_VOID, 0) + ZEND_ARG_TYPE_INFO(0, credentials, IS_LONG, 0) +ZEND_END_ARG_INFO() + +#define arginfo_aws_crt_credentials_provider_release arginfo_aws_crt_credentials_release + +#define arginfo_aws_crt_credentials_provider_static_options_new arginfo_aws_crt_last_error + +#define arginfo_aws_crt_credentials_provider_static_options_release arginfo_aws_crt_credentials_options_release + +#define arginfo_aws_crt_credentials_provider_static_options_set_access_key_id arginfo_aws_crt_credentials_options_set_access_key_id + +#define arginfo_aws_crt_credentials_provider_static_options_set_secret_access_key arginfo_aws_crt_credentials_options_set_secret_access_key + +#define arginfo_aws_crt_credentials_provider_static_options_set_session_token arginfo_aws_crt_credentials_options_set_session_token + +#define arginfo_aws_crt_credentials_provider_static_new arginfo_aws_crt_event_loop_group_new + ZEND_FUNCTION(aws_crt_last_error); ZEND_FUNCTION(aws_crt_error_name); @@ -42,6 +87,21 @@ ZEND_FUNCTION(aws_crt_event_loop_group_options_release); ZEND_FUNCTION(aws_crt_event_loop_group_options_set_max_threads); ZEND_FUNCTION(aws_crt_event_loop_group_new); ZEND_FUNCTION(aws_crt_event_loop_group_release); +ZEND_FUNCTION(aws_crt_credentials_options_new); +ZEND_FUNCTION(aws_crt_credentials_options_release); +ZEND_FUNCTION(aws_crt_credentials_options_set_access_key_id); +ZEND_FUNCTION(aws_crt_credentials_options_set_secret_access_key); +ZEND_FUNCTION(aws_crt_credentials_options_set_session_token); +ZEND_FUNCTION(aws_crt_credentials_options_set_expiration_timepoint_seconds); +ZEND_FUNCTION(aws_crt_credentials_new); +ZEND_FUNCTION(aws_crt_credentials_release); +ZEND_FUNCTION(aws_crt_credentials_provider_release); +ZEND_FUNCTION(aws_crt_credentials_provider_static_options_new); +ZEND_FUNCTION(aws_crt_credentials_provider_static_options_release); +ZEND_FUNCTION(aws_crt_credentials_provider_static_options_set_access_key_id); +ZEND_FUNCTION(aws_crt_credentials_provider_static_options_set_secret_access_key); +ZEND_FUNCTION(aws_crt_credentials_provider_static_options_set_session_token); +ZEND_FUNCTION(aws_crt_credentials_provider_static_new); static const zend_function_entry ext_functions[] = { @@ -54,5 +114,20 @@ static const zend_function_entry ext_functions[] = { ZEND_FE(aws_crt_event_loop_group_options_set_max_threads, arginfo_aws_crt_event_loop_group_options_set_max_threads) ZEND_FE(aws_crt_event_loop_group_new, arginfo_aws_crt_event_loop_group_new) ZEND_FE(aws_crt_event_loop_group_release, arginfo_aws_crt_event_loop_group_release) + ZEND_FE(aws_crt_credentials_options_new, arginfo_aws_crt_credentials_options_new) + ZEND_FE(aws_crt_credentials_options_release, arginfo_aws_crt_credentials_options_release) + ZEND_FE(aws_crt_credentials_options_set_access_key_id, arginfo_aws_crt_credentials_options_set_access_key_id) + ZEND_FE(aws_crt_credentials_options_set_secret_access_key, arginfo_aws_crt_credentials_options_set_secret_access_key) + ZEND_FE(aws_crt_credentials_options_set_session_token, arginfo_aws_crt_credentials_options_set_session_token) + ZEND_FE(aws_crt_credentials_options_set_expiration_timepoint_seconds, arginfo_aws_crt_credentials_options_set_expiration_timepoint_seconds) + ZEND_FE(aws_crt_credentials_new, arginfo_aws_crt_credentials_new) + ZEND_FE(aws_crt_credentials_release, arginfo_aws_crt_credentials_release) + ZEND_FE(aws_crt_credentials_provider_release, arginfo_aws_crt_credentials_provider_release) + ZEND_FE(aws_crt_credentials_provider_static_options_new, arginfo_aws_crt_credentials_provider_static_options_new) + ZEND_FE(aws_crt_credentials_provider_static_options_release, arginfo_aws_crt_credentials_provider_static_options_release) + ZEND_FE(aws_crt_credentials_provider_static_options_set_access_key_id, arginfo_aws_crt_credentials_provider_static_options_set_access_key_id) + ZEND_FE(aws_crt_credentials_provider_static_options_set_secret_access_key, arginfo_aws_crt_credentials_provider_static_options_set_secret_access_key) + ZEND_FE(aws_crt_credentials_provider_static_options_set_session_token, arginfo_aws_crt_credentials_provider_static_options_set_session_token) + ZEND_FE(aws_crt_credentials_provider_static_new, arginfo_aws_crt_credentials_provider_static_new) ZEND_FE_END }; diff --git a/ext/crt.c b/ext/crt.c index dac54ff..b1a2620 100644 --- a/ext/crt.c +++ b/ext/crt.c @@ -132,6 +132,181 @@ PHP_FUNCTION(aws_crt_event_loop_group_release) { aws_crt_event_loop_group_release(elg); } +PHP_FUNCTION(aws_crt_credentials_options_new) { + aws_crt_credentials_options *options = aws_crt_credentials_options_new(); + RETURN_LONG((zend_ulong)options); +} + +PHP_FUNCTION(aws_crt_credentials_options_release) { + zend_ulong php_options = 0; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &php_options) == FAILURE) { + RETURN_NULL(); + } + + aws_crt_credentials_options *options = (void *)php_options; + aws_crt_credentials_options_release(options); +} + +PHP_FUNCTION(aws_crt_credentials_options_set_access_key_id) { + zend_ulong php_options = 0; + const char *access_key_id = NULL; + size_t access_key_id_len = 0; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls", &php_options, &access_key_id, &access_key_id_len) == + FAILURE) { + RETURN_NULL(); + } + + aws_crt_credentials_options *options = (void *)php_options; + aws_crt_credentials_options_set_access_key_id(options, (uint8_t *)access_key_id, access_key_id_len); +} + +PHP_FUNCTION(aws_crt_credentials_options_set_secret_access_key) { + zend_ulong php_options = 0; + const char *secret_access_key = NULL; + size_t secret_access_key_len = 0; + + if (zend_parse_parameters( + ZEND_NUM_ARGS() TSRMLS_CC, "ls", &php_options, &secret_access_key, &secret_access_key_len) == FAILURE) { + RETURN_NULL(); + } + + aws_crt_credentials_options *options = (void *)php_options; + aws_crt_credentials_options_set_secret_access_key(options, (uint8_t *)secret_access_key, secret_access_key_len); +} + +PHP_FUNCTION(aws_crt_credentials_options_set_session_token) { + zend_ulong php_options = 0; + const char *session_token = NULL; + size_t session_token_len = 0; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls", &php_options, &session_token, &session_token_len) == + FAILURE) { + RETURN_NULL(); + } + + aws_crt_credentials_options *options = (void *)php_options; + aws_crt_credentials_options_set_session_token(options, (uint8_t *)session_token, session_token_len); +} + +PHP_FUNCTION(aws_crt_credentials_options_set_expiration_timepoint_seconds) { + zend_ulong php_options = 0; + zend_ulong expiration_timepoint_seconds = 0; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &php_options, &expiration_timepoint_seconds) == + FAILURE) { + RETURN_NULL(); + } + + aws_crt_credentials_options *options = (void *)php_options; + aws_crt_credentials_options_set_expiration_timepoint_seconds(options, expiration_timepoint_seconds); +} + +PHP_FUNCTION(aws_crt_credentials_new) { + zend_ulong php_options = 0; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &php_options) == FAILURE) { + RETURN_NULL(); + } + + aws_crt_credentials_options *options = (void *)php_options; + aws_crt_credentials *credentials = aws_crt_credentials_new(options); + RETURN_LONG((zend_ulong)credentials); +} + +PHP_FUNCTION(aws_crt_credentials_release) { + zend_ulong php_credentials = 0; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &php_credentials) == FAILURE) { + RETURN_NULL(); + } + + aws_crt_credentials *credentials = (void *)php_credentials; + aws_crt_credentials_release(credentials); +} + +PHP_FUNCTION(aws_crt_credentials_provider_release) { + zend_ulong php_creds_provider = 0; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &php_creds_provider) == FAILURE) { + RETURN_NULL(); + } + + aws_crt_credentials_provider *provider = (void *)php_creds_provider; + aws_crt_credentials_provider_release(provider); +} + +PHP_FUNCTION(aws_crt_credentials_provider_static_options_new) { + aws_crt_credentials_provider_static_options *options = aws_crt_credentials_provider_static_options_new(); + RETURN_LONG((zend_ulong)options); +} + +PHP_FUNCTION(aws_crt_credentials_provider_static_options_release) { + zend_ulong php_options = 0; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &php_options) == FAILURE) { + RETURN_NULL(); + } + + aws_crt_credentials_provider_static_options *options = (void *)php_options; + aws_crt_credentials_provider_static_options_release(options); +} + +PHP_FUNCTION(aws_crt_credentials_provider_static_options_set_access_key_id) { + zend_ulong php_options = 0; + const char *access_key_id = NULL; + size_t access_key_id_len = 0; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls", &php_options, &access_key_id, &access_key_id_len) == + FAILURE) { + RETURN_NULL(); + } + + aws_crt_credentials_provider_static_options *options = (void *)php_options; + aws_crt_credentials_provider_static_options_set_access_key_id(options, (uint8_t *)access_key_id, access_key_id_len); +} + +PHP_FUNCTION(aws_crt_credentials_provider_static_options_set_secret_access_key) { + zend_ulong php_options = 0; + const char *secret_access_key = NULL; + size_t secret_access_key_len = 0; + + if (zend_parse_parameters( + ZEND_NUM_ARGS() TSRMLS_CC, "ls", &php_options, &secret_access_key, &secret_access_key_len) == FAILURE) { + RETURN_NULL(); + } + + aws_crt_credentials_provider_static_options *options = (void *)php_options; + aws_crt_credentials_provider_static_options_set_secret_access_key( + options, (uint8_t *)secret_access_key, secret_access_key_len); +} + +PHP_FUNCTION(aws_crt_credentials_provider_static_options_set_session_token) { + zend_ulong php_options = 0; + const char *session_token = NULL; + size_t session_token_len = 0; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls", &php_options, &session_token, &session_token_len) == + FAILURE) { + RETURN_NULL(); + } + + aws_crt_credentials_provider_static_options *options = (void *)php_options; + aws_crt_credentials_provider_static_options_set_session_token(options, (uint8_t *)session_token, session_token_len); +} + +PHP_FUNCTION(aws_crt_credentials_provider_static_new) { + zend_ulong php_options = 0; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &php_options) == FAILURE) { + RETURN_NULL(); + } + + aws_crt_credentials_provider_static_options *options = (void *)php_options; + aws_crt_credentials_provider *provider = aws_crt_credentials_provider_static_new(options); + RETURN_LONG((zend_ulong)provider); +} + zend_module_entry awscrt_module_entry = { STANDARD_MODULE_HEADER, "awscrt", diff --git a/ext/php_aws_crt.h b/ext/php_aws_crt.h index 4e8ad25..f09c8c4 100644 --- a/ext/php_aws_crt.h +++ b/ext/php_aws_crt.h @@ -39,6 +39,7 @@ ZEND_EXTERN_MODULE_GLOBALS(awscrt) static const zend_arg_info name[] = {{NULL, 0, NULL, required_num_args, return_reference, 0, 0}, /* PHP5 doesn't really handle type hints well, so elide them */ +# undef ZEND_ARG_TYPE_INFO # define ZEND_ARG_TYPE_INFO(pass_by_ref, name, type_hint, allow_null) \ {#name, sizeof(#name) - 1, NULL, 0, 0, pass_by_ref, allow_null, 0}, #endif diff --git a/src/AWS/CRT/Auth/AwsCredentials.php b/src/AWS/CRT/Auth/AwsCredentials.php new file mode 100644 index 0000000..87b3606 --- /dev/null +++ b/src/AWS/CRT/Auth/AwsCredentials.php @@ -0,0 +1,68 @@ + '', + 'secret_access_key' => '', + 'session_token' => '', + 'expiration_timepoint_seconds' => 0, + ); + } + + private $access_key_id; + private $secret_access_key; + private $session_token; + private $expiration_timepoint_seconds = 0; + + public function __get($name) { + return $this->$name; + } + + function __construct($options = []) { + parent::__construct(); + if (count($options) == 0) { + $options = self::defaults(); + } + $options = new Options($options); + $this->access_key_id = $options->getString('access_key_id'); + $this->secret_access_key = $options->getString('secret_access_key'); + $this->session_token = $options->getString('session_token'); + $this->expiration_timepoint_seconds = $options->getInt('expiration_timepoint_seconds'); + + if (strlen($this->access_key_id) == 0) { + throw new \InvalidArgumentException("access_key_id must be provided"); + } + if (strlen($this->secret_access_key) == 0) { + throw new \InvalidArgumentException("secret_access_key must be provided"); + } + + $creds_options = self::$crt->aws_credentials_options_new(); + self::$crt->aws_credentials_options_set_access_key_id($creds_options, $this->access_key_id); + self::$crt->aws_credentials_options_set_secret_access_key($creds_options, $this->secret_access_key); + self::$crt->aws_credentials_options_set_session_token($creds_options, $this->session_token); + self::$crt->aws_credentials_options_set_expiration_timepoint_seconds($creds_options, $this->expiration_timepoint_seconds); + $this->acquire(self::$crt->aws_credentials_new($creds_options)); + self::$crt->aws_credentials_options_release($creds_options); + } + + function __destruct() { + self::$crt->aws_credentials_release($this->release()); + parent::__destruct(); + } +} diff --git a/src/AWS/CRT/Auth/CredentialsProvider.php b/src/AWS/CRT/Auth/CredentialsProvider.php new file mode 100644 index 0000000..cd0ee6e --- /dev/null +++ b/src/AWS/CRT/Auth/CredentialsProvider.php @@ -0,0 +1,20 @@ +aws_credentials_provider_release($this->release()); + parent::__destruct(); + } +} diff --git a/src/AWS/CRT/Auth/StaticCredentialsProvider.php b/src/AWS/CRT/Auth/StaticCredentialsProvider.php new file mode 100644 index 0000000..e376949 --- /dev/null +++ b/src/AWS/CRT/Auth/StaticCredentialsProvider.php @@ -0,0 +1,43 @@ + '', + 'secret_access_key' => '', + 'session_token' => '', + ); + } + + private $credentials = null; + + public function __get($name) { + return $this->$name; + } + + function __construct(array $options = []) { + parent::__construct(); + if (count($options) == 0) { + $options = self::defaults(); + } + $this->credentials = new AwsCredentials($options); + + $provider_options = self::$crt->aws_credentials_provider_static_options_new(); + self::$crt->aws_credentials_provider_static_options_set_access_key_id($provider_options, $this->credentials->access_key_id); + self::$crt->aws_credentials_provider_static_options_set_secret_access_key($provider_options, $this->credentials->secret_access_key); + self::$crt->aws_credentials_provider_static_options_set_session_token($provider_options, $this->credentials->session_token); + $this->acquire(self::$crt->aws_credentials_provider_static_new($provider_options)); + self::$crt->aws_credentials_provider_static_options_release($provider_options); + } +} diff --git a/src/AWS/CRT/CRT.php b/src/AWS/CRT/CRT.php index fcddcd4..4d1877c 100644 --- a/src/AWS/CRT/CRT.php +++ b/src/AWS/CRT/CRT.php @@ -3,7 +3,6 @@ namespace AWS\CRT; use AWS\CRT\Internal\Extension; -use AWS\CRT\Internal\FFI; use \RuntimeException; @@ -22,14 +21,6 @@ final class CRT { if (is_null(self::$impl)) { // Figure out what backends are/should be available $backends = ['Extension']; - if (version_compare(PHP_VERSION, '7.0.0') >= 0) { - $backends = ['Extension', 'FFI']; - if (getenv('AWS_CRT_PHP_EXTENSION')) { - $backends = ['Extension']; - } else if (getenv('AWS_CRT_PHP_FFI')) { - $backends = ['FFI']; - } - } // Try to load each backend, give up if none succeed $exceptions = []; @@ -113,4 +104,67 @@ final class CRT { function event_loop_group_release($elg) { return self::$impl->aws_crt_event_loop_group_release($elg); } + + /** + * return object Pointer to native AWS credentials options + */ + function aws_credentials_options_new() { + return self::$impl->aws_crt_credentials_options_new(); + } + + function aws_credentials_options_release($options) { + self::$impl->aws_crt_credentials_options_release($options); + } + + function aws_credentials_options_set_access_key_id($options, $access_key_id) { + self::$impl->aws_crt_credentials_options_set_access_key_id($options, $access_key_id); + } + + function aws_credentials_options_set_secret_access_key($options, $secret_access_key) { + self::$impl->aws_crt_credentials_options_set_secret_access_key($options, $secret_access_key); + } + + function aws_credentials_options_set_session_token($options, $session_token) { + self::$impl->aws_crt_credentials_options_set_session_token($options, $session_token); + } + + function aws_credentials_options_set_expiration_timepoint_seconds($options, $expiration_timepoint_seconds) { + self::$impl->aws_crt_credentials_options_set_expiration_timepoint_seconds($options, $expiration_timepoint_seconds); + } + + function aws_credentials_new($options) { + return self::$impl->aws_crt_credentials_new($options); + } + + function aws_credentials_release($credentials) { + self::$impl->aws_crt_credentials_release($credentials); + } + + function aws_credentials_provider_release($provider) { + self::$impl->aws_crt_credentials_provider_release($provider); + } + + function aws_credentials_provider_static_options_new() { + return self::$impl->aws_crt_credentials_provider_static_options_new(); + } + + function aws_credentials_provider_static_options_release($options) { + self::$impl->aws_crt_credentials_provider_static_options_release($options); + } + + function aws_credentials_provider_static_options_set_access_key_id($options, $access_key_id) { + self::$impl->aws_crt_credentials_provider_static_options_set_access_key_id($options, $access_key_id); + } + + function aws_credentials_provider_static_options_set_secret_access_key($options, $secret_access_key) { + self::$impl->aws_crt_credentials_provider_static_options_set_secret_access_key($options, $secret_access_key); + } + + function aws_credentials_provider_static_options_set_session_token($options, $session_token) { + self::$impl->aws_crt_credentials_provider_static_options_set_session_token($options, $session_token); + } + + function aws_credentials_provider_static_new($options) { + return self::$impl->aws_crt_credentials_provider_static_new($options); + } } diff --git a/src/AWS/CRT/IO/EventLoopGroup.php b/src/AWS/CRT/IO/EventLoopGroup.php index 30c350f..b6e38bc 100644 --- a/src/AWS/CRT/IO/EventLoopGroup.php +++ b/src/AWS/CRT/IO/EventLoopGroup.php @@ -3,6 +3,7 @@ namespace AWS\CRT\IO; use AWS\CRT\NativeResource as NativeResource; +use AWS\CRT\Options as Options; /** * Represents 1 or more event loops (1 per thread) for doing I/O and background tasks. @@ -19,13 +20,14 @@ final class EventLoopGroup extends NativeResource { ); } - function __construct(array $options = array()) { + function __construct(array $options = []) { parent::__construct(); if (count($options) == 0) { $options = self::defaults(); } + $options = new Options($options); $elg_options = self::$crt->event_loop_group_options_new(); - self::$crt->event_loop_group_options_set_max_threads($elg_options, $options['max_threads']); + self::$crt->event_loop_group_options_set_max_threads($elg_options, $options->getInt('max_threads')); $this->acquire(self::$crt->event_loop_group_new($elg_options)); self::$crt->event_loop_group_options_release($elg_options); } diff --git a/src/AWS/CRT/Internal/FFI.php b/src/AWS/CRT/Internal/FFI.php deleted file mode 100644 index c10e03b..0000000 --- a/src/AWS/CRT/Internal/FFI.php +++ /dev/null @@ -1,51 +0,0 @@ -aws_crt_init(); - } - - private static function clean_up() { - return self::$ffi->aws_crt_clean_up(); - } -} diff --git a/src/AWS/CRT/Options.php b/src/AWS/CRT/Options.php new file mode 100644 index 0000000..56f2366 --- /dev/null +++ b/src/AWS/CRT/Options.php @@ -0,0 +1,32 @@ +options = $opts; + } + + public function __get($name) { + return (isset($this->options[$name])) ? $this->options[$name] : null; + } + + public function asArray() { + return $this->options; + } + + public function toArray() { + return array_merge_recursive([], $this->options); + } + + public function getInt($name) { + $val = $this->$name; + return (is_null($val)) ? 0 : (int)$val; + } + + public function getString($name) { + return !empty($this->options[$name]) ? strval($this->options[$name]) : ""; + } +} diff --git a/src/tests/CredentialsTest.php b/src/tests/CredentialsTest.php new file mode 100644 index 0000000..f645e1b --- /dev/null +++ b/src/tests/CredentialsTest.php @@ -0,0 +1,43 @@ +expectException(InvalidArgumentException::class); + $creds = new AwsCredentials(AwsCredentials::defaults()); + $this->assertNotNull($creds, "Failed to create default/empty credentials"); + $creds = null; + } + + private function getCredentialsConfig() { + $options = AwsCredentials::defaults(); + $options['access_key_id'] = 'TESTAWSACCESSKEYID'; + $options['secret_access_key'] = 'TESTSECRETaccesskeyThatDefinitelyDoesntWork'; + $options['session_token'] = 'ThisIsMyTestSessionTokenIMadeItUpMyself'; + $options['expiration_timepoint_seconds'] = 42; + return $options; + } + + public function testCredentialsLifetime() { + $options = $this->getCredentialsConfig(); + $creds = new AwsCredentials($options); + $this->assertNotNull($creds, "Failed to create Credentials with options"); + $this->assertEquals($creds->access_key_id, $options['access_key_id']); + $this->assertEquals($creds->secret_access_key, $options['secret_access_key']); + $this->assertEquals($creds->session_token, $options['session_token']); + $this->assertEquals($creds->expiration_timepoint_seconds, $options['expiration_timepoint_seconds']); + $creds = null; + } + + public function testStaticCredentialsProviderLifetime() { + $options = $this->getCredentialsConfig(); + $provider = new StaticCredentialsProvider($options); + $this->assertNotNull($provider, "Failed to create StaticCredentialsProvider"); + $provider = null; + } +} diff --git a/src/tests/EventLoopGroupTest.php b/src/tests/EventLoopGroupTest.php index 0ddb66d..8328e70 100644 --- a/src/tests/EventLoopGroupTest.php +++ b/src/tests/EventLoopGroupTest.php @@ -2,7 +2,9 @@ use AWS\CRT\IO\EventLoopGroup as EventLoopGroup; -final class EventLoopGroupTest extends PHPUnit_Framework_TestCase { +require_once('common.inc'); + +final class EventLoopGroupTest extends CrtTestCase { public function testLifetime() { $elg = new EventLoopGroup(); diff --git a/src/tests/common.inc b/src/tests/common.inc new file mode 100644 index 0000000..588850d --- /dev/null +++ b/src/tests/common.inc @@ -0,0 +1,12 @@ +fail("Test left an error on the stack: " . CRT::error_name(CRT::last_error())); + } + } +}