Skip to content

Commit ce60845

Browse files
authored
Merge pull request #14 from DenisaCG/credentialsCLI
Credentials extraction from specified path or from AWS CLI
2 parents c82834c + d860647 commit ce60845

File tree

3 files changed

+99
-8
lines changed

3 files changed

+99
-8
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,38 @@ To install the extension, execute:
1919
pip install jupyter_drives
2020
```
2121

22+
## Configure Credentials
23+
24+
To begin using the extension and gain access to your drives, you need to configure your user credentials generated by the provider (e.g.: `access_key`, `secret-access-key` and if applicable `session_token`).
25+
26+
For those working with `S3` drives using the `AWS` CLI, the credentials will be automatically extracted from `~/.aws/credentials`. There is nothing that needs to be done on your side.
27+
28+
> Note: This is only applicable for Linux or macOS `AWS` [CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) users. In case you are using the `AWS` CLI from Windows, you can use the custom file path configuration with `C:\Users\USERNAME\.aws\credentials`. You can read more about this [here](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html).
29+
30+
### Configuration file
31+
32+
Otherwise, you can add your credentials to the server configuration file. Create a `jupyter_notebook_config.py` file in one of the
33+
[Jupyter config directories](https://jupyter.readthedocs.io/en/latest/use/jupyter-directories.html#id1),
34+
for example: `~/.jupyter/jupyter_notebook_config.py`, and specify your long-term or short-term credentials.
35+
36+
```python
37+
c = get_config()
38+
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>"
42+
```
43+
44+
### Custom credentials file path
45+
46+
You can also just specify the location of a local file which contains your credentials. The `acccess_key`, `secret_access_key` and if applicable the `session_token` will be automatically extracted from there.
47+
48+
```python
49+
c = get_config()
50+
51+
c.DrivesConfig.custom_credentials_path = "path/to/file/containing/credentials"
52+
```
53+
2254
## Uninstall
2355

2456
To remove the extension, execute:

jupyter_drives/base.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
from sys import platform
13
import entrypoints
24
from traitlets import Enum, Unicode, default
35
from traitlets.config import Configurable
@@ -45,6 +47,13 @@ class DrivesConfig(Configurable):
4547
help="Base URL of the provider service REST API.",
4648
)
4749

50+
custom_credentials_path = Unicode(
51+
None,
52+
config = True,
53+
allow_none = True,
54+
help="Custom path of file where credentials are located. Extension automatically checks jupyter_notebook_config.py or directly in ~/.aws/credentials for AWS CLI users."
55+
)
56+
4857
@default("api_base_url")
4958
def set_default_api_base_url(self):
5059
# for AWS S3 drives
@@ -60,4 +69,41 @@ def set_default_api_base_url(self):
6069
default_value="s3",
6170
config=True,
6271
help="The source control provider.",
63-
)
72+
)
73+
74+
def __init__(self, **kwargs):
75+
super().__init__(**kwargs)
76+
self._load_credentials()
77+
78+
def _load_credentials(self):
79+
# check if credentials were already set in jupyter_notebook_config.py
80+
if self.access_key_id is not None and self.secret_access_key is not None:
81+
return
82+
83+
# check if user provided custom path for credentials extraction
84+
if self.custom_credentials_path is not None:
85+
self.access_key_id, self.secret_access_key, self.session_token = self._extract_credentials_from_file(self.custom_credentials_path)
86+
return
87+
88+
# if not, try to load credentials from AWS CLI
89+
aws_credentials_path = "~/.aws/credentials" #add read me about credentials path in windows: https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html
90+
if os.path_exists(aws_credentials_path):
91+
self.access_key_id, self.secret_access_key, self.session_token = self._extract_credentials_from_file(aws_credentials_path)
92+
return
93+
94+
def _extract_credentials_from_file(self, file_path):
95+
try:
96+
with open(file_path, 'r') as file:
97+
access_key_id, secret_access_key, session_token = None, None, None
98+
lines = file.readlines()
99+
for line in lines:
100+
if line.startswith("aws_access_key_id ="):
101+
access_key_id = line.split("=")[1].strip()
102+
elif line.startswith("aws_secret_access_key ="):
103+
secret_access_key = line.split("=")[1].strip()
104+
elif line.startswith("session_token ="):
105+
session_token = line.split("=")[1].strip()
106+
return access_key_id, secret_access_key, session_token
107+
except Exception as e:
108+
print(f"Failed loading credentials from {file_path}: {e}")
109+
return

jupyter_drives/managers/s3.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,28 @@ async def mount_drive(self, drive_name):
8585
S3ContentsManager
8686
'''
8787
try :
88-
s3_contents_manager = S3ContentsManager(
89-
access_key = self._config.access_key_id,
90-
secret_access_key = self._config.secret_access_key,
91-
endpoint_url = self._config.api_base_url,
92-
bucket = drive_name
93-
)
94-
9588
# checking if the drive wasn't mounted already
9689
if self.s3_content_managers[drive_name] is None:
90+
91+
# dealing with long-term credentials (access key, secret key)
92+
if self._config.session_token is None:
93+
s3_contents_manager = S3ContentsManager(
94+
access_key = self._config.access_key_id,
95+
secret_access_key = self._config.secret_access_key,
96+
endpoint_url = self._config.api_base_url,
97+
bucket = drive_name
98+
)
99+
100+
# dealing with short-term credentials (access key, secret key, session token)
101+
else:
102+
s3_contents_manager = S3ContentsManager(
103+
access_key = self._config.access_key_id,
104+
secret_access_key = self._config.secret_access_key,
105+
session_token = self._config.session_token,
106+
endpoint_url = self._config.api_base_url,
107+
bucket = drive_name
108+
)
109+
97110
self.s3_content_managers[drive_name] = s3_contents_manager
98111

99112
response = {

0 commit comments

Comments
 (0)