danielcweeks commented on a change in pull request #277: Moving/Renaming hadoop module to filesystem URL: https://github.com/apache/incubator-iceberg/pull/277#discussion_r303155919
########## File path: python/iceberg/core/filesystem/s3_filesystem_wrapper.py ########## @@ -0,0 +1,283 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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 datetime +import io +import logging +import time +from urllib.parse import urlparse + +import boto3 +import requests +from retrying import retry +from s3fs import S3FileSystem + +from .file_status import FileStatus +from .file_system import FileSystem + +_logger = logging.getLogger(__name__) + + +S3_CLIENT = dict() +BOTO_STS_CLIENT = boto3.client('sts') + +STS_CACHED_RESPONSE = {} +REFRESH_THRESHOLD = 300 + + +@retry(wait_incrementing_start=100, wait_exponential_multiplier=4, + wait_exponential_max=5000, stop_max_delay=600000, stop_max_attempt_number=3) +def get_s3(role_arn="default", obj="resource"): + if role_arn not in S3_CLIENT: + S3_CLIENT[role_arn] = dict() + + if role_arn == "default": + S3_CLIENT["default"]["resource"] = boto3.resource('s3') + S3_CLIENT["default"]["client"] = boto3.client('s3') + else: + key, secret, token = get_sts_session_keys(role_arn) + sess = boto3.Session(aws_access_key_id=key, + aws_secret_access_key=secret, + aws_session_token=token) + S3_CLIENT[role_arn]["resource"] = sess.resource("s3") + S3_CLIENT[role_arn]["client"] = sess.client("s3") + + return S3_CLIENT.get(role_arn, dict()).get(obj) + + +def get_sts_session_keys(role_arn): + if role_arn is None: + return None, None, None + + if STS_CACHED_RESPONSE.get(role_arn) is None \ + or should_refresh_creds(role_arn): + STS_CACHED_RESPONSE[role_arn] = BOTO_STS_CLIENT.assume_role( + RoleArn=role_arn, + RoleSessionName='iceberg_python_client_{}'.format(int(time.time()))) + + return (STS_CACHED_RESPONSE[role_arn]["Credentials"]["AccessKeyId"], + STS_CACHED_RESPONSE[role_arn]["Credentials"]["SecretAccessKey"], + STS_CACHED_RESPONSE[role_arn]["Credentials"]["SessionToken"]) + + +def should_refresh_creds(role_arn): Review comment: I believe boto3 has a `RefreshableCredentials` that might work for this to auto refresh (though I'm not clear on the behavior around it). ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
