{ "cells": [ { "cell_type": "markdown", "id": "5fb09685-7cc9-406b-8790-90cdaa2faed4", "metadata": {}, "source": [ "# Authentication for NASA Earthdata " ] }, { "cell_type": "markdown", "id": "6d24bb81-cb31-45b8-918a-830ff7f72dcf", "metadata": {}, "source": [ "## Summary\n", "This notebook creates a hidden `.netrc` file with Earthdata login credentials in your home directory. This file is needed to access NASA Earthdata assets from a scripting environment like Python.\n", "\n", "### Earthdata Login\n", "\n", "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 to register and manage your Earthdata Login account. This account is free to create and only takes a moment to set up.\n", "\n", "### Authentication via netrc File\n", "\n", "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.\n", "\n", "```text\n", "machine urs.earthdata.nasa.gov\n", "login \n", "password \n", "```\n", "\n", "`` and `` would be replaced by your actual Earthdata Login username and password respectively." ] }, { "cell_type": "markdown", "id": "numerical-wilderness", "metadata": {}, "source": [ "## Import Required Packages" ] }, { "cell_type": "code", "execution_count": null, "id": "induced-shell", "metadata": {}, "outputs": [], "source": [ "from netrc import netrc\n", "from subprocess import Popen\n", "from platform import system\n", "from getpass import getpass\n", "import os" ] }, { "cell_type": "markdown", "id": "dominican-carry", "metadata": {}, "source": [ "The code below will:\n", "\n", "1. check what operating system (OS) is being used to determine which netrc file to check for/create (.netrc or _netrc)\n", "2. check if you have an netrc file, and if so, varify if those credentials are for the Earthdata endpoint\n", "3. create a netrc file if a netrc file is not present." ] }, { "cell_type": "code", "execution_count": null, "id": "signal-slide", "metadata": {}, "outputs": [], "source": [ "urs = 'urs.earthdata.nasa.gov' # Earthdata URL endpoint for authentication\n", "prompts = ['Enter NASA Earthdata Login Username: ',\n", " 'Enter NASA Earthdata Login Password: ']\n", "\n", "# Determine the OS (Windows machines usually use an '_netrc' file)\n", "netrc_name = '.netrc'\n", "\n", "# Determine if netrc file exists, and if so, if it includes NASA Earthdata Login Credentials\n", "try:\n", " netrcDir = os.path.expanduser(f\"~/{netrc_name}\")\n", " netrc(netrcDir).authenticators(urs)[0]\n", "\n", "# Below, create a netrc file and prompt user for NASA Earthdata Login Username and Password\n", "except FileNotFoundError:\n", " homeDir = os.path.expanduser(\"~\")\n", " Popen('touch {0}{2} | echo machine {1} >> {0}{2}'.format(homeDir + os.sep, urs, netrc_name), shell=True)\n", " Popen('echo login {} >> {}{}'.format(getpass(prompt=prompts[0]), homeDir + os.sep, netrc_name), shell=True)\n", " Popen('echo \\'password {} \\'>> {}{}'.format(getpass(prompt=prompts[1]), homeDir + os.sep, netrc_name), shell=True)\n", " # Set restrictive permissions\n", " Popen('chmod 0600 {0}{1}'.format(homeDir + os.sep, netrc_name), shell=True)\n", "\n", " # Determine OS and edit netrc file if it exists but is not set up for NASA Earthdata Login\n", "except TypeError:\n", " homeDir = os.path.expanduser(\"~\")\n", " Popen('echo machine {1} >> {0}{2}'.format(homeDir + os.sep, urs, netrc_name), shell=True)\n", " Popen('echo login {} >> {}{}'.format(getpass(prompt=prompts[0]), homeDir + os.sep, netrc_name), shell=True)\n", " Popen('echo \\'password {} \\'>> {}{}'.format(getpass(prompt=prompts[1]), homeDir + os.sep, netrc_name), shell=True)" ] }, { "cell_type": "markdown", "id": "white-democracy", "metadata": {}, "source": [ "#### See if the file was created" ] }, { "cell_type": "markdown", "id": "modern-italic", "metadata": {}, "source": [ "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. \n", "\n", "> **!!! Beware,** your password will be visible if the `.netrc` file is opened in the text editor." ] }, { "cell_type": "code", "execution_count": null, "id": "adjusted-render", "metadata": {}, "outputs": [], "source": [ "!ls -al ~/" ] }, { "cell_type": "code", "execution_count": null, "id": "16a9429c-1437-44d0-a038-48bbb735ca90", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "py39", "language": "python", "name": "py39" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.16" } }, "nbformat": 4, "nbformat_minor": 5 }