1010from loguru import logger
1111import yaml
1212
13- from monitoring .monitorlib .dicts import remove_elements
13+ from monitoring .monitorlib .dicts import remove_elements , get_element_or_default
1414from monitoring .monitorlib .versioning import get_code_version , get_commit_hash
1515from monitoring .uss_qualifier .configurations .configuration import (
1616 USSQualifierConfiguration ,
@@ -73,6 +73,13 @@ def parseArgs() -> argparse.Namespace:
7373 help = "JSON string containing runtime metadata to record in the test run report (if specified)." ,
7474 )
7575
76+ parser .add_argument (
77+ "--disallow-unredacted" ,
78+ type = bool ,
79+ default = False ,
80+ help = "When true, do not run a test configuration which would produce unredacted sensitive information in its artifacts" ,
81+ )
82+
7683 return parser .parse_args ()
7784
7885
@@ -140,13 +147,32 @@ def execute_test_run(
140147 )
141148
142149
150+ def raise_for_unredacted_information (config : USSQualifierConfiguration ) -> None :
151+ """Raises a ValueError if the provided configuration would produce or display unredacted information."""
152+
153+ required_values = {
154+ "v1.artifacts.globally_expanded_report.redact_access_tokens" : True ,
155+ "v1.artifacts.raw_report.redact_access_tokens" : True ,
156+ "v1.artifacts.report_html.redact_access_tokens" : True ,
157+ "v1.artifacts.sequence_view.redact_access_tokens" : True ,
158+ }
159+
160+ for json_address , required_value in required_values .items ():
161+ actual_value = get_element_or_default (config , json_address , required_value )
162+ if actual_value != required_value :
163+ raise ValueError (
164+ f"Configuration element { json_address } must be { required_value } to disallow unredacted information, but was instead set to { actual_value } "
165+ )
166+
167+
143168def run_config (
144169 config_name : str ,
145170 config_output : str ,
146171 skip_validation : bool ,
147172 exit_before_execution : bool ,
148173 output_path : Optional [str ],
149174 runtime_metadata : Optional [dict ],
175+ disallow_unredacted : bool ,
150176):
151177 config_src = load_dict_with_references (config_name )
152178
@@ -192,6 +218,9 @@ def run_config(
192218 logger .info ("Exiting because --exit-before-execution specified." )
193219 return
194220
221+ if disallow_unredacted :
222+ raise_for_unredacted_information (whole_config )
223+
195224 config : USSQualifierConfigurationV1 = whole_config .v1
196225
197226 if config .artifacts and not output_path :
@@ -206,7 +235,7 @@ def run_config(
206235 report .runtime_metadata = runtime_metadata
207236
208237 if config .artifacts :
209- generate_artifacts (report , config .artifacts , output_path )
238+ generate_artifacts (report , config .artifacts , output_path , disallow_unredacted )
210239
211240 if "validation" in config and config .validation :
212241 logger .info (f"Validating test run report for configuration '{ config_name } '" )
@@ -228,6 +257,8 @@ def main() -> int:
228257 if runtime_metadata is not None and not isinstance (runtime_metadata , dict ):
229258 raise ValueError ("--runtime-metadata must specify a JSON dictionary" )
230259
260+ disallow_unredacted = args .disallow_unredacted
261+
231262 config_names = str (args .config ).split ("," )
232263
233264 if args .config_output :
@@ -254,6 +285,7 @@ def main() -> int:
254285 args .exit_before_execution ,
255286 output_path ,
256287 runtime_metadata ,
288+ disallow_unredacted ,
257289 )
258290 if exit_code != os .EX_OK :
259291 return exit_code
0 commit comments