Skip to content

Commit

Permalink
added token get,delete, refresh and list operations
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-gangl committed Aug 24, 2022
1 parent 9680163 commit 15aba90
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 19 deletions.
72 changes: 53 additions & 19 deletions subscriber/podaac_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import hashlib
from datetime import datetime
import time
from requests.auth import HTTPBasicAuth



import requests
Expand All @@ -29,7 +31,8 @@
extensions = [".nc", ".h5", ".zip", ".tar.gz", ".tiff"]
edl = "urs.earthdata.nasa.gov"
cmr = "cmr.earthdata.nasa.gov"
token_url = "https://" + cmr + "/legacy-services/rest/tokens"
token_url = "https://" + edl + "/api/users"


IPAddr = "127.0.0.1" # socket.gethostbyname(hostname)

Expand Down Expand Up @@ -92,17 +95,26 @@ def setup_earthdata_login_auth(endpoint):
###############################################################################
# GET TOKEN FROM CMR
###############################################################################
def get_token(url: str, client_id: str, endpoint: str) -> str:
@tenacity.retry(wait=tenacity.wait_random_exponential(multiplier=1, max=60),
stop=tenacity.stop_after_attempt(3),
reraise=True,
retry=(tenacity.retry_if_result(lambda x: x == ''))
)
def get_token(url: str) -> str:
try:
token: str = ''
username, _, password = netrc.netrc().authenticators(endpoint)
xml: str = """<?xml version='1.0' encoding='utf-8'?>
<token><username>{}</username><password>{}</password><client_id>{}</client_id>
<user_ip_address>{}</user_ip_address></token>""".format(username, password, client_id, IPAddr) # noqa E501
headers: Dict = {'Content-Type': 'application/xml', 'Accept': 'application/json'} # noqa E501
resp = requests.post(url, headers=headers, data=xml)
username, _, password = netrc.netrc().authenticators(edl)
headers: Dict = {'Accept': 'application/json'} # noqa E501
resp = requests.post(url+"/token", headers=headers, auth=HTTPBasicAuth(username, password))
response_content: Dict = json.loads(resp.content)
token = response_content['token']['id']
if "error" in response_content:
if response_content["error"] == "max_token_limit":
logging.error("Max tokens acquired from URS. Deleting existing tokens")
for t in list_tokens(url):
delete_token(token_url,t)
return ''
#logging.debug("Status: {}".format(resp.status_code))
token = response_content['access_token']

# What error is thrown here? Value Error? Request Errors?
except: # noqa E722
Expand All @@ -113,23 +125,45 @@ def get_token(url: str, client_id: str, endpoint: str) -> str:
###############################################################################
# DELETE TOKEN FROM CMR
###############################################################################
def delete_token(url: str, token: str) -> None:
def delete_token(url: str, token: str) -> bool:
try:
headers: Dict = {'Content-Type': 'application/xml', 'Accept': 'application/json'} # noqa E501
url = '{}/{}'.format(url, token)
resp = requests.request('DELETE', url, headers=headers)
if resp.status_code == 204:
logging.info("CMR token successfully deleted")
username, _, password = netrc.netrc().authenticators(edl)
headers: Dict = {'Accept': 'application/json'}
resp = requests.post(url+"/revoke_token",params={"token":token}, headers=headers, auth=HTTPBasicAuth(username, password))

if resp.status_code == 200:
logging.info("EDL token successfully deleted")
return True
else:
logging.info("CMR token deleting failed.")
logging.info("EDL token deleting failed.")

except: # noqa E722
logging.warning("Error deleting the token")

return False

def list_tokens(url: str):
try:
tokens = []
username, _, password = netrc.netrc().authenticators(edl)
headers: Dict = {'Accept': 'application/json'} # noqa E501
resp = requests.get(url+"/tokens", headers=headers, auth=HTTPBasicAuth(username, password))
response_content = json.loads(resp.content)

for x in response_content:
tokens.append(x['access_token'])

except: # noqa E722
logging.warning("Error getting the token - check user name and password")
return tokens




def refresh_token(old_token: str, client_id: str):
def refresh_token(old_token: str):
setup_earthdata_login_auth(edl)
delete_token(token_url, old_token)
return get_token(token_url, client_id, edl)
delete_token(token_url,old_token)
return get_token(token_url)


def validate(args):
Expand Down
49 changes: 49 additions & 0 deletions tests/test_token_regression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import pytest
import os
from os.path import exists

from subscriber import podaac_access as pa
import shutil
from pathlib import Path


def setup_module(module):
print('*****SETUP*****')
tokens = pa.list_tokens(pa.token_url)
for x in tokens:
pa.delete_token(pa.token_url, x)

def teardown_module(module):
print('*****TEARDOWN*****')
tokens = pa.list_tokens(pa.token_url)
for x in tokens:
pa.delete_token(pa.token_url, x)

# REGRESSION TEST CURRENTLY REQUIRES A .NETRC file for CMR/Data Download
# token API can be found here: https://wiki.earthdata.nasa.gov/display/EL/API+Documentation
def test_list_tokens():
tokens = pa.list_tokens(pa.token_url)
for x in tokens:
pa.delete_token(pa.token_url, x)

def test_edl_getToken():
token = pa.get_token(pa.token_url)
assert token != ""
token = pa.refresh_token(token)
assert token != ""
tokens = pa.list_tokens(pa.token_url)

assert len(tokens) == 1
for x in tokens:
assert x != ""

assert True == pa.delete_token(pa.token_url, token)

def test_edl_max_token():
#call this 3 times since we're capped out at 2 total...
token = pa.get_token(pa.token_url)
assert token != ""
token = pa.get_token(pa.token_url)
assert token != ""
token = pa.get_token(pa.token_url)
assert token != ""

0 comments on commit 15aba90

Please sign in to comment.