Skip to content

Commit

Permalink
Merge pull request #395 from jrbourbeau/check-region
Browse files Browse the repository at this point in the history
Add region to running in AWS check
  • Loading branch information
betolink authored Dec 5, 2023
2 parents bd6dce8 + 86f98d4 commit f5dc61e
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions earthaccess/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def make_instance(

# When sending EarthAccessFiles between processes, it's possible that
# we will need to switch between s3 <--> https protocols.
if (earthaccess.__store__.running_in_aws and cls is not s3fs.S3File) or (
not earthaccess.__store__.running_in_aws and cls is s3fs.S3File
if (earthaccess.__store__.in_region and cls is not s3fs.S3File) or (
not earthaccess.__store__.in_region and cls is s3fs.S3File
):
# NOTE: This uses the first data_link listed in the granule. That's not
# guaranteed to be the right one.
Expand Down Expand Up @@ -115,7 +115,7 @@ def __init__(self, auth: Any, pre_authorize: bool = False) -> None:
else:
print("Warning: the current session is not authenticated with NASA")
self.auth = None
self.running_in_aws = self._am_i_in_aws()
self.in_region = self._running_in_us_west_2()

def _derive_concept_provider(self, concept_id: Optional[str] = None) -> str:
if concept_id is not None:
Expand All @@ -139,18 +139,25 @@ def _own_s3_credentials(self, links: List[Dict[str, Any]]) -> Union[str, None]:
return link["URL"]
return None

def _am_i_in_aws(self) -> bool:
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
resp = session.put(
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:

if resp.status_code == 200 and b"us-west-2" == resp.content:
# On AWS in region us-west-2
return True
return False

Expand Down Expand Up @@ -333,7 +340,7 @@ def _open_granules(
"A valid Earthdata login instance is required to retrieve credentials"
)

if self.running_in_aws:
if self.in_region:
if granules[0].cloud_hosted:
access = "direct"
provider = granules[0]["meta"]["provider-id"]
Expand Down Expand Up @@ -395,7 +402,7 @@ def _open_urls(
)

url_mapping: Mapping[str, None] = {url: None for url in granules}
if self.running_in_aws and granules[0].startswith("s3"):
if self.in_region and granules[0].startswith("s3"):
if provider is not None:
s3_fs = self.get_s3fs_session(provider=provider)
if s3_fs is not None:
Expand Down Expand Up @@ -499,12 +506,12 @@ def _get_urls(
) -> List[str]:
data_links = granules
downloaded_files: List = []
if provider is None and self.running_in_aws and "cumulus" in data_links[0]:
if provider is None and self.in_region and "cumulus" in data_links[0]:
raise ValueError(
"earthaccess can't yet guess the provider for cloud collections, "
"we need to use one from earthaccess.list_cloud_providers()"
)
if self.running_in_aws and data_links[0].startswith("s3"):
if self.in_region and data_links[0].startswith("s3"):
print(f"Accessing cloud dataset using provider: {provider}")
s3_fs = self.get_s3fs_session(provider=provider)
# TODO: make this parallel or concurrent
Expand Down Expand Up @@ -532,11 +539,11 @@ def _get_granules(
provider = granules[0]["meta"]["provider-id"]
endpoint = self._own_s3_credentials(granules[0]["umm"]["RelatedUrls"])
cloud_hosted = granules[0].cloud_hosted
access = "direct" if (cloud_hosted and self.running_in_aws) else "external"
access = "direct" if (cloud_hosted and self.in_region) else "external"
data_links = list(
# we are not in region
chain.from_iterable(
granule.data_links(access=access, in_region=self.running_in_aws)
granule.data_links(access=access, in_region=self.in_region)
for granule in granules
)
)
Expand Down

0 comments on commit f5dc61e

Please sign in to comment.