2022-11-14 12:04:15 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
import allure
|
|
|
|
import pytest
|
2023-10-31 14:51:09 +00:00
|
|
|
from frostfs_testlib.s3 import S3ClientWrapper, VersioningStatus
|
2023-05-15 09:59:33 +00:00
|
|
|
from frostfs_testlib.steps.cli.container import search_container_by_name
|
|
|
|
from frostfs_testlib.steps.s3 import s3_helper
|
|
|
|
from frostfs_testlib.steps.storage_policy import get_simple_object_copies
|
2023-08-02 11:54:03 +00:00
|
|
|
from frostfs_testlib.storage.dataclasses.object_size import ObjectSize
|
2023-05-15 09:59:33 +00:00
|
|
|
from frostfs_testlib.testing.cluster_test_base import ClusterTestBase
|
|
|
|
from frostfs_testlib.testing.test_control import expect_not_raises
|
|
|
|
from frostfs_testlib.utils.file_utils import generate_file
|
2022-11-14 12:04:15 +00:00
|
|
|
|
|
|
|
|
2023-10-31 14:51:09 +00:00
|
|
|
@pytest.mark.sanity
|
2022-11-14 12:04:15 +00:00
|
|
|
@pytest.mark.s3_gate
|
2023-10-31 14:51:09 +00:00
|
|
|
@pytest.mark.parametrize("s3_policy", ["pytest_tests/resources/files/policy.json"], indirect=True)
|
2023-05-15 09:59:33 +00:00
|
|
|
class TestS3GatePolicy(ClusterTestBase):
|
2023-09-08 10:35:34 +00:00
|
|
|
@allure.title("Bucket creation with retention policy applied (s3_client={s3_client})")
|
2023-10-31 14:51:09 +00:00
|
|
|
def test_s3_bucket_location(self, default_wallet: str, s3_client: S3ClientWrapper, simple_object_size: ObjectSize):
|
2023-08-02 11:54:03 +00:00
|
|
|
file_path_1 = generate_file(simple_object_size.value)
|
2023-05-15 09:59:33 +00:00
|
|
|
file_name_1 = s3_helper.object_key_from_file_path(file_path_1)
|
2023-08-02 11:54:03 +00:00
|
|
|
file_path_2 = generate_file(simple_object_size.value)
|
2023-05-15 09:59:33 +00:00
|
|
|
file_name_2 = s3_helper.object_key_from_file_path(file_path_2)
|
2022-11-14 12:04:15 +00:00
|
|
|
|
|
|
|
with allure.step("Create two buckets with different bucket configuration"):
|
2023-05-15 09:59:33 +00:00
|
|
|
bucket_1 = s3_client.create_bucket(location_constraint="complex")
|
|
|
|
s3_helper.set_bucket_versioning(s3_client, bucket_1, VersioningStatus.ENABLED)
|
|
|
|
bucket_2 = s3_client.create_bucket(location_constraint="rep-3")
|
|
|
|
s3_helper.set_bucket_versioning(s3_client, bucket_2, VersioningStatus.ENABLED)
|
|
|
|
list_buckets = s3_client.list_buckets()
|
2022-11-14 12:04:15 +00:00
|
|
|
assert (
|
|
|
|
bucket_1 in list_buckets and bucket_2 in list_buckets
|
|
|
|
), f"Expected two buckets {bucket_1, bucket_2}, got {list_buckets}"
|
|
|
|
|
2023-05-15 09:59:33 +00:00
|
|
|
with allure.step("Check head buckets"):
|
|
|
|
with expect_not_raises():
|
|
|
|
s3_client.head_bucket(bucket_1)
|
|
|
|
s3_client.head_bucket(bucket_2)
|
2022-11-14 12:04:15 +00:00
|
|
|
|
|
|
|
with allure.step("Put objects into buckets"):
|
2023-05-15 09:59:33 +00:00
|
|
|
version_id_1 = s3_client.put_object(bucket_1, file_path_1)
|
|
|
|
version_id_2 = s3_client.put_object(bucket_2, file_path_2)
|
|
|
|
s3_helper.check_objects_in_bucket(s3_client, bucket_1, [file_name_1])
|
|
|
|
s3_helper.check_objects_in_bucket(s3_client, bucket_2, [file_name_2])
|
2022-11-14 12:04:15 +00:00
|
|
|
|
|
|
|
with allure.step("Check bucket location"):
|
2023-05-15 09:59:33 +00:00
|
|
|
bucket_loc_1 = s3_client.get_bucket_location(bucket_1)
|
|
|
|
bucket_loc_2 = s3_client.get_bucket_location(bucket_2)
|
2022-11-14 12:04:15 +00:00
|
|
|
assert bucket_loc_1 == "complex"
|
|
|
|
assert bucket_loc_2 == "rep-3"
|
|
|
|
|
|
|
|
with allure.step("Check object policy"):
|
2022-12-05 22:31:45 +00:00
|
|
|
cid_1 = search_container_by_name(
|
2023-05-15 09:59:33 +00:00
|
|
|
default_wallet,
|
|
|
|
bucket_1,
|
|
|
|
shell=self.shell,
|
|
|
|
endpoint=self.cluster.default_rpc_endpoint,
|
2022-12-05 22:31:45 +00:00
|
|
|
)
|
2022-11-14 12:04:15 +00:00
|
|
|
copies_1 = get_simple_object_copies(
|
2023-05-15 09:59:33 +00:00
|
|
|
wallet=default_wallet,
|
2022-12-05 22:31:45 +00:00
|
|
|
cid=cid_1,
|
|
|
|
oid=version_id_1,
|
|
|
|
shell=self.shell,
|
|
|
|
nodes=self.cluster.storage_nodes,
|
2022-11-14 12:04:15 +00:00
|
|
|
)
|
|
|
|
assert copies_1 == 1
|
2022-12-05 22:31:45 +00:00
|
|
|
cid_2 = search_container_by_name(
|
2023-05-15 09:59:33 +00:00
|
|
|
default_wallet,
|
|
|
|
bucket_2,
|
|
|
|
shell=self.shell,
|
|
|
|
endpoint=self.cluster.default_rpc_endpoint,
|
2022-12-05 22:31:45 +00:00
|
|
|
)
|
2022-11-14 12:04:15 +00:00
|
|
|
copies_2 = get_simple_object_copies(
|
2023-05-15 09:59:33 +00:00
|
|
|
wallet=default_wallet,
|
2022-12-05 22:31:45 +00:00
|
|
|
cid=cid_2,
|
|
|
|
oid=version_id_2,
|
|
|
|
shell=self.shell,
|
|
|
|
nodes=self.cluster.storage_nodes,
|
2022-11-14 12:04:15 +00:00
|
|
|
)
|
|
|
|
assert copies_2 == 3
|
|
|
|
|
2023-09-08 10:35:34 +00:00
|
|
|
@allure.title("Bucket with unexisting location constraint (s3_client={s3_client})")
|
2023-05-02 14:41:02 +00:00
|
|
|
def test_s3_bucket_wrong_location(self, s3_client: S3ClientWrapper):
|
|
|
|
with allure.step("Create bucket with unenxisting location constraint policy"):
|
|
|
|
with pytest.raises(Exception):
|
|
|
|
s3_client.create_bucket(location_constraint="UNEXISTING LOCATION CONSTRAINT")
|
|
|
|
|
2023-09-08 10:35:34 +00:00
|
|
|
@allure.title("Bucket policy (s3_client={s3_client})")
|
2023-05-15 09:59:33 +00:00
|
|
|
def test_s3_bucket_policy(self, s3_client: S3ClientWrapper):
|
2022-11-14 12:04:15 +00:00
|
|
|
with allure.step("Create bucket with default policy"):
|
2023-05-15 09:59:33 +00:00
|
|
|
bucket = s3_client.create_bucket()
|
|
|
|
s3_helper.set_bucket_versioning(s3_client, bucket, VersioningStatus.ENABLED)
|
2022-11-14 12:04:15 +00:00
|
|
|
|
|
|
|
with allure.step("GetBucketPolicy"):
|
2023-05-15 09:59:33 +00:00
|
|
|
s3_client.get_bucket_policy(bucket)
|
2022-11-14 12:04:15 +00:00
|
|
|
|
|
|
|
with allure.step("Put new policy"):
|
2023-02-27 16:54:27 +00:00
|
|
|
custom_policy = f"file://{os.getcwd()}/pytest_tests/resources/files/bucket_policy.json"
|
2022-11-14 12:04:15 +00:00
|
|
|
custom_policy = {
|
|
|
|
"Version": "2008-10-17",
|
|
|
|
"Id": "aaaa-bbbb-cccc-dddd",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Sid": "AddPerm",
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Principal": {"AWS": "*"},
|
|
|
|
"Action": ["s3:GetObject"],
|
|
|
|
"Resource": [f"arn:aws:s3:::{bucket}/*"],
|
|
|
|
}
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
2023-05-15 09:59:33 +00:00
|
|
|
s3_client.put_bucket_policy(bucket, custom_policy)
|
2022-11-14 12:04:15 +00:00
|
|
|
with allure.step("GetBucketPolicy"):
|
2023-05-15 09:59:33 +00:00
|
|
|
policy_1 = s3_client.get_bucket_policy(bucket)
|
2022-11-14 12:04:15 +00:00
|
|
|
print(policy_1)
|
|
|
|
|
2023-09-08 10:35:34 +00:00
|
|
|
@allure.title("Bucket CORS (s3_client={s3_client})")
|
2023-05-15 09:59:33 +00:00
|
|
|
def test_s3_cors(self, s3_client: S3ClientWrapper):
|
2022-11-14 12:04:15 +00:00
|
|
|
with allure.step("Create bucket without cors"):
|
2023-05-15 09:59:33 +00:00
|
|
|
bucket = s3_client.create_bucket()
|
|
|
|
s3_helper.set_bucket_versioning(s3_client, bucket, VersioningStatus.ENABLED)
|
2022-11-14 12:04:15 +00:00
|
|
|
|
|
|
|
with pytest.raises(Exception):
|
2023-05-15 09:59:33 +00:00
|
|
|
bucket_cors = s3_client.get_bucket_cors(bucket)
|
2022-11-14 12:04:15 +00:00
|
|
|
|
|
|
|
with allure.step("Put bucket cors"):
|
|
|
|
cors = {
|
|
|
|
"CORSRules": [
|
|
|
|
{
|
|
|
|
"AllowedOrigins": ["http://www.example.com"],
|
|
|
|
"AllowedHeaders": ["*"],
|
|
|
|
"AllowedMethods": ["PUT", "POST", "DELETE"],
|
|
|
|
"MaxAgeSeconds": 3000,
|
|
|
|
"ExposeHeaders": ["x-amz-server-side-encryption"],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"AllowedOrigins": ["*"],
|
|
|
|
"AllowedHeaders": ["Authorization"],
|
|
|
|
"AllowedMethods": ["GET"],
|
|
|
|
"MaxAgeSeconds": 3000,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
2023-05-15 09:59:33 +00:00
|
|
|
s3_client.put_bucket_cors(bucket, cors)
|
|
|
|
bucket_cors = s3_client.get_bucket_cors(bucket)
|
2023-10-31 14:51:09 +00:00
|
|
|
assert bucket_cors == cors.get("CORSRules"), f"Expected CORSRules must be {cors.get('CORSRules')}"
|
2022-11-14 12:04:15 +00:00
|
|
|
|
|
|
|
with allure.step("delete bucket cors"):
|
2023-05-15 09:59:33 +00:00
|
|
|
s3_client.delete_bucket_cors(bucket)
|
2022-11-14 12:04:15 +00:00
|
|
|
|
|
|
|
with pytest.raises(Exception):
|
2023-05-15 09:59:33 +00:00
|
|
|
bucket_cors = s3_client.get_bucket_cors(bucket)
|