Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(xtest): pyright suggestions #246

Merged
merged 2 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions xtest/abac.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ class Attribute(BaseModel):
active: BoolValue | None = None
metadata: Metadata | None = None

@property
def value_fqns(self) -> list[str]:
if not self.values:
return []
v = [v.fqn for v in self.values if v.fqn]
assert len(v) == len(self.values)
return v


class SubjectMappingOperatorEnum(enum.IntEnum):
IN = 1
Expand Down
2 changes: 1 addition & 1 deletion xtest/assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class Statement(BaseModel):
format: str
schema: str
value: str | dict
value: str | dict[str, str]


class Binding(BaseModel):
Expand Down
47 changes: 28 additions & 19 deletions xtest/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import abac


def pytest_addoption(parser):
def pytest_addoption(parser: pytest.Parser):
parser.addoption(
"--large",
action="store_true",
Expand All @@ -29,39 +29,48 @@ def pytest_addoption(parser):
parser.addoption("--containers", help="which container formats to test")


def pytest_generate_tests(metafunc):
def pytest_generate_tests(metafunc: pytest.Metafunc):
if "size" in metafunc.fixturenames:
metafunc.parametrize(
"size",
["large" if metafunc.config.getoption("large") else "small"],
scope="session",
)

def list_opt(name: str) -> list[str]:
v = metafunc.config.getoption(name)
if not v:
return []
if type(v) is not str:
raise ValueError(f"Invalid value for {name}: {v}")
return v.split()

if "encrypt_sdk" in metafunc.fixturenames:
if metafunc.config.getoption("--sdks-encrypt"):
encrypt_sdks = metafunc.config.getoption("--sdks-encrypt").split()
encrypt_sdks = list_opt("--sdks-encrypt")
elif metafunc.config.getoption("--sdks"):
encrypt_sdks = metafunc.config.getoption("--sdks").split()
encrypt_sdks = list_opt("--sdks")
else:
encrypt_sdks = ["js", "go", "java"]
metafunc.parametrize("encrypt_sdk", encrypt_sdks)
if "decrypt_sdk" in metafunc.fixturenames:
if metafunc.config.getoption("--sdks-decrypt"):
decrypt_sdks = metafunc.config.getoption("--sdks-decrypt").split()
decrypt_sdks = list_opt("--sdks-decrypt")
elif metafunc.config.getoption("--sdks"):
decrypt_sdks = metafunc.config.getoption("--sdks").split()
decrypt_sdks = list_opt("--sdks")
else:
decrypt_sdks = ["js", "go", "java"]
metafunc.parametrize("decrypt_sdk", decrypt_sdks)
if "container" in metafunc.fixturenames:
if metafunc.config.getoption("--containers"):
containers = metafunc.config.getoption("--containers").split()
containers = list_opt("--containers")
else:
containers = ["nano", "ztdf", "nano-with-ecdsa"]
metafunc.parametrize("container", containers)


@pytest.fixture(scope="module")
def pt_file(tmp_dir, size):
def pt_file(tmp_dir: str, size: str):
pt_file = f"{tmp_dir}test-plain-{size}.txt"
length = (5 * 2**30) if size == "large" else 128
with open(pt_file, "w") as f:
Expand Down Expand Up @@ -305,7 +314,7 @@ def one_attribute_attr_kas_grant(
otdfctl: abac.OpentdfCommandLineTool,
kas_url_attr: str,
temporary_namespace: abac.Namespace,
):
) -> abac.Attribute:
anyof = otdfctl.attribute_create(
temporary_namespace, "attrgrant", abac.AttributeRule.ANY_OF, ["alpha"]
)
Expand Down Expand Up @@ -350,7 +359,7 @@ def attr_and_value_kas_grants_or(
kas_url_attr: str,
kas_url_value1: str,
temporary_namespace: abac.Namespace,
):
) -> abac.Attribute:
anyof = otdfctl.attribute_create(
temporary_namespace,
"attrorvalgrant",
Expand Down Expand Up @@ -404,7 +413,7 @@ def attr_and_value_kas_grants_and(
kas_url_attr: str,
kas_url_value1: str,
temporary_namespace: abac.Namespace,
):
) -> abac.Attribute:
allof = otdfctl.attribute_create(
temporary_namespace,
"attrandvalgrant",
Expand Down Expand Up @@ -459,7 +468,7 @@ def one_attribute_ns_kas_grant(
otdfctl: abac.OpentdfCommandLineTool,
kas_url_ns: str,
temporary_namespace: abac.Namespace,
):
) -> abac.Attribute:
anyof = otdfctl.attribute_create(
temporary_namespace, "nsgrant", abac.AttributeRule.ANY_OF, ["alpha"]
)
Expand Down Expand Up @@ -503,7 +512,7 @@ def ns_and_value_kas_grants_or(
otdfctl: abac.OpentdfCommandLineTool,
kas_url_value1: str,
kas_url_ns: str,
):
) -> abac.Attribute:
temp_namespace = create_temp_namesapce(otdfctl)
anyof = otdfctl.attribute_create(
temp_namespace,
Expand Down Expand Up @@ -557,7 +566,7 @@ def ns_and_value_kas_grants_and(
otdfctl: abac.OpentdfCommandLineTool,
kas_url_value1: str,
kas_url_ns: str,
):
) -> abac.Attribute:
temp_namespace = create_temp_namesapce(otdfctl)
allof = otdfctl.attribute_create(
temp_namespace,
Expand Down Expand Up @@ -609,12 +618,12 @@ def ns_and_value_kas_grants_and(


@pytest.fixture(scope="module")
def hs256_key():
def hs256_key() -> str:
return base64.b64encode(secrets.token_bytes(32)).decode("ascii")


@pytest.fixture(scope="module")
def rs256_keys():
def rs256_keys() -> tuple[str, str]:
# Generate an RSA private key
private_key = rsa.generate_private_key(public_exponent=65537, key_size=2048)

Expand Down Expand Up @@ -670,7 +679,7 @@ def assertion_file_no_keys():


@pytest.fixture(scope="module")
def assertion_file_rs_and_hs_keys(hs256_key, rs256_keys):
def assertion_file_rs_and_hs_keys(hs256_key: str, rs256_keys: tuple[str, str]):
rs256_private, _ = rs256_keys
assertion_list = [
assertions.Assertion(
Expand Down Expand Up @@ -709,11 +718,11 @@ def assertion_file_rs_and_hs_keys(hs256_key, rs256_keys):

def write_assertion_verification_keys_to_file(
file_name: str,
assertion_verificaiton_keys: assertions.AssertionVerificationKeys = None,
assertion_verification_keys: assertions.AssertionVerificationKeys,
):
as_file = f"{tmp_dir}test-assertion-verification-{file_name}.json"
assertion_verification_json = json.dumps(
to_jsonable_python(assertion_verificaiton_keys, exclude_none=True)
to_jsonable_python(assertion_verification_keys, exclude_none=True)
)
with open(as_file, "w") as f:
f.write(assertion_verification_json)
Expand Down
10 changes: 5 additions & 5 deletions xtest/nano.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class EccMode(ct.EnumBase):
secp256k1 = 3

@property
def bit_length(self) -> int:
def mode_bit_length(self) -> int:
if self in (EccMode.secp256r1, EccMode.secp256k1):
return 256
if self == EccMode.secp384r1:
Expand All @@ -64,7 +64,7 @@ def bit_length(self) -> int:

@property
def byte_length(self) -> int:
return (self.bit_length + 7) >> 3
return (self.mode_bit_length + 7) >> 3

@property
def signature_length(self) -> int:
Expand All @@ -74,7 +74,7 @@ def signature_length(self) -> int:
def public_key_length(self) -> int:
return self.byte_length + 1

def __getitem__(self, key):
def __getitem__(self, key: str):
return getattr(self, key)


Expand Down Expand Up @@ -103,7 +103,7 @@ def mac_length(self) -> int:
return 16
raise ValueError("invalid EC cipher mode")

def __getitem__(self, key):
def __getitem__(self, key: str):
return getattr(self, key)


Expand Down Expand Up @@ -142,7 +142,7 @@ def size(self) -> int:
return 32
return 0

def __getitem__(self, key):
def __getitem__(self, key: str):
return getattr(self, key)


Expand Down
Loading
Loading