Skip to content
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
42 changes: 42 additions & 0 deletions mock-bpa-test/_test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,17 @@
# the prime contract 80NM0018D0004 between the Caltech and NASA under
# subcontract 1700763.
#
import contextlib
import json
import logging
import tempfile
from collections.abc import Generator
from dataclasses import dataclass
from enum import IntEnum, unique
from typing import Any, Optional

LOGGER = logging.getLogger(__name__)


@unique
class DataFormat(IntEnum):
Expand Down Expand Up @@ -71,3 +78,38 @@ class _TestCase:

use_bcb_rng: bool = False
""" If true, test will use custom rng callback for BCB testing """


@contextlib.contextmanager
def sc_config_modifier(orig: str, modify: dict[str, Any]) -> Generator[str, None, None]:
"""A context for modifying baseline configurations

:param orig: The original file path, relative to this directory.
:param modify: Updates to the context parameters, either modifying or adding.
:return: A generator for temporary files which exist for the duration
of this context.
"""
with tempfile.NamedTemporaryFile("w+", suffix=".json") as polfile:
with open(orig, "r") as infile:
poldata = json.load(infile)

params = poldata["policyrule_set"][0]["policyrule"]["spec"]["sc_parms"]
LOGGER.debug("Original params:\n%s", params)
if isinstance(params, dict):
params |= modify
elif isinstance(params, list):
# replace existing
for pair in params:
key = pair["id"]
if key in modify:
pair["value"] = str(modify.pop(key))
# add remaining
for key, val in modify.items():
params.append({"id": key, "value": str(val)})
else:
raise TypeError(f"bad type {type(params)}")
LOGGER.debug("Modified params:\n%s", params)

json.dump(poldata, polfile)
polfile.flush()
yield polfile.name
19 changes: 19 additions & 0 deletions mock-bpa-test/data/default-scs/keyset-1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"keys": [
{
"kty": "oct",
"kid": "ExampleA.1",
"k": "GisaKxorGisaKxorGisaKw=="
},
{
"kty": "oct",
"kid": "ExampleA.2-cek",
"k": "cXdlcnR5dWlvcGFzZGZnaA=="
},
{
"kty": "oct",
"kid": "ExampleA.2",
"k": "YWJjZGVmZ2hpamtsbW5vcA=="
}
]
}
27 changes: 27 additions & 0 deletions mock-bpa-test/data/default-scs/policy-exA.1-accept.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"policyrule_set": [
{
"policyrule": {
"desc": "Integrity accept rule",
"filter": {
"rule_id": "1",
"role": "a",
"tgt": 1,
"loc": "appout",
"sc_id": 1
},
"spec": {
"svc": "bib",
"sc_id": 1,
"sc_parms": {
"key_name": "ExampleA.1",
"sha_variant": 7,
"scope_flags": 0,
"key_wrap": 0
}
},
"policy_action_on_fail": "delete_bundle"
}
}
]
}
27 changes: 27 additions & 0 deletions mock-bpa-test/data/default-scs/policy-exA.1-source.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"policyrule_set": [
{
"policyrule": {
"desc": "Integrity source rule",
"filter": {
"rule_id": "1",
"role": "s",
"tgt": 1,
"loc": "appin",
"sc_id": 1
},
"spec": {
"svc": "bib",
"sc_id": 1,
"sc_parms": {
"key_name": "ExampleA.1",
"sha_variant": 7,
"scope_flags": 0,
"key_wrap": 0
}
},
"policy_action_on_fail": "delete_bundle"
}
}
]
}
27 changes: 27 additions & 0 deletions mock-bpa-test/data/default-scs/policy-exA.2-accept.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"policyrule_set": [
{
"policyrule": {
"desc": "Confidentiality accept rule",
"filter": {
"rule_id": "1",
"role": "a",
"tgt": 1,
"loc": "appout",
"sc_id": 2
},
"spec": {
"svc": "bcb",
"sc_id": 2,
"sc_parms": {
"key_name": "ExampleA.2",
"aes_variant": 1,
"aad_scope": 0,
"key_wrap": 1
}
},
"policy_action_on_fail": "delete_bundle"
}
}
]
}
27 changes: 27 additions & 0 deletions mock-bpa-test/data/default-scs/policy-exA.2-cek-source.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"policyrule_set": [
{
"policyrule": {
"desc": "Confidentiality source rule",
"filter": {
"rule_id": "1",
"role": "s",
"tgt": 1,
"loc": "appin",
"sc_id": 2
},
"spec": {
"svc": "bcb",
"sc_id": 2,
"sc_parms": {
"key_name": "ExampleA.2-cek",
"aes_variant": 1,
"aad_scope": 0,
"key_wrap": 0
}
},
"policy_action_on_fail": "delete_bundle"
}
}
]
}
27 changes: 27 additions & 0 deletions mock-bpa-test/data/default-scs/policy-exA.2-source.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"policyrule_set": [
{
"policyrule": {
"desc": "Confidentiality source rule",
"filter": {
"rule_id": "1",
"role": "s",
"tgt": 1,
"loc": "appin",
"sc_id": 2
},
"spec": {
"svc": "bcb",
"sc_id": 2,
"sc_parms": {
"key_name": "ExampleA.2",
"aes_variant": 1,
"aad_scope": 0,
"key_wrap": 1
}
},
"policy_action_on_fail": "delete_bundle"
}
}
]
}
12 changes: 6 additions & 6 deletions mock-bpa-test/test_bpa.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,24 @@ def _start(self, testcase: Optional[_TestCase]):
sec_src_eid = testcase.sec_src_eid

pol_is_json = policy_config.endswith(".json")
if pol_is_json:
policy_config = os.path.join(OWNPATH, policy_config)

use_bcb_rng = testcase.use_bcb_rng
key_set = testcase.key_set
key_set = os.path.join(OWNPATH, testcase.key_set)
# freshen derived file
if key_set.endswith(".cbordiag"):
key_file = key_set[:-4]
with (
open(os.path.join(OWNPATH, key_set), "r") as infile,
open(os.path.join(OWNPATH, key_file), "wb") as outfile,
open(key_set, "r") as infile,
open(key_file, "wb") as outfile,
):
outfile.write(diag2cbor(infile.read()))
key_set = key_file

else:
policy_config = "0x00"
key_set = "data/key_set_1.json"
key_set = os.path.join(OWNPATH, "data/key_set_1.json")

# fmt: off
arglist = [
Expand Down Expand Up @@ -188,8 +190,6 @@ def _single_test(self, testcase: _TestCase):
with self.assertRaises(TimeoutError):
self._wait_for(test_sock, timeout=0.1)

LOGGER.warning("Check log output to validate expected error")

err_case_str = testcase.expected_output
LOGGER.debug(f"ERR CASE STR: {err_case_str}")

Expand Down
Loading
Loading