Skip to content

Commit 1ee50ea

Browse files
authored
Merge pull request #30 from DenisaCG/credentials
Update credentials extraction
2 parents abaafc1 + f4603f3 commit 1ee50ea

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ for example: `~/.jupyter/jupyter_notebook_config.py`, and specify your long-term
3636
```python
3737
c = get_config()
3838

39-
c.DrivesConfig.access_key_id = "<AWS Access Key ID / IAM Access Key ID>"
40-
c.DrivesConfig.secret_access_key = "<AWS Secret Access Key / IAM Secret>"
41-
c.DrivesConfig.session_token = "<AWS Session Token / IAM Session Token>"
39+
c.DrivesConfig.access_key_id = "<Drives Access Key ID / IAM Access Key ID>"
40+
c.DrivesConfig.secret_access_key = "<Drives Secret Access Key / IAM Secret>"
41+
c.DrivesConfig.session_token = "<Drives Session Token / IAM Session Token>"
4242
```
4343

4444
### Custom credentials file path
@@ -51,13 +51,26 @@ c = get_config()
5151
c.DrivesConfig.custom_credentials_path = "path/to/file/containing/credentials"
5252
```
5353

54+
### Environment variables
55+
56+
The credentials can also be set through environment variables that will be automatically extracted.
57+
58+
````bash
59+
export JP_DRIVES_PROVIDER="<Drives provider e.g.: s3, gcs>"
60+
export JP_DRIVES_ACCESS_KEY_ID="<Drives Access Key ID>"
61+
export JP_DRIVES_SECRET_ACCESS_KEY="<Drives Secret Access Key>"
62+
export JP_DRIVES_SESSION_TOKEN="<Drives Session Token (optional)>"
63+
export JP_DRIVES_CUSTOM_CREDENTIALS_PATH="<Path to local file which contains credentials (optional)>"
64+
```
65+
66+
5467
## Uninstall
5568

5669
To remove the extension, execute:
5770

5871
```bash
5972
pip uninstall jupyter_drives
60-
```
73+
````
6174
6275
## Troubleshoot
6376

jupyter_drives/base.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,11 @@ def _load_credentials(self):
8585
if self.access_key_id is not None and self.secret_access_key is not None:
8686
return
8787

88-
# check if user provided custom path for credentials extraction
88+
# check if user provided custom path for credentials extraction
89+
if self.custom_credentials_path is None and "JP_DRIVES_CUSTOM_CREDENTIALS_PATH" in os.environ:
90+
self.custom_credentials_path = os.environ["JP_DRIVES_CUSTOM_CREDENTIALS_PATH"]
8991
if self.custom_credentials_path is not None:
90-
self.access_key_id, self.secret_access_key, self.session_token = self._extract_credentials_from_file(self.custom_credentials_path)
92+
self.provider, self.access_key_id, self.secret_access_key, self.session_token = self._extract_credentials_from_file(self.custom_credentials_path)
9193
return
9294

9395
# if not, try to load credentials from AWS CLI
@@ -96,25 +98,31 @@ def _load_credentials(self):
9698
self.access_key_id, self.secret_access_key, self.session_token = self._extract_credentials_from_file(aws_credentials_path)
9799
return
98100

99-
# for dev purposes, use environment variables
100-
if os.environ["JP_AWS_ACCESS_KEY_ID"] is not None and os.environ["JP_AWS_SECRET_ACCESS_KEY"] is not None:
101-
self.access_key_id = os.environ["JP_AWS_ACCESS_KEY_ID"]
102-
self.secret_access_key = os.environ["JP_AWS_SECRET_ACCESS_KEY"]
101+
# as a last resort, use environment variables
102+
if "JP_DRIVES_ACCESS_KEY_ID" in os.environ and "JP_DRIVES_SECRET_ACCESS_KEY" in os.environ:
103+
self.access_key_id = os.environ["JP_DRIVES_ACCESS_KEY_ID"]
104+
self.secret_access_key = os.environ["JP_DRIVES_SECRET_ACCESS_KEY"]
105+
if "JP_DRIVES_SESSION_TOKEN" in os.environ:
106+
self.session_token = os.environ["JP_DRIVES_SESSION_TOKEN"]
107+
if "JP_DRIVES_PROVIDER" in os.environ:
108+
self.provider = os.environ["JP_DRIVES_PROVIDER"]
103109
return
104110

105111
def _extract_credentials_from_file(self, file_path):
106112
try:
107113
with open(file_path, 'r') as file:
108-
access_key_id, secret_access_key, session_token = None, None, None
114+
provider, access_key_id, secret_access_key, session_token = None, None, None, None
109115
lines = file.readlines()
110116
for line in lines:
111-
if line.startswith("aws_access_key_id ="):
117+
if line.startswith("provider ="):
118+
provider = line.split("=")[1].strip()
119+
elif line.startswith("drives_access_key_id ="):
112120
access_key_id = line.split("=")[1].strip()
113-
elif line.startswith("aws_secret_access_key ="):
121+
elif line.startswith("drives_secret_access_key ="):
114122
secret_access_key = line.split("=")[1].strip()
115-
elif line.startswith("session_token ="):
123+
elif line.startswith("drives_session_token ="):
116124
session_token = line.split("=")[1].strip()
117-
return access_key_id, secret_access_key, session_token
125+
return provider, access_key_id, secret_access_key, session_token
118126
except Exception as e:
119127
print(f"Failed loading credentials from {file_path}: {e}")
120128
return

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ const driveFileBrowser: JupyterFrontEndPlugin<void> = {
218218

219219
// create drive for drive file browser
220220
const drive = new Drive({
221-
name: 'jupyter-drives',
221+
name: 's3',
222222
drivesList: drivesList
223223
});
224224

0 commit comments

Comments
 (0)