| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include "auth.h" |
| 7 |
|
| 8 |
#include "http.h" |
| 9 |
#include "io.h" |
| 10 |
|
| 11 |
#include <aws/auth/credentials.h> |
| 12 |
#include <aws/common/string.h> |
| 13 |
#include <aws/http/proxy.h> |
| 14 |
#include <aws/io/tls_channel_handler.h> |
| 15 |
|
| 16 |
static const char *s_capsule_name_credentials = "aws_credentials"; |
| 17 |
static const char *s_capsule_name_credentials_provider = "aws_credentials_provider"; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
static void s_credentials_capsule_destructor(PyObject *capsule) { |
| 22 |
struct aws_credentials *credentials = PyCapsule_GetPointer(capsule, s_capsule_name_credentials); |
| 23 |
aws_credentials_release(credentials); |
| 24 |
} |
| 25 |
|
| 26 |
PyObject *aws_py_credentials_new_request_from_native(struct aws_credentials *credentials) { |
| 27 |
PyObject *capsule = PyCapsule_New(credentials, s_capsule_name_credentials, s_credentials_capsule_destructor); |
| 28 |
if (!capsule) { |
| 29 |
return NULL; |
| 30 |
} |
| 31 |
aws_credentials_acquire(credentials); |
| 32 |
return capsule; |
| 33 |
} |
| 34 |
|
| 35 |
PyObject *aws_py_credentials_new(PyObject *self, PyObject *args) { |
| 36 |
(void)self; |
| 37 |
|
| 38 |
struct aws_byte_cursor access_key_id; |
| 39 |
struct aws_byte_cursor secret_access_key; |
| 40 |
struct aws_byte_cursor session_token; |
| 41 |
uint64_t expiration_timestamp_sec; |
| 42 |
if (!PyArg_ParseTuple( |
| 43 |
args, |
| 44 |
"s#s#z#K", |
| 45 |
&access_key_id.ptr, |
| 46 |
&access_key_id.len, |
| 47 |
&secret_access_key.ptr, |
| 48 |
&secret_access_key.len, |
| 49 |
&session_token.ptr, |
| 50 |
&session_token.len, |
| 51 |
&expiration_timestamp_sec)) { |
| 52 |
return NULL; |
| 53 |
} |
| 54 |
|
| 55 |
struct aws_credentials *credentials = aws_credentials_new( |
| 56 |
aws_py_get_allocator(), access_key_id, secret_access_key, session_token, expiration_timestamp_sec); |
| 57 |
if (!credentials) { |
| 58 |
return PyErr_AwsLastError(); |
| 59 |
} |
| 60 |
|
| 61 |
PyObject *capsule = PyCapsule_New(credentials, s_capsule_name_credentials, s_credentials_capsule_destructor); |
| 62 |
if (!capsule) { |
| 63 |
aws_credentials_release(credentials); |
| 64 |
return NULL; |
| 65 |
} |
| 66 |
|
| 67 |
return capsule; |
| 68 |
} |
| 69 |
|
| 70 |
struct aws_credentials *aws_py_get_credentials(PyObject *credentials) { |
| 71 |
return aws_py_get_binding(credentials, s_capsule_name_credentials, "AwsCredentials"); |
| 72 |
} |
| 73 |
|
| 74 |
enum credentials_member { |
| 75 |
CREDENTIALS_MEMBER_ACCESS_KEY_ID, |
| 76 |
CREDENTIALS_MEMBER_SECRET_ACCESS_KEY, |
| 77 |
CREDENTIALS_MEMBER_SESSION_TOKEN, |
| 78 |
}; |
| 79 |
|
| 80 |
static PyObject *s_credentials_get_member_str(PyObject *args, enum credentials_member member) { |
| 81 |
PyObject *capsule; |
| 82 |
if (!PyArg_ParseTuple(args, "O", &capsule)) { |
| 83 |
return NULL; |
| 84 |
} |
| 85 |
|
| 86 |
const struct aws_credentials *credentials = PyCapsule_GetPointer(capsule, s_capsule_name_credentials); |
| 87 |
if (!credentials) { |
| 88 |
return NULL; |
| 89 |
} |
| 90 |
|
| 91 |
struct aws_byte_cursor cursor; |
| 92 |
switch (member) { |
| 93 |
case CREDENTIALS_MEMBER_ACCESS_KEY_ID: |
| 94 |
cursor = aws_credentials_get_access_key_id(credentials); |
| 95 |
break; |
| 96 |
case CREDENTIALS_MEMBER_SECRET_ACCESS_KEY: |
| 97 |
cursor = aws_credentials_get_secret_access_key(credentials); |
| 98 |
break; |
| 99 |
case CREDENTIALS_MEMBER_SESSION_TOKEN: |
| 100 |
cursor = aws_credentials_get_session_token(credentials); |
| 101 |
break; |
| 102 |
default: |
| 103 |
AWS_FATAL_ASSERT(0); |
| 104 |
} |
| 105 |
|
| 106 |
if (member == CREDENTIALS_MEMBER_SESSION_TOKEN && cursor.len == 0) { |
| 107 |
Py_RETURN_NONE; |
| 108 |
} |
| 109 |
|
| 110 |
return PyUnicode_FromAwsByteCursor(&cursor); |
| 111 |
} |
| 112 |
|
| 113 |
PyObject *aws_py_credentials_access_key_id(PyObject *self, PyObject *args) { |
| 114 |
(void)self; |
| 115 |
return s_credentials_get_member_str(args, CREDENTIALS_MEMBER_ACCESS_KEY_ID); |
| 116 |
} |
| 117 |
|
| 118 |
PyObject *aws_py_credentials_secret_access_key(PyObject *self, PyObject *args) { |
| 119 |
(void)self; |
| 120 |
return s_credentials_get_member_str(args, CREDENTIALS_MEMBER_SECRET_ACCESS_KEY); |
| 121 |
} |
| 122 |
|
| 123 |
PyObject *aws_py_credentials_session_token(PyObject *self, PyObject *args) { |
| 124 |
(void)self; |
| 125 |
return s_credentials_get_member_str(args, CREDENTIALS_MEMBER_SESSION_TOKEN); |
| 126 |
} |
| 127 |
|
| 128 |
PyObject *aws_py_credentials_expiration_timestamp_seconds(PyObject *self, PyObject *args) { |
| 129 |
(void)self; |
| 130 |
|
| 131 |
PyObject *capsule; |
| 132 |
if (!PyArg_ParseTuple(args, "O", &capsule)) { |
| 133 |
return NULL; |
| 134 |
} |
| 135 |
|
| 136 |
const struct aws_credentials *credentials = PyCapsule_GetPointer(capsule, s_capsule_name_credentials); |
| 137 |
if (!credentials) { |
| 138 |
return NULL; |
| 139 |
} |
| 140 |
|
| 141 |
uint64_t timestamp = aws_credentials_get_expiration_timepoint_seconds(credentials); |
| 142 |
return PyLong_FromUnsignedLongLong(timestamp); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
struct credentials_provider_binding { |
| 149 |
struct aws_credentials_provider *native; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
PyObject *py_delegate; |
| 154 |
}; |
| 155 |
|
| 156 |
|
| 157 |
static void s_credentials_provider_binding_clean_up(struct credentials_provider_binding *binding) { |
| 158 |
Py_XDECREF(binding->py_delegate); |
| 159 |
|
| 160 |
aws_mem_release(aws_py_get_allocator(), binding); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
static void s_credentials_provider_shutdown_complete(void *user_data) { |
| 165 |
s_credentials_provider_binding_clean_up(user_data); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
static void s_credentials_provider_capsule_destructor(PyObject *capsule) { |
| 170 |
struct credentials_provider_binding *binding = PyCapsule_GetPointer(capsule, s_capsule_name_credentials_provider); |
| 171 |
|
| 172 |
|
| 173 |
if (binding->native) { |
| 174 |
|
| 175 |
|
| 176 |
aws_credentials_provider_release(binding->native); |
| 177 |
} else { |
| 178 |
|
| 179 |
s_credentials_provider_binding_clean_up(binding); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
struct aws_credentials_provider *aws_py_get_credentials_provider(PyObject *credentials_provider) { |
| 184 |
AWS_PY_RETURN_NATIVE_FROM_BINDING( |
| 185 |
credentials_provider, |
| 186 |
s_capsule_name_credentials_provider, |
| 187 |
"AwsCredentialsProvider", |
| 188 |
credentials_provider_binding); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
static void s_on_get_credentials_complete(struct aws_credentials *credentials, int error_code, void *user_data) { |
| 193 |
PyObject *on_complete_cb = user_data; |
| 194 |
|
| 195 |
|
| 196 |
PyGILState_STATE state; |
| 197 |
if (aws_py_gilstate_ensure(&state)) { |
| 198 |
return; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
PyObject *capsule = NULL; |
| 203 |
if (!error_code) { |
| 204 |
AWS_FATAL_ASSERT(credentials); |
| 205 |
|
| 206 |
capsule = PyCapsule_New(credentials, s_capsule_name_credentials, s_credentials_capsule_destructor); |
| 207 |
if (capsule) { |
| 208 |
aws_credentials_acquire(credentials); |
| 209 |
} else { |
| 210 |
|
| 211 |
|
| 212 |
aws_py_raise_error(); |
| 213 |
error_code = aws_last_error(); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
if (capsule == NULL) { |
| 218 |
capsule = Py_None; |
| 219 |
Py_INCREF(capsule); |
| 220 |
} |
| 221 |
|
| 222 |
PyObject *result = PyObject_CallFunction(on_complete_cb, "(iO)", error_code, capsule); |
| 223 |
if (result) { |
| 224 |
Py_DECREF(result); |
| 225 |
} else { |
| 226 |
PyErr_WriteUnraisable(PyErr_Occurred()); |
| 227 |
} |
| 228 |
|
| 229 |
Py_DECREF(on_complete_cb); |
| 230 |
Py_DECREF(capsule); |
| 231 |
|
| 232 |
PyGILState_Release(state); |
| 233 |
|
| 234 |
} |
| 235 |
|
| 236 |
PyObject *aws_py_credentials_provider_get_credentials(PyObject *self, PyObject *args) { |
| 237 |
(void)self; |
| 238 |
PyObject *capsule; |
| 239 |
PyObject *on_complete_cb; |
| 240 |
if (!PyArg_ParseTuple(args, "OO", &capsule, &on_complete_cb)) { |
| 241 |
return NULL; |
| 242 |
} |
| 243 |
|
| 244 |
struct credentials_provider_binding *provider = PyCapsule_GetPointer(capsule, s_capsule_name_credentials_provider); |
| 245 |
if (!provider) { |
| 246 |
return NULL; |
| 247 |
} |
| 248 |
|
| 249 |
AWS_FATAL_ASSERT(on_complete_cb != Py_None); |
| 250 |
|
| 251 |
Py_INCREF(on_complete_cb); |
| 252 |
if (aws_credentials_provider_get_credentials(provider->native, s_on_get_credentials_complete, on_complete_cb)) { |
| 253 |
Py_DECREF(on_complete_cb); |
| 254 |
return PyErr_AwsLastError(); |
| 255 |
} |
| 256 |
|
| 257 |
Py_RETURN_NONE; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
static PyObject *s_new_credentials_provider_binding_and_capsule(struct credentials_provider_binding **out_binding) { |
| 263 |
*out_binding = NULL; |
| 264 |
|
| 265 |
struct credentials_provider_binding *binding = |
| 266 |
aws_mem_calloc(aws_py_get_allocator(), 1, sizeof(struct credentials_provider_binding)); |
| 267 |
if (!binding) { |
| 268 |
return PyErr_AwsLastError(); |
| 269 |
} |
| 270 |
|
| 271 |
PyObject *capsule = |
| 272 |
PyCapsule_New(binding, s_capsule_name_credentials_provider, s_credentials_provider_capsule_destructor); |
| 273 |
if (!capsule) { |
| 274 |
aws_mem_release(aws_py_get_allocator(), binding); |
| 275 |
return NULL; |
| 276 |
} |
| 277 |
|
| 278 |
*out_binding = binding; |
| 279 |
return capsule; |
| 280 |
} |
| 281 |
|
| 282 |
PyObject *aws_py_credentials_provider_new_chain_default(PyObject *self, PyObject *args) { |
| 283 |
(void)self; |
| 284 |
|
| 285 |
PyObject *bootstrap_py; |
| 286 |
if (!PyArg_ParseTuple(args, "O", &bootstrap_py)) { |
| 287 |
return NULL; |
| 288 |
} |
| 289 |
|
| 290 |
struct aws_client_bootstrap *bootstrap = aws_py_get_client_bootstrap(bootstrap_py); |
| 291 |
if (!bootstrap) { |
| 292 |
return NULL; |
| 293 |
} |
| 294 |
|
| 295 |
struct credentials_provider_binding *binding; |
| 296 |
PyObject *capsule = s_new_credentials_provider_binding_and_capsule(&binding); |
| 297 |
if (!capsule) { |
| 298 |
return NULL; |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
struct aws_credentials_provider_chain_default_options options = { |
| 305 |
.bootstrap = bootstrap, |
| 306 |
.shutdown_options = {s_credentials_provider_shutdown_complete, binding}, |
| 307 |
}; |
| 308 |
|
| 309 |
binding->native = aws_credentials_provider_new_chain_default(aws_py_get_allocator(), &options); |
| 310 |
if (!binding->native) { |
| 311 |
PyErr_SetAwsLastError(); |
| 312 |
goto error; |
| 313 |
} |
| 314 |
|
| 315 |
return capsule; |
| 316 |
|
| 317 |
error: |
| 318 |
Py_DECREF(capsule); |
| 319 |
return NULL; |
| 320 |
} |
| 321 |
|
| 322 |
PyObject *aws_py_credentials_provider_new_static(PyObject *self, PyObject *args) { |
| 323 |
(void)self; |
| 324 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 325 |
|
| 326 |
struct aws_byte_cursor access_key_id; |
| 327 |
struct aws_byte_cursor secret_access_key; |
| 328 |
struct aws_byte_cursor session_token; |
| 329 |
if (!PyArg_ParseTuple( |
| 330 |
args, |
| 331 |
"s#s#z#", |
| 332 |
&access_key_id.ptr, |
| 333 |
&access_key_id.len, |
| 334 |
&secret_access_key.ptr, |
| 335 |
&secret_access_key.len, |
| 336 |
&session_token.ptr, |
| 337 |
&session_token.len)) { |
| 338 |
return NULL; |
| 339 |
} |
| 340 |
|
| 341 |
struct credentials_provider_binding *binding; |
| 342 |
PyObject *capsule = s_new_credentials_provider_binding_and_capsule(&binding); |
| 343 |
if (!capsule) { |
| 344 |
return NULL; |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
struct aws_credentials_provider_static_options options = { |
| 351 |
.access_key_id = access_key_id, |
| 352 |
.secret_access_key = secret_access_key, |
| 353 |
.session_token = session_token, |
| 354 |
.shutdown_options = {s_credentials_provider_shutdown_complete, binding}, |
| 355 |
}; |
| 356 |
|
| 357 |
binding->native = aws_credentials_provider_new_static(allocator, &options); |
| 358 |
if (!binding->native) { |
| 359 |
PyErr_SetAwsLastError(); |
| 360 |
goto error; |
| 361 |
} |
| 362 |
|
| 363 |
return capsule; |
| 364 |
error: |
| 365 |
Py_DECREF(capsule); |
| 366 |
return NULL; |
| 367 |
} |
| 368 |
|
| 369 |
PyObject *aws_py_credentials_provider_new_profile(PyObject *self, PyObject *args) { |
| 370 |
(void)self; |
| 371 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 372 |
|
| 373 |
PyObject *bootstrap_py; |
| 374 |
struct aws_byte_cursor profile_name; |
| 375 |
struct aws_byte_cursor config_file_name; |
| 376 |
struct aws_byte_cursor credentials_file_name; |
| 377 |
|
| 378 |
if (!PyArg_ParseTuple( |
| 379 |
args, |
| 380 |
"Oz#z#z#", |
| 381 |
&bootstrap_py, |
| 382 |
&profile_name.ptr, |
| 383 |
&profile_name.len, |
| 384 |
&config_file_name.ptr, |
| 385 |
&config_file_name.len, |
| 386 |
&credentials_file_name.ptr, |
| 387 |
&credentials_file_name.len)) { |
| 388 |
return NULL; |
| 389 |
} |
| 390 |
|
| 391 |
struct aws_client_bootstrap *bootstrap = aws_py_get_client_bootstrap(bootstrap_py); |
| 392 |
if (!bootstrap) { |
| 393 |
return NULL; |
| 394 |
} |
| 395 |
|
| 396 |
struct credentials_provider_binding *binding; |
| 397 |
PyObject *capsule = s_new_credentials_provider_binding_and_capsule(&binding); |
| 398 |
if (!capsule) { |
| 399 |
return NULL; |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
struct aws_credentials_provider_profile_options options = { |
| 406 |
.bootstrap = bootstrap, |
| 407 |
.profile_name_override = profile_name, |
| 408 |
.config_file_name_override = config_file_name, |
| 409 |
.credentials_file_name_override = credentials_file_name, |
| 410 |
.shutdown_options = |
| 411 |
{ |
| 412 |
.shutdown_callback = s_credentials_provider_shutdown_complete, |
| 413 |
.shutdown_user_data = binding, |
| 414 |
}, |
| 415 |
}; |
| 416 |
|
| 417 |
binding->native = aws_credentials_provider_new_profile(allocator, &options); |
| 418 |
if (!binding->native) { |
| 419 |
PyErr_SetAwsLastError(); |
| 420 |
goto error; |
| 421 |
} |
| 422 |
|
| 423 |
return capsule; |
| 424 |
error: |
| 425 |
Py_DECREF(capsule); |
| 426 |
return NULL; |
| 427 |
} |
| 428 |
|
| 429 |
PyObject *aws_py_credentials_provider_new_process(PyObject *self, PyObject *args) { |
| 430 |
(void)self; |
| 431 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 432 |
|
| 433 |
struct aws_byte_cursor profile_to_use; |
| 434 |
|
| 435 |
if (!PyArg_ParseTuple(args, "z#", &profile_to_use.ptr, &profile_to_use.len)) { |
| 436 |
return NULL; |
| 437 |
} |
| 438 |
|
| 439 |
struct credentials_provider_binding *binding; |
| 440 |
PyObject *capsule = s_new_credentials_provider_binding_and_capsule(&binding); |
| 441 |
if (!capsule) { |
| 442 |
return NULL; |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
struct aws_credentials_provider_process_options options = { |
| 449 |
.profile_to_use = profile_to_use, |
| 450 |
.shutdown_options = |
| 451 |
{ |
| 452 |
.shutdown_callback = s_credentials_provider_shutdown_complete, |
| 453 |
.shutdown_user_data = binding, |
| 454 |
}, |
| 455 |
}; |
| 456 |
|
| 457 |
binding->native = aws_credentials_provider_new_process(allocator, &options); |
| 458 |
if (!binding->native) { |
| 459 |
PyErr_SetAwsLastError(); |
| 460 |
goto error; |
| 461 |
} |
| 462 |
|
| 463 |
return capsule; |
| 464 |
error: |
| 465 |
Py_DECREF(capsule); |
| 466 |
return NULL; |
| 467 |
} |
| 468 |
|
| 469 |
PyObject *aws_py_credentials_provider_new_environment(PyObject *self, PyObject *args) { |
| 470 |
(void)self; |
| 471 |
(void)args; |
| 472 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 473 |
|
| 474 |
struct credentials_provider_binding *binding; |
| 475 |
PyObject *capsule = s_new_credentials_provider_binding_and_capsule(&binding); |
| 476 |
if (!capsule) { |
| 477 |
return NULL; |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
struct aws_credentials_provider_environment_options options = { |
| 484 |
.shutdown_options = |
| 485 |
{ |
| 486 |
.shutdown_callback = s_credentials_provider_shutdown_complete, |
| 487 |
.shutdown_user_data = binding, |
| 488 |
}, |
| 489 |
}; |
| 490 |
|
| 491 |
binding->native = aws_credentials_provider_new_environment(allocator, &options); |
| 492 |
if (!binding->native) { |
| 493 |
PyErr_SetAwsLastError(); |
| 494 |
goto error; |
| 495 |
} |
| 496 |
|
| 497 |
return capsule; |
| 498 |
error: |
| 499 |
Py_DECREF(capsule); |
| 500 |
return NULL; |
| 501 |
} |
| 502 |
|
| 503 |
PyObject *aws_py_credentials_provider_new_chain(PyObject *self, PyObject *args) { |
| 504 |
(void)self; |
| 505 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 506 |
|
| 507 |
PyObject *providers_arg; |
| 508 |
|
| 509 |
if (!PyArg_ParseTuple(args, "O", &providers_arg)) { |
| 510 |
return NULL; |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
bool success = false; |
| 516 |
PyObject *providers_pyseq = NULL; |
| 517 |
struct aws_credentials_provider **providers_carray = NULL; |
| 518 |
PyObject *capsule = NULL; |
| 519 |
|
| 520 |
|
| 521 |
providers_pyseq = PySequence_Fast(providers_arg, "Expected sequence of AwsCredentialsProviders"); |
| 522 |
if (!providers_pyseq) { |
| 523 |
goto done; |
| 524 |
} |
| 525 |
size_t provider_count = (size_t)PySequence_Size(providers_pyseq); |
| 526 |
if (provider_count == 0) { |
| 527 |
PyErr_SetString(PyExc_ValueError, "Must supply at least one AwsCredentialsProvider."); |
| 528 |
goto done; |
| 529 |
} |
| 530 |
|
| 531 |
providers_carray = aws_mem_calloc(allocator, provider_count, sizeof(void *)); |
| 532 |
if (!providers_carray) { |
| 533 |
PyErr_SetAwsLastError(); |
| 534 |
goto done; |
| 535 |
} |
| 536 |
|
| 537 |
for (size_t i = 0; i < provider_count; ++i) { |
| 538 |
PyObject *provider_py = PySequence_GetItem(providers_pyseq, i); |
| 539 |
providers_carray[i] = aws_py_get_credentials_provider(provider_py); |
| 540 |
Py_XDECREF(provider_py); |
| 541 |
if (!providers_carray[i]) { |
| 542 |
goto done; |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
struct credentials_provider_binding *binding; |
| 547 |
capsule = s_new_credentials_provider_binding_and_capsule(&binding); |
| 548 |
if (!capsule) { |
| 549 |
goto done; |
| 550 |
} |
| 551 |
|
| 552 |
struct aws_credentials_provider_chain_options options = { |
| 553 |
.provider_count = provider_count, |
| 554 |
.providers = providers_carray, |
| 555 |
.shutdown_options = |
| 556 |
{ |
| 557 |
.shutdown_callback = s_credentials_provider_shutdown_complete, |
| 558 |
.shutdown_user_data = binding, |
| 559 |
}, |
| 560 |
}; |
| 561 |
|
| 562 |
binding->native = aws_credentials_provider_new_chain(allocator, &options); |
| 563 |
if (!binding->native) { |
| 564 |
PyErr_SetAwsLastError(); |
| 565 |
goto done; |
| 566 |
} |
| 567 |
|
| 568 |
success = true; |
| 569 |
|
| 570 |
done: |
| 571 |
Py_XDECREF(providers_pyseq); |
| 572 |
aws_mem_release(allocator, providers_carray); |
| 573 |
|
| 574 |
if (success) { |
| 575 |
return capsule; |
| 576 |
} |
| 577 |
|
| 578 |
Py_XDECREF(capsule); |
| 579 |
return NULL; |
| 580 |
} |
| 581 |
|
| 582 |
static int s_credentials_provider_delegate_get_credentials( |
| 583 |
void *delegate_user_data, |
| 584 |
aws_on_get_credentials_callback_fn callback, |
| 585 |
void *callback_user_data) { |
| 586 |
|
| 587 |
struct credentials_provider_binding *binding = delegate_user_data; |
| 588 |
|
| 589 |
PyGILState_STATE state; |
| 590 |
if (aws_py_gilstate_ensure(&state)) { |
| 591 |
|
| 592 |
return aws_raise_error(AWS_ERROR_INVALID_STATE); |
| 593 |
} |
| 594 |
|
| 595 |
struct aws_credentials *native_credentials = NULL; |
| 596 |
|
| 597 |
PyObject *py_result = PyObject_CallFunction(binding->py_delegate, "()"); |
| 598 |
if (!py_result) { |
| 599 |
AWS_LOGF_ERROR( |
| 600 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 601 |
"(id=%p) Exception in get_credentials() delegate callback", |
| 602 |
(void *)binding->native); |
| 603 |
|
| 604 |
PyErr_WriteUnraisable(binding->py_delegate); |
| 605 |
goto done; |
| 606 |
} |
| 607 |
|
| 608 |
|
| 609 |
native_credentials = aws_py_get_credentials(py_result); |
| 610 |
if (!native_credentials) { |
| 611 |
AWS_LOGF_ERROR( |
| 612 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 613 |
"(id=%p) get_credentials() delegate callback failed to return AwsCredentials", |
| 614 |
(void *)binding->native); |
| 615 |
|
| 616 |
PyErr_WriteUnraisable(binding->py_delegate); |
| 617 |
goto done; |
| 618 |
} |
| 619 |
|
| 620 |
|
| 621 |
aws_credentials_acquire(native_credentials); |
| 622 |
|
| 623 |
done: |
| 624 |
|
| 625 |
Py_XDECREF(py_result); |
| 626 |
|
| 627 |
PyGILState_Release(state); |
| 628 |
|
| 629 |
if (!native_credentials) { |
| 630 |
return aws_raise_error(AWS_AUTH_CREDENTIALS_PROVIDER_DELEGATE_FAILURE); |
| 631 |
} |
| 632 |
|
| 633 |
callback(native_credentials, AWS_ERROR_SUCCESS, callback_user_data); |
| 634 |
aws_credentials_release(native_credentials); |
| 635 |
return AWS_OP_SUCCESS; |
| 636 |
} |
| 637 |
|
| 638 |
PyObject *aws_py_credentials_provider_new_delegate(PyObject *self, PyObject *args) { |
| 639 |
(void)self; |
| 640 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 641 |
|
| 642 |
PyObject *py_delegate; |
| 643 |
|
| 644 |
if (!PyArg_ParseTuple(args, "O", &py_delegate)) { |
| 645 |
return NULL; |
| 646 |
} |
| 647 |
|
| 648 |
struct credentials_provider_binding *binding; |
| 649 |
PyObject *capsule = s_new_credentials_provider_binding_and_capsule(&binding); |
| 650 |
if (!capsule) { |
| 651 |
return NULL; |
| 652 |
} |
| 653 |
|
| 654 |
binding->py_delegate = py_delegate; |
| 655 |
Py_INCREF(py_delegate); |
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
struct aws_credentials_provider_delegate_options options = { |
| 661 |
.get_credentials = s_credentials_provider_delegate_get_credentials, |
| 662 |
.delegate_user_data = binding, |
| 663 |
.shutdown_options = |
| 664 |
{ |
| 665 |
.shutdown_callback = s_credentials_provider_shutdown_complete, |
| 666 |
.shutdown_user_data = binding, |
| 667 |
}, |
| 668 |
}; |
| 669 |
|
| 670 |
binding->native = aws_credentials_provider_new_delegate(allocator, &options); |
| 671 |
if (!binding->native) { |
| 672 |
PyErr_SetAwsLastError(); |
| 673 |
goto error; |
| 674 |
} |
| 675 |
|
| 676 |
return capsule; |
| 677 |
error: |
| 678 |
Py_DECREF(capsule); |
| 679 |
return NULL; |
| 680 |
} |
| 681 |
|
| 682 |
PyObject *aws_py_credentials_provider_new_cognito(PyObject *self, PyObject *args) { |
| 683 |
(void)self; |
| 684 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 685 |
|
| 686 |
struct aws_byte_cursor endpoint_cursor; |
| 687 |
AWS_ZERO_STRUCT(endpoint_cursor); |
| 688 |
struct aws_byte_cursor identity_cursor; |
| 689 |
AWS_ZERO_STRUCT(identity_cursor); |
| 690 |
PyObject *logins_list_py = NULL; |
| 691 |
struct aws_byte_cursor custom_role_arn_cursor; |
| 692 |
AWS_ZERO_STRUCT(custom_role_arn_cursor); |
| 693 |
PyObject *tls_context_py = NULL; |
| 694 |
PyObject *client_bootstrap_py = NULL; |
| 695 |
PyObject *http_proxy_options_py = NULL; |
| 696 |
|
| 697 |
if (!PyArg_ParseTuple( |
| 698 |
args, |
| 699 |
"s#s#OOOz#O", |
| 700 |
&endpoint_cursor.ptr, |
| 701 |
&endpoint_cursor.len, |
| 702 |
&identity_cursor.ptr, |
| 703 |
&identity_cursor.len, |
| 704 |
&tls_context_py, |
| 705 |
&client_bootstrap_py, |
| 706 |
&logins_list_py, |
| 707 |
&custom_role_arn_cursor.ptr, |
| 708 |
&custom_role_arn_cursor.len, |
| 709 |
&http_proxy_options_py)) { |
| 710 |
return NULL; |
| 711 |
} |
| 712 |
|
| 713 |
struct aws_client_bootstrap *bootstrap = aws_py_get_client_bootstrap(client_bootstrap_py); |
| 714 |
if (!bootstrap) { |
| 715 |
return NULL; |
| 716 |
} |
| 717 |
|
| 718 |
struct aws_tls_ctx *tls_context = aws_py_get_tls_ctx(tls_context_py); |
| 719 |
if (!tls_context) { |
| 720 |
return NULL; |
| 721 |
} |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
bool success = false; |
| 726 |
PyObject *logins_pyseq = NULL; |
| 727 |
struct aws_cognito_identity_provider_token_pair *logins_carray = NULL; |
| 728 |
PyObject *capsule = NULL; |
| 729 |
size_t logins_count = 0; |
| 730 |
|
| 731 |
if (logins_list_py != Py_None) { |
| 732 |
logins_pyseq = PySequence_Fast(logins_list_py, "Expected sequence of login token tuples"); |
| 733 |
if (!logins_pyseq) { |
| 734 |
goto done; |
| 735 |
} |
| 736 |
|
| 737 |
logins_count = (size_t)PySequence_Size(logins_pyseq); |
| 738 |
if (logins_count > 0) { |
| 739 |
|
| 740 |
logins_carray = |
| 741 |
aws_mem_calloc(allocator, logins_count, sizeof(struct aws_cognito_identity_provider_token_pair)); |
| 742 |
if (!logins_carray) { |
| 743 |
PyErr_SetAwsLastError(); |
| 744 |
goto done; |
| 745 |
} |
| 746 |
|
| 747 |
for (size_t i = 0; i < logins_count; ++i) { |
| 748 |
PyObject *login_tuple_py = PySequence_GetItem(logins_pyseq, i); |
| 749 |
struct aws_cognito_identity_provider_token_pair *login_entry = &logins_carray[i]; |
| 750 |
AWS_ZERO_STRUCT(*login_entry); |
| 751 |
|
| 752 |
if (!PyArg_ParseTuple( |
| 753 |
login_tuple_py, |
| 754 |
"s#s#", |
| 755 |
&login_entry->identity_provider_name.ptr, |
| 756 |
&login_entry->identity_provider_name.len, |
| 757 |
&login_entry->identity_provider_token.ptr, |
| 758 |
&login_entry->identity_provider_token.len)) { |
| 759 |
PyErr_Format( |
| 760 |
PyExc_TypeError, |
| 761 |
"cognito credentials provider: logins[%zu] is invalid, should be type (str, str)", |
| 762 |
i); |
| 763 |
Py_XDECREF(login_tuple_py); |
| 764 |
goto done; |
| 765 |
} |
| 766 |
Py_XDECREF(login_tuple_py); |
| 767 |
} |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
struct aws_http_proxy_options http_proxy_options_storage; |
| 772 |
struct aws_http_proxy_options *http_proxy_options = NULL; |
| 773 |
if (http_proxy_options_py != Py_None) { |
| 774 |
http_proxy_options = &http_proxy_options_storage; |
| 775 |
if (!aws_py_http_proxy_options_init(http_proxy_options, http_proxy_options_py)) { |
| 776 |
goto done; |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
struct credentials_provider_binding *binding = NULL; |
| 781 |
capsule = s_new_credentials_provider_binding_and_capsule(&binding); |
| 782 |
if (!capsule) { |
| 783 |
goto done; |
| 784 |
} |
| 785 |
|
| 786 |
struct aws_credentials_provider_cognito_options options = { |
| 787 |
.endpoint = endpoint_cursor, |
| 788 |
.identity = identity_cursor, |
| 789 |
.shutdown_options = |
| 790 |
{ |
| 791 |
.shutdown_callback = s_credentials_provider_shutdown_complete, |
| 792 |
.shutdown_user_data = binding, |
| 793 |
}, |
| 794 |
.tls_ctx = tls_context, |
| 795 |
.bootstrap = bootstrap, |
| 796 |
.http_proxy_options = http_proxy_options, |
| 797 |
}; |
| 798 |
|
| 799 |
if (logins_count > 0) { |
| 800 |
options.login_count = logins_count; |
| 801 |
options.logins = logins_carray; |
| 802 |
} |
| 803 |
|
| 804 |
if (custom_role_arn_cursor.ptr != NULL) { |
| 805 |
options.custom_role_arn = &custom_role_arn_cursor; |
| 806 |
} |
| 807 |
|
| 808 |
binding->native = aws_credentials_provider_new_cognito(allocator, &options); |
| 809 |
if (!binding->native) { |
| 810 |
PyErr_SetAwsLastError(); |
| 811 |
goto done; |
| 812 |
} |
| 813 |
|
| 814 |
success = true; |
| 815 |
|
| 816 |
done: |
| 817 |
Py_XDECREF(logins_pyseq); |
| 818 |
aws_mem_release(allocator, logins_carray); |
| 819 |
|
| 820 |
if (success) { |
| 821 |
return capsule; |
| 822 |
} |
| 823 |
|
| 824 |
Py_XDECREF(capsule); |
| 825 |
return NULL; |
| 826 |
} |
| 827 |
|
| 828 |
PyObject *aws_py_credentials_provider_new_x509(PyObject *self, PyObject *args) { |
| 829 |
(void)self; |
| 830 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 831 |
|
| 832 |
struct aws_byte_cursor endpoint_cursor; |
| 833 |
AWS_ZERO_STRUCT(endpoint_cursor); |
| 834 |
struct aws_byte_cursor thing_name_cursor; |
| 835 |
AWS_ZERO_STRUCT(thing_name_cursor); |
| 836 |
struct aws_byte_cursor role_alias_cursor; |
| 837 |
AWS_ZERO_STRUCT(role_alias_cursor); |
| 838 |
PyObject *tls_context_py = NULL; |
| 839 |
PyObject *client_bootstrap_py = NULL; |
| 840 |
PyObject *http_proxy_options_py = NULL; |
| 841 |
struct aws_tls_connection_options tls_connection_options; |
| 842 |
AWS_ZERO_STRUCT(tls_connection_options); |
| 843 |
|
| 844 |
if (!PyArg_ParseTuple( |
| 845 |
args, |
| 846 |
"s#s#s#OOO", |
| 847 |
&endpoint_cursor.ptr, |
| 848 |
&endpoint_cursor.len, |
| 849 |
&thing_name_cursor.ptr, |
| 850 |
&thing_name_cursor.len, |
| 851 |
&role_alias_cursor.ptr, |
| 852 |
&role_alias_cursor.len, |
| 853 |
&tls_context_py, |
| 854 |
&client_bootstrap_py, |
| 855 |
&http_proxy_options_py )) { |
| 856 |
return NULL; |
| 857 |
} |
| 858 |
|
| 859 |
struct aws_client_bootstrap *bootstrap = aws_py_get_client_bootstrap(client_bootstrap_py); |
| 860 |
if (!bootstrap) { |
| 861 |
return NULL; |
| 862 |
} |
| 863 |
|
| 864 |
struct aws_tls_ctx *tls_context = aws_py_get_tls_ctx(tls_context_py); |
| 865 |
if (!tls_context) { |
| 866 |
return NULL; |
| 867 |
} |
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
PyObject *capsule = NULL; |
| 872 |
bool success = false; |
| 873 |
aws_tls_connection_options_init_from_ctx(&tls_connection_options, tls_context); |
| 874 |
|
| 875 |
struct aws_http_proxy_options http_proxy_options_storage; |
| 876 |
struct aws_http_proxy_options *http_proxy_options = NULL; |
| 877 |
if (http_proxy_options_py != Py_None) { |
| 878 |
http_proxy_options = &http_proxy_options_storage; |
| 879 |
if (!aws_py_http_proxy_options_init(http_proxy_options, http_proxy_options_py)) { |
| 880 |
goto done; |
| 881 |
} |
| 882 |
} |
| 883 |
|
| 884 |
struct credentials_provider_binding *binding = NULL; |
| 885 |
capsule = s_new_credentials_provider_binding_and_capsule(&binding); |
| 886 |
if (!capsule) { |
| 887 |
goto done; |
| 888 |
} |
| 889 |
|
| 890 |
struct aws_credentials_provider_x509_options options = { |
| 891 |
.endpoint = endpoint_cursor, |
| 892 |
.thing_name = thing_name_cursor, |
| 893 |
.role_alias = role_alias_cursor, |
| 894 |
.shutdown_options = |
| 895 |
{ |
| 896 |
.shutdown_callback = s_credentials_provider_shutdown_complete, |
| 897 |
.shutdown_user_data = binding, |
| 898 |
}, |
| 899 |
.tls_connection_options = &tls_connection_options, |
| 900 |
.bootstrap = bootstrap, |
| 901 |
.proxy_options = http_proxy_options, |
| 902 |
}; |
| 903 |
|
| 904 |
binding->native = aws_credentials_provider_new_x509(allocator, &options); |
| 905 |
if (!binding->native) { |
| 906 |
PyErr_SetAwsLastError(); |
| 907 |
goto done; |
| 908 |
} |
| 909 |
success = true; |
| 910 |
|
| 911 |
done: |
| 912 |
aws_tls_connection_options_clean_up(&tls_connection_options); |
| 913 |
if (success) { |
| 914 |
return capsule; |
| 915 |
} |
| 916 |
Py_XDECREF(capsule); |
| 917 |
return NULL; |
| 918 |
} |