diff --git a/earthaccess/__init__.py b/earthaccess/__init__.py index aacf3e58..a1cb435f 100644 --- a/earthaccess/__init__.py +++ b/earthaccess/__init__.py @@ -14,6 +14,7 @@ get_s3_filesystem, get_s3fs_session, granule_query, + in_us_west_2, login, open, search_data, diff --git a/earthaccess/api.py b/earthaccess/api.py index d780e272..0be1ad0b 100644 --- a/earthaccess/api.py +++ b/earthaccess/api.py @@ -644,3 +644,19 @@ 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() -> str: + """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 + """ + 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." + ) diff --git a/earthaccess/auth.py b/earthaccess/auth.py index a0033b0e..c8f25833 100644 --- a/earthaccess/auth.py +++ b/earthaccess/auth.py @@ -95,12 +95,29 @@ 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 __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 + + @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, + } + + return summary_dict + + def login(self, strategy: str = "netrc", persist: bool = False) -> Any: """Authenticate with Earthdata login. Parameters: diff --git a/earthaccess/store.py b/earthaccess/store.py index 4a06e5f2..6ce496ef 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 botocore.session import fsspec import requests import s3fs @@ -231,26 +232,10 @@ 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 botocore.session.get_session().get_config_variable("region") == "us-west-2": return True - return False + else: + return False def set_requests_session( self, url: str, method: str = "get", bearer_token: bool = True