Skip to content

Protect a Google Cloud install with Cloud Armor

Cloud Armor is Google’s web application firewall for the global HTTPS load balancer. One policy in front of an Authwise install blocks the scanner traffic every public host receives, rate-limits the sign-in and token paths per client, and absorbs layer 7 floods. This page sets it up the way Authwise runs it: every rule in preview first, a look at what preview would have blocked, then one variable to enforce.

  • An install behind Google’s HTTPS load balancer. Cloud Armor attaches to a backend service of that load balancer, which a GKE Gateway of class gke-l7-global-external-managed or a GKE Ingress of class gce creates. An ingress controller such as ingress-nginx sits behind a passthrough Network Load Balancer and gets no Cloud Armor policy. The Gateway shape is the one in Expose part of the Admin API.
  • Terraform with the Google provider, and a workspace that already holds the install’s project.
  • Load balancer logging on, so the preview period has something to read. The backend policy below turns it on.

One policy serves every install’s Gateway in the project. It holds three kinds of rule. The rate limits come first by priority, then the OWASP rule sets at their lowest sensitivity, then a default allow. The enforce variable decides whether the rules act or only log.

variable "armor_enforce" {
type = bool
description = "false = every rule in preview: logged, not enforced."
default = false
}
locals {
# Google's preconfigured OWASP rule sets, pinned to a stable version.
armor_waf_rules = {
1000 = "sqli-v33-stable"
1010 = "xss-v33-stable"
1020 = "lfi-v33-stable"
1030 = "rfi-v33-stable"
1040 = "rce-v33-stable"
1050 = "methodenforcement-v33-stable"
1060 = "scannerdetection-v33-stable"
1070 = "protocolattack-v33-stable"
1080 = "sessionfixation-v33-stable"
}
# Per client address, per minute.
armor_rate_limits = {
100 = { path = "request.path == '/oauth/token'", count = 300 }
110 = { path = "request.path == '/authorize'", count = 120 }
120 = { path = "request.path.startsWith('/api/v1/')", count = 600 }
}
}
resource "google_compute_security_policy" "edge" {
project = var.project
name = "authwise-edge"
description = "WAF and rate limits for the Authwise Gateways."
type = "CLOUD_ARMOR"
adaptive_protection_config {
layer_7_ddos_defense_config {
enable = true
}
}
advanced_options_config {
log_level = "NORMAL"
}
dynamic "rule" {
for_each = local.armor_rate_limits
content {
priority = rule.key
action = "throttle"
preview = !var.armor_enforce
description = "rate limit: ${rule.value.path}"
match {
expr {
expression = rule.value.path
}
}
rate_limit_options {
conform_action = "allow"
exceed_action = "deny(429)"
enforce_on_key = "IP"
rate_limit_threshold {
count = rule.value.count
interval_sec = 60
}
}
}
}
dynamic "rule" {
for_each = local.armor_waf_rules
content {
priority = rule.key
action = "deny(403)"
preview = !var.armor_enforce
description = "OWASP: ${rule.value}"
match {
expr {
expression = "evaluatePreconfiguredWaf('${rule.value}', {'sensitivity': 1})"
}
}
}
}
rule {
priority = 2147483647
action = "allow"
description = "default allow"
match {
versioned_expr = "SRC_IPS_V1"
config {
src_ip_ranges = ["*"]
}
}
}
}

Sensitivity 1 is the lowest: only the signatures with the fewest false positives. Authwise’s sign-in forms, JSON APIs, and SAML responses pass it. Raise it only after a preview period at the new level.

The rate limits count requests per client address per minute. The sign-in redirect, /authorize, gets the tightest limit because a client in a redirect loop hits it fastest. A large office behind one address signs in at well under 120 per minute; raise the count if your preview logs show otherwise.

With a Gateway, a GCPBackendPolicy per public Service puts the policy on that Service’s backend and turns on request logging. Add one for each Service a public route names, in the install’s namespace:

apiVersion: networking.gke.io/v1
kind: GCPBackendPolicy
metadata:
name: authwise-engine
namespace: NAMESPACE
spec:
default:
securityPolicy: authwise-edge
logging:
enabled: true
sampleRate: 1000000
targetRef:
group: ""
kind: Service
name: authwise-engine

With an Ingress of class gce, the equivalent is a BackendConfig with securityPolicy.name and logging, referenced from each Service through the cloud.google.com/backend-config annotation.

A spike in denials means an attack or a false positive, and you want to know either way. One log-based metric counts denials, enforced or previewed, and one alert fires on a spike:

resource "google_logging_metric" "armor_denied" {
project = var.project
name = "armor_denied"
filter = "resource.type=\"http_load_balancer\" AND (jsonPayload.enforcedSecurityPolicy.outcome=\"DENY\" OR jsonPayload.previewSecurityPolicy.outcome=\"DENY\")"
metric_descriptor {
metric_kind = "DELTA"
value_type = "INT64"
}
}
resource "google_monitoring_alert_policy" "armor_spike" {
project = var.project
display_name = "Cloud Armor denials spiking"
combiner = "OR"
conditions {
display_name = "> 100 denials in 5 minutes"
condition_threshold {
filter = "metric.type=\"logging.googleapis.com/user/${google_logging_metric.armor_denied.name}\" AND resource.type=\"l7_lb_rule\""
comparison = "COMPARISON_GT"
threshold_value = 100
duration = "0s"
aggregations {
alignment_period = "300s"
per_series_aligner = "ALIGN_SUM"
cross_series_reducer = "REDUCE_SUM"
}
}
}
notification_channels = [NOTIFICATION_CHANNEL_ID]
}

Apply with armor_enforce = false and leave it for two weeks of real traffic. In preview every rule logs the decision it would have made and lets the request through. Then ask three questions of the logs. Replace PROJECT_ID with the project that holds the load balancer.

What would have been blocked, by rule and path?

Terminal window
gcloud logging read \
'resource.type="http_load_balancer" AND jsonPayload.previewSecurityPolicy.outcome="DENY"' \
--project PROJECT_ID --freshness=14d --limit=5000 \
--format="value(jsonPayload.previewSecurityPolicy.priority,httpRequest.requestMethod,httpRequest.requestUrl)" \
| sed -E 's#https?://([^/]+)(/[^? ]*).*#\1\2#' | sort | uniq -c | sort -rn | head -40

Expect scanner probes: /.env, /.git/config, /proc/self/environ, wp-config.php, and POSTs with payloads to / and /login. Those are the rules working.

Did any rule match a legitimate path?

Terminal window
gcloud logging read \
'resource.type="http_load_balancer" AND jsonPayload.previewSecurityPolicy.outcome="DENY" AND (httpRequest.requestUrl:"/authorize" OR httpRequest.requestUrl:"/oauth/" OR httpRequest.requestUrl:"/api/v1/" OR httpRequest.requestUrl:"/v1/" OR httpRequest.requestUrl:"/account" OR httpRequest.requestUrl:"/saml")' \
--project PROJECT_ID --freshness=14d --limit=500 \
--format="value(jsonPayload.previewSecurityPolicy.priority,httpRequest.requestMethod,httpRequest.requestUrl,httpRequest.status)"

A match here with a 200 or 302 status from the backend is a request that worked and would be blocked after enforcement. Look at it before you enforce: a sign-in that carries an unusual character in a field, or an API client sending something a rule reads as an attack. Raise the rule’s sensitivity threshold, exclude the path, or fix the client. A match with a 404, 400, or 405 is a probe that happened to use a real path name.

Did a rate limit trip, and on whom?

Terminal window
gcloud logging read \
'resource.type="http_load_balancer" AND jsonPayload.previewSecurityPolicy.outcome="DENY" AND (jsonPayload.previewSecurityPolicy.priority=100 OR jsonPayload.previewSecurityPolicy.priority=110 OR jsonPayload.previewSecurityPolicy.priority=120)' \
--project PROJECT_ID --freshness=14d --limit=5000 \
--format="value(timestamp.date('%Y-%m-%d'),jsonPayload.previewSecurityPolicy.priority,httpRequest.remoteIp)" \
| sort | uniq -c

A client that trips /authorize is usually an application in a redirect loop, and a 429 is the right answer for it. A client that trips the API limit steadily is an integration to look at, or a count to raise.

For comparison, Authwise’s own production estate after two weeks of preview: more than 5,000 would-be denials, every one a scanner probe; no rule matched a legitimate path; one client tripped the /authorize limit for one minute while looping. The policy was enforced as configured.

Set armor_enforce = true, plan, and apply. The plan changes one field, preview, on each rule and nothing else. Then check three things:

Terminal window
curl -si https://CONSOLE_HOST/.env | head -1
curl -si https://ADMIN_HOST/.well-known/openid-configuration | head -1

The first answers 403 where it answered 404 or 200 before. The second still answers 200. Then sign in to the console once, and confirm that denials appear as enforced within the hour, since scanners never stop:

Terminal window
gcloud logging read \
'resource.type="http_load_balancer" AND jsonPayload.enforcedSecurityPolicy.outcome="DENY"' \
--project PROJECT_ID --freshness=1h --limit=5 \
--format="value(timestamp,httpRequest.requestUrl)"

To roll back, set the variable to false and apply again. The rules return to preview with no other change.