From f802f97bb4b81f0eafd5e7f5813d9430663e46da Mon Sep 17 00:00:00 2001 From: Jessica Scheick Date: Mon, 8 Jan 2024 13:45:17 -0500 Subject: [PATCH 01/17] add auth_info property --- earthaccess/auth.py | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/earthaccess/auth.py b/earthaccess/auth.py index a0033b0e..e8fcea31 100644 --- a/earthaccess/auth.py +++ b/earthaccess/auth.py @@ -95,12 +95,34 @@ def __init__(self) -> None: self.token: Optional[Mapping[str, str]] = None self._set_earthdata_system(PROD) - def login( - self, - strategy: str = "netrc", - persist: bool = False, - system: Optional[System] = None, - ) -> Any: + def __str__(self) -> str: + print_str = "Authentication Info\n" + "----------\n" + for k, v in self.auth_info: + print_str += str("{}: {}\n".format(k, v)) + + return print_str + + @property + def auth_info(self) -> Dict: + """Get information about the authentication session. + + Returns: + Dict: information about the auth object + """ + summary_dict: Dict[str, Any] + summary_dict = { + "authenticated?": self.authenticated, + "tokens": self.tokens, + } + + #modify this to get the region check if in s3 + # add a separate in uswest2 access point to api? + if "Region" in self.s3_bucket(): + summary_dict["cloud-info"] = self.s3_bucket() + + return summary_dict + + def login(self, strategy: str = "netrc", persist: bool = False) -> Any: """Authenticate with Earthdata login. Parameters: From 4d8d254456f0a9abcbd778e330714202289643fd Mon Sep 17 00:00:00 2001 From: Jessica Scheick Date: Mon, 8 Jan 2024 14:35:15 -0500 Subject: [PATCH 02/17] update function lists --- earthaccess/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/earthaccess/__init__.py b/earthaccess/__init__.py index aacf3e58..c2fc5270 100644 --- a/earthaccess/__init__.py +++ b/earthaccess/__init__.py @@ -13,6 +13,7 @@ get_s3_credentials, get_s3_filesystem, get_s3fs_session, + get_edl_token, granule_query, login, open, @@ -20,6 +21,7 @@ search_datasets, search_services, status, + in_us_west_2, ) from .auth import Auth from .dmrpp_zarr import open_virtual_dataset, open_virtual_mfdataset From 0087b7a21448d0cab7f68583382105f14fd53077 Mon Sep 17 00:00:00 2001 From: Jessica Scheick Date: Mon, 8 Jan 2024 14:38:44 -0500 Subject: [PATCH 03/17] add in_us_west_2 check to API --- earthaccess/api.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/earthaccess/api.py b/earthaccess/api.py index eafff703..0fd067cd 100644 --- a/earthaccess/api.py +++ b/earthaccess/api.py @@ -551,3 +551,12 @@ def auth_environ() -> Dict[str, str]: "`auth_environ()` requires you to first authenticate with `earthaccess.login()`" ) return {"EARTHDATA_USERNAME": auth.username, "EARTHDATA_PASSWORD": auth.password} + + +def in_us_west_2() -> bool: + """Returns true if the user is in AWS region us-west-2 + + Returns: + bool: boolean indicating if the user is in AWS region us-west-2 + """ + return earthaccess.__store__._running_in_us_west_2() From 0514dd991e4203441878e9863e79a8f3e6e8035f Mon Sep 17 00:00:00 2001 From: Jessica Scheick Date: Mon, 8 Jan 2024 14:39:25 -0500 Subject: [PATCH 04/17] switch to repr for auth --- earthaccess/auth.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/earthaccess/auth.py b/earthaccess/auth.py index e8fcea31..c8f25833 100644 --- a/earthaccess/auth.py +++ b/earthaccess/auth.py @@ -95,9 +95,9 @@ def __init__(self) -> None: self.token: Optional[Mapping[str, str]] = None self._set_earthdata_system(PROD) - def __str__(self) -> str: - print_str = "Authentication Info\n" + "----------\n" - for k, v in self.auth_info: + def __repr__(self) -> str: + print_str = "Authentication Info\n" + "-------------------\n" + for k, v in self.auth_info.items(): print_str += str("{}: {}\n".format(k, v)) return print_str @@ -115,11 +115,6 @@ def auth_info(self) -> Dict: "tokens": self.tokens, } - #modify this to get the region check if in s3 - # add a separate in uswest2 access point to api? - if "Region" in self.s3_bucket(): - summary_dict["cloud-info"] = self.s3_bucket() - return summary_dict def login(self, strategy: str = "netrc", persist: bool = False) -> Any: From 4d711423d366a4b0eee6425a69a3b7904688eac9 Mon Sep 17 00:00:00 2001 From: Jessica Scheick Date: Mon, 8 Jan 2024 14:43:26 -0500 Subject: [PATCH 05/17] return string instead of boolean --- earthaccess/api.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/earthaccess/api.py b/earthaccess/api.py index 0fd067cd..ce4f0c6e 100644 --- a/earthaccess/api.py +++ b/earthaccess/api.py @@ -553,10 +553,14 @@ def auth_environ() -> Dict[str, str]: return {"EARTHDATA_USERNAME": auth.username, "EARTHDATA_PASSWORD": auth.password} -def in_us_west_2() -> bool: +def in_us_west_2() -> str: """Returns true if the user is in AWS region us-west-2 Returns: - bool: boolean indicating if the user is in AWS region us-west-2 + str: string indicating if the user is in AWS region us-west-2 """ - return earthaccess.__store__._running_in_us_west_2() + if earthaccess.__store__._running_in_us_west_2() is True: + msg = "You are running in AWS region 'us-west-2'" + else: + msg = "You are not running in AWS region 'us-west-2'" + return msg From ffaf52ae8f1570537d2a8e4be78e9546783a3b2a Mon Sep 17 00:00:00 2001 From: Jessica Scheick Date: Wed, 10 Jan 2024 15:11:10 -0500 Subject: [PATCH 06/17] run linter --- earthaccess/__init__.py | 10 ++++++++-- earthaccess/api.py | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/earthaccess/__init__.py b/earthaccess/__init__.py index c2fc5270..b6524ca2 100644 --- a/earthaccess/__init__.py +++ b/earthaccess/__init__.py @@ -13,15 +13,17 @@ get_s3_credentials, get_s3_filesystem, get_s3fs_session, - get_edl_token, - granule_query, + in_us_west_2, login, open, search_data, search_datasets, +<<<<<<< HEAD search_services, status, in_us_west_2, +======= +>>>>>>> run linter ) from .auth import Auth from .dmrpp_zarr import open_virtual_dataset, open_virtual_mfdataset @@ -44,9 +46,13 @@ "get_fsspec_https_session", "get_s3fs_session", "get_s3_credentials", +<<<<<<< HEAD "get_s3_filesystem", "get_edl_token", "granule_query", +======= + "get_edl_token" "granule_query", +>>>>>>> run linter "collection_query", "open", "download", diff --git a/earthaccess/api.py b/earthaccess/api.py index ce4f0c6e..a59d4aa5 100644 --- a/earthaccess/api.py +++ b/earthaccess/api.py @@ -561,6 +561,6 @@ def in_us_west_2() -> str: """ if earthaccess.__store__._running_in_us_west_2() is True: msg = "You are running in AWS region 'us-west-2'" - else: + else: msg = "You are not running in AWS region 'us-west-2'" return msg From 5f851cf25fc080f1794617077e6967f4ea6169cc Mon Sep 17 00:00:00 2001 From: Jessica Scheick Date: Tue, 6 Feb 2024 13:41:59 -0500 Subject: [PATCH 07/17] replace us-west-2 check with boto3 functionality --- earthaccess/__init__.py | 7 ------- earthaccess/store.py | 25 ++++++------------------- 2 files changed, 6 insertions(+), 26 deletions(-) diff --git a/earthaccess/__init__.py b/earthaccess/__init__.py index b6524ca2..383c72f9 100644 --- a/earthaccess/__init__.py +++ b/earthaccess/__init__.py @@ -18,12 +18,9 @@ open, search_data, search_datasets, -<<<<<<< HEAD search_services, status, in_us_west_2, -======= ->>>>>>> run linter ) from .auth import Auth from .dmrpp_zarr import open_virtual_dataset, open_virtual_mfdataset @@ -46,13 +43,9 @@ "get_fsspec_https_session", "get_s3fs_session", "get_s3_credentials", -<<<<<<< HEAD "get_s3_filesystem", "get_edl_token", "granule_query", -======= - "get_edl_token" "granule_query", ->>>>>>> run linter "collection_query", "open", "download", diff --git a/earthaccess/store.py b/earthaccess/store.py index b6ddd955..bbe753d0 100644 --- a/earthaccess/store.py +++ b/earthaccess/store.py @@ -9,6 +9,7 @@ from typing import Any, Dict, List, Mapping, Optional, Tuple, Union from uuid import uuid4 +import boto3 import fsspec import requests import s3fs @@ -221,26 +222,12 @@ def _own_s3_credentials(self, links: List[Dict[str, Any]]) -> Union[str, None]: return None def _running_in_us_west_2(self) -> bool: - session = self.auth.get_session() - try: - # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html - token_ = session.put( - "http://169.254.169.254/latest/api/token", - headers={"X-aws-ec2-metadata-token-ttl-seconds": "21600"}, - timeout=1, - ) - resp = session.get( - "http://169.254.169.254/latest/meta-data/placement/region", - timeout=1, - headers={"X-aws-ec2-metadata-token": token_.text}, - ) - except Exception: - return False - - if resp.status_code == 200 and b"us-west-2" == resp.content: - # On AWS, in region us-west-2 + if (boto3.client('s3').meta.region_name == 'us-west-2'): return True - return False + else: + raise ValueError('Your instance is not running inside the' + ' AWS us-west-2 region.' + ' You will not be able to directly access NASA Earthdata S3 buckets') def set_requests_session( self, url: str, method: str = "get", bearer_token: bool = True From 0de00629e92ce1ebeb0c0770840601acb8f4475f Mon Sep 17 00:00:00 2001 From: jessicas11 Date: Tue, 6 Feb 2024 19:17:21 +0000 Subject: [PATCH 08/17] change api to error --- earthaccess/api.py | 7 ++++--- earthaccess/store.py | 4 +--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/earthaccess/api.py b/earthaccess/api.py index a59d4aa5..e65dc0ea 100644 --- a/earthaccess/api.py +++ b/earthaccess/api.py @@ -560,7 +560,8 @@ def in_us_west_2() -> str: str: string indicating if the user is in AWS region us-west-2 """ if earthaccess.__store__._running_in_us_west_2() is True: - msg = "You are running in AWS region 'us-west-2'" + return "You are running in AWS region 'us-west-2'" else: - msg = "You are not running in AWS region 'us-west-2'" - return msg + raise ValueError('Your instance is not running inside the' + ' AWS us-west-2 region.' + ' You will not be able to directly access NASA Earthdata S3 buckets') diff --git a/earthaccess/store.py b/earthaccess/store.py index bbe753d0..09de10df 100644 --- a/earthaccess/store.py +++ b/earthaccess/store.py @@ -225,9 +225,7 @@ def _running_in_us_west_2(self) -> bool: if (boto3.client('s3').meta.region_name == 'us-west-2'): return True else: - raise ValueError('Your instance is not running inside the' - ' AWS us-west-2 region.' - ' You will not be able to directly access NASA Earthdata S3 buckets') + return False def set_requests_session( self, url: str, method: str = "get", bearer_token: bool = True From 5caccf9f31de87b6898c7a1b3d6ebbaa45b39bd5 Mon Sep 17 00:00:00 2001 From: Chris Battisto <34325676+battistowx@users.noreply.github.com> Date: Tue, 6 Feb 2024 13:37:01 -0600 Subject: [PATCH 09/17] Change boto3 region check to use botocore --- earthaccess/store.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/earthaccess/store.py b/earthaccess/store.py index 09de10df..bb906c22 100644 --- a/earthaccess/store.py +++ b/earthaccess/store.py @@ -9,7 +9,7 @@ from typing import Any, Dict, List, Mapping, Optional, Tuple, Union from uuid import uuid4 -import boto3 +import botocore.session import fsspec import requests import s3fs @@ -222,7 +222,7 @@ def _own_s3_credentials(self, links: List[Dict[str, Any]]) -> Union[str, None]: return None def _running_in_us_west_2(self) -> bool: - if (boto3.client('s3').meta.region_name == 'us-west-2'): + if (botocore.session.get_session().get_config_variable('region') == 'us-west-2'): return True else: return False From 43c4e250a73e0b6e70110d532b07b5cd348ef0c7 Mon Sep 17 00:00:00 2001 From: Jessica Scheick Date: Tue, 6 Feb 2024 14:53:08 -0500 Subject: [PATCH 10/17] use double quotes for ruff --- earthaccess/api.py | 6 +++--- earthaccess/store.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/earthaccess/api.py b/earthaccess/api.py index e65dc0ea..d8469fb3 100644 --- a/earthaccess/api.py +++ b/earthaccess/api.py @@ -562,6 +562,6 @@ def in_us_west_2() -> str: if earthaccess.__store__._running_in_us_west_2() is True: return "You are running in AWS region 'us-west-2'" else: - raise ValueError('Your instance is not running inside the' - ' AWS us-west-2 region.' - ' You will not be able to directly access NASA Earthdata S3 buckets') + raise ValueError("Your instance is not running inside the" + " AWS us-west-2 region." + " You will not be able to directly access NASA Earthdata S3 buckets.") diff --git a/earthaccess/store.py b/earthaccess/store.py index bb906c22..a0451e55 100644 --- a/earthaccess/store.py +++ b/earthaccess/store.py @@ -222,7 +222,7 @@ def _own_s3_credentials(self, links: List[Dict[str, Any]]) -> Union[str, None]: return None def _running_in_us_west_2(self) -> bool: - if (botocore.session.get_session().get_config_variable('region') == 'us-west-2'): + if (botocore.session.get_session().get_config_variable("region") == "us-west-2"): return True else: return False From 62b552da4affcffc32b385cb3053e5bd7c75120f Mon Sep 17 00:00:00 2001 From: Jessica Scheick Date: Tue, 6 Feb 2024 14:55:42 -0500 Subject: [PATCH 11/17] fix more formatting --- earthaccess/api.py | 8 +++++--- earthaccess/store.py | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/earthaccess/api.py b/earthaccess/api.py index d8469fb3..1147c7af 100644 --- a/earthaccess/api.py +++ b/earthaccess/api.py @@ -562,6 +562,8 @@ def in_us_west_2() -> str: if earthaccess.__store__._running_in_us_west_2() is True: return "You are running in AWS region 'us-west-2'" else: - raise ValueError("Your instance is not running inside the" - " AWS us-west-2 region." - " You will not be able to directly access NASA Earthdata S3 buckets.") + raise ValueError( + "Your instance is not running inside the" + " AWS us-west-2 region." + " You will not be able to directly access NASA Earthdata S3 buckets." + ) \ No newline at end of file diff --git a/earthaccess/store.py b/earthaccess/store.py index a0451e55..9276e385 100644 --- a/earthaccess/store.py +++ b/earthaccess/store.py @@ -222,7 +222,7 @@ def _own_s3_credentials(self, links: List[Dict[str, Any]]) -> Union[str, None]: return None def _running_in_us_west_2(self) -> bool: - if (botocore.session.get_session().get_config_variable("region") == "us-west-2"): + if botocore.session.get_session().get_config_variable("region") == "us-west-2": return True else: return False From 48514ddc0fb8282479d0ab05717b31a2ca263d72 Mon Sep 17 00:00:00 2001 From: Jessica Scheick Date: Tue, 6 Feb 2024 14:56:45 -0500 Subject: [PATCH 12/17] add newline --- earthaccess/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/earthaccess/api.py b/earthaccess/api.py index 1147c7af..304e7126 100644 --- a/earthaccess/api.py +++ b/earthaccess/api.py @@ -566,4 +566,4 @@ def in_us_west_2() -> str: "Your instance is not running inside the" " AWS us-west-2 region." " You will not be able to directly access NASA Earthdata S3 buckets." - ) \ No newline at end of file + ) From 39bbea65d9333db88682cdb837c6eea5a42a9b9e Mon Sep 17 00:00:00 2001 From: Jessica Scheick Date: Tue, 6 Feb 2024 14:57:47 -0500 Subject: [PATCH 13/17] remove tab --- earthaccess/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/earthaccess/api.py b/earthaccess/api.py index 304e7126..616ffecd 100644 --- a/earthaccess/api.py +++ b/earthaccess/api.py @@ -566,4 +566,4 @@ def in_us_west_2() -> str: "Your instance is not running inside the" " AWS us-west-2 region." " You will not be able to directly access NASA Earthdata S3 buckets." - ) + ) From 32d25e5e8b98fc4572d2ef651161f5f3be7ae6f1 Mon Sep 17 00:00:00 2001 From: Jessica Scheick Date: Tue, 6 Feb 2024 15:02:29 -0500 Subject: [PATCH 14/17] remove more tabs --- earthaccess/api.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/earthaccess/api.py b/earthaccess/api.py index 616ffecd..495231fb 100644 --- a/earthaccess/api.py +++ b/earthaccess/api.py @@ -563,7 +563,7 @@ def in_us_west_2() -> str: return "You are running in AWS region 'us-west-2'" else: raise ValueError( - "Your instance is not running inside the" - " AWS us-west-2 region." - " You will not be able to directly access NASA Earthdata S3 buckets." - ) + "Your instance is not running inside the" + " AWS us-west-2 region." + " You will not be able to directly access NASA Earthdata S3 buckets." + ) From a668ccb3348e4edf8c4c82b08f1f3f7a2d7b0aa0 Mon Sep 17 00:00:00 2001 From: Jessica Scheick Date: Tue, 6 Feb 2024 15:03:26 -0500 Subject: [PATCH 15/17] remove more tabs --- earthaccess/api.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/earthaccess/api.py b/earthaccess/api.py index 495231fb..f1f1cedd 100644 --- a/earthaccess/api.py +++ b/earthaccess/api.py @@ -563,7 +563,7 @@ def in_us_west_2() -> str: return "You are running in AWS region 'us-west-2'" else: raise ValueError( - "Your instance is not running inside the" - " AWS us-west-2 region." - " You will not be able to directly access NASA Earthdata S3 buckets." - ) + "Your instance is not running inside the" + " AWS us-west-2 region." + " You will not be able to directly access NASA Earthdata S3 buckets." + ) From f18335e875145cafa0c01e652831a0cc21d54fe1 Mon Sep 17 00:00:00 2001 From: Jessica Scheick Date: Tue, 6 Feb 2024 15:05:47 -0500 Subject: [PATCH 16/17] update docstring --- earthaccess/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/earthaccess/api.py b/earthaccess/api.py index f1f1cedd..ff6dfcf6 100644 --- a/earthaccess/api.py +++ b/earthaccess/api.py @@ -554,7 +554,7 @@ def auth_environ() -> Dict[str, str]: def in_us_west_2() -> str: - """Returns true if the user is in AWS region us-west-2 + """Returns a message indicating if the user is in AWS region us-west-2 Returns: str: string indicating if the user is in AWS region us-west-2 From d7ccdc1df7787363e87348c8a8cf9da536992c0b Mon Sep 17 00:00:00 2001 From: Jessica Scheick Date: Tue, 16 Sep 2025 13:45:39 -0400 Subject: [PATCH 17/17] cleanup after resolving conflicts --- earthaccess/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/earthaccess/__init__.py b/earthaccess/__init__.py index 383c72f9..a1cb435f 100644 --- a/earthaccess/__init__.py +++ b/earthaccess/__init__.py @@ -13,6 +13,7 @@ get_s3_credentials, get_s3_filesystem, get_s3fs_session, + granule_query, in_us_west_2, login, open, @@ -20,7 +21,6 @@ search_datasets, search_services, status, - in_us_west_2, ) from .auth import Auth from .dmrpp_zarr import open_virtual_dataset, open_virtual_mfdataset