Authentication for NASA Earthdata

Summary

This notebook creates a hidden .netrc file (_netrc for Window OS) with Earthdata login credentials in your home directory. This file is needed to access NASA Earthdata assets from a scripting environment like Python.

Earthdata Login

An Earthdata Login account is required to access data, as well as discover restricted data, from the NASA Earthdata system. Thus, to access NASA data, you need Earthdata Login. Please visit https://urs.earthdata.nasa.gov to register and manage your Earthdata Login account. This account is free to create and only takes a moment to set up.

Authentication via netrc File

You will need a netrc file containing your NASA Earthdata Login credentials in order to execute the notebooks. A netrc file can be created manually within text editor and saved to your home directory. An example of the required content is below.

machine urs.earthdata.nasa.gov
login <USERNAME>
password <PASSWORD>

<USERNAME> and <PASSWORD> would be replaced by your actual Earthdata Login username and password respectively.

Import Required Packages

from netrc import netrc
from subprocess import Popen
from platform import system
from getpass import getpass
import os

The code below will:

  1. check what operating system (OS) is being used to determine which netrc file to check for/create (.netrc or _netrc)
  2. check if you have an netrc file, and if so, varify if those credentials are for the Earthdata endpoint
  3. create a netrc file if a netrc file is not present.
urs = 'urs.earthdata.nasa.gov'    # Earthdata URL endpoint for authentication
prompts = ['Enter NASA Earthdata Login Username: ',
           'Enter NASA Earthdata Login Password: ']

# Determine the OS (Windows machines usually use an '_netrc' file)
netrc_name = "_netrc" if system()=="Windows" else ".netrc"

# Determine if netrc file exists, and if so, if it includes NASA Earthdata Login Credentials
try:
    netrcDir = os.path.expanduser(f"~/{netrc_name}")
    netrc(netrcDir).authenticators(urs)[0]

# Below, create a netrc file and prompt user for NASA Earthdata Login Username and Password
except FileNotFoundError:
    homeDir = os.path.expanduser("~")
    Popen('touch {0}{2} | echo machine {1} >> {0}{2}'.format(homeDir + os.sep, urs, netrc_name), shell=True)
    Popen('echo login {} >> {}{}'.format(getpass(prompt=prompts[0]), homeDir + os.sep, netrc_name), shell=True)
    Popen('echo \'password {} \'>> {}{}'.format(getpass(prompt=prompts[1]), homeDir + os.sep, netrc_name), shell=True)
    # Set restrictive permissions
    Popen('chmod 0600 {0}{1}'.format(homeDir + os.sep, netrc_name), shell=True)

    # Determine OS and edit netrc file if it exists but is not set up for NASA Earthdata Login
except TypeError:
    homeDir = os.path.expanduser("~")
    Popen('echo machine {1} >> {0}{2}'.format(homeDir + os.sep, urs, netrc_name), shell=True)
    Popen('echo login {} >> {}{}'.format(getpass(prompt=prompts[0]), homeDir + os.sep, netrc_name), shell=True)
    Popen('echo \'password {} \'>> {}{}'.format(getpass(prompt=prompts[1]), homeDir + os.sep, netrc_name), shell=True)

See if the file was created

If the file was created, we’ll see a .netrc file (_netrc for Window OS) in the list printed below. To view the contents from a Jupyter environment, click File on the top toolbar, select Open from Path…, type .netrc, and click Open. The .netrc file will open within the text editor.

!!! Beware, your password will be visible if the .netrc file is opened in the text editor.

!ls -al ~/
total 128
drwxr-xr-x 25 jovyan jovyan  6144 Nov 11 18:59  .
drwxr-xr-x  1 root   root      20 Mar 11  2022  ..
-rw-------  1 jovyan jovyan 12347 Nov 11 20:21  .bash_history
drwxr-xr-x  8 jovyan jovyan  6144 Nov  8 20:16  .cache
drwxrwsr-x  2 jovyan jovyan  6144 Apr 12  2022  .conda
drwxr-xr-x  4 jovyan jovyan  6144 Feb 28  2022  .config
drwx------  2 jovyan jovyan  6144 Nov 11 17:36  .git-credential-cache
-rw-r--r--  1 jovyan jovyan    56 Nov  4 15:14  .gitconfig
drwxr-xr-x  3 jovyan jovyan  6144 Apr 13  2022  .hidden_dir
drwxr-xr-x  2 jovyan jovyan  6144 Nov 11 02:45  .ipynb_checkpoints
drwxr-xr-x  5 jovyan jovyan  6144 Jul 20  2021  .ipython
drwxr-xr-x  3 jovyan jovyan  6144 Jul 20  2021  .jupyter
-rw-r--r--  1 jovyan jovyan     0 Jul 20  2021  .jupyter-server-log.txt
drwxr-xr-x  3 jovyan jovyan  6144 Jul 20  2021  .local
-rw-------  1 jovyan jovyan    73 Nov 11 18:24  .netrc
drwx------  2 jovyan jovyan  6144 Aug  4 15:42  .ssh
drwxr-xr-x 12 jovyan jovyan  6144 Nov 11 05:57  2022-ECOSTRESS-Cloud-Workshop
drwxr-xr-x 13 jovyan jovyan  6144 Nov 14 05:11  2022-Fall-ECOSTRESS-Cloud-Workshop
drwxr-xr-x 15 jovyan jovyan  6144 Nov  9 19:18  2022-Fall-ECOSTRESS-Cloud-Workshop_MJ
drwxr-xr-x 13 jovyan jovyan  6144 Nov 11 16:43  2022-Fall-ECOSTRESS-Cloud-Workshop_mmm
drwxr-xr-x  5 jovyan jovyan  6144 Nov 10 14:37 'Untitled Folder'
drwxr-xr-x  5 jovyan jovyan  6144 Aug 29 15:17  appeears-cloud-optimized-format-prototype
-rw-r--r--  1 jovyan jovyan   131 Nov 11 18:59  cookies.txt
drwxr-xr-x 15 jovyan jovyan  6144 Mar 10  2022  earthdata-cloud-cookbook
drwxr-xr-x  6 jovyan jovyan  6144 Apr 28  2022  lpdaac_cloud_data_access
drwxr-xr-x  6 jovyan jovyan  6144 Oct 20  2021  lpdaac_cloud_data_access1
drwxr-xr-x  4 jovyan jovyan  6144 Jul 20  2021  lpdaac_hls_tutorial
drwxr-xr-x  4 jovyan jovyan  6144 Sep 19 21:29  mentors-2022
drwxr-xr-x 27 jovyan jovyan  6144 Oct 28 01:20  shared
drwxr-xr-x 27 jovyan jovyan  6144 Oct 28 01:20  shared-readwrite