Ensure Log Metric Filter and Alerts Exist for Project Ownership Assignments/Changes

In order to prevent unnecessary project ownership assignments to users or service-accounts and further misuses of projects and resources, all roles/Owner assignments should be monitored. Members (users or service-accounts) with a role assignment to primitive role roles/Owner are project owners.The project owner has all the privileges on the project the role belongs to. These are summarized below: - All viewer permissions on all GCP Services within the project - Permissions for actions that modify the state of all GCP services within the project - Manage roles and permissions for a project and all resources within the project - Set up billing for a project Granting the owner role to a member (user or Service-Account) will allow that member to modify the Identity and Access Management (IAM) policy. Therefore, grant the owner role only if the member has a legitimate purpose to manage the IAM policy. This is because the project IAM policy contains sensitive access control data. Having a minimal set of users allowed to manage IAM policy will simplify any auditing that may be necessary.

Risk Level: Low
Cloud Entity: GCP AlertPolicy
CloudGuard Rule ID: D9.GCP.LOG.19
Covered by Spectral: No
Category: Management Tools

GSL LOGIC

AlertPolicy where conditions contain [ conditionThreshold.logName] and enabled='true' should have conditions contain [ getResource('LogBasedMetric',conditionThreshold.logName)  getValue('filter') ='(protoPayload.serviceName="cloudresourcemanager.googleapis.com") AND (ProjectOwnership OR projectOwnerInvitee) OR (protoPayload.serviceData.policyDelta.bindingDeltas.action="REMOVE" AND protoPayload.serviceData.policyDelta.bindingDeltas.role="roles/owner") OR (protoPayload.serviceData.policyDelta.bindingDeltas.action="ADD" AND protoPayload.serviceData.policyDelta.bindingDeltas.role="roles/owner")']

REMEDIATION

From Portal
Create the prescribed Log Metric:

  1. Go to Logging:Logs-based Metrics by visiting https://console.cloud.google.com/logs/metrics and click "CREATE METRIC".
  2. Select Metric Type as Counter, enter Log metric name and Units to 1.
  3. Clear any text and add:
(protoPayload.serviceName="cloudresourcemanager.googleapis.com") AND (ProjectOwnership OR projectOwnerInvitee) OR (protoPayload.serviceData.policyDelta.bindingDeltas.action="REMOVE" AND protoPayload.serviceData.policyDelta.bindingDeltas.role="roles/owner") OR (protoPayload.serviceData.policyDelta.bindingDeltas.action="ADD" AND protoPayload.serviceData.policyDelta.bindingDeltas.role="roles/owner")
  1. Click CREATE METRIC. The logs display based on the filter text entered by the user.

Create the prescribed alert policy:

  1. Identify the newly created metric under the section User-defined Metrics at https://console.cloud.google.com/logs/metrics.
  2. Click the 3-dot icon in the rightmost column for the new metric and select 'Create alert from Metric'. A new page appears.
  3. Fill out the alert policy configuration and click Save. Choose the alerting threshold and configuration that makes sense for the user's organization. For example, a threshold of zero(0) for the most recent value will ensure that a notification is triggered for every owner change in the user's project:
  4. Configure the desired notification channels in the section Notifications.
  5. Name the policy and click Save.

From Command Line

  1. Create the prescribed log metric. Reference for command usage: https://cloud.google.com/sdk/gcloud/reference/beta/logging/metrics/create
    use the command:
gcloud beta logging metrics create METRIC_NAME --description="DESCRIPTION" --log-filter="LOG_FILTER"
  1. You can find the notification channel ID using:
gcloud alpha monitoring channels list --filter='displayName="channel_name"' --format='value(name)'
  1. Create the prescribed alert policy.Reference for command usage: https://cloud.google.com/sdk/gcloud/reference/alpha/monitoring/policies/create
  • Create a json file with required properties and values for the policy(try creating a policy from console and then download it's json to have a reference) and use the command:
gcloud alpha monitoring policies create --policy-from-file="policyfile.json"

From TF

  1. Create log based metric with expected filter.
  2. Create an alerting policy with type 'metric' based on the previous created log based metric.

Resources;
google_logging_metric
google_monitoring_alert_policy

See below example;

resource "google_logging_metric" "my_log_metrics" {
	project = "My-project-id"
	name = "my-(custom)/metric"
	filter = "(protoPayload.serviceName="cloudresourcemanager.googleapis.com") AND (ProjectOwnership OR projectOwnerInvitee) OR (protoPayload.serviceData.policyDelta.bindingDeltas.action="REMOVE" AND protoPayload.serviceData.policyDelta.bindingDeltas.role="roles/owner") OR (protoPayload.serviceData.policyDelta.bindingDeltas.action="ADD" AND protoPayload.serviceData.policyDelta.bindingDeltas.role="roles/owner")"
	description = "..."
	metric_descriptor {
		metric_kind = "DELTA"
		value_type  = "INT64"
	}
}

And

resource "google_monitoring_alert_policy" "alert_policy" {
	project = "My-project-id"
	display_name = "My Alert Policy"
	combiner = "OR"
	conditions {
		display_name = "test condition"
		condition_threshold {
			filter = "metric.type="logging.googleapis.com/user/my-(custom)/metric" AND resource.type="cloud_composer_environment""
			duration   = "0s"
			comparison = "COMPARISON_GT"
		}
	}
}

References

  1. https://cloud.google.com/logging/docs/logs-based-metrics/
  2. https://cloud.google.com/monitoring/custom-metrics/
  3. https://cloud.google.com/monitoring/alerts/
  4. https://cloud.google.com/logging/docs/reference/tools/gcloud-logging
  5. https://workbench.cisecurity.org/sections/507170/recommendations/827550

GCP AlertPolicy

Alerting policy describes the circumstances under which you want to be alerted and how you want to be notified. This page provides an overview of alerting policies

Compliance Frameworks

  • CloudGuard GCP All Rules Ruleset
  • GCP CIS Controls V 8
  • GCP CIS Foundations v. 1.0.0
  • GCP CIS Foundations v. 1.1.0
  • GCP CIS Foundations v. 1.2.0
  • GCP CIS Foundations v. 1.3.0
  • GCP CIS Foundations v. 2.0
  • GCP CloudGuard Best Practices
  • GCP MITRE ATT&CK Framework v12.1
  • GCP NIST 800-53 Rev 5
  • GCP PCI-DSS 4.0