MLSecOps: Operating AI Systems Like DevSecOps
In Part 2, we covered model vulnerability scanning and supply chain security. Completing MLSecOps means automatically integrating all of these security controls into the CI/CD pipeline.
Automating AI Pipeline Security
# .github/workflows/ml-security.yml
name: ML Security Gate
on:
push:
paths:
- "models/**"
- "training/**"
- "inference/**"
jobs:
security-scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name:
run: python scripts/verify_model_hashes.py
- name: PII
run: python scripts/scan_training_data.py --threshold 0
- name:
run: |
pip-audit --requirement requirements.txt
safety check
- name:
run: python scripts/bias_eval.py --fail-threshold 0.15
- name:
run: python scripts/adversarial_test.py --attack pgd --eps 0.03
- name:
run: python scripts/generate_security_report.py
if: always()Automatic Model Bias Detection
import numpy as np
from itertools import combinations
class BiasDetector:
""" """
PROTECTED_ATTRS = ["gender", "age_group", "nationality", "disability"]
def evaluate(self, predictions: list, labels: list, metadata: list) -> dict:
results = {}
for attr in self.PROTECTED_ATTRS:
groups = self._group_by(predictions, labels, metadata, attr)
if len(groups) < 2:
continue
results[attr] = self._compute_metrics(groups)
return results
def _compute_metrics(self, groups: dict) -> dict:
accuracies = {g: self._accuracy(p, l) for g, (p, l) in groups.items()}
tprs = {g: self._tpr(p, l) for g, (p, l) in groups.items()}
max_acc_gap = max(accuracies.values()) - min(accuracies.values())
max_tpr_gap = max(tprs.values()) - min(tprs.values())
return {
"accuracy_by_group": accuracies,
"tpr_by_group": tprs,
"max_accuracy_gap": max_acc_gap,
"max_tpr_gap": max_tpr_gap,
"bias_detected": max_acc_gap > 0.1 or max_tpr_gap > 0.1,
}Federated Learning Security
Even when you share only model parameters and never move the data, you still need security.
import numpy as np
class SecureAggregator:
""" when + """
def __init__(self, epsilon: float = 1.0, delta: float = 1e-5):
self.epsilon = epsilon # when
self.delta = delta
def add_noise(self, gradient: np.ndarray, sensitivity: float) -> np.ndarray:
"""when """
sigma = sensitivity * np.sqrt(2 * np.log(1.25 / self.delta)) / self.epsilon
noise = np.random.normal(0, sigma, gradient.shape)
return gradient + noise
def aggregate(self, client_gradients: list[np.ndarray]) -> np.ndarray:
""" """
clipped = []
for g in client_gradients:
norm = np.linalg.norm(g)
clipped.append(g / max(1.0, norm)) # L2
avg = np.mean(clipped, axis=0)
return self.add_noise(avg, sensitivity=1.0 / len(client_gradients))Model Explainability (XAI) Compliance
The EU AI Act mandates explainability for high-risk AI systems.
import shap
class ExplainabilityLayer:
def __init__(self, model, background_data):
self.explainer = shap.Explainer(model, background_data)
def explain(self, input_data) -> dict:
shap_values = self.explainer(input_data)
feature_importance = dict(zip(
input_data.columns,
np.abs(shap_values.values).mean(axis=0)
))
top_features = sorted(
feature_importance.items(),
key=lambda x: x[1],
reverse=True
)[:5]
return {
"prediction": float(self.model.predict(input_data)[0]),
"confidence": float(self.model.predict_proba(input_data).max()),
"top_factors": [
{"feature": f, "importance": float(v), "direction": "increases" if shap_values[0][f] > 0 else "decreases"}
for f, v in top_features
],
"audit_record": self._log(input_data, shap_values),
}AI Governance Maturity Checklist
(Security)
☑
☑ PII
☑
(Compliance)
☑ GDPR
☑ AI Act
☑ API
(Operations)
☑ CI/CD
☑ when
☑ whenThis three-part series completes the AI Governance & MLSecOps Master Guide (governance fundamentals → security deep-dive → operations automation).
Sources and checks vary by article. Only articles with a recorded check date, scope and sources below were checked, and only within that scope. For other articles, verify against the current docs for your version before applying anything, and email us about errors. Editorial policy · email us
This article has no recorded human check (date and scope) yet.
Comments
Be the first to comment.