Skip to content

Commit 915ef3a

Browse files
authored
Merge pull request #39 from ellisonbg/s3-credentials
Use boto3 to get credentials.
2 parents 3a56547 + f4ffef3 commit 915ef3a

File tree

1 file changed

+27
-42
lines changed

1 file changed

+27
-42
lines changed

jupyter_drives/base.py

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import entrypoints
44
from traitlets import Enum, Unicode, default
55
from traitlets.config import Configurable
6+
import boto3
67

78
# Supported third-party services
89
MANAGERS = {}
@@ -42,8 +43,9 @@ class DrivesConfig(Configurable):
4243
)
4344

4445
region_name = Unicode(
45-
"eu-north-1",
46-
config = True,
46+
None,
47+
config = True,
48+
allow_none=True,
4749
help = "Region name.",
4850
)
4951

@@ -52,13 +54,6 @@ class DrivesConfig(Configurable):
5254
help="Base URL of the provider service REST API.",
5355
)
5456

55-
custom_credentials_path = Unicode(
56-
None,
57-
config = True,
58-
allow_none = True,
59-
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."
60-
)
61-
6257
@default("api_base_url")
6358
def set_default_api_base_url(self):
6459
# for AWS S3 drives
@@ -80,25 +75,27 @@ def __init__(self, **kwargs):
8075
super().__init__(**kwargs)
8176
self._load_credentials()
8277

83-
def _load_credentials(self):
78+
def _load_credentials(self):
8479
# check if credentials were already set in jupyter_notebook_config.py
8580
if self.access_key_id is not None and self.secret_access_key is not None:
8681
return
87-
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"]
91-
if self.custom_credentials_path is not None:
92-
self.provider, self.access_key_id, self.secret_access_key, self.session_token = self._extract_credentials_from_file(self.custom_credentials_path)
93-
return
9482

95-
# if not, try to load credentials from AWS CLI
96-
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
97-
if os.path.exists(aws_credentials_path):
98-
self.access_key_id, self.secret_access_key, self.session_token = self._extract_credentials_from_file(aws_credentials_path)
83+
# automatically extract credentials for S3 drives
84+
try:
85+
s = boto3.Session()
86+
c = s.get_credentials()
87+
if c is not None:
88+
self.access_key_id = c.access_key
89+
self.secret_access_key = c.secret_key
90+
self.region_name = s.region_name
91+
self.session_token = c.token
92+
self.provider = 's3'
9993
return
100-
101-
# as a last resort, use environment variables
94+
except:
95+
# S3 credentials couldn't automatically be extracted through boto
96+
pass
97+
98+
# use environment variables
10299
if "JP_DRIVES_ACCESS_KEY_ID" in os.environ and "JP_DRIVES_SECRET_ACCESS_KEY" in os.environ:
103100
self.access_key_id = os.environ["JP_DRIVES_ACCESS_KEY_ID"]
104101
self.secret_access_key = os.environ["JP_DRIVES_SECRET_ACCESS_KEY"]
@@ -107,22 +104,10 @@ def _load_credentials(self):
107104
if "JP_DRIVES_PROVIDER" in os.environ:
108105
self.provider = os.environ["JP_DRIVES_PROVIDER"]
109106
return
110-
111-
def _extract_credentials_from_file(self, file_path):
112-
try:
113-
with open(file_path, 'r') as file:
114-
provider, access_key_id, secret_access_key, session_token = None, None, None, None
115-
lines = file.readlines()
116-
for line in lines:
117-
if line.startswith("drives_provider ="):
118-
provider = line.split("=")[1].strip()
119-
elif line.startswith("drives_access_key_id ="):
120-
access_key_id = line.split("=")[1].strip()
121-
elif line.startswith("drives_secret_access_key ="):
122-
secret_access_key = line.split("=")[1].strip()
123-
elif line.startswith("drives_session_token ="):
124-
session_token = line.split("=")[1].strip()
125-
return provider, access_key_id, secret_access_key, session_token
126-
except Exception as e:
127-
print(f"Failed loading credentials from {file_path}: {e}")
128-
return
107+
108+
s = boto3.Session()
109+
c = s.get_credentials()
110+
self.access_key_id = c.access_key
111+
self.secret_access_key = c.secret_key
112+
self.region_name = s.region_name
113+
self.session_token = c.token

0 commit comments

Comments
 (0)