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

refactor minio.credentials #901

Merged
merged 4 commits into from
May 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
check:
@which pylint >/dev/null || pip install --user --upgrade pylint
@if python --version | grep -qi 'python 3'; then pylint --reports=no --score=no --disable=R0401 minio/*py; fi
@if python --version | grep -qi 'python 3'; then pylint --reports=no --score=no minio/select tests/functional; fi
@if python --version | grep -qi 'python 3'; then pylint --reports=no --score=no minio/credentials minio/select tests/functional; fi

@which isort >/dev/null || pip install --user --upgrade isort
@isort --diff --recursive .
Expand Down
39 changes: 16 additions & 23 deletions examples/assume_role.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
# MinIO Python Library for Amazon S3 Compatible Cloud Storage, (C) 2020 MinIO, Inc.
# MinIO Python Library for Amazon S3 Compatible Cloud Storage,
# (C) 2020 MinIO, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,13 +33,15 @@
# dummy values, please replace them with original values.
# - To use MinIO with AWS, pass the function - `get_assume_role_creds`
# as a lambda to the AssumeRoleProvider

from minio import Minio
from minio.credentials import AssumeRoleProvider, Credentials

client = Minio('s3.amazonaws.com',
access_key='YOUR-ACCESSKEYID',
secret_key='YOUR-SECRETKEY'
)
user_client = Minio(
's3.amazonaws.com',
access_key='YOUR-ACCESSKEYID',
secret_key='YOUR-SECRETKEY',
)

_RESTRICTED_UPLOAD_POLICY = """{
"Version": "2012-10-17",
Expand All @@ -54,24 +57,14 @@
"Sid": "Upload-access-to-specific-bucket-only"
}
]
}
}
"""

credentials_provider = AssumeRoleProvider(
get_assume_role_creds=lambda: client.get_assume_role_creds(policy=_RESTRICTED_UPLOAD_POLICY))
temp_creds = Credentials(provider=credentials_provider)

# User can access the credentials for e.g. serialization
print("Retrieved temporary credentials:")
print(temp_creds.get().access_key)
print(temp_creds.get().secret_key)

# Initialize Minio client with the temporary credentials
restricted_client = Minio('s3.amazonaws.com', credentials=temp_creds)

# Get a full object.
provider = AssumeRoleProvider(
lambda: user_client.get_assume_role_creds(_RESTRICTED_UPLOAD_POLICY),
)

data = restricted_client.get_object('my-bucket', 'my-object')
with open('/tmp/testfile', 'wb') as file_data:
for d in data.stream(32*1024):
file_data.write(d)
client = Minio(
's3.amazonaws.com',
credentials=Credentials(provider),
)
6 changes: 3 additions & 3 deletions examples/aws_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
# the 'endpoint' paramater to the IamEc2MetaData credentials object

from minio import Minio
from minio.credentials import Credentials, IamEc2MetaData
from minio.credentials import Credentials, IAMProvider

# Initialize Minio with IamEc2MetaData default credentials object
client = Minio('s3.amazonaws.com',
credentials=Credentials(
provider=IamEc2MetaData()
provider=IAMProvider()
))

# Initialize Minio with IamEc2MetaData custom
client = Minio('s3.amazonaws.com',
credentials=Credentials(
provider=IamEc2MetaData(endpoint='custom.endpoint')
provider=IAMProvider(endpoint='custom.endpoint')
))
5 changes: 2 additions & 3 deletions examples/chain_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@
# 'providers' list

from minio import Minio
from minio.credentials import (Chain, Credentials, EnvAWS, EnvMinio,
IamEc2MetaData)
from minio.credentials import Chain, Credentials, EnvAWS, EnvMinio, IAMProvider

client = Minio('s3.amazonaws.com',
credentials=Credentials(
provider=Chain(
providers=[
IamEc2MetaData(),
IAMProvider(),
EnvAWS(),
EnvMinio()
]
Expand Down
2 changes: 1 addition & 1 deletion minio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
__author__ = 'MinIO, Inc.'
__version__ = '6.0.0'
__license__ = 'Apache 2.0'
__copyright__ = 'Copyright 2015, 2016, 2017, 2018, 2019 MinIO, Inc.'
__copyright__ = 'Copyright 2015, 2016, 2017, 2018, 2019, 2020 MinIO, Inc.'

from .api import Minio # pylint: disable=unused-import
from .copy_conditions import CopyConditions # pylint: disable=unused-import
Expand Down
4 changes: 1 addition & 3 deletions minio/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@
from . import __title__, __version__
from .compat import range # pylint: disable=redefined-builtin
from .compat import basestring, quote, urlencode, urlsplit
from .credentials import (Chain, Credentials, EnvAWS, EnvMinio, IamEc2MetaData,
Static)
from .credentials import Chain, Credentials, EnvAWS, EnvMinio, Static
from .definitions import Object, UploadPart
from .error import (AccessDenied, InvalidArgumentError, InvalidSizeError,
InvalidXMLError, NoSuchBucket, ResponseError)
Expand Down Expand Up @@ -171,7 +170,6 @@ def __init__(self, endpoint, access_key=None,
Static(access_key, secret_key, session_token),
EnvAWS(),
EnvMinio(),
IamEc2MetaData(),
Copy link
Member

Choose a reason for hiding this comment

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

Well, this is a breaking change, we should not forget about mentioning it when we release minio-py

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. I think we are covering in #902

]
)
)
Expand Down
35 changes: 28 additions & 7 deletions minio/credentials/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
from .assume_role import AssumeRoleProvider
from .aws_iam import IamEc2MetaData
from .chain import Chain
from .credentials import Credentials, Value
from .env_aws import EnvAWS
from .env_minio import EnvMinio
from .static import Static
# -*- coding: utf-8 -*-
# MinIO Python Library for Amazon S3 Compatible Cloud Storage,
# (C) 2020 MinIO, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Credential module."""

from .credentials import Credentials # pylint: disable=unused-import
from .credentials import Value # pylint: disable=unused-import
from .providers import AssumeRoleProvider # pylint: disable=unused-import
from .providers import Chain # pylint: disable=unused-import
from .providers import EnvAWS # pylint: disable=unused-import
from .providers import EnvMinio # pylint: disable=unused-import
from .providers import FileAWSCredentials # pylint: disable=unused-import
from .providers import FileMinioClient # pylint: disable=unused-import
from .providers import IAMProvider # pylint: disable=unused-import
from .providers import Static # pylint: disable=unused-import
43 changes: 0 additions & 43 deletions minio/credentials/assume_role.py

This file was deleted.

81 changes: 0 additions & 81 deletions minio/credentials/aws_iam.py

This file was deleted.

36 changes: 0 additions & 36 deletions minio/credentials/chain.py

This file was deleted.

Loading