Skip to content

Commit

Permalink
Support .env files (#110)
Browse files Browse the repository at this point in the history
Resolves #107 

Instead of having to export/set the variables in the user's shell, it'd
be handy to support a `.env` file where the prerequisite variables could
be set. We still support setting the variables directly in the shell /
Python code.

This PR adds the `python-dotenv` package to support .env files, and
clarifies the instructions in the README.

## Testing
Set the environment variables in a `.env` file and verify that they are
reflected properly in the app.
  • Loading branch information
sfc-gh-cnivera authored Aug 1, 2024
1 parent 533905f commit 584654a
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 6 deletions.
28 changes: 28 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Example config for username/password auth
SNOWFLAKE_ROLE="<my_role>"
SNOWFLAKE_WAREHOUSE="<my_warehouse>"
SNOWFLAKE_USER="<my_user>"
SNOWFLAKE_PASSWORD="<my_pw>"
SNOWFLAKE_ACCOUNT_LOCATOR="<my_snowflake_account>"
SNOWFLAKE_HOST="<my_snowflake_host>"


# Example config for externalbrowser auth
SNOWFLAKE_ROLE="<my_role>"
SNOWFLAKE_WAREHOUSE="<my_warehouse>"
SNOWFLAKE_USER="<my_user>"
SNOWFLAKE_PASSWORD="<my_pw>"
SNOWFLAKE_ACCOUNT_LOCATOR="<my_snowflake_account>"
SNOWFLAKE_HOST="<my_snowflake_host>"
SNOWFLAKE_AUTHENTICATOR="externalbrowser"


# Example config for username/password auth using MFA
SNOWFLAKE_ROLE="<my_role>"
SNOWFLAKE_WAREHOUSE="<my_warehouse>"
SNOWFLAKE_USER="<my_user>"
SNOWFLAKE_PASSWORD="<my_pw>"
SNOWFLAKE_ACCOUNT_LOCATOR="<my_snowflake_account>"
SNOWFLAKE_HOST="<my_snowflake_host>"
SNOWFLAKE_AUTHENTICATOR="username_password_mfa"
SNOWFLAKE_MFA_PASSCODE="<my_mfa_passcode>"
60 changes: 55 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ account, [follow these instructions](https://docs.snowflake.com/en/user-guide/or
* `SNOWFLAKE_HOST` is required if you are using the Streamlit app, but may not be required for the CLI tool depending on
your Snowflake deployment. We would recommend setting it regardless.

We recommend setting these environment variables by creating a `.env` file in the root directory of this repo. See the
examples in [`.env.example`](.env.example) for reference.

1. To set these on Mac OS/Linux:
However, if you would like to set these variables directly in your shell/Python environment,

1. MacOS/Linux syntax:

```bash
export SNOWFLAKE_ROLE="<your-snowflake-role>"
Expand All @@ -42,7 +46,7 @@ export SNOWFLAKE_ACCOUNT_LOCATOR="<your-snowflake-account-locator>"
export SNOWFLAKE_HOST="<your-snowflake-host>"
```

2. To set these on Windows:
2. Windows syntax:

```bash
set SNOWFLAKE_ROLE=<your-snowflake-role>
Expand All @@ -52,7 +56,7 @@ set SNOWFLAKE_ACCOUNT_LOCATOR=<your-snowflake-account-locator>
set SNOWFLAKE_HOST=<your-snowflake-host>
```

3. To set these within a Python environment:
3. Python syntax:

```python
import os
Expand All @@ -75,34 +79,80 @@ is set, the default is `snowflake`, which uses standard username/password suppor

```bash
# no SNOWFLAKE_AUTHENTICATOR needed
SNOWFLAKE_PASSWORD="<your-snowflake-password>"

# MacOS/Linux
export SNOWFLAKE_PASSWORD="<your-snowflake-password>"

# Windows
set SNOWFLAKE_PASSWORD=<your-snowflake-password>

# Python
os.environ['SNOWFLAKE_PASSWORD'] = '<your-snowflake-password>'
```

2. Username/Password with MFA passcode

Using a passcode from your authenticator app:

```bash
SNOWFLAKE_AUTHENTICATOR="username_password_mfa"
SNOWFLAKE_PASSWORD="<your-snowflake-password>"
SNOWFLAKE_MFA_PASSCODE="<your-snowflake-mfa-passcode>" # if your authenticator app reads "123 456", fill in "123456" (No spaces)

# MacOS/Linux
export SNOWFLAKE_AUTHENTICATOR="username_password_mfa"
export SNOWFLAKE_PASSWORD="<your-snowflake-password>"
export SNOWFLAKE_MFA_PASSCODE="<your-snowflake-mfa-passcode>"

# Windows
set SNOWFLAKE_AUTHENTICATOR=username_password_mfa
set SNOWFLAKE_PASSWORD=<your-snowflake-password>
set SNOWFLAKE_MFA_PASSCODE=<your-snowflake-mfa-passcode>

# if your authenticator app says "123 456", enter "123456" (no spaces)
export SNOWFLAKE_MFA_PASSCODE="<your-snowflake-mfa-passcode>"
# Python
os.environ['SNOWFLAKE_AUTHENTICATOR'] = 'username_password_mfa'
os.environ['SNOWFLAKE_PASSWORD'] = '<your-snowflake-password>'
os.environ['SNOWFLAKE_MFA_PASSCODE'] = '<your-snowflake-mfa-passcode>'
```

Using a passcode embedded in the password:

```bash
SNOWFLAKE_AUTHENTICATOR="username_password_mfa"
SNOWFLAKE_PASSWORD="<your-snowflake-password>"
SNOWFLAKE_MFA_PASSCODE_IN_PASSWORD="true"

# MacOS/Linux
export SNOWFLAKE_AUTHENTICATOR="username_password_mfa"
export SNOWFLAKE_PASSWORD="<your-snowflake-password>"
export SNOWFLAKE_MFA_PASSCODE_IN_PASSWORD="true"

# Windows
set SNOWFLAKE_AUTHENTICATOR=username_password_mfa
set SNOWFLAKE_PASSWORD=<your-snowflake-password>
set SNOWFLAKE_MFA_PASSCODE_IN_PASSWORD=true

# Python
os.environ['SNOWFLAKE_AUTHENTICATOR'] = 'username_password_mfa'
os.environ['SNOWFLAKE_PASSWORD'] = '<your-snowflake-password>'
os.environ['SNOWFLAKE_MFA_PASSCODE_IN_PASSWORD'] = 'true'
```

3. Single Sign-On (SSO) with Okta

```bash
# no SNOWFLAKE_PASSWORD needed
SNOWFLAKE_AUTHENTICATOR="externalbrowser"

# MacOS/Linux
export SNOWFLAKE_AUTHENTICATOR="externalbrowser"

# Windows
set SNOWFLAKE_AUTHENTICATOR=externalbrowser

# Python
os.environ['SNOWFLAKE_AUTHENTICATOR'] = 'externalbrowser'
```

## Usage
Expand Down
1 change: 1 addition & 0 deletions admin_apps/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
numpy==1.24.4
python-dotenv==1.0.1
urllib3==1.26.19
requests==2.32.3
sqlglot==23.17.0
Expand Down
16 changes: 15 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ sqlglot = "^23.10.0"
strictyaml = "^1.7.3"
streamlit-nightly = "^1.35.1.dev20240609"
numpy = "^1.24.4"
python-dotenv = "^1.0.1"

[tool.poetry.group.dev.dependencies]
mypy = "^1.9.0"
Expand Down
3 changes: 3 additions & 0 deletions semantic_model_generator/snowflake_utils/env_vars.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import os

from dotenv import load_dotenv

load_dotenv()
DEFAULT_SESSION_TIMEOUT_SEC = int(os.environ.get("SNOWFLAKE_SESSION_TIMEOUT_SEC", 120))
SNOWFLAKE_ROLE = os.getenv("SNOWFLAKE_ROLE")
SNOWFLAKE_WAREHOUSE = os.getenv("SNOWFLAKE_WAREHOUSE")
Expand Down

0 comments on commit 584654a

Please sign in to comment.