Test Google Cloud Model Armor Security
Promptfoo example testing Google Cloud Model Armor's content-screening filters directly and via Vertex AI integration.
Why it matters
Leverage Promptfoo to rigorously test and verify the security capabilities of Google Cloud Model Armor. Ensure your AI models are protected against adversarial attacks and unauthorized access.
Outcomes
What it gets done
Automate security testing for Google Cloud Model Armor.
Verify model resilience against common attack vectors.
Generate reports on security vulnerabilities and access controls.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/pfoo-provider-model-armor | bash Steps
Steps in the chain
Overview
Provider Model Armor
A promptfoo example testing Google Cloud Model Armor's content-safety filters, both directly via its sanitization API and natively through Vertex AI, using guardrails assertions. Use when deploying LLM applications on Google Cloud and you need to verify Model Armor's safety filters catch adversarial prompts before shipping.
What it does
This directory contains examples for testing Google Cloud Model Armor with promptfoo. Model Armor is a managed service that screens LLM prompts and responses for Responsible AI issues (hate speech, harassment, sexually explicit, dangerous content), CSAM (child safety content detection, always enabled), prompt injection and jailbreak attempts, malicious URLs (phishing/threat detection), and Sensitive Data Protection (credit cards, SSNs, API keys, etc.). Two example configs are provided: direct Model Armor API testing, which calls the sanitizeUserPrompt API directly, maps filter results into promptfoo's guardrails format, and tests both benign and adversarial prompts; and Vertex AI integration testing, which exercises Gemini models with Model Armor templates natively enabled, comparing model behavior with and without Model Armor and using the guardrails/not-guardrails assertion types.
When to use - and when NOT to
Use the direct API config (promptfooconfig.yaml) when you want detailed per-filter results from Model Armor's sanitization API; use the Vertex AI config (promptfooconfig.vertex.yaml) for production-like testing of Gemini models with Model Armor enabled end to end. It is not a general content-moderation tutorial - it is specific to Google Cloud Model Armor's filter types and its promptfoo guardrails integration.
Inputs and outputs
Setup requires enabling the Model Armor API:
gcloud services enable modelarmor.googleapis.com --project=YOUR_PROJECT_ID
then granting the Vertex AI service account the roles/modelarmor.user IAM role, setting the regional API endpoint override for direct API testing, and creating a Model Armor template (e.g. basic-safety) with RAI filters, prompt-injection/jailbreak detection, and malicious-URL detection enabled. For direct API testing, set GOOGLE_PROJECT_ID, MODEL_ARMOR_LOCATION, MODEL_ARMOR_TEMPLATE, and a short-lived GCLOUD_ACCESS_TOKEN (expires after 1 hour; use service account keys or Workload Identity Federation for CI/CD). The example ships promptfooconfig.yaml, promptfooconfig.vertex.yaml, a transforms/sanitize-response.js response transformer, and a datasets/model-armor-test.csv dataset of prompts per filter type, loadable via tests: file://datasets/model-armor-test.csv.
Integrations
Integrates Google Cloud Model Armor both as a direct HTTP-called API and as a native Vertex AI safety layer in front of Gemini models. Results surface as guardrails.flagged, guardrails.flaggedInput, guardrails.flaggedOutput, and a guardrails.reason explaining which filters matched; the raw response is inspectable in metadata.modelArmor.sanitizationResult for per-filter states and confidence levels. The not-guardrails assertion verifies dangerous prompts get caught - the test passes when content is blocked and fails when it slips through. Templates can be removed afterward with gcloud model-armor templates delete basic-safety --location=us-central1.
Who it's for
Teams deploying LLM applications on Google Cloud who need to verify that Model Armor's safety, prompt-injection, malicious-URL, and sensitive-data filters actually catch adversarial inputs before shipping - whether calling the sanitization API directly or through Vertex AI's native integration.
Source README
provider-model-armor (Google Cloud Model Armor)
This directory contains examples for testing Google Cloud Model Armor with Promptfoo.
You can run this example with:
npx promptfoo@latest init --example provider-model-armor
cd provider-model-armor
Model Armor is a managed service that screens LLM prompts and responses for:
- Responsible AI (RAI): Hate speech, harassment, sexually explicit, dangerous content
- CSAM: Child safety content detection (always enabled)
- Prompt Injection & Jailbreak: Detects manipulation attempts
- Malicious URLs: Phishing and threat detection
- Sensitive Data Protection (SDP): Credit cards, SSNs, API keys, etc.
Prerequisites
Enable Model Armor API:
gcloud services enable modelarmor.googleapis.com --project=YOUR_PROJECT_IDGrant IAM Permissions (for Vertex AI integration):
PROJECT_NUMBER=$(gcloud projects describe YOUR_PROJECT_ID --format="value(projectNumber)") gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \ --member="serviceAccount:service-${PROJECT_NUMBER}@gcp-sa-aiplatform.iam.gserviceaccount.com" \ --role="roles/modelarmor.user"Set the regional API endpoint (for direct API testing):
gcloud config set api_endpoint_overrides/modelarmor \ "https://modelarmor.us-central1.rep.googleapis.com/"Create a Model Armor template:
gcloud model-armor templates create basic-safety \ --location=us-central1 \ --rai-settings-filters='[{"filterType":"HATE_SPEECH","confidenceLevel":"MEDIUM_AND_ABOVE"},{"filterType":"HARASSMENT","confidenceLevel":"MEDIUM_AND_ABOVE"},{"filterType":"DANGEROUS","confidenceLevel":"MEDIUM_AND_ABOVE"},{"filterType":"SEXUALLY_EXPLICIT","confidenceLevel":"MEDIUM_AND_ABOVE"}]' \ --pi-and-jailbreak-filter-settings-enforcement=enabled \ --pi-and-jailbreak-filter-settings-confidence-level=medium-and-above \ --malicious-uri-filter-settings-enforcement=enabled \ --basic-config-filter-enforcement=enabledSet environment variables (for direct API testing):
export GOOGLE_PROJECT_ID=your-project-id export MODEL_ARMOR_LOCATION=us-central1 export MODEL_ARMOR_TEMPLATE=basic-safety export GCLOUD_ACCESS_TOKEN=$(gcloud auth print-access-token)Note: Access tokens expire after 1 hour. For CI/CD, use service account keys or Workload Identity Federation.
Examples
1. Direct Model Armor API Testing
Test Model Armor's sanitization API directly using the HTTP provider:
promptfoo eval -c promptfooconfig.yaml
This example:
- Calls the
sanitizeUserPromptAPI directly - Maps filter results to Promptfoo's guardrails format
- Tests both benign and adversarial prompts
2. Vertex AI with Model Armor Integration
Test Gemini models with Model Armor templates:
promptfoo eval -c promptfooconfig.vertex.yaml
This example:
- Uses Vertex AI's native Model Armor integration
- Compares models with and without Model Armor enabled
- Uses the
guardrailsandnot-guardrailsassertion types
Configuration Files
promptfooconfig.yaml- Direct Model Armor API testing (recommended for detailed filter results)promptfooconfig.vertex.yaml- Vertex AI integration with Model Armor (recommended for production-like testing)transforms/sanitize-response.js- Response transformer for the sanitization APIdatasets/model-armor-test.csv- Test dataset with prompts for each filter type
Using the Dataset
The included CSV dataset contains test prompts for each Model Armor filter type. Load it in your config:
tests: file://datasets/model-armor-test.csv
Each row includes a prompt and expected behavior (benign vs. adversarial).
Understanding Results
When Model Armor blocks content, you'll see:
guardrails.flagged: true- Content was flaggedguardrails.flaggedInput: true- The input prompt was blockedguardrails.flaggedOutput: true- The generated response was blockedguardrails.reason- Detailed explanation of which filters matched
For debugging, inspect the raw Model Armor response in metadata.modelArmor, which contains the full sanitizationResult including individual filter states and confidence levels.
Use not-guardrails to verify dangerous prompts get caught - the test passes when content is blocked, fails when it slips through.
Cleanup
After testing, you can delete the Model Armor template if no longer needed:
gcloud model-armor templates delete basic-safety --location=us-central1
Learn More
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.