-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgarak_adapter.py
More file actions
1485 lines (1263 loc) · 57.7 KB
/
garak_adapter.py
File metadata and controls
1485 lines (1263 loc) · 57.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Garak Framework Adapter for eval-hub.
This adapter integrates NVIDIA's Garak red-teaming framework with the
eval-hub evaluation platform. It supports two execution modes:
- **simple** (default): Runs garak as a subprocess inside the K8s Job pod.
- **kfp**: Delegates scan execution to a Kubeflow Pipeline, using S3
(via a Data Connection secret) for artifact transfer. The adapter
submits the pipeline, polls for completion, and downloads results
from the shared S3 bucket.
The adapter:
1. Reads JobSpec from mounted ConfigMap (/etc/eval-job/spec.json)
2. Builds and executes Garak CLI commands (simple) or submits KFP pipeline (kfp)
3. Parses results from Garak's JSONL reports
4. Persists artifacts to OCI registry
5. Reports results back to eval-hub via sidecar callbacks
"""
from __future__ import annotations
import json
import logging
import os
import time
from datetime import UTC, datetime
from pathlib import Path
from typing import Any, TYPE_CHECKING
if TYPE_CHECKING:
from .kfp_pipeline import KFPConfig
from evalhub.adapter import (
DefaultCallbacks,
FrameworkAdapter,
JobCallbacks,
JobPhase,
JobResults,
JobSpec,
JobStatus,
JobStatusUpdate,
OCIArtifactSpec,
)
from evalhub.adapter.models.job import ErrorInfo, MessageInfo
from evalhub.models.api import EvaluationResult
from ..core.command_builder import build_generator_options
from ..core.config_resolution import (
build_effective_garak_config,
resolve_scan_profile,
resolve_timeout_seconds,
)
from ..core.garak_runner import convert_to_avid_report, GarakScanResult, run_garak_scan
from ..garak_command_config import GarakCommandConfig
from ..result_utils import (
combine_parsed_results,
generate_art_report,
parse_aggregated_from_avid_content,
parse_digest_from_report_content,
parse_generations_from_report_content,
)
from ..utils import get_scan_base_dir, as_bool
from ..constants import (
DEFAULT_TIMEOUT,
DEFAULT_MODEL_TYPE,
DEFAULT_EVAL_THRESHOLD,
EXECUTION_MODE_SIMPLE,
EXECUTION_MODE_KFP,
DEFAULT_SDG_FLOW_ID,
)
logger = logging.getLogger(__name__)
class GarakAdapter(FrameworkAdapter):
"""Garak red-teaming framework adapter for eval-hub.
Supports two execution modes:
- **simple**: Runs garak as a local subprocess (default).
- **kfp**: Submits a Kubeflow Pipeline, polls for completion, and
downloads results from S3 via a Data Connection secret.
Benchmark Configuration:
The adapter expects benchmark_config in JobSpec to contain:
- probes: List of Garak probe names
- probe_tags: Optional probe tag filters
- eval_threshold: Threshold for vulnerability detection (default: 0.5)
- execution_mode: "simple" or "kfp" (default: "simple")
- kfp_config: dict with KFP connection details (for kfp mode)
- Other Garak CLI options (detectors, buffs, etc.)
Results:
Returns one EvaluationResult per probe with:
- attack_success_rate: Percentage of successful attacks
- vulnerable_responses: Count of vulnerable responses
- total_attempts: Total number of probe attempts
"""
def run_benchmark_job(self, config: JobSpec, callbacks: JobCallbacks) -> JobResults:
"""Run a Garak security scan job.
Dispatches to either local subprocess execution or KFP pipeline
execution depending on the resolved execution_mode.
"""
start_time = time.time()
logger.info(f"Starting Garak job {config.id} for benchmark {config.benchmark_id}")
try:
# Phase 1: Initialize
callbacks.report_status(
JobStatusUpdate(
status=JobStatus.RUNNING,
phase=JobPhase.INITIALIZING,
progress=0.0,
message=MessageInfo(
message="Validating configuration and building scan command",
message_code="initializing",
),
)
)
self._validate_config(config)
benchmark_config = config.parameters or {}
execution_mode = self._resolve_execution_mode(benchmark_config)
logger.info("Execution mode: %s", execution_mode)
# Early check: intents benchmarks require KFP mode
_intents_required = benchmark_config.get("art_intents")
if _intents_required is None:
_profile = self._resolve_profile(config.benchmark_id)
_intents_required = _profile.get("art_intents", False) if _profile else False
if _intents_required and execution_mode != EXECUTION_MODE_KFP:
raise ValueError("Intents benchmarks are only supported in KFP execution mode. ")
# Build merged garak config (common to both modes)
scan_dir = get_scan_base_dir() / config.id
scan_dir.mkdir(parents=True, exist_ok=True)
report_prefix = scan_dir / "scan"
garak_config_dict, profile, intents_params = self._build_config_from_spec(config, report_prefix)
if not garak_config_dict:
raise ValueError("Garak command config is empty")
art_intents = intents_params.get("art_intents", False)
timeout = resolve_timeout_seconds(
benchmark_config,
profile,
default_timeout=DEFAULT_TIMEOUT,
)
logger.info("Using timeout=%ss for benchmark=%s", timeout, config.benchmark_id)
eval_threshold = float(garak_config_dict.get("run", {}).get("eval_threshold", DEFAULT_EVAL_THRESHOLD))
# Phase 2: Execute scan (mode-dependent)
if execution_mode == EXECUTION_MODE_KFP:
result, scan_dir = self._run_via_kfp(
config=config,
callbacks=callbacks,
garak_config_dict=garak_config_dict,
timeout=timeout,
intents_params=intents_params,
eval_threshold=eval_threshold,
)
else:
result = self._run_simple(
config=config,
callbacks=callbacks,
garak_config_dict=garak_config_dict,
scan_dir=scan_dir,
timeout=timeout,
)
if not result.success:
error_msg = f"Garak scan failed: {result.stderr}" if result.stderr else "Unknown error"
if result.timed_out:
error_msg = f"Scan timed out after {timeout} seconds"
callbacks.report_status(
JobStatusUpdate(
status=JobStatus.FAILED,
error=ErrorInfo(message=error_msg, message_code="scan_failed"),
)
)
raise RuntimeError(error_msg)
# Phase 3: Parse results (common to both modes)
callbacks.report_status(
JobStatusUpdate(
status=JobStatus.RUNNING,
phase=JobPhase.POST_PROCESSING,
progress=0.8,
message=MessageInfo(
message="Parsing scan results",
message_code="parsing_results",
),
)
)
metrics, overall_score, num_examples, overall_summary = self._parse_results(
result,
eval_threshold,
art_intents=art_intents,
)
logger.info(f"Parsed {len(metrics)} probe metrics, overall score: {overall_score}")
# Phase 3b: Generate ART HTML report for intents scans
if art_intents and result.report_jsonl.exists():
try:
report_content = result.report_jsonl.read_text()
if report_content.strip():
art_html_path = scan_dir / "scan.intents.html"
if not art_html_path.exists():
art_html = generate_art_report(report_content)
art_html_path.write_text(art_html)
logger.info("Generated ART HTML report: %s", art_html_path)
except Exception as e:
logger.warning("Failed to generate ART HTML report: %s", e)
# Redact api_key values from config.json before OCI export
config_file = scan_dir / "config.json"
if config_file.exists():
try:
from ..core.pipeline_steps import redact_api_keys
cfg = json.loads(config_file.read_text())
config_file.write_text(json.dumps(redact_api_keys(cfg), indent=1))
except Exception as exc:
logger.warning("Could not redact config.json: %s", exc)
# Phase 4: Persist artifacts
oci_artifact = None
if config.exports and config.exports.oci:
oci_artifact = callbacks.create_oci_artifact(
OCIArtifactSpec(
files_path=scan_dir,
coordinates=config.exports.oci.coordinates,
)
)
logger.info(f"Persisted scan artifacts: {oci_artifact.reference}")
# Compute duration
duration = time.time() - start_time
eval_meta: dict[str, Any] = {
"framework": "garak",
"eval_threshold": eval_threshold,
"timed_out": result.timed_out,
"execution_mode": execution_mode,
"art_intents": art_intents,
"overall": overall_summary,
}
if execution_mode == EXECUTION_MODE_KFP:
from .kfp_pipeline import DEFAULT_S3_PREFIX
_bc = config.parameters or {}
_kfp_ov = _bc.get("kfp_config", {}) if isinstance(_bc.get("kfp_config"), dict) else {}
_prefix = _kfp_ov.get("s3_prefix", os.getenv("KFP_S3_PREFIX", DEFAULT_S3_PREFIX))
_bucket = _kfp_ov.get("s3_bucket", os.getenv("AWS_S3_BUCKET", ""))
s3_prefix = f"{_prefix}/{config.id}"
artifact_keys: dict[str, str] = {
"scan_report": f"{s3_prefix}/scan.report.jsonl",
"scan_html_report": f"{s3_prefix}/scan.report.html",
}
if art_intents:
artifact_keys["sdg_raw_output"] = f"{s3_prefix}/sdg_raw_output.csv"
artifact_keys["sdg_normalized_output"] = f"{s3_prefix}/sdg_normalized_output.csv"
artifact_keys["intents_html_report"] = f"{s3_prefix}/scan.intents.html"
s3_artifacts: dict[str, str] = {}
for name, key in artifact_keys.items():
local_file = scan_dir / key.split("/")[-1]
if local_file.exists():
s3_artifacts[name] = f"s3://{_bucket}/{key}" if _bucket else key
else:
logger.debug("Artifact not downloaded locally, skipping: %s", key)
eval_meta["artifacts"] = s3_artifacts
results = JobResults(
id=config.id,
benchmark_id=config.benchmark_id,
benchmark_index=config.benchmark_index,
model_name=config.model.name,
results=metrics,
overall_score=overall_score,
num_examples_evaluated=num_examples,
duration_seconds=duration,
completed_at=datetime.now(UTC),
evaluation_metadata=eval_meta,
oci_artifact=oci_artifact,
)
# Phase 5: Save to MLflow (if experiment_name configured)
try:
from evalhub.adapter.mlflow import MlflowArtifact
mlflow_artifacts: list[MlflowArtifact] = []
if result.report_html.exists():
mlflow_artifacts.append(
MlflowArtifact(
"scan.report.html",
result.report_html.read_bytes(),
"text/html",
)
)
art_html_path = scan_dir / "scan.intents.html"
if art_html_path.exists():
mlflow_artifacts.append(
MlflowArtifact(
"scan.intents.html",
art_html_path.read_bytes(),
"text/html",
)
)
if result.report_jsonl.exists():
mlflow_artifacts.append(
MlflowArtifact(
"scan.report.jsonl",
result.report_jsonl.read_bytes(),
"application/jsonl",
)
)
sdg_raw = scan_dir / "sdg_raw_output.csv"
if sdg_raw.exists():
mlflow_artifacts.append(
MlflowArtifact(
"sdg_raw_output.csv",
sdg_raw.read_bytes(),
"text/csv",
)
)
sdg_norm = scan_dir / "sdg_normalized_output.csv"
if sdg_norm.exists():
mlflow_artifacts.append(
MlflowArtifact(
"sdg_normalized_output.csv",
sdg_norm.read_bytes(),
"text/csv",
)
)
callbacks.mlflow.save(results, config, artifacts=mlflow_artifacts)
logger.info("Saved results and %d artifacts to MLflow", len(mlflow_artifacts))
except Exception as mlflow_exc:
logger.warning("MLflow save failed (non-fatal): %s", mlflow_exc)
return results
except Exception as e:
logger.exception(f"Garak job {config.id} failed")
callbacks.report_status(
JobStatusUpdate(
status=JobStatus.FAILED,
error=ErrorInfo(message=str(e), message_code="job_failed"),
error_details={"exception_type": type(e).__name__},
)
)
raise
# ------------------------------------------------------------------
# Execution mode helpers
# ------------------------------------------------------------------
@staticmethod
def _resolve_execution_mode(benchmark_config: dict) -> str:
"""Resolve execution mode from benchmark_config or env var.
Priority: benchmark_config > env var > default ("simple").
"""
mode = benchmark_config.get("execution_mode") or os.getenv("EVALHUB_EXECUTION_MODE", EXECUTION_MODE_SIMPLE)
mode = str(mode).strip().lower()
if mode not in (EXECUTION_MODE_SIMPLE, EXECUTION_MODE_KFP):
logger.warning("Unknown execution_mode '%s', falling back to simple", mode)
mode = EXECUTION_MODE_SIMPLE
return mode
# ------------------------------------------------------------------
# Simple (subprocess) execution
# ------------------------------------------------------------------
def _run_simple(
self,
config: JobSpec,
callbacks: JobCallbacks,
garak_config_dict: dict,
scan_dir: Path,
timeout: int,
) -> GarakScanResult:
"""Run garak as a local subprocess."""
log_file = scan_dir / "scan.log"
report_prefix = scan_dir / "scan"
config_file = scan_dir / "config.json"
with open(config_file, "w") as f:
json.dump(garak_config_dict, f, indent=1)
callbacks.report_status(
JobStatusUpdate(
status=JobStatus.RUNNING,
phase=JobPhase.RUNNING_EVALUATION,
progress=0.1,
message=MessageInfo(
message=f"Running Garak scan for {config.benchmark_id}",
message_code="running_scan",
),
current_step="Executing probes",
)
)
result = run_garak_scan(
config_file=config_file,
timeout_seconds=timeout,
log_file=log_file,
report_prefix=report_prefix,
)
# AVID conversion
if result.success:
callbacks.report_status(
JobStatusUpdate(
status=JobStatus.RUNNING,
phase=JobPhase.POST_PROCESSING,
progress=0.7,
message=MessageInfo(
message="Converting results to AVID format",
message_code="post_processing",
),
)
)
convert_to_avid_report(result.report_jsonl)
return result
# ------------------------------------------------------------------
# KFP execution
# ------------------------------------------------------------------
def _run_via_kfp(
self,
config: JobSpec,
callbacks: JobCallbacks,
garak_config_dict: dict,
timeout: int,
intents_params: dict[str, Any] | None = None,
eval_threshold: float = DEFAULT_EVAL_THRESHOLD,
) -> tuple[GarakScanResult, Path]:
"""Submit a KFP pipeline, poll until done, download results from S3.
The KFP component uploads scan output to S3 under
``{kfp_config.s3_prefix}/{job_id}/``. After the run completes,
this method downloads those files to a local scan_dir for parsing.
Both sides get S3 credentials from the same Data Connection secret:
- KFP pod: injected via ``kubernetes.use_secret_as_env``
- Adapter pod: mounted via ``envFrom`` by the eval-hub service
Returns:
Tuple of (GarakScanResult, local scan_dir with downloaded results).
"""
from .kfp_pipeline import KFPConfig, evalhub_garak_pipeline
benchmark_config = config.parameters or {}
kfp_config = KFPConfig.from_env_and_config(benchmark_config)
if not kfp_config.s3_secret_name:
raise ValueError(
"S3 data-connection secret name is required for KFP mode. "
"Set KFP_S3_SECRET_NAME or provide "
"kfp_config.s3_secret_name in benchmark_config."
)
s3_prefix = f"{kfp_config.s3_prefix}/{config.id}"
scan_dir = get_scan_base_dir() / config.id
scan_dir.mkdir(parents=True, exist_ok=True)
from ..core.pipeline_steps import redact_api_keys
sanitised_config = redact_api_keys(garak_config_dict)
config_json = json.dumps(sanitised_config)
ip = intents_params or {}
if ip.get("art_intents"):
if ip.get("policy_s3_key") and ip.get("intents_s3_key"):
raise ValueError(
"policy_s3_key and intents_s3_key are mutually exclusive. "
"Provide a taxonomy for SDG (policy_s3_key) OR "
"pre-generated prompts to bypass SDG (intents_s3_key), not both."
)
if not ip.get("intents_s3_key"):
if not ip.get("sdg_model"):
raise ValueError(
"Intents benchmark (art_intents=True) requires "
"sdg_model for prompt generation when intents_s3_key "
"is not provided."
)
if not ip.get("sdg_api_base"):
raise ValueError(
"Intents benchmark (art_intents=True) requires "
"sdg_api_base for prompt generation when intents_s3_key "
"is not provided."
)
callbacks.report_status(
JobStatusUpdate(
status=JobStatus.RUNNING,
phase=JobPhase.RUNNING_EVALUATION,
progress=0.1,
message=MessageInfo(
message=f"Submitting KFP pipeline for {config.benchmark_id}",
message_code="kfp_submitting",
),
current_step="Submitting to Kubeflow Pipelines",
)
)
# Resolve model auth secret from EvalHub SDK model.auth.secret_ref
# Falls back to pipeline default ("model-auth") when not specified.
model_auth_secret = ""
try:
model_auth = getattr(config.model, "auth", None)
if model_auth:
model_auth_secret = getattr(model_auth, "secret_ref", "") or ""
except Exception:
pass
if model_auth_secret:
logger.info("Using model auth secret: %s", model_auth_secret)
kfp_client = self._create_kfp_client(kfp_config)
pipeline_args: dict[str, Any] = {
"config_json": config_json,
"s3_prefix": s3_prefix,
"timeout_seconds": timeout,
"s3_secret_name": kfp_config.s3_secret_name,
"eval_threshold": eval_threshold,
"art_intents": ip.get("art_intents", False),
"policy_s3_key": ip.get("policy_s3_key", ""),
"policy_format": ip.get("policy_format", "csv"),
"intents_s3_key": ip.get("intents_s3_key", ""),
"intents_format": ip.get("intents_format", "csv"),
"sdg_model": ip.get("sdg_model", ""),
"sdg_api_base": ip.get("sdg_api_base", ""),
"sdg_flow_id": ip.get("sdg_flow_id", DEFAULT_SDG_FLOW_ID),
}
if model_auth_secret:
pipeline_args["model_auth_secret_name"] = model_auth_secret
disable_cache = as_bool(ip.get("disable_cache", False))
run = kfp_client.create_run_from_pipeline_func(
evalhub_garak_pipeline,
arguments=pipeline_args,
run_name=f"evalhub-garak-{config.id}",
namespace=kfp_config.namespace,
experiment_name=kfp_config.experiment_name,
enable_caching=not disable_cache,
)
kfp_run_id = run.run_id
logger.info("Submitted KFP run %s", kfp_run_id)
poll_timeout = int(timeout * 2) if timeout > 0 else 0
final_state = self._poll_kfp_run(
kfp_client,
kfp_run_id,
callbacks,
kfp_config.poll_interval_seconds,
timeout=poll_timeout,
)
timed_out = final_state == "TIMED_OUT"
success = final_state == "SUCCEEDED"
if success:
callbacks.report_status(
JobStatusUpdate(
status=JobStatus.RUNNING,
phase=JobPhase.POST_PROCESSING,
progress=0.7,
message=MessageInfo(
message="Downloading scan results from S3",
message_code="downloading_results",
),
)
)
s3_bucket = kfp_config.s3_bucket or os.getenv("AWS_S3_BUCKET", "")
creds = (
self._read_s3_credentials_from_secret(
kfp_config.s3_secret_name,
kfp_config.namespace,
)
if kfp_config.s3_secret_name
else {}
)
if kfp_config.s3_secret_name and not creds:
logger.warning(
"S3 credentials from secret '%s/%s' are empty. "
"Ensure the secret exists and the Job pod's service account "
"has RBAC permissions to read secrets in namespace '%s'. "
"Falling back to environment variables for S3 access."
"If no environment variables are set, the job will fail",
"as it will not be able to interact with S3.",
kfp_config.namespace,
kfp_config.s3_secret_name,
kfp_config.namespace,
)
self._download_results_from_s3(
s3_bucket,
s3_prefix,
scan_dir,
endpoint_url=kfp_config.s3_endpoint or None,
**creds,
)
report_prefix = scan_dir / "scan"
return GarakScanResult(
returncode=0 if success else 1,
stdout="",
stderr="" if success else f"KFP run ended with state: {final_state}",
report_prefix=report_prefix,
timed_out=timed_out,
), scan_dir
@staticmethod
def _create_kfp_client(kfp_config: "KFPConfig"):
"""Create a KFP Client from KFPConfig."""
from kfp import Client
ssl_ca_cert = kfp_config.ssl_ca_cert or None
token = kfp_config.auth_token or None
# If no explicit token, try to get one from the cluster service account
if not token:
sa_token_path = Path("/var/run/secrets/kubernetes.io/serviceaccount/token")
if sa_token_path.exists():
token = sa_token_path.read_text().strip()
logger.debug("Using service account token for KFP auth")
return Client(
host=kfp_config.endpoint,
existing_token=token,
verify_ssl=kfp_config.verify_ssl,
ssl_ca_cert=ssl_ca_cert,
)
@staticmethod
def _poll_kfp_run(
kfp_client,
run_id: str,
callbacks: JobCallbacks,
poll_interval: int,
timeout: int = 0,
) -> str:
"""Poll a KFP run until it reaches a terminal state or times out.
Relays progress updates to the eval-hub sidecar via callbacks.
Args:
timeout: Maximum wall-clock seconds to wait. 0 means no limit.
Returns:
Final run state string (e.g. "SUCCEEDED", "FAILED", "TIMED_OUT").
"""
terminal_states = {"SUCCEEDED", "FAILED", "SKIPPED", "CANCELED", "CANCELING"}
deadline = (time.monotonic() + timeout) if timeout > 0 else None
while True:
run = kfp_client.get_run(run_id)
state = run.state or "UNKNOWN"
logger.info("KFP run %s state: %s", run_id, state)
if state in terminal_states:
return state
if deadline and time.monotonic() >= deadline:
logger.error(
"KFP run %s timed out after %ss (last state: %s)",
run_id,
timeout,
state,
)
return "TIMED_OUT"
callbacks.report_status(
JobStatusUpdate(
status=JobStatus.RUNNING,
phase=JobPhase.RUNNING_EVALUATION,
progress=0.3,
message=MessageInfo(
message=f"KFP pipeline running (state: {state})",
message_code="kfp_running",
),
current_step=f"KFP state: {state}",
)
)
time.sleep(poll_interval)
@staticmethod
def _read_s3_credentials_from_secret(secret_name: str, namespace: str) -> dict:
"""Read S3 credentials from a Kubernetes secret.
Falls back gracefully if the secret cannot be read (e.g. outside a
cluster or missing RBAC), returning an empty dict so env-var fallback
in ``create_s3_client`` still applies.
.. note:: **RBAC requirement** — The Job pod's service account must
have ``get`` permission on Secrets in the target namespace.
Example Role/RoleBinding::
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: secret-reader
namespace: <namespace>
rules:
- apiGroups: [""]
resources: ["secrets"]
verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: evalhub-secret-reader
namespace: <namespace>
subjects:
- kind: ServiceAccount
name: <job-service-account>
namespace: <namespace>
roleRef:
kind: Role
name: secret-reader
apiGroup: rbac.authorization.k8s.io
Without this, the call returns an empty dict and S3 operations
fall back to environment variables (which may also be empty,
causing job failures).
"""
import base64
try:
from kubernetes import client as k8s_client, config as k8s_config
try:
k8s_config.load_incluster_config()
except Exception:
k8s_config.load_kube_config()
v1 = k8s_client.CoreV1Api()
secret = v1.read_namespaced_secret(secret_name, namespace)
data = secret.data or {}
def _decode(key: str) -> str:
val = data.get(key, "")
return base64.b64decode(val).decode() if val else ""
return {
"access_key": _decode("AWS_ACCESS_KEY_ID"),
"secret_key": _decode("AWS_SECRET_ACCESS_KEY"),
"region": _decode("AWS_DEFAULT_REGION"),
}
except Exception as exc:
logger.warning("Could not read S3 credentials from secret %s/%s: %s", namespace, secret_name, exc)
return {}
@staticmethod
def _create_s3_client(
endpoint_url: str | None = None,
access_key: str | None = None,
secret_key: str | None = None,
region: str | None = None,
):
"""Create a boto3 S3 client.
Explicit parameters take precedence over environment variables, which
allows the adapter pod to supply credentials read from a k8s secret
when they are not present in its own environment.
"""
from .s3_utils import create_s3_client
return create_s3_client(
endpoint_url=endpoint_url,
access_key=access_key,
secret_key=secret_key,
region=region,
)
@staticmethod
def _download_results_from_s3(
bucket: str,
prefix: str,
local_dir: Path,
endpoint_url: str | None = None,
access_key: str | None = None,
secret_key: str | None = None,
region: str | None = None,
) -> None:
"""Download all scan result files from S3 to a local directory.
Uses pagination to handle prefixes with many objects.
Explicit credential parameters take precedence over environment variables.
"""
if not bucket:
logger.warning("No S3 bucket configured; skipping result download")
return
s3 = GarakAdapter._create_s3_client(
endpoint_url=endpoint_url,
access_key=access_key,
secret_key=secret_key,
region=region,
)
paginator = s3.get_paginator("list_objects_v2")
downloaded = 0
for page in paginator.paginate(Bucket=bucket, Prefix=prefix):
for obj in page.get("Contents", []):
key = obj["Key"]
relative = key[len(prefix) :].lstrip("/")
if not relative:
continue
local_path = local_dir / relative
local_path.parent.mkdir(parents=True, exist_ok=True)
s3.download_file(bucket, key, str(local_path))
downloaded += 1
logger.info(
"Downloaded %d files from s3://%s/%s to %s",
downloaded,
bucket,
prefix,
local_dir,
)
def _resolve_profile(self, benchmark_id: str) -> dict:
"""Resolve a benchmark_id to a GarakScanConfig profile dict.
Checks both FRAMEWORK_PROFILES and SCAN_PROFILES, with and without
the 'trustyai_garak::' prefix, so that bare IDs like 'owasp_llm_top10'
and fully-qualified IDs like 'trustyai_garak::owasp_llm_top10' both work.
"""
return resolve_scan_profile(benchmark_id)
def _validate_config(self, config: JobSpec) -> None:
"""Validate job configuration."""
if not config.benchmark_id:
raise ValueError("benchmark_id is required")
if not config.model.url:
raise ValueError("model.url is required")
if not config.model.name:
raise ValueError("model.name is required")
profile = self._resolve_profile(config.benchmark_id)
benchmark_config = config.parameters or {}
explicit_garak_cfg = benchmark_config.get("garak_config", {})
if not isinstance(explicit_garak_cfg, dict):
explicit_garak_cfg = {}
explicit_probes = benchmark_config.get("probes") or explicit_garak_cfg.get("plugins", {}).get("probe_spec")
explicit_tags = benchmark_config.get("probe_tags") or explicit_garak_cfg.get("run", {}).get("probe_tags")
if not explicit_probes and not explicit_tags and not profile:
logger.warning(
"benchmark_id '%s' does not match a known profile and no probes or "
"probe_tags provided in parameters — all probes will run",
config.benchmark_id,
)
logger.debug("Configuration validated successfully")
def _build_config_from_spec(
self, config: JobSpec, report_prefix: Path
) -> tuple[dict[str, Any], dict[str, Any], dict[str, Any]]:
"""Build Garak command config dict from JobSpec.
Returns:
Tuple of (garak_config_dict, profile, intents_params) where
intents_params contains art_intents and related settings
extracted from the profile and benchmark_config.
"""
benchmark_config = config.parameters or {}
profile = self._resolve_profile(config.benchmark_id)
garak_config: GarakCommandConfig = build_effective_garak_config(
benchmark_config=benchmark_config,
profile=profile,
)
if profile:
logger.info(
"Resolved benchmark_id '%s' to profile '%s'",
config.benchmark_id,
profile.get("name"),
)
model_params = benchmark_config.get("model_parameters") or {}
model_type = benchmark_config.get("model_type", DEFAULT_MODEL_TYPE)
# set generators if not already set by user (because there's a eval-hub config.model for this)
if model_type == DEFAULT_MODEL_TYPE and not garak_config.plugins.generators:
from evalhub.adapter.auth import read_model_auth_key
api_key = (
getattr(config.model, "api_key", None)
or read_model_auth_key("api-key")
or os.getenv("OPENAICOMPATIBLE_API_KEY")
or "DUMMY"
)
garak_config.plugins.generators = build_generator_options(
model_endpoint=self._normalize_url(config.model.url),
model_name=config.model.name,
api_key=api_key,
extra_params=model_params,
)
garak_config.plugins.target_type = model_type
garak_config.plugins.target_name = config.model.name
garak_config.reporting.report_prefix = str(report_prefix)
art_intents = (
bool(benchmark_config.get("art_intents"))
if "art_intents" in benchmark_config
else bool(profile.get("art_intents", False))
)
intents_params: dict[str, Any] = {
"art_intents": art_intents,
"policy_s3_key": benchmark_config.get("policy_s3_key", profile.get("policy_s3_key", "")),
"policy_format": benchmark_config.get("policy_format", profile.get("policy_format", "csv")),
"intents_s3_key": benchmark_config.get("intents_s3_key", profile.get("intents_s3_key", "")),
"intents_format": benchmark_config.get("intents_format", profile.get("intents_format", "csv")),
"sdg_flow_id": benchmark_config.get("sdg_flow_id", profile.get("sdg_flow_id", DEFAULT_SDG_FLOW_ID)),
"disable_cache": as_bool(benchmark_config.get("disable_cache", False)),
}
if art_intents:
sdg_params = self._apply_intents_model_config(garak_config, benchmark_config, profile)
intents_params.update(sdg_params)
return garak_config.to_dict(exclude_none=True), profile, intents_params
# ------------------------------------------------------------------
# Intents model configuration
# ------------------------------------------------------------------
def _apply_intents_model_config(
self,
garak_config: GarakCommandConfig,
benchmark_config: dict,
profile: dict,
) -> dict[str, Any]:
"""Configure judge/attacker/evaluator/SDG models from ``intents_models``.
Users provide per-role model endpoints in ``benchmark_config``:
.. code-block:: json
{
"intents_models": {
"judge": {"url": "...", "name": "..."},
"attacker": {"url": "...", "name": "..."},
"evaluator": {"url": "...", "name": "..."},
"sdg": {"url": "...", "name": "..."}
}
}
Exactly 1 or all 3 of judge/attacker/evaluator must have a ``url``:
- **1 provided**: the configured role is used for all three.
- **3 provided**: each role uses its own config.
- **0 provided, but models pre-configured in garak_config**: the
override is skipped and models from garak_config are used as-is.
SDG params are extracted from flat keys (``sdg_model``,
``sdg_api_base``).
- **0 provided, no garak_config models**: raises ``ValueError``.
- **2 provided**: raises ``ValueError`` — ambiguous which should fill
the missing role.
``sdg`` has no fallback — it must be provided explicitly if SDG
generation is needed.
API keys are **no longer** embedded in the config dict. They are
injected at pod level via a Kubernetes Secret
(``model_auth_secret_name``). Placeholder values (``__FROM_ENV__``)
are written into the config and resolved inside the KFP pod by
``core.pipeline_steps._resolve_config_api_keys``.
Returns:
Dict with SDG-related keys (``sdg_model``, ``sdg_api_base``)
extracted from the ``sdg`` role, or empty strings if SDG is
not configured.
"""
intents_models = benchmark_config.get("intents_models", {})
if not isinstance(intents_models, dict):
intents_models = {}
judge_cfg = intents_models.get("judge") or {}