diff --git a/binder/environment.yml b/binder/environment.yml index 2bc59969..c59ef68d 100644 --- a/binder/environment.yml +++ b/binder/environment.yml @@ -2,6 +2,7 @@ name: earthaccess channels: - conda-forge dependencies: + - boto3 - python=3.9 - xarray>=0.19 - dask>=2022.1 diff --git a/earthaccess/__init__.py b/earthaccess/__init__.py index 4977bf38..c8f859f1 100644 --- a/earthaccess/__init__.py +++ b/earthaccess/__init__.py @@ -10,7 +10,7 @@ get_requests_https_session, get_s3_credentials, get_s3fs_session, - granule_query, + in_us_west_2, login, open, search_data, @@ -24,6 +24,7 @@ logger = logging.getLogger(__name__) __all__ = [ + # api.py "login", "search_datasets", "search_data", @@ -31,15 +32,20 @@ "get_fsspec_https_session", "get_s3fs_session", "get_s3_credentials", - "granule_query", + "get_edl_token" "granule_query", "collection_query", "open", "download", + "auth_environ", + "in_us_west_2", + # search.py "DataGranules", "DataCollections", + # auth.py "Auth", + # store.py "Store", - "auth_environ", + # kerchunk "consolidate_metadata", ] diff --git a/earthaccess/api.py b/earthaccess/api.py index a7d35fb0..f93ad637 100644 --- a/earthaccess/api.py +++ b/earthaccess/api.py @@ -353,3 +353,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 2a820402..b8dd6657 100644 --- a/earthaccess/auth.py +++ b/earthaccess/auth.py @@ -76,6 +76,28 @@ def __init__(self) -> None: self.EDL_GENERATE_TOKENS_URL = "https://urs.earthdata.nasa.gov/api/users/token" self.EDL_REVOKE_TOKEN = "https://urs.earthdata.nasa.gov/api/users/revoke_token" + 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. @@ -168,6 +190,7 @@ def get_s3_credentials( provider: A valid cloud provider. Each DAAC has a provider code for their cloud distributions. endpoint: Getting the credentials directly from the S3Credentials URL. + Returns: A Python dictionary with the temporary AWS S3 credentials. """ diff --git a/earthaccess/store.py b/earthaccess/store.py index 15ae4ef2..f17f29ef 100644 --- a/earthaccess/store.py +++ b/earthaccess/store.py @@ -8,6 +8,7 @@ from typing import Any, Dict, List, Mapping, Optional, Tuple, Union from uuid import uuid4 +import botocore.session import fsspec import requests import s3fs @@ -137,26 +138,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 = False