Skip to content

Commit 0448bc1

Browse files
authored
This fixes the cloud run policies in a few ways. (#162)
1. Switch `logName` to admin activity (excl. system events and data access) The `logName` addition pointing at `data_access` excluded mutations, which is the key thing we want to monitor, and most of what that left was things we want to ignore because GetService is pretty benign and common via the console. 2. Split a separate alert policy off for RunJob monitoring This is the main exception that IS data_access, so for simplicity just make this it's own alert policy. Signed-off-by: Matt Moore <[email protected]>
1 parent 5603467 commit 0448bc1

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

modules/cron/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ No requirements.
8181
| [google_cloud_run_v2_job_iam_binding.authorize-calls](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/cloud_run_v2_job_iam_binding) | resource |
8282
| [google_cloud_scheduler_job.cron](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/cloud_scheduler_job) | resource |
8383
| [google_monitoring_alert_policy.anomalous-job-access](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/monitoring_alert_policy) | resource |
84+
| [google_monitoring_alert_policy.anomalous-job-execution](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/monitoring_alert_policy) | resource |
8485
| [google_project_service.cloud_run_api](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/project_service) | resource |
8586
| [google_project_service.cloudscheduler](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/project_service) | resource |
8687
| [google_service_account.delivery](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/service_account) | resource |

modules/cron/main.tf

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ resource "google_monitoring_alert_policy" "anomalous-job-access" {
206206

207207
condition_matched_log {
208208
filter = <<EOT
209-
logName="projects/prod-enforce-fabc/logs/cloudaudit.googleapis.com%2Fdata_access"
209+
logName="projects/${var.project_id}/logs/cloudaudit.googleapis.com%2Factivity"
210210
protoPayload.serviceName="run.googleapis.com"
211211
protoPayload.resourceName=("${join("\" OR \"", [
212212
"namespaces/${var.project_id}/jobs/${var.name}-cron",
@@ -216,14 +216,51 @@ resource "google_monitoring_alert_policy" "anomalous-job-access" {
216216
-- Allow CI to reconcile jobs and their IAM policies.
217217
-(
218218
protoPayload.authenticationInfo.principalEmail="${data.google_client_openid_userinfo.me.email}"
219-
protoPayload.methodName=("google.cloud.run.v2.Jobs.GetJob" OR "google.cloud.run.v2.Jobs.UpdateJob" OR "google.cloud.run.v2.Jobs.GetIamPolicy" OR "google.cloud.run.v2.Jobs.SetIamPolicy")
219+
protoPayload.methodName=("${join("\" OR \"", [
220+
"google.cloud.run.v2.Jobs.UpdateJob",
221+
"google.cloud.run.v2.Jobs.SetIamPolicy",
222+
])}")
220223
)
224+
EOT
225+
}
226+
}
221227

222-
-- Allow the delivery service account to run the job.
223-
-(
224-
protoPayload.authenticationInfo.principalEmail="${google_service_account.delivery.email}"
225-
protoPayload.methodName="google.cloud.run.v1.Jobs.RunJob"
226-
)
228+
# TODO(mattmoor): Enable notifications once this stabilizes.
229+
# notification_channels = var.notification_channels
230+
231+
enabled = "true"
232+
project = var.project_id
233+
}
234+
235+
// Create an alert policy to notify if the job is accessed by an unauthorized entity.
236+
resource "google_monitoring_alert_policy" "anomalous-job-execution" {
237+
# In the absence of data, incident will auto-close after an hour
238+
alert_strategy {
239+
auto_close = "3600s"
240+
241+
notification_rate_limit {
242+
period = "3600s" // re-alert hourly if condition still valid.
243+
}
244+
}
245+
246+
display_name = "Abnormal Job Execution: ${var.name}"
247+
combiner = "OR"
248+
249+
conditions {
250+
display_name = "Abnormal Job Execution: ${var.name}"
251+
252+
condition_matched_log {
253+
filter = <<EOT
254+
logName="projects/${var.project_id}/logs/cloudaudit.googleapis.com%2Fdata_access"
255+
protoPayload.serviceName="run.googleapis.com"
256+
protoPayload.methodName="google.cloud.run.v1.Jobs.RunJob"
257+
protoPayload.resourceName=("${join("\" OR \"", [
258+
"namespaces/${var.project_id}/jobs/${var.name}-cron",
259+
"projects/${var.project_id}/locations/${var.region}/jobs/${var.name}-cron",
260+
])}")
261+
262+
-- Allow the delivery service account to run the job, but flag anyone else
263+
-protoPayload.authenticationInfo.principalEmail="${google_service_account.delivery.email}"
227264
EOT
228265
}
229266
}

modules/regional-go-service/main.tf

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ resource "google_monitoring_alert_policy" "anomalous-service-access" {
210210

211211
condition_matched_log {
212212
filter = <<EOT
213-
logName="projects/${var.project_id}/logs/cloudaudit.googleapis.com%2Fdata_access"
213+
logName="projects/${var.project_id}/logs/cloudaudit.googleapis.com%2Factivity"
214214
protoPayload.serviceName="run.googleapis.com"
215215
protoPayload.resourceName=("${join("\" OR \"", concat([
216216
"namespaces/${var.project_id}/services/${var.name}"
@@ -223,8 +223,6 @@ resource "google_monitoring_alert_policy" "anomalous-service-access" {
223223
-(
224224
protoPayload.authenticationInfo.principalEmail="${data.google_client_openid_userinfo.me.email}"
225225
protoPayload.methodName=("${join("\" OR \"", [
226-
"google.cloud.run.v2.Services.GetService",
227-
"google.cloud.run.v2.Services.GetIamPolicy",
228226
"google.cloud.run.v2.Services.UpdateService",
229227
"google.cloud.run.v2.Services.SetIamPolicy",
230228
])}")

0 commit comments

Comments
 (0)