Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Populate with earthaccess.login guide in User Guide Authentications docs #862

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html)

## [Unreleased]

- Add authentication to User Guide documentation. ([#763](https://github.com/nsidc/earthaccess/pull/763)) ([@andypbarrett](https://github.com/andypbarrett))

### Changed

- Use built-in `assert` statements instead of `unittest` assertions in
Expand Down
140 changes: 134 additions & 6 deletions docs/user_guide/authenticate.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,139 @@
# Authentication

Introduces the `earthaccess.login` method for managing Earthdata Login and cloud credentials.
You can use `earthaccess` to search for datasets and data without needing to login. However, to access (download or stream) NASA Earth science data, whether from one of the NASA
andypbarrett marked this conversation as resolved.
Show resolved Hide resolved
Distributed Active Archive Centers (DAACs) or from NASA Earthdata Cloud, you need
an Earthdata Login. You can register for a a free Earthdata Login (EDL) account [here](https://urs.earthdata.nasa.gov/).
andypbarrett marked this conversation as resolved.
Show resolved Hide resolved

!!! note "This Page is a Work in Progress"
Once you have an Earthdata Login, the `earthaccess.login` method manages Earthdata Login, and cloud credentials when you are working with cloud-hosted data.
andypbarrett marked this conversation as resolved.
Show resolved Hide resolved

We are reorganizing and updating the documentation, so not all pages are complete. If you are looking for information about authenticating using earthaccess see the
How-Tos and Tutorials in links below.
`earthaccess.login` offers three methods of logging in (or authenticating) using EDL: [a manual login method](#login-manually), where you enter EDL username and password manually; and two automatic login methods using EDL credentials stored in a [`.netrc`](#login-using-a-netrc) file or in [environment variables](#login-using-environment-variables). By default, `earthaccess.login()` will look for a `.netrc` or environment variables first. If neither of these are found, it will prompt you to enter your username and password. The three methods are described in detail below.
andypbarrett marked this conversation as resolved.
Show resolved Hide resolved

* [Quick start](../quick-start.md)
* [How-To Authenticate with earthaccess](../howto/authenticate.md)
`earthaccess.login` can also be used to login to [different endpoints](#accessing-different-endpoints) and [get S3 credentials](#using-earthaccess-to-get-credentials).

## Login Manually

If you have not created a `.netrc` file or `EARTHDATA_USERNAME` and `EARTHDATA_PASSWORD`, you can use the following approach to login.
andypbarrett marked this conversation as resolved.
Show resolved Hide resolved

```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
```
```python

>>> import earthaccess

>>> auth = earthaccess.login()
Enter your Earthdata Login username: your_username
Enter your Earthdata password:
```

You don't need to assign the result of `earthaccess.login()` to a variable but doing so enables access session information. These are discussed in [Accessing Session Information]().
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
You don't need to assign the result of `earthaccess.login()` to a variable but doing so enables access session information. These are discussed in [Accessing Session Information]().
You don't need to assign the result of `earthaccess.login()` to a variable but doing so enables access to session information. These are discussed in [Accessing Session Information]().


Setting `strategy=interactive` will force a manual login.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Setting `strategy=interactive` will force a manual login.
Setting `earthaccess.login(strategy=interactive)` will force a manual login.


## Login using a `.netrc`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a note here might be really useful -- we don't (yet) support encrypted netrc files, and this strategy of writing credentials in plain text to disk should not be used on untrusted machines or shared user accounts.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion. I will add an admonition


### Creating a `.netrc` file

#### Using `earthaccess.login` to create a `.netrc` file

You can use `earthaccess.login` to create a `.netrc` for you.
```
earthaccess.login(persist=True)
```
You will then be prompted for your Earthdata Login username and password. A `.netrc` (or `_netrc`) file will be created automatically.

#### Manually creating a `.netrc` file for Earthdata Login Credentials

=== "MacOS"
Type the following on your command line, replacing `<username>` and `<password>` with your
Earthdata Login credentials.

```
echo "machine urs.earthdata.nasa.gov login <username> password <password>" >> $HOME/.netrc
chmod 600 $HOME/.netrc
```

=== "Linux"
Type the following on your command line, replacing `<username>` and `<password>` with your
Earthdata Login credentials.

```
echo "machine urs.earthdata.nasa.gov login <username> password <password>" >> $HOME/.netrc
chmod 600 $HOME/.netrc
```
Comment on lines +50 to +66
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
=== "MacOS"
Type the following on your command line, replacing `<username>` and `<password>` with your
Earthdata Login credentials.
```
echo "machine urs.earthdata.nasa.gov login <username> password <password>" >> $HOME/.netrc
chmod 600 $HOME/.netrc
```
=== "Linux"
Type the following on your command line, replacing `<username>` and `<password>` with your
Earthdata Login credentials.
```
echo "machine urs.earthdata.nasa.gov login <username> password <password>" >> $HOME/.netrc
chmod 600 $HOME/.netrc
```
=== "MacOS & Linux"
Type the following on your command line, replacing `<username>` and `<password>` with your
Earthdata Login credentials.
```bash
echo "machine urs.earthdata.nasa.gov login <username> password <password>" >> $HOME/.netrc
chmod 600 $HOME/.netrc
```


=== "Windows"
In a `CMD` session, create a `%HOME%` environment variable. The following line
creates `%HOME%` from the path in `%USERPROFILE%`, which looks something like
`C:\Users\"username"`.
```
setx HOME %USERPROFILE%
```
You now need to create a `_netrc` file in `%HOME%`.
```
echo "machine urs.earthdata.nasa.gov login <username> password <password>" >> %HOME%\_netrc
```

## Login using environment variables

Alternatively, Earthdata Login credentials can be created as environment variables EARTHDATA_USERNAME and EARTHDATA_PASSWORD.
andypbarrett marked this conversation as resolved.
Show resolved Hide resolved

=== "MacOS"
If you want to set the environment variables for the current shell session, type the following on the command line.
```
export EARTHDATA_USERNAME="username"
export EARTHDATA_PASSWORD="password"
```
If you want to set these environmental variables permanently, add these two lines to your `.profile` file.


=== "Linux"
If you want to set the environment variables for the current shell session, type the following on the command line.
```
export EARTHDATA_USERNAME="username"
export EARTHDATA_PASSWORD="password"
```
If you want to set these environmental variables permanently, add these two lines to your `.bashrc` file.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.bashrc for Linux and .profile for Mac won't always be correct. I'm not very familiar with zsh yet, but I believe it uses a .zprofile file? Bash users can permanently set envvars in .profile as well, but that will only work for "login" shells. There isn't really a one-size-fits-all answer here, it depends on the shell.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So should we just say "add [these] to the appropriate configuration files for your operating system."

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that makes sense as a fallback instruction; e.g.:

If you use bash and would like to set these environment variables permanently, add these two lines to your ~/.profile file: ... code ... . Otherwise, use the recommended method to persistently set environment variables for whichever shell you use.

Something like that?


=== "Windows"
To set the environment variables for the current `CMD` session, type the following:
```
setx EARTHDATA_USERNAME "username"
setx EARTHDATA_PASSWORD "password"
```
To set these environmental variables permanantly
1. Open the start menu.
2. Search for the "Advanced System Settings" control panel and click on it.
3. Click on the "Environment Variables" button toward the bottom of the screen.
4. Follow the prompts to add the variable to the user table.


## Accessing different endpoints

### Earthdata User Acceptance Testing (UAT) environment
andypbarrett marked this conversation as resolved.
Show resolved Hide resolved

If your EDL account is authorized to access the User Acceptance Testing (UAT) system,
you can set earthaccess to work with its EDL and CMR endpoints
by setting the `system` argument at login, as follows:

```python
import earthaccess

earthaccess.login(system=earthaccess.UAT)

```

## Using `earthaccess` to get credentials
andypbarrett marked this conversation as resolved.
Show resolved Hide resolved

`earthaccess.login` is a very convenient way to manage and provide Earthdata Login credentials. `earthaccess.login` can also be used to obtain S3 credentials to access NASA Earthdata Cloud. If you use `earthaccess` to access data in the cloud, you do not have to use this option, `earthaccess` handles this. However, if you are using other packages, such as `h5coro`, `earthaccess` can save a lot of time.

```
import earthaccess
import xarray as xr
import h5coro

auth = earthaccess.login()
s3_credentials = auth.get_s3_credentials(daac="NSIDC")

s3url_atl23 = 'nsidc-cumulus-prod-protected/ATLAS/ATL23/001/2023/03/' \
'01/ATL23_20230401000000_10761801_001_01.h5'
ds = xr.open_dataset(s3url_atl23, engine='h5coro',
group='/mid_latitude/beam_1',
credentials=s3_credentials)
```
Loading