2022-03-11 16:08:14 +00:00
|
|
|
#!/usr/bin/python3
|
2020-11-30 10:33:05 +00:00
|
|
|
|
2022-03-11 16:08:14 +00:00
|
|
|
import json
|
2020-11-30 10:33:05 +00:00
|
|
|
import os
|
2020-12-11 11:35:02 +00:00
|
|
|
import uuid
|
2022-03-11 16:08:14 +00:00
|
|
|
|
2021-11-03 12:48:31 +00:00
|
|
|
import boto3
|
2022-03-11 16:08:14 +00:00
|
|
|
import botocore
|
|
|
|
from cli_helpers import _run_with_passwd
|
2020-11-30 10:33:05 +00:00
|
|
|
|
2022-03-11 16:08:14 +00:00
|
|
|
from common import GATE_PUB_KEY, NEOFS_ENDPOINT, S3_GATE
|
|
|
|
import urllib3
|
2020-11-30 10:33:05 +00:00
|
|
|
from robot.api.deco import keyword
|
|
|
|
from robot.api import logger
|
|
|
|
|
2022-03-11 16:08:14 +00:00
|
|
|
##########################################################
|
|
|
|
# Disabling warnings on self-signed certificate which the
|
|
|
|
# boto library produces on requests to S3-gate in dev-env.
|
|
|
|
urllib3.disable_warnings()
|
|
|
|
##########################################################
|
2020-11-30 10:33:05 +00:00
|
|
|
|
2021-04-20 09:51:53 +00:00
|
|
|
ROBOT_AUTO_KEYWORDS = False
|
2020-12-11 11:35:02 +00:00
|
|
|
|
2021-08-16 09:28:43 +00:00
|
|
|
NEOFS_EXEC = os.getenv('NEOFS_EXEC', 'neofs-authmate')
|
2020-12-11 11:35:02 +00:00
|
|
|
|
|
|
|
@keyword('Init S3 Credentials')
|
2021-08-16 09:28:43 +00:00
|
|
|
def init_s3_credentials(wallet):
|
2020-12-11 11:35:02 +00:00
|
|
|
bucket = str(uuid.uuid4())
|
2022-03-11 16:08:14 +00:00
|
|
|
s3_bearer_rules = "robot/resources/files/s3_bearer_rules.json"
|
|
|
|
cmd = (
|
2021-08-16 09:28:43 +00:00
|
|
|
f'{NEOFS_EXEC} --debug --with-log issue-secret --wallet {wallet} '
|
|
|
|
f'--gate-public-key={GATE_PUB_KEY} --peer {NEOFS_ENDPOINT} '
|
2022-03-11 16:08:14 +00:00
|
|
|
f'--container-friendly-name {bucket} '
|
|
|
|
f'--bearer-rules {s3_bearer_rules}'
|
2021-01-17 11:55:10 +00:00
|
|
|
)
|
2022-03-11 16:08:14 +00:00
|
|
|
logger.info(f"Executing command: {cmd}")
|
2021-08-16 09:28:43 +00:00
|
|
|
|
2020-12-11 11:35:02 +00:00
|
|
|
try:
|
2022-03-11 16:08:14 +00:00
|
|
|
output = _run_with_passwd(cmd)
|
2021-08-16 09:28:43 +00:00
|
|
|
logger.info(f"Command completed with output: {output}")
|
2022-03-11 16:08:14 +00:00
|
|
|
# first five string are log output, cutting them off and parse
|
|
|
|
# the rest of the output as JSON
|
|
|
|
output = '\n'.join(output.split('\n')[5:])
|
|
|
|
output_dict = json.loads(output)
|
2020-12-11 11:35:02 +00:00
|
|
|
|
2022-03-11 16:08:14 +00:00
|
|
|
return (output_dict['container_id'],
|
|
|
|
bucket,
|
|
|
|
output_dict['access_key_id'],
|
|
|
|
output_dict['secret_access_key'],
|
|
|
|
output_dict['owner_private_key'])
|
2020-12-11 11:35:02 +00:00
|
|
|
|
2022-03-11 16:08:14 +00:00
|
|
|
except Exception as exc:
|
|
|
|
raise RuntimeError("failed to init s3 credentials") from exc
|
2020-12-11 11:35:02 +00:00
|
|
|
|
2021-10-01 09:32:07 +00:00
|
|
|
|
2020-12-11 11:35:02 +00:00
|
|
|
@keyword('Config S3 client')
|
|
|
|
def config_s3_client(access_key_id, secret_access_key):
|
2021-10-01 09:32:07 +00:00
|
|
|
try:
|
2022-03-11 16:08:14 +00:00
|
|
|
|
2021-10-01 09:32:07 +00:00
|
|
|
session = boto3.session.Session()
|
2020-12-11 11:35:02 +00:00
|
|
|
|
2021-10-01 09:32:07 +00:00
|
|
|
s3_client = session.client(
|
|
|
|
service_name='s3',
|
|
|
|
aws_access_key_id=access_key_id,
|
|
|
|
aws_secret_access_key=secret_access_key,
|
|
|
|
endpoint_url=S3_GATE, verify=False
|
|
|
|
)
|
|
|
|
|
|
|
|
return s3_client
|
|
|
|
|
|
|
|
except botocore.exceptions.ClientError as err:
|
|
|
|
raise Exception(f"Error Message: {err.response['Error']['Message']}\n"
|
|
|
|
f"Http status code: {err.response['ResponseMetadata']['HTTPStatusCode']}") from err
|
2020-12-11 11:35:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
@keyword('List objects S3 v2')
|
|
|
|
def list_objects_s3_v2(s3_client, bucket):
|
2021-10-01 09:32:07 +00:00
|
|
|
try:
|
|
|
|
response = s3_client.list_objects_v2(Bucket=bucket)
|
2022-03-11 16:08:14 +00:00
|
|
|
logger.info(f"S3 v2 List objects result: {response['Contents']}")
|
2021-10-01 09:32:07 +00:00
|
|
|
obj_list = []
|
|
|
|
for obj in response['Contents']:
|
|
|
|
obj_list.append(obj['Key'])
|
2022-03-11 16:08:14 +00:00
|
|
|
logger.info(f"Found s3 objects: {obj_list}")
|
2021-10-01 09:32:07 +00:00
|
|
|
return obj_list
|
|
|
|
|
|
|
|
except botocore.exceptions.ClientError as err:
|
|
|
|
raise Exception(f"Error Message: {err.response['Error']['Message']}\n"
|
|
|
|
f"Http status code: {err.response['ResponseMetadata']['HTTPStatusCode']}") from err
|
2020-12-11 11:35:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
@keyword('List objects S3')
|
|
|
|
def list_objects_s3(s3_client, bucket):
|
2021-10-01 09:32:07 +00:00
|
|
|
try:
|
|
|
|
response = s3_client.list_objects(Bucket=bucket)
|
2022-03-11 16:08:14 +00:00
|
|
|
logger.info(f"S3 List objects result: {response['Contents']}")
|
2021-10-01 09:32:07 +00:00
|
|
|
obj_list = []
|
|
|
|
for obj in response['Contents']:
|
|
|
|
obj_list.append(obj['Key'])
|
2022-03-11 16:08:14 +00:00
|
|
|
logger.info(f"Found s3 objects: {obj_list}")
|
2021-10-01 09:32:07 +00:00
|
|
|
return obj_list
|
|
|
|
|
|
|
|
except botocore.exceptions.ClientError as err:
|
|
|
|
raise Exception(f"Error Message: {err.response['Error']['Message']}\n"
|
|
|
|
f"Http status code: {err.response['ResponseMetadata']['HTTPStatusCode']}") from err
|
|
|
|
|
2020-12-11 11:35:02 +00:00
|
|
|
|
2021-08-16 09:28:43 +00:00
|
|
|
@keyword('Create bucket S3')
|
|
|
|
def create_bucket_s3(s3_client):
|
|
|
|
bucket_name = str(uuid.uuid4())
|
2021-10-01 09:32:07 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
s3_bucket = s3_client.create_bucket(Bucket=bucket_name)
|
2022-03-11 16:08:14 +00:00
|
|
|
logger.info(f"Created S3 bucket: {s3_bucket}")
|
2021-10-01 09:32:07 +00:00
|
|
|
return bucket_name
|
|
|
|
|
|
|
|
except botocore.exceptions.ClientError as err:
|
|
|
|
raise Exception(f"Error Message: {err.response['Error']['Message']}\n"
|
2022-03-11 16:08:14 +00:00
|
|
|
f"Http status code: {err.response['ResponseMetadata']['HTTPStatusCode']}") from err
|
2021-10-01 09:32:07 +00:00
|
|
|
|
2020-12-11 11:35:02 +00:00
|
|
|
|
|
|
|
@keyword('List buckets S3')
|
|
|
|
def list_buckets_s3(s3_client):
|
|
|
|
found_buckets = []
|
2021-10-01 09:32:07 +00:00
|
|
|
try:
|
|
|
|
response = s3_client.list_buckets()
|
2022-03-11 16:08:14 +00:00
|
|
|
logger.info(f"S3 List buckets result: {response}")
|
2020-12-11 11:35:02 +00:00
|
|
|
|
2021-10-01 09:32:07 +00:00
|
|
|
for bucket in response['Buckets']:
|
|
|
|
found_buckets.append(bucket['Name'])
|
|
|
|
|
|
|
|
return found_buckets
|
|
|
|
|
|
|
|
except botocore.exceptions.ClientError as err:
|
|
|
|
raise Exception(f"Error Message: {err.response['Error']['Message']}\n"
|
2022-03-11 16:08:14 +00:00
|
|
|
f"Http status code: {err.response['ResponseMetadata']['HTTPStatusCode']}") from err
|
2020-12-11 11:35:02 +00:00
|
|
|
|
2021-01-17 11:55:10 +00:00
|
|
|
|
2021-09-02 12:17:55 +00:00
|
|
|
@keyword('Delete bucket S3')
|
|
|
|
def delete_bucket_s3(s3_client, bucket):
|
2021-10-01 09:32:07 +00:00
|
|
|
try:
|
|
|
|
response = s3_client.delete_bucket(Bucket=bucket)
|
|
|
|
logger.info(f"S3 Delete bucket result: {response}")
|
2022-03-11 16:08:14 +00:00
|
|
|
|
2021-10-01 09:32:07 +00:00
|
|
|
return response
|
|
|
|
|
|
|
|
except botocore.exceptions.ClientError as err:
|
|
|
|
raise Exception(f"Error Message: {err.response['Error']['Message']}\n"
|
|
|
|
f"Http status code: {err.response['ResponseMetadata']['HTTPStatusCode']}") from err
|
2021-09-02 12:17:55 +00:00
|
|
|
|
|
|
|
|
2022-03-11 16:08:14 +00:00
|
|
|
@keyword('Head bucket S3')
|
|
|
|
def head_bucket(s3_client, bucket):
|
2021-10-01 09:32:07 +00:00
|
|
|
try:
|
|
|
|
response = s3_client.head_bucket(Bucket=bucket)
|
2022-03-11 16:08:14 +00:00
|
|
|
logger.info(f"S3 Head bucket result: {response}")
|
2021-10-01 09:32:07 +00:00
|
|
|
return response
|
|
|
|
|
|
|
|
except botocore.exceptions.ClientError as err:
|
|
|
|
raise Exception(f"Error Message: {err.response['Error']['Message']}\n"
|
|
|
|
f"Http status code: {err.response['ResponseMetadata']['HTTPStatusCode']}") from err
|
2021-09-02 12:17:55 +00:00
|
|
|
|
|
|
|
|
2020-12-11 11:35:02 +00:00
|
|
|
@keyword('Put object S3')
|
|
|
|
def put_object_s3(s3_client, bucket, filepath):
|
|
|
|
filename = os.path.basename(filepath)
|
|
|
|
|
2022-03-11 16:08:14 +00:00
|
|
|
with open(filepath, "rb") as put_file:
|
|
|
|
file_content = put_file.read()
|
2020-12-11 11:35:02 +00:00
|
|
|
|
2021-10-01 09:32:07 +00:00
|
|
|
try:
|
2022-03-11 16:08:14 +00:00
|
|
|
response = s3_client.put_object(Body=file_content, Bucket=bucket, Key=filename)
|
|
|
|
logger.info(f"S3 Put object result: {response}")
|
2021-10-01 09:32:07 +00:00
|
|
|
except botocore.exceptions.ClientError as err:
|
|
|
|
raise Exception(f"Error Message: {err.response['Error']['Message']}\n"
|
|
|
|
f"Http status code: {err.response['ResponseMetadata']['HTTPStatusCode']}") from err
|
2020-12-11 11:35:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
@keyword('Head object S3')
|
|
|
|
def head_object_s3(s3_client, bucket, object_key):
|
2021-10-01 09:32:07 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
response = s3_client.head_object(Bucket=bucket, Key=object_key)
|
2022-03-11 16:08:14 +00:00
|
|
|
logger.info(f"S3 Head object result: {response}")
|
2021-10-01 09:32:07 +00:00
|
|
|
return response
|
|
|
|
|
|
|
|
except botocore.exceptions.ClientError as err:
|
|
|
|
raise Exception(f"Error Message: {err.response['Error']['Message']}\n"
|
2022-03-11 16:08:14 +00:00
|
|
|
f"Http status code: {err.response['ResponseMetadata']['HTTPStatusCode']}") from err
|
2020-12-11 11:35:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
@keyword('Delete object S3')
|
|
|
|
def delete_object_s3(s3_client, bucket, object_key):
|
2021-10-01 09:32:07 +00:00
|
|
|
try:
|
|
|
|
response = s3_client.delete_object(Bucket=bucket, Key=object_key)
|
2022-03-11 16:08:14 +00:00
|
|
|
logger.info(f"S3 Put object result: {response}")
|
2021-10-01 09:32:07 +00:00
|
|
|
return response
|
2020-12-11 11:35:02 +00:00
|
|
|
|
2021-10-01 09:32:07 +00:00
|
|
|
except botocore.exceptions.ClientError as err:
|
|
|
|
raise Exception(f"Error Message: {err.response['Error']['Message']}\n"
|
|
|
|
f"Http status code: {err.response['ResponseMetadata']['HTTPStatusCode']}") from err
|
2020-12-11 11:35:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
@keyword('Copy object S3')
|
2022-03-11 16:08:14 +00:00
|
|
|
def copy_object_s3(s3_client, bucket, object_key):
|
|
|
|
filename = f"{os.getcwd()}/{uuid.uuid4()}"
|
2021-09-02 12:17:55 +00:00
|
|
|
try:
|
2022-03-11 16:08:14 +00:00
|
|
|
response = s3_client.copy_object(Bucket=bucket,
|
|
|
|
CopySource=f"{bucket}/{object_key}",
|
|
|
|
Key=filename)
|
|
|
|
logger.info(f"S3 Copy object result: {response}")
|
|
|
|
return filename
|
2021-10-01 09:32:07 +00:00
|
|
|
|
|
|
|
except botocore.exceptions.ClientError as err:
|
|
|
|
raise Exception(f"Error Message: {err.response['Error']['Message']}\n"
|
|
|
|
f"Http status code: {err.response['ResponseMetadata']['HTTPStatusCode']}") from err
|
2020-12-11 11:35:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
@keyword('Get object S3')
|
2022-03-11 16:08:14 +00:00
|
|
|
def get_object_s3(s3_client, bucket, object_key):
|
|
|
|
filename = f"{os.getcwd()}/{uuid.uuid4()}"
|
2021-10-01 09:32:07 +00:00
|
|
|
try:
|
|
|
|
response = s3_client.get_object(Bucket=bucket, Key=object_key)
|
2020-12-11 11:35:02 +00:00
|
|
|
|
2022-03-11 16:08:14 +00:00
|
|
|
with open(f"{filename}", 'wb') as get_file:
|
2020-12-11 11:35:02 +00:00
|
|
|
chunk = response['Body'].read(1024)
|
2021-10-01 09:32:07 +00:00
|
|
|
while chunk:
|
2022-03-11 16:08:14 +00:00
|
|
|
get_file.write(chunk)
|
2021-10-01 09:32:07 +00:00
|
|
|
chunk = response['Body'].read(1024)
|
|
|
|
|
2022-03-11 16:08:14 +00:00
|
|
|
return filename
|
2020-12-11 11:35:02 +00:00
|
|
|
|
2021-10-01 09:32:07 +00:00
|
|
|
except botocore.exceptions.ClientError as err:
|
|
|
|
raise Exception(f"Error Message: {err.response['Error']['Message']}\n"
|
|
|
|
f"Http status code: {err.response['ResponseMetadata']['HTTPStatusCode']}") from err
|