PostgreSQL Log Parser Stats Flag Should Be Off
More Info:
Ensure that the log_parser_stats database flag for a Cloud SQL PostgreSQL instance is set to off.
Risk Level
Low
Address
Reliability, Security
Compliance Standards
- Cloudanix Best Practice
Triage and Remediation
- Remediation
Remediation
Using Console
To remediate the PostgreSQL Log Parser Stats Flag Should Be Off misconfiguration in GCP using the GCP console, follow these steps:
- Open the GCP Console and navigate to the Cloud SQL instances page.
- Click on the name of the instance that has the PostgreSQL Log Parser Stats Flag Should Be Off misconfiguration.
- In the left-hand menu, click on the Configuration tab.
- Scroll down to the Flags section and click on Edit.
- In the Flags editor, search for the log_parser_stats flag.
- If the log_parser_stats flag is set to ON, toggle it to OFF.
- Click Save to apply the changes.
Once the changes are saved, the PostgreSQL Log Parser Stats Flag will be turned off and the misconfiguration will be remediated.
Using CLI
To remediate the PostgreSQL Log Parser Stats Flag Should Be Off misconfiguration in GCP using GCP CLI, you can follow these steps:
-
Open the Cloud Shell in the GCP console.
-
Run the following command to list all the Cloud SQL instances in your project:
gcloud sql instances list -
Identify the instance that has the PostgreSQL Log Parser Stats Flag Should Be Off misconfiguration.
-
Run the following command to update the instance settings:
gcloud sql instances patch [INSTANCE_NAME] --database-flags log_parser_stats=offReplace [INSTANCE_NAME] with the name of the instance that you identified in step 3.
-
Verify that the log_parser_stats flag is set to off by running the following command:
gcloud sql instances describe [INSTANCE_NAME]Replace [INSTANCE_NAME] with the name of the instance that you identified in step 3.
Look for the following line in the output:
databaseFlags:log_parser_stats: offIf you see this line, the misconfiguration has been remediated successfully.
That's it! You have successfully remediated the PostgreSQL Log Parser Stats Flag Should Be Off misconfiguration for the specified GCP instance using GCP CLI.
Using Python
To remediate the PostgreSQL Log Parser Stats Flag Should Be Off misconfiguration in GCP using python, follow these steps:
-
Install the
google-cloud-secret-managerandgoogle-cloud-loggingpython packages using pip.pip install google-cloud-secret-manager google-cloud-logging -
Import the necessary libraries.
from google.cloud import secretmanager_v1beta1from google.cloud import logging_v2 -
Retrieve the value of the
POSTGRES_STATS_FLAGsecret from Google Secret Manager.client = secretmanager_v1beta1.SecretManagerServiceClient()name = client.secret_version_path('<PROJECT_ID>', '<SECRET_NAME>', '<SECRET_VERSION>')response = client.access_secret_version(name)postgres_stats_flag = response.payload.data.decode('UTF-8')Replace
<PROJECT_ID>,<SECRET_NAME>, and<SECRET_VERSION>with the appropriate values. -
Update the log sink for PostgreSQL to disable the log parser stats flag.
client = logging_v2.LoggingServiceV2Client()parent = client.project_path('<PROJECT_ID>')resource = {"type": "gce_instance","labels": {"instance_id": "<INSTANCE_ID>","zone": "<ZONE>"}}filter = 'resource.type="gce_instance" AND resource.labels.instance_id="<INSTANCE_ID>" AND logName="projects/<PROJECT_ID>/logs/postgresql.log"'sinks = client.list_sinks(parent)for sink in sinks:if sink.filter == filter:sink.destination.update_gcs_bucket = Nonesink.destination.disable_log_entry_types = ["LOG_ENTRY_TYPE_UNSPECIFIED", "PROTO_PAYLOAD"]sink.destination.enable_log_entry_types = []sink.filter = 'resource.type="gce_instance" AND resource.labels.instance_id="<INSTANCE_ID>" AND logName="projects/<PROJECT_ID>/logs/postgresql.log" AND NOT jsonPayload.message:("LOG: statement stats:")'client.update_sink(sink)breakReplace
<PROJECT_ID>,<INSTANCE_ID>, and<ZONE>with the appropriate values. -
Verify that the log sink has been updated successfully.
gcloud logging sinks describe <SINK_NAME> --project=<PROJECT_ID>Replace
<SINK_NAME>and<PROJECT_ID>with the appropriate values.
That's it! The PostgreSQL Log Parser Stats Flag Should Be Off misconfiguration has been remediated for GCP using python.
Using Terraform
resource "google_sql_database_instance" "POSTGRES_INSTANCE" {
name = "POSTGRES_INSTANCE_NAME" # replace with your instance name
database_version = "POSTGRES_15" # replace with your version
region = "INSTANCE_REGION" # replace with your region
settings {
tier = "db-custom-2-7680" # replace with your machine tier
# Ensure log_parser_stats is explicitly disabled
database_flags {
name = "log_parser_stats"
value = "off"
}
# ...other settings...
}
}
This change may cause a restart of the Cloud SQL instance (brief outage) when applied.
To verify, terraform plan should show either creation of this database_flags entry with value = "off" or an update from the previous value to "off" on the existing google_sql_database_instance.POSTGRES_INSTANCE.