Skip to content

Commit abb462e

Browse files
committed
add obfuscator methods to decision tables and dp groups objects
this is based on discussion in #795. Not sure if we're going to keep this or not.
1 parent cb5da14 commit abb462e

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/ssvc/decision_tables/base.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,26 @@ def validate_mapping(self):
166166

167167
return self
168168

169+
def obfuscate(self) -> "DecisionTable":
170+
"""
171+
Obfuscate the decision table by renaming the dict keys.
172+
"""
173+
obfuscated_dpg, translator = self.decision_point_group.obfuscate()
174+
175+
new_table = self.copy(deep=True)
176+
new_table.decision_point_group = obfuscated_dpg
177+
new_table.outcome = translator.get(self.outcome, self.outcome)
178+
# replace all the keys in mapping dicts
179+
new_table.mapping = []
180+
for row in self.mapping:
181+
new_row = {}
182+
for key in row.keys():
183+
new_key = translator[key]
184+
new_row[new_key] = row[key]
185+
new_table.mapping.append(new_row)
186+
187+
return new_table
188+
169189

170190
def decision_table_to_df(dt: DecisionTable, longform=False) -> pd.DataFrame:
171191
"""
@@ -531,6 +551,10 @@ def main() -> None:
531551
print(table.model_dump_json(indent=2))
532552
print("```")
533553

554+
print("## Obfuscated JSON representation of the decision table:")
555+
obfuscated = table.obfuscate()
556+
print(obfuscated.model_dump_json(indent=2))
557+
534558

535559
if __name__ == "__main__":
536560
main()

src/ssvc/dp_groups/base.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"""
2323
Provides a DecisionPointGroup object for use in SSVC.
2424
"""
25+
import secrets
2526
from collections.abc import MutableMapping
2627
from itertools import product
2728

@@ -86,6 +87,37 @@ def add(self, decision_point: DecisionPoint) -> None:
8687
# set the decision point in the dictionary
8788
self.decision_points[decision_point.id] = decision_point
8889

90+
def obfuscate(self) -> tuple["DecisionPointGroup", dict[str, str]]:
91+
"""
92+
Returns a new DecisionPointGroup object, with the keys of the decision points dict obfuscated.
93+
94+
Returns:
95+
tuple: A tuple containing the new DecisionPointGroup and a dictionary mapping old keys to new obfuscated keys.
96+
"""
97+
token_len = 4
98+
new_dict = {}
99+
translator = {}
100+
for old_key in self.decision_points.keys():
101+
while True:
102+
new_key = secrets.token_hex(token_len)
103+
# make the new key match NNNN-NNNN...
104+
new_key = "-".join(
105+
new_key[i : i + token_len]
106+
for i in range(0, len(new_key), token_len)
107+
)
108+
# uppercase the new key
109+
new_key = new_key.upper()
110+
if new_key not in translator:
111+
break
112+
# got a unique new_key
113+
translator[old_key] = new_key
114+
new_dict[new_key] = self.decision_points[old_key]
115+
116+
new_group = self.copy(deep=True)
117+
new_group.decision_points = new_dict
118+
119+
return (new_group, translator)
120+
89121
def combination_strings(self) -> list[tuple[str, ...]]:
90122
"""
91123
Generate all combinations of decision point values as strings.

0 commit comments

Comments
 (0)