From c03323db02650b70cec7d5bc68ca43d2865d3e03 Mon Sep 17 00:00:00 2001 From: "Jonathan M. Henson" Date: Fri, 6 Oct 2023 16:57:43 -0700 Subject: [PATCH] Clean up with actual ec2 instance detection from imds. --- awscrt/common.py | 13 +++++++++--- awscrt/s3.py | 4 ---- crt/aws-c-s3 | 2 +- source/common.c | 49 ++++++++++++++++++++++++++++++++++++++++------ source/common.h | 1 + source/module.c | 3 ++- source/s3.h | 2 -- source/s3_client.c | 31 ----------------------------- 8 files changed, 57 insertions(+), 48 deletions(-) diff --git a/awscrt/common.py b/awscrt/common.py index 396a40a04..e43380445 100644 --- a/awscrt/common.py +++ b/awscrt/common.py @@ -24,9 +24,16 @@ class SystemEnvironment: def __init__(self): self._env = _awscrt.load_system_environment() + if self.is_ec2_nitro_instance(): + self._detected_instance_type = self.get_ec2_instance_type() + else: + self._detected_instance_type = None + def is_ec2_nitro_instance(self) -> bool: return _awscrt.is_env_ec2(self._env) - def get_ec2_instance_type(self) -> str: - return _awscrt.get_ec2_instance_type(self._env) - \ No newline at end of file + def get_ec2_instance_type(self) -> str: + return self._detected_instance_type + + def is_crt_s3_optimized_for_system_env(self) -> bool: + return _awscrt.is_crt_s3_optimized_for_system(self._env, self._detected_instance_type) \ No newline at end of file diff --git a/awscrt/s3.py b/awscrt/s3.py index 94086e700..e3e2da0ad 100644 --- a/awscrt/s3.py +++ b/awscrt/s3.py @@ -11,14 +11,10 @@ from awscrt.http import HttpRequest from awscrt.io import ClientBootstrap, TlsConnectionOptions from awscrt.auth import AwsCredentialsProvider, AwsSignatureType, AwsSignedBodyHeaderType, AwsSignedBodyValue, AwsSigningAlgorithm, AwsSigningConfig -from awscrt.common import SystemEnvironment import awscrt.exceptions import threading from enum import IntEnum -def is_crt_s3_optimized_for_system_env(env: SystemEnvironment, optInstanceTypeOverride: str = None) -> bool: - return _awscrt.s3_is_crt_s3_optimized_for_system_env(env._env, optInstanceTypeOverride) - class S3RequestType(IntEnum): """The type of the AWS S3 request""" diff --git a/crt/aws-c-s3 b/crt/aws-c-s3 index 894c11242..3d69e1186 160000 --- a/crt/aws-c-s3 +++ b/crt/aws-c-s3 @@ -1 +1 @@ -Subproject commit 894c11242f074713f9ea5185ed8adfeabacf5a83 +Subproject commit 3d69e1186b9cbf9d292d2ec024cc68c13dea13d6 diff --git a/source/common.c b/source/common.c index 702e59f3e..af7423abd 100644 --- a/source/common.c +++ b/source/common.c @@ -69,11 +69,9 @@ PyObject *aws_py_is_env_ec2(PyObject *self, PyObject *args) { struct aws_system_environment *env = PyCapsule_GetPointer(env_capsule, s_capsule_name_sys_env); if (!env) { return PyErr_AwsLastError(); - } - - struct aws_byte_cursor system_virt_name = aws_system_environment_get_virtualization_vendor(env); + } - if (aws_byte_cursor_eq_c_str_ignore_case(&system_virt_name, "amazon ec2")) { + if (aws_s3_is_running_on_ec2(env)) { Py_RETURN_TRUE; } @@ -89,11 +87,50 @@ PyObject *aws_py_get_ec2_instance_type(PyObject *self, PyObject *args) { struct aws_system_environment *env = PyCapsule_GetPointer(env_capsule, s_capsule_name_sys_env); if (!env) { return PyErr_AwsLastError(); + } + + struct aws_allocator *allocator = aws_py_get_allocator(); + + struct aws_string *instance_type = aws_s3_get_ec2_instance_type(allocator, env); + + if (instance_type) { + PyObject *ret_value = PyUnicode_FromAwsString(&instance_type); + aws_string_destroy(instance_type); + return ret_value; } - struct aws_byte_cursor product_name = aws_system_environment_get_virtualization_product_name(env); + return NULL; +} + +PyObject *aws_py_is_crt_s3_optimized_for_system(PyObject *self, PyObject *args) { + PyObject *env_capsule = NULL; + const char *instance_type_str = NULL; + Py_ssize_t instance_type_str_len = 0; + + if (!PyArg_ParseTuple(args, "Oz#", &env_capsule, &instance_type_str, &instance_type_str_len)) { + return PyErr_AwsLastError(); + } + + struct aws_system_environment *env = PyCapsule_GetPointer(env_capsule, s_capsule_name_sys_env); + if (!env) { + return PyErr_AwsLastError(); + } + + struct aws_byte_cursor *instance_type_to_pass = NULL; + struct aws_byte_cursor instance_type_cur; + + if (instance_type_str_len > 0) { + instance_type_cur = aws_byte_cursor_from_array(instance_type_str, (size_t)instance_type_str_len); + instance_type_to_pass = &instance_type_cur; + } - return PyUnicode_FromAwsByteCursor(&product_name); + bool is_optimized = aws_s3_is_optimized_for_system_env(env, instance_type_to_pass); + + if (is_optimized) { + Py_RETURN_TRUE; + } + + Py_RETURN_FALSE; } PyObject *aws_py_thread_join_all_managed(PyObject *self, PyObject *args) { diff --git a/source/common.h b/source/common.h index a8d0fe849..cdba0f506 100644 --- a/source/common.h +++ b/source/common.h @@ -18,5 +18,6 @@ PyObject *aws_py_thread_join_all_managed(PyObject *self, PyObject *args); PyObject *aws_py_load_system_environment(PyObject *self, PyObject *args); PyObject *aws_py_is_env_ec2(PyObject *self, PyObject *args); PyObject *aws_py_get_ec2_instance_type(PyObject *self, PyObject *args); +PyObject *aws_py_is_crt_s3_optimized_for_system(PyObject *self, PyObject *args); #endif /* AWS_CRT_PYTHON_COMMON_H */ diff --git a/source/module.c b/source/module.c index 573bf5282..6fa2d0179 100644 --- a/source/module.c +++ b/source/module.c @@ -671,6 +671,8 @@ static PyMethodDef s_module_methods[] = { AWS_PY_METHOD_DEF(load_system_environment, METH_NOARGS), AWS_PY_METHOD_DEF(is_env_ec2, METH_VARARGS), AWS_PY_METHOD_DEF(get_ec2_instance_type, METH_VARARGS), + AWS_PY_METHOD_DEF(is_crt_s3_optimized_for_system, METH_VARARGS), + /* IO */ AWS_PY_METHOD_DEF(is_alpn_available, METH_NOARGS), @@ -794,7 +796,6 @@ static PyMethodDef s_module_methods[] = { AWS_PY_METHOD_DEF(s3_client_new, METH_VARARGS), AWS_PY_METHOD_DEF(s3_client_make_meta_request, METH_VARARGS), AWS_PY_METHOD_DEF(s3_meta_request_cancel, METH_VARARGS), - AWS_PY_METHOD_DEF(s3_is_crt_s3_optimized_for_system_env, METH_VARARGS), /* WebSocket */ diff --git a/source/s3.h b/source/s3.h index 6f9dcefea..7a35c26e0 100644 --- a/source/s3.h +++ b/source/s3.h @@ -7,8 +7,6 @@ #include "module.h" -PyObject *aws_py_s3_is_crt_s3_optimized_for_system_env(PyObject *self, PyObject *args); - PyObject *aws_py_s3_client_new(PyObject *self, PyObject *args); PyObject *aws_py_s3_client_make_meta_request(PyObject *self, PyObject *args); diff --git a/source/s3_client.c b/source/s3_client.c index c199c2bae..64ec8041f 100644 --- a/source/s3_client.c +++ b/source/s3_client.c @@ -182,34 +182,3 @@ PyObject *aws_py_s3_client_new(PyObject *self, PyObject *args) { Py_DECREF(capsule); return NULL; } - -PyObject *aws_py_s3_is_crt_s3_optimized_for_system_env(PyObject *self, PyObject *args) { - PyObject *env_capsule = NULL; - const char *override_str = NULL; - Py_ssize_t override_str_len = 0; - - if (!PyArg_ParseTuple(args, "Oz#", &env_capsule, &override_str, &override_str_len)) { - return PyErr_AwsLastError(); - } - - struct aws_system_environment *env = PyCapsule_GetPointer(env_capsule, s_capsule_name_sys_env); - if (!env) { - return PyErr_AwsLastError(); - } - - struct aws_byte_cursor *override_param = NULL; - struct aws_byte_cursor override_cur; - - if (override_str_len > 0) { - override_cur = aws_byte_cursor_from_array(override_str, (size_t)override_str_len); - override_param = &override_cur; - } - - bool is_optimized = aws_s3_is_optimized_for_system_env(env, override_param); - - if (is_optimized) { - Py_RETURN_TRUE; - } - - Py_RETURN_FALSE; -}