GCP Introduction
GCP Pricing
GCP Threats
GCP Misconfigurations
- Getting Started with GCP Audit
- CloudSql Audit
- Cloud Tasks Monitoring
- Dataflow Monitoring
- Function Monitoring
- Monitoring Compliance
- PubSubLite Monitoring
- Spanner Monitoring
- NoSQL Monitoring
- Compute Audit
- IAM Audit
- BigQuery Monitoring
- CDN Monitoring
- DNS Monitoring
- KMS Monitoring
- Kubernetes Audit
- Load Balancer Monitoring
- Log Monitoring
- Storage Audit
- Pub/Sub Monitoring
- VPC Audit
- IAM Deep Dive
GCP Threats
PostgreSQL Log Statement Flag Should Be Set Appropriately
More Info:
The value of log_statement flag determined the SQL statements that are logged. Valid values are: • none • ddl • mod • all The value ddl logs all data definition statements. The value mod logs all ddl statements, plus data-modifying statements. The statements are logged after a basic parsing is done and statement type is determined, thus this does not logs statements with errors. When using extended query protocol, logging occurs after an Execute message is received and values of the Bind parameters are included. A value of ‘ddl’ is recommended unless otherwise directed by your organization’s logging policy.
Risk Level
Low
Address
Reliability, Security
Compliance Standards
CISGCP, CBP
Triage and Remediation
Remediation
To remediate the PostgreSQL Log Statement Flag misconfiguration in GCP using GCP console, please follow the steps below:
- Login to your GCP console and select the project where the PostgreSQL instance is located.
- In the navigation menu, select SQL under the Storage section.
- Select the PostgreSQL instance where the misconfiguration exists.
- Click on the Edit button at the top of the page.
- Scroll down to the Flags section and look for the log_statement flag.
- Set the log_statement flag to one of the following values based on your requirements:
- none: Disables logging of all statements
- ddl: Logs only data definition language (DDL) statements
- mod: Logs only statements that modify data (INSERT, UPDATE, DELETE)
- all: Logs all statements (default)
- Once you have set the log_statement flag appropriately, click on the Save button at the bottom of the page.
- Wait for a few minutes for the changes to take effect.
That’s it! You have successfully remediated the PostgreSQL Log Statement Flag misconfiguration in GCP using GCP console.
To remediate the PostgreSQL log statement flag misconfiguration in GCP using GCP CLI, follow these steps:
- Open the Cloud Shell in your GCP console.
- Run the following command to list all the instances in your project:
gcloud sql instances list
- Identify the instance that has the PostgreSQL log statement flag misconfiguration.
- Run the following command to get the current flags of the instance:
Replace [INSTANCE_NAME] with the name of the instance that you identified in step 3.
gcloud sql instances describe [INSTANCE_NAME]
- Look for the
databaseFlags
field in the output of the previous command. This field contains a list of flags that are currently set for the instance. - Check if the
log_statement
flag is set toall
. If it is not set toall
, then it is misconfigured. - To set the
log_statement
flag toall
, run the following command:Replace [INSTANCE_NAME] with the name of the instance that you identified in step 3.gcloud sql instances patch [INSTANCE_NAME] --database-flags log_statement=all
- Verify that the flag has been set correctly by running the
gcloud sql instances describe
command again and checking thedatabaseFlags
field. Thelog_statement
flag should now be set toall
.
By following these steps, you should be able to remediate the PostgreSQL log statement flag misconfiguration in GCP using GCP CLI.
To remediate the PostgreSQL Log Statement Flag misconfiguration in GCP using Python, follow these steps:
- Install the
google-cloud-logging
library using pip:
pip install google-cloud-logging
-
Create a service account with the necessary permissions to access the GCP project where the PostgreSQL instance is located. Download the JSON key file for the service account.
-
Set the
GOOGLE_APPLICATION_CREDENTIALS
environment variable to the path of the JSON key file:
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/keyfile.json
- Use the following Python code to set the PostgreSQL log_statement flag to
all
:
from google.cloud import logging_v2
from google.oauth2 import service_account
# Set the PostgreSQL instance ID
instance_id = "my-postgres-instance"
# Set the log_statement flag to "all"
log_statement = "all"
# Authenticate using the service account key file
credentials = service_account.Credentials.from_service_account_file(
"/path/to/keyfile.json"
)
# Create the Logging client
client = logging_v2.LoggingServiceV2Client(credentials=credentials)
# Create the log sink for the PostgreSQL instance
sink_name = f"projects/{client.project}/sinks/postgresql-{instance_id}"
sink = client.get_sink(sink_name)
# Update the sink's filter to include the log_statement flag
new_filter = f'resource.type="gce_instance" AND resource.labels.instance_id="{instance_id}" AND textPayload:"log_statement = {log_statement}"'
sink.filter = new_filter
# Update the sink in GCP
client.update_sink(sink=sink, update_mask={"paths": ["filter"]})
This code will update the log sink for the specified PostgreSQL instance to include the log_statement = all
filter. This will ensure that all SQL statements executed on the instance are logged to the Cloud Logging service in GCP.