-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement credentials object (similar to minio-go) to enable AWS IAM (#…
…817)
- Loading branch information
1 parent
d1659e5
commit c5d66fc
Showing
29 changed files
with
1,160 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# -*- 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. | ||
# | ||
# IamEc2MetaData will call AWS metadata service to retrieve credentials | ||
# | ||
# The default AWS metadata service can be found at: | ||
# -> 169.254.169.254/latest/meta-data/iam/security-credentials | ||
# | ||
# If you wish to retieve credentials from a different place you can provide | ||
# the 'endpoint' paramater to the IamEc2MetaData credentials object | ||
|
||
from minio.credentials import IamEc2MetaData | ||
|
||
# Initialize Minio with IamEc2MetaData default credentials object | ||
client = Minio('s3.amazonaws.com', | ||
credentials=IamEc2MetaData()) | ||
|
||
# Initialize Minio with IamEc2MetaData custom | ||
|
||
client = Minio('s3.amazonaws.com', | ||
credentials=IamEc2MetaData(endpoint='custom.endpoint')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# -*- 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. | ||
|
||
# A Chain credentials provider, provides a way of chaining multiple providers together | ||
# and will pick the first available using priority order of the 'providers' list | ||
|
||
from minio.credentials import Chain, EnvAWS, EnvMinio, IamEc2MetaData | ||
|
||
client = Minio('s3.amazonaws.com', | ||
credentials=Chain( | ||
providers=[ | ||
IamEc2MetaData(), | ||
EnvAWS(), | ||
EnvMinio() | ||
] | ||
)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from .static import Static | ||
from .credentials import Credentials | ||
from .chain import Chain | ||
from .aws_iam import IamEc2MetaData | ||
from .env_aws import EnvAWS | ||
from .env_minio import EnvMinio |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# -*- 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. | ||
|
||
import os, json, urllib3, datetime | ||
from .credentials import Provider, Value, Expiry | ||
from minio.error import ResponseError | ||
|
||
class IamEc2MetaData(Provider): | ||
|
||
iam_security_creds_path = '/latest/meta-data/iam/security-credentials' | ||
|
||
default_expiry_window = datetime.timedelta(minutes=5) | ||
|
||
def __init__(self, endpoint=None): | ||
super(Provider, self).__init__() | ||
if endpoint == "" or endpoint is None: | ||
endpoint = "http://169.254.169.254" | ||
self._endpoint = endpoint | ||
self._expiry = Expiry() | ||
self._http_client = urllib3.PoolManager( | ||
retries=urllib3.Retry( | ||
total=5, | ||
backoff_factor=0.2, | ||
status_forcelist=[500, 502, 503, 504] | ||
) | ||
) | ||
|
||
def request_cred_list(self): | ||
url = self._endpoint + self.iam_security_creds_path | ||
try: | ||
res = self._http_client.urlopen('GET', url) | ||
if res['status'] != 200: | ||
return [] | ||
except: | ||
return [] | ||
creds = res['data'].split('\n') | ||
return creds | ||
|
||
def request_cred(self, creds_name): | ||
url = self._endpoint + self.iam_security_creds_path + "/" + creds_name | ||
res = self._http_client.urlopen('GET', url) | ||
if res['status'] != 200: | ||
raise ResponseError(res, 'GET') | ||
|
||
data = json.loads(res['data']) | ||
if data['Code'] != 'Success': | ||
raise ResponseError(res) | ||
|
||
return data | ||
|
||
def retrieve(self): | ||
creds_list = self.request_cred_list() | ||
if len(creds_list) == 0: | ||
return Value() | ||
|
||
creds_name = creds_list[0] | ||
role_creds = self.request_cred(creds_name) | ||
expiration = datetime.datetime.strptime(role_creds['Expiration'], '%Y-%m-%dT%H:%M:%SZ') | ||
self._expiry.set_expiration(expiration, self.default_expiry_window) | ||
|
||
return Value( | ||
access_key=role_creds['AccessKeyId'], | ||
secret_key=role_creds['SecretAccessKey'], | ||
session_token=role_creds['Token'] | ||
) | ||
|
||
def is_expired(self): | ||
return self._expiry.is_expired() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# -*- 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. | ||
|
||
from .credentials import Provider, Value | ||
|
||
class Chain(Provider): | ||
def __init__(self, providers): | ||
super(Provider, self).__init__() | ||
self._providers = providers | ||
self._current = None | ||
|
||
def retrieve(self): | ||
for provider in self._providers: | ||
creds = provider.retrieve() | ||
if ((creds.access_key is None or creds.access_key == "") and | ||
(creds.secret_key is None or creds.secret_key == "")): | ||
continue | ||
self._current = provider | ||
return creds | ||
self._current = None | ||
return Value() | ||
|
||
def is_expired(self): | ||
if self._current == None: | ||
return True | ||
return self._current.is_expired() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"version": "8", | ||
"hosts": { | ||
"play": { | ||
"url": "https://play.minio.io:9000", | ||
"accessKey": "Q3AM3UQ867SPQQA43P2F", | ||
"secretKey": "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG", | ||
"api": "S3v2" | ||
}, | ||
"s3": { | ||
"url": "https://s3.amazonaws.com", | ||
"accessKey": "accessKey", | ||
"secretKey": "secret", | ||
"api": "S3v4" | ||
} | ||
} | ||
} |
Empty file.
Oops, something went wrong.