Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S3: Use key_id and key_secret directly #4224

Merged
merged 14 commits into from
Jul 19, 2020
2 changes: 2 additions & 0 deletions dvc/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ class RelPath(str):
"profile": str,
"credentialpath": str,
"endpointurl": str,
"access_key_id": str,
"secret_access_key": str,
Optional("listobjects", default=False): Bool,
Optional("use_ssl", default=True): Bool,
"sse": str,
Expand Down
14 changes: 11 additions & 3 deletions dvc/tree/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ def __init__(self, repo, config):

self._append_aws_grants_to_extra_args(config)

self.access_key_id = config.get("access_key_id")
self.secret_access_key = config.get("secret_access_key")
Comment on lines +57 to +58
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we assign it instead of using self.config directly later?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following the pattern in other remotes (OSS, GDrive etc).


shared_creds = config.get("credentialpath")
if shared_creds:
os.environ.setdefault("AWS_SHARED_CREDENTIALS_FILE", shared_creds)
Expand All @@ -63,9 +66,14 @@ def __init__(self, repo, config):
def s3(self):
import boto3

session = boto3.session.Session(
profile_name=self.profile, region_name=self.region
)
session_opts = dict(profile_name=self.profile, region_name=self.region)

if self.access_key_id:
session_opts["aws_access_key_id"] = self.access_key_id
if self.secret_access_key:
session_opts["aws_secret_access_key"] = self.secret_access_key

session = boto3.session.Session(**session_opts)

return session.client(
"s3", endpoint_url=self.endpoint_url, use_ssl=self.use_ssl
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/remote/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
bucket_name = "bucket-name"
prefix = "some/prefix"
url = f"s3://{bucket_name}/{prefix}"
key_id = "key-id"
key_secret = "key-secret"


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -57,3 +59,12 @@ def test_grants_mutually_exclusive_acl_error(dvc, grants):
def test_sse_kms_key_id(dvc):
tree = S3RemoteTree(dvc, {"url": url, "sse_kms_key_id": "key"})
assert tree.extra_args["SSEKMSKeyId"] == "key"


def test_key_id_and_secret(dvc):
tree = S3RemoteTree(
dvc,
{"url": url, "access_key_id": key_id, "secret_access_key": key_secret},
)
assert tree.access_key_id == key_id
assert tree.secret_access_key == key_secret