Skip to content

Commit bad87fa

Browse files
authored
Use boto3 to get credentials.
1 parent 2136412 commit bad87fa

File tree

1 file changed

+11
-55
lines changed

1 file changed

+11
-55
lines changed

jupyter_drives/base.py

Lines changed: 11 additions & 55 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,49 +75,10 @@ def __init__(self, **kwargs):
8075
super().__init__(**kwargs)
8176
self._load_credentials()
8277

83-
def _load_credentials(self):
84-
# check if credentials were already set in jupyter_notebook_config.py
85-
if self.access_key_id is not None and self.secret_access_key is not None:
86-
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
94-
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)
99-
return
100-
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"]
109-
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
78+
def _load_credentials(self):
79+
s = boto3.Session()
80+
c = s.get_credentials()
81+
self.access_key_id = c.access_key
82+
self.secret_access_key = c.secret_key
83+
self.region_name = s.region_name
84+
self.session_token = c.token

0 commit comments

Comments
 (0)