2022-06-09 13:08:11 +00:00
|
|
|
import base64
|
2021-09-10 12:44:40 +00:00
|
|
|
import json
|
2022-08-25 10:57:55 +00:00
|
|
|
import logging
|
2021-09-10 12:44:40 +00:00
|
|
|
import os
|
|
|
|
import uuid
|
2022-08-25 10:57:55 +00:00
|
|
|
from dataclasses import dataclass
|
|
|
|
from enum import Enum
|
|
|
|
from time import sleep
|
|
|
|
from typing import Any, Dict, List, Optional, Union
|
2021-09-10 12:44:40 +00:00
|
|
|
|
2022-08-25 10:57:55 +00:00
|
|
|
import allure
|
2021-09-10 12:44:40 +00:00
|
|
|
import base58
|
2022-12-05 22:31:45 +00:00
|
|
|
from common import ASSETS_DIR, NEOFS_CLI_EXEC, WALLET_CONFIG
|
2022-08-01 10:15:02 +00:00
|
|
|
from data_formatters import get_wallet_public_key
|
2022-10-13 18:53:44 +00:00
|
|
|
from neofs_testlib.cli import NeofsCli
|
|
|
|
from neofs_testlib.shell import Shell
|
2021-09-10 12:44:40 +00:00
|
|
|
|
2022-09-28 12:07:16 +00:00
|
|
|
logger = logging.getLogger("NeoLogger")
|
2021-09-10 12:44:40 +00:00
|
|
|
EACL_LIFETIME = 100500
|
2022-08-25 10:57:55 +00:00
|
|
|
NEOFS_CONTRACT_CACHE_TIMEOUT = 30
|
|
|
|
|
|
|
|
|
|
|
|
class EACLOperation(Enum):
|
2022-09-28 12:07:16 +00:00
|
|
|
PUT = "put"
|
|
|
|
GET = "get"
|
|
|
|
HEAD = "head"
|
|
|
|
GET_RANGE = "getrange"
|
|
|
|
GET_RANGE_HASH = "getrangehash"
|
|
|
|
SEARCH = "search"
|
|
|
|
DELETE = "delete"
|
2022-08-25 10:57:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
class EACLAccess(Enum):
|
2022-09-28 12:07:16 +00:00
|
|
|
ALLOW = "allow"
|
|
|
|
DENY = "deny"
|
2022-08-25 10:57:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
class EACLRole(Enum):
|
2022-09-28 12:07:16 +00:00
|
|
|
OTHERS = "others"
|
|
|
|
USER = "user"
|
|
|
|
SYSTEM = "system"
|
2022-08-25 10:57:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
class EACLHeaderType(Enum):
|
2022-09-28 12:07:16 +00:00
|
|
|
REQUEST = "req" # Filter request headers
|
|
|
|
OBJECT = "obj" # Filter object headers
|
|
|
|
SERVICE = "SERVICE" # Filter service headers. These are not processed by NeoFS nodes and exist for service use only
|
2022-08-25 10:57:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
class EACLMatchType(Enum):
|
2022-09-28 12:07:16 +00:00
|
|
|
STRING_EQUAL = "=" # Return true if strings are equal
|
|
|
|
STRING_NOT_EQUAL = "!=" # Return true if strings are different
|
2022-08-25 10:57:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class EACLFilter:
|
|
|
|
header_type: EACLHeaderType = EACLHeaderType.REQUEST
|
|
|
|
match_type: EACLMatchType = EACLMatchType.STRING_EQUAL
|
|
|
|
key: Optional[str] = None
|
|
|
|
value: Optional[str] = None
|
|
|
|
|
|
|
|
def to_dict(self) -> Dict[str, Any]:
|
2022-09-28 12:07:16 +00:00
|
|
|
return {
|
|
|
|
"headerType": self.header_type,
|
|
|
|
"matchType": self.match_type,
|
|
|
|
"key": self.key,
|
|
|
|
"value": self.value,
|
|
|
|
}
|
2022-08-25 10:57:55 +00:00
|
|
|
|
2021-09-10 12:44:40 +00:00
|
|
|
|
2022-08-25 10:57:55 +00:00
|
|
|
@dataclass
|
|
|
|
class EACLFilters:
|
|
|
|
filters: Optional[List[EACLFilter]] = None
|
2022-06-09 13:08:11 +00:00
|
|
|
|
2022-08-25 10:57:55 +00:00
|
|
|
def __str__(self):
|
2022-09-28 12:07:16 +00:00
|
|
|
return (
|
|
|
|
",".join(
|
|
|
|
[
|
2022-10-13 18:53:44 +00:00
|
|
|
f"{filter.header_type.value}:"
|
|
|
|
f"{filter.key}{filter.match_type.value}{filter.value}"
|
2022-09-28 12:07:16 +00:00
|
|
|
for filter in self.filters
|
|
|
|
]
|
|
|
|
)
|
|
|
|
if self.filters
|
|
|
|
else []
|
|
|
|
)
|
2021-09-10 12:44:40 +00:00
|
|
|
|
2022-06-09 13:08:11 +00:00
|
|
|
|
2022-08-25 10:57:55 +00:00
|
|
|
@dataclass
|
|
|
|
class EACLPubKey:
|
|
|
|
keys: Optional[List[str]] = None
|
2021-09-10 12:44:40 +00:00
|
|
|
|
|
|
|
|
2022-08-25 10:57:55 +00:00
|
|
|
@dataclass
|
|
|
|
class EACLRule:
|
|
|
|
operation: Optional[EACLOperation] = None
|
|
|
|
access: Optional[EACLAccess] = None
|
|
|
|
role: Optional[Union[EACLRole, str]] = None
|
|
|
|
filters: Optional[EACLFilters] = None
|
|
|
|
|
|
|
|
def to_dict(self) -> Dict[str, Any]:
|
2022-09-28 12:07:16 +00:00
|
|
|
return {
|
|
|
|
"Operation": self.operation,
|
|
|
|
"Access": self.access,
|
|
|
|
"Role": self.role,
|
|
|
|
"Filters": self.filters or [],
|
|
|
|
}
|
2022-08-25 10:57:55 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2022-09-28 12:07:16 +00:00
|
|
|
role = (
|
|
|
|
self.role.value
|
|
|
|
if isinstance(self.role, EACLRole)
|
|
|
|
else f'pubkey:{get_wallet_public_key(self.role, "")}'
|
|
|
|
)
|
2022-08-25 10:57:55 +00:00
|
|
|
return f'{self.access.value} {self.operation.value} {self.filters or ""} {role}'
|
|
|
|
|
|
|
|
|
2022-09-28 12:07:16 +00:00
|
|
|
@allure.title("Get extended ACL")
|
2022-12-05 22:31:45 +00:00
|
|
|
def get_eacl(wallet_path: str, cid: str, shell: Shell, endpoint: str) -> Optional[str]:
|
2022-10-13 18:53:44 +00:00
|
|
|
cli = NeofsCli(shell, NEOFS_CLI_EXEC, WALLET_CONFIG)
|
2021-09-10 12:44:40 +00:00
|
|
|
try:
|
2022-12-05 22:31:45 +00:00
|
|
|
result = cli.container.get_eacl(wallet=wallet_path, rpc_endpoint=endpoint, cid=cid)
|
2021-09-10 12:44:40 +00:00
|
|
|
except RuntimeError as exc:
|
|
|
|
logger.info("Extended ACL table is not set for this container")
|
|
|
|
logger.info(f"Got exception while getting eacl: {exc}")
|
|
|
|
return None
|
2022-11-01 09:30:05 +00:00
|
|
|
if "extended ACL table is not set for this container" in result.stdout:
|
2022-08-25 10:57:55 +00:00
|
|
|
return None
|
2022-11-01 09:30:05 +00:00
|
|
|
return result.stdout
|
2021-09-10 12:44:40 +00:00
|
|
|
|
|
|
|
|
2022-09-28 12:07:16 +00:00
|
|
|
@allure.title("Set extended ACL")
|
2022-11-30 08:26:38 +00:00
|
|
|
def set_eacl(
|
|
|
|
wallet_path: str,
|
|
|
|
cid: str,
|
|
|
|
eacl_table_path: str,
|
|
|
|
shell: Shell,
|
2022-12-05 22:31:45 +00:00
|
|
|
endpoint: str,
|
2022-11-30 08:26:38 +00:00
|
|
|
session_token: Optional[str] = None,
|
|
|
|
) -> None:
|
2022-10-13 18:53:44 +00:00
|
|
|
cli = NeofsCli(shell, NEOFS_CLI_EXEC, WALLET_CONFIG)
|
2022-09-28 12:07:16 +00:00
|
|
|
cli.container.set_eacl(
|
|
|
|
wallet=wallet_path,
|
2022-12-05 22:31:45 +00:00
|
|
|
rpc_endpoint=endpoint,
|
2022-09-28 12:07:16 +00:00
|
|
|
cid=cid,
|
|
|
|
table=eacl_table_path,
|
|
|
|
await_mode=True,
|
2022-11-30 08:26:38 +00:00
|
|
|
session=session_token,
|
2022-09-28 12:07:16 +00:00
|
|
|
)
|
2021-09-10 12:44:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _encode_cid_for_eacl(cid: str) -> str:
|
|
|
|
cid_base58 = base58.b58decode(cid)
|
|
|
|
return base64.b64encode(cid_base58).decode("utf-8")
|
|
|
|
|
2022-06-09 13:08:11 +00:00
|
|
|
|
2022-10-13 18:53:44 +00:00
|
|
|
def create_eacl(cid: str, rules_list: List[EACLRule], shell: Shell) -> str:
|
2022-10-18 07:11:57 +00:00
|
|
|
table_file_path = os.path.join(os.getcwd(), ASSETS_DIR, f"eacl_table_{str(uuid.uuid4())}.json")
|
2022-10-13 18:53:44 +00:00
|
|
|
cli = NeofsCli(shell, NEOFS_CLI_EXEC, WALLET_CONFIG)
|
|
|
|
cli.acl.extended_create(cid=cid, out=table_file_path, rule=rules_list)
|
2022-02-17 09:52:48 +00:00
|
|
|
|
2022-09-28 12:07:16 +00:00
|
|
|
with open(table_file_path, "r") as file:
|
2022-08-01 10:15:02 +00:00
|
|
|
table_data = file.read()
|
2022-08-03 12:20:29 +00:00
|
|
|
logger.info(f"Generated eACL:\n{table_data}")
|
|
|
|
|
2022-08-01 10:15:02 +00:00
|
|
|
return table_file_path
|
2022-02-17 09:52:48 +00:00
|
|
|
|
2021-09-10 12:44:40 +00:00
|
|
|
|
2022-09-28 12:07:16 +00:00
|
|
|
def form_bearertoken_file(
|
2022-12-05 22:31:45 +00:00
|
|
|
wif: str,
|
|
|
|
cid: str,
|
|
|
|
eacl_rule_list: List[Union[EACLRule, EACLPubKey]],
|
|
|
|
shell: Shell,
|
|
|
|
endpoint: str,
|
2022-09-28 12:07:16 +00:00
|
|
|
) -> str:
|
2021-09-10 12:44:40 +00:00
|
|
|
"""
|
|
|
|
This function fetches eACL for given <cid> on behalf of <wif>,
|
2022-08-25 10:57:55 +00:00
|
|
|
then extends it with filters taken from <eacl_rules>, signs
|
2021-09-10 12:44:40 +00:00
|
|
|
with bearer token and writes to file
|
|
|
|
"""
|
2022-12-06 15:06:39 +00:00
|
|
|
enc_cid = _encode_cid_for_eacl(cid) if cid else None
|
2022-10-18 07:11:57 +00:00
|
|
|
file_path = os.path.join(os.getcwd(), ASSETS_DIR, str(uuid.uuid4()))
|
2021-09-10 12:44:40 +00:00
|
|
|
|
2022-12-05 22:31:45 +00:00
|
|
|
eacl = get_eacl(wif, cid, shell, endpoint)
|
2021-09-10 12:44:40 +00:00
|
|
|
json_eacl = dict()
|
|
|
|
if eacl:
|
2022-09-28 12:07:16 +00:00
|
|
|
eacl = eacl.replace("eACL: ", "").split("Signature")[0]
|
2021-09-10 12:44:40 +00:00
|
|
|
json_eacl = json.loads(eacl)
|
|
|
|
logger.info(json_eacl)
|
|
|
|
eacl_result = {
|
2022-09-28 12:07:16 +00:00
|
|
|
"body": {
|
2022-12-06 15:06:39 +00:00
|
|
|
"eaclTable": {"containerID": {"value": enc_cid} if cid else enc_cid, "records": []},
|
2022-09-28 12:07:16 +00:00
|
|
|
"lifetime": {"exp": EACL_LIFETIME, "nbf": "1", "iat": "0"},
|
|
|
|
}
|
2022-06-09 13:08:11 +00:00
|
|
|
}
|
2021-09-10 12:44:40 +00:00
|
|
|
|
2022-09-28 12:07:16 +00:00
|
|
|
assert eacl_rules, "Got empty eacl_records list"
|
2022-08-25 10:57:55 +00:00
|
|
|
for rule in eacl_rule_list:
|
2021-09-10 12:44:40 +00:00
|
|
|
op_data = {
|
2022-08-25 10:57:55 +00:00
|
|
|
"operation": rule.operation.value.upper(),
|
|
|
|
"action": rule.access.value.upper(),
|
|
|
|
"filters": rule.filters or [],
|
2022-09-28 12:07:16 +00:00
|
|
|
"targets": [],
|
2022-06-09 13:08:11 +00:00
|
|
|
}
|
2021-09-10 12:44:40 +00:00
|
|
|
|
2022-08-25 10:57:55 +00:00
|
|
|
if isinstance(rule.role, EACLRole):
|
2022-09-28 12:07:16 +00:00
|
|
|
op_data["targets"] = [{"role": rule.role.value.upper()}]
|
2022-08-25 10:57:55 +00:00
|
|
|
elif isinstance(rule.role, EACLPubKey):
|
2022-09-28 12:07:16 +00:00
|
|
|
op_data["targets"] = [{"keys": rule.role.keys}]
|
2021-09-10 12:44:40 +00:00
|
|
|
|
|
|
|
eacl_result["body"]["eaclTable"]["records"].append(op_data)
|
|
|
|
|
|
|
|
# Add records from current eACL
|
|
|
|
if "records" in json_eacl.keys():
|
|
|
|
for record in json_eacl["records"]:
|
|
|
|
eacl_result["body"]["eaclTable"]["records"].append(record)
|
|
|
|
|
2022-09-28 12:07:16 +00:00
|
|
|
with open(file_path, "w", encoding="utf-8") as eacl_file:
|
2021-09-10 12:44:40 +00:00
|
|
|
json.dump(eacl_result, eacl_file, ensure_ascii=False, indent=4)
|
|
|
|
|
|
|
|
logger.info(f"Got these extended ACL records: {eacl_result}")
|
2022-11-01 09:30:05 +00:00
|
|
|
sign_bearer(shell, wif, file_path)
|
2021-09-10 12:44:40 +00:00
|
|
|
return file_path
|
|
|
|
|
2022-08-01 10:15:02 +00:00
|
|
|
|
|
|
|
def eacl_rules(access: str, verbs: list, user: str) -> list[str]:
|
2022-07-18 10:19:05 +00:00
|
|
|
"""
|
2022-09-28 12:07:16 +00:00
|
|
|
This function creates a list of eACL rules.
|
|
|
|
Args:
|
|
|
|
access (str): identifies if the following operation(s)
|
|
|
|
is allowed or denied
|
|
|
|
verbs (list): a list of operations to set rules for
|
|
|
|
user (str): a group of users (user/others) or a wallet of
|
|
|
|
a certain user for whom rules are set
|
|
|
|
Returns:
|
|
|
|
(list): a list of eACL rules
|
2022-07-18 10:19:05 +00:00
|
|
|
"""
|
2022-09-28 12:07:16 +00:00
|
|
|
if user not in ("others", "user"):
|
2022-08-01 10:15:02 +00:00
|
|
|
pubkey = get_wallet_public_key(user, wallet_password="")
|
2022-07-22 07:04:26 +00:00
|
|
|
user = f"pubkey:{pubkey}"
|
2022-07-18 10:19:05 +00:00
|
|
|
|
|
|
|
rules = []
|
|
|
|
for verb in verbs:
|
2022-08-01 10:15:02 +00:00
|
|
|
rule = f"{access} {verb} {user}"
|
|
|
|
rules.append(rule)
|
2022-07-18 10:19:05 +00:00
|
|
|
return rules
|
|
|
|
|
|
|
|
|
2022-11-01 09:30:05 +00:00
|
|
|
def sign_bearer(shell: Shell, wallet_path: str, eacl_rules_file: str) -> None:
|
|
|
|
neofscli = NeofsCli(shell=shell, neofs_cli_exec_path=NEOFS_CLI_EXEC, config_file=WALLET_CONFIG)
|
|
|
|
neofscli.util.sign_bearer_token(
|
|
|
|
wallet=wallet_path, from_file=eacl_rules_file, to_file=eacl_rules_file, json=True
|
2021-09-10 12:44:40 +00:00
|
|
|
)
|
2022-08-25 10:57:55 +00:00
|
|
|
|
|
|
|
|
2022-09-28 12:07:16 +00:00
|
|
|
@allure.title("Wait for eACL cache expired")
|
2022-08-25 10:57:55 +00:00
|
|
|
def wait_for_cache_expired():
|
|
|
|
sleep(NEOFS_CONTRACT_CACHE_TIMEOUT)
|
|
|
|
return
|