|
18 | 18 | from functools import partial |
19 | 19 | from io import StringIO |
20 | 20 | from logging import getLogger |
21 | | -from threading import Lock, Thread |
| 21 | +from threading import Lock |
22 | 22 | from types import TracebackType |
23 | 23 | from typing import Any, Callable, Generator, Iterable, Iterator, NamedTuple, Sequence |
24 | 24 | from uuid import UUID |
25 | 25 |
|
26 | | -import boto3 |
27 | | -from botocore.utils import IMDSFetcher |
28 | 26 | from cryptography.hazmat.backends import default_backend |
29 | 27 | from cryptography.hazmat.primitives import serialization |
30 | 28 | from cryptography.hazmat.primitives.asymmetric.rsa import RSAPrivateKey |
|
121 | 119 | from .time_util import HeartBeatTimer, get_time_millis |
122 | 120 | from .url_util import extract_top_level_domain_from_hostname |
123 | 121 | from .util_text import construct_hostname, parse_account, split_statements |
124 | | -from .vendored import requests |
125 | | -from .wif_util import DEFAULT_ENTRA_SNOWFLAKE_RESOURCE, AttestationProvider |
| 122 | +from .wif_util import AttestationProvider |
126 | 123 |
|
127 | 124 | DEFAULT_CLIENT_PREFETCH_THREADS = 4 |
128 | 125 | MAX_CLIENT_PREFETCH_THREADS = 10 |
@@ -277,10 +274,6 @@ def _get_private_bytes_from_file( |
277 | 274 | True, |
278 | 275 | bool, |
279 | 276 | ), # Whether to log imported packages in telemetry |
280 | | - "log_platform_in_telemetry": ( |
281 | | - True, |
282 | | - bool, |
283 | | - ), # Whether to log platform in telemetry |
284 | 277 | "disable_query_context_cache": ( |
285 | 278 | False, |
286 | 279 | bool, |
@@ -387,133 +380,6 @@ class TypeAndBinding(NamedTuple): |
387 | 380 | binding: str | None |
388 | 381 |
|
389 | 382 |
|
390 | | -def detect_platforms() -> list[str]: |
391 | | - def is_ec2_instance(timeout=0.5): |
392 | | - try: |
393 | | - fetcher = IMDSFetcher(timeout=timeout, num_attempts=2) |
394 | | - document = fetcher._get_request( |
395 | | - "/latest/dynamic/instance-identity/document", |
396 | | - None, |
397 | | - fetcher._fetch_metadata_token(), |
398 | | - ) |
399 | | - return bool(document.content) |
400 | | - except Exception: |
401 | | - return False |
402 | | - |
403 | | - def is_aws_lambda(): |
404 | | - return "LAMBDA_TASK_ROOT" in os.environ |
405 | | - |
406 | | - def is_valid_arn_for_wif(arn: str) -> bool: |
407 | | - patterns = [ |
408 | | - r"^arn:[^:]+:iam::[^:]+:user/.+$", |
409 | | - r"^arn:[^:]+:sts::[^:]+:assumed-role/.+$", |
410 | | - ] |
411 | | - return any(re.match(p, arn) for p in patterns) |
412 | | - |
413 | | - def has_aws_identity(): |
414 | | - try: |
415 | | - caller_identity = boto3.client("sts").get_caller_identity() |
416 | | - if not caller_identity or "Arn" not in caller_identity: |
417 | | - return False |
418 | | - else: |
419 | | - return is_valid_arn_for_wif(caller_identity["Arn"]) |
420 | | - except Exception: |
421 | | - return False |
422 | | - |
423 | | - def is_azure_vm(timeout=0.5): |
424 | | - try: |
425 | | - token_resp = requests.get( |
426 | | - "http://169.254.169.254/metadata/instance?api-version=2021-02-01", |
427 | | - headers={"Metadata": "true"}, |
428 | | - timeout=timeout, |
429 | | - ) |
430 | | - return token_resp.status_code == 200 |
431 | | - except requests.RequestException: |
432 | | - return False |
433 | | - |
434 | | - def is_azure_function(): |
435 | | - service_vars = [ |
436 | | - "FUNCTIONS_WORKER_RUNTIME", |
437 | | - "FUNCTIONS_EXTENSION_VERSION", |
438 | | - "AzureWebJobsStorage", |
439 | | - ] |
440 | | - return all(var in os.environ for var in service_vars) |
441 | | - |
442 | | - def is_managed_identity_available_on_azure_vm( |
443 | | - resource=DEFAULT_ENTRA_SNOWFLAKE_RESOURCE, timeout=0.5 |
444 | | - ): |
445 | | - endpoint = f"http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource={resource}" |
446 | | - headers = {"Metadata": "true"} |
447 | | - try: |
448 | | - response = requests.get(endpoint, headers=headers, timeout=timeout) |
449 | | - return response.status_code == 200 |
450 | | - except requests.RequestException: |
451 | | - return False |
452 | | - |
453 | | - def has_azure_managed_identity(on_azure_vm, on_azure_function): |
454 | | - if on_azure_function: |
455 | | - return bool(os.environ.get("IDENTITY_HEADER")) |
456 | | - if on_azure_vm: |
457 | | - return is_managed_identity_available_on_azure_vm() |
458 | | - return False |
459 | | - |
460 | | - def is_gce_vm(timeout=0.5): |
461 | | - try: |
462 | | - response = requests.get("http://metadata.google.internal", timeout=timeout) |
463 | | - return response.headers.get("Metadata-Flavor") == "Google" |
464 | | - except requests.RequestException: |
465 | | - return False |
466 | | - |
467 | | - def is_gce_cloud_run_service(): |
468 | | - service_vars = ["K_SERVICE", "K_REVISION", "K_CONFIGURATION"] |
469 | | - return all(var in os.environ for var in service_vars) |
470 | | - |
471 | | - def is_gce_cloud_run_job(): |
472 | | - job_vars = ["CLOUD_RUN_JOB", "CLOUD_RUN_EXECUTION"] |
473 | | - return all(var in os.environ for var in job_vars) |
474 | | - |
475 | | - def has_gcp_identity(timeout=2): |
476 | | - try: |
477 | | - response = requests.get( |
478 | | - "http://metadata/computeMetadata/v1/instance/service-accounts/default/email", |
479 | | - headers={"Metadata-Flavor": "Google"}, |
480 | | - timeout=timeout, |
481 | | - ) |
482 | | - response.raise_for_status() |
483 | | - return bool(response.text) |
484 | | - except requests.RequestException: |
485 | | - return False |
486 | | - |
487 | | - def is_github_action(): |
488 | | - return "GITHUB_ACTIONS" in os.environ |
489 | | - |
490 | | - with ThreadPoolExecutor(max_workers=10) as executor: |
491 | | - futures = { |
492 | | - "is_ec2_instance": executor.submit(is_ec2_instance), |
493 | | - "is_aws_lambda": executor.submit(is_aws_lambda), |
494 | | - "has_aws_identity": executor.submit(has_aws_identity), |
495 | | - "is_azure_vm": executor.submit(is_azure_vm), |
496 | | - "is_azure_function": executor.submit(is_azure_function), |
497 | | - "is_gce_vm": executor.submit(is_gce_vm), |
498 | | - "is_gce_cloud_run_service": executor.submit(is_gce_cloud_run_service), |
499 | | - "is_gce_cloud_run_job": executor.submit(is_gce_cloud_run_job), |
500 | | - "has_gcp_identity": executor.submit(has_gcp_identity), |
501 | | - "is_github_action": executor.submit(is_github_action), |
502 | | - } |
503 | | - |
504 | | - platforms = {key: future.result() for key, future in futures.items()} |
505 | | - |
506 | | - platforms["azure_managed_identity"] = has_azure_managed_identity( |
507 | | - platforms["is_azure_vm"], platforms["is_azure_function"] |
508 | | - ) |
509 | | - |
510 | | - detected_platforms = [ |
511 | | - platform for platform, detected in platforms.items() if detected |
512 | | - ] |
513 | | - |
514 | | - return detected_platforms |
515 | | - |
516 | | - |
517 | 383 | class SnowflakeConnection: |
518 | 384 | """Implementation of the connection object for the Snowflake Database. |
519 | 385 |
|
@@ -682,8 +548,6 @@ def __init__( |
682 | 548 |
|
683 | 549 | # get the imported modules from sys.modules |
684 | 550 | self._log_telemetry_imported_packages() |
685 | | - # log the platform of the client |
686 | | - Thread(target=self._log_telemetry_platform_info(), daemon=True).start() |
687 | 551 | # check SNOW-1218851 for long term improvement plan to refactor ocsp code |
688 | 552 | atexit.register(self._close_at_exit) |
689 | 553 |
|
@@ -2339,20 +2203,6 @@ def _log_telemetry_imported_packages(self) -> None: |
2339 | 2203 | ) |
2340 | 2204 | ) |
2341 | 2205 |
|
2342 | | - def _log_telemetry_platform_info(self) -> None: |
2343 | | - if self._log_platform_in_telemetry: |
2344 | | - ts = get_time_millis() |
2345 | | - self._log_telemetry( |
2346 | | - TelemetryData.from_telemetry_data_dict( |
2347 | | - from_dict={ |
2348 | | - TelemetryField.KEY_TYPE.value: TelemetryField.PLATFORM_INFO.value, |
2349 | | - TelemetryField.KEY_VALUE.value: str(detect_platforms()), |
2350 | | - }, |
2351 | | - timestamp=ts, |
2352 | | - connection=self, |
2353 | | - ) |
2354 | | - ) |
2355 | | - |
2356 | 2206 | def is_valid(self) -> bool: |
2357 | 2207 | """This function tries to answer the question: Is this connection still good for sending queries? |
2358 | 2208 | Attempts to validate the connections both on the TCP/IP and Session levels.""" |
|
0 commit comments