SQL Query Insights Enabled Remediation
Triage and Remediation
- Remediation
Remediation
Using Console
To remediate the misconfiguration "SQL Query Insights should be Enabled" for GCP using the GCP Console, follow the below steps:
- Open the GCP Console and go to the Cloud SQL Instances page.
- Select the instance for which you want to enable SQL Query Insights.
- Click on the "Edit" button at the top of the page.
- Scroll down to the "Flags" section and click on the "Add item" button.
- In the "Name" field, enter "query-insights-enabled".
- In the "Value" field, enter "on".
- Click on the "Save" button at the bottom of the page to save the changes.
After following the above steps, SQL Query Insights will be enabled for the selected Cloud SQL instance in GCP.
Using CLI
To remediate the SQL Query Insights misconfiguration for GCP using GCP CLI, follow these steps:
-
Open the Cloud Shell in the GCP Console.
-
Run the following command to enable SQL Query Insights for a Cloud SQL instance:
gcloud sql instances patch [INSTANCE_NAME] --database-flags cloudsql.enable_query_log=onReplace
[INSTANCE_NAME]with the name of your Cloud SQL instance. -
Verify that SQL Query Insights is enabled by running the following command:
gcloud sql instances describe [INSTANCE_NAME] | grep queryLogIf SQL Query Insights is enabled, the output should include
"queryLogConfig": {"enableQueryLog": true}.
That's it! SQL Query Insights should now be enabled for your Cloud SQL instance in GCP.
Using Python
To remediate the misconfiguration "SQL Query Insights should be Enabled" in GCP using Python, follow these steps:
- Import the necessary libraries:
from google.cloud import bigquery
from google.cloud.bigquery import QueryJobConfig
- Create a client object for the BigQuery API:
client = bigquery.Client()
- Define the project ID and dataset ID where the query insights should be enabled:
project_id = 'your-project-id'
dataset_id = 'your-dataset-id'
- Enable query insights for the dataset:
dataset_ref = client.dataset(dataset_id, project=project_id)
dataset = client.get_dataset(dataset_ref)
dataset.default_query_job_config.use_query_cache = False
dataset.default_query_job_config.use_legacy_sql = False
dataset.default_query_job_config.labels = {"queryinsights-enabled": "true"}
client.update_dataset(dataset, ["labels"])
This code will update the dataset with a label "queryinsights-enabled" set to "true", which will enable query insights for the dataset.
Note: To run this code, you need to have the necessary permissions to update the dataset in GCP.
Using Terraform
resource "google_sql_database_instance" "PRIMARY_INSTANCE" {
name = "PRIMARY_INSTANCE_NAME" # replace with your instance name
database_version = "POSTGRES_14" # replace with your engine/version
region = "INSTANCE_REGION" # e.g. "us-central1"
settings {
tier = "db-custom-2-7680" # replace with your machine tier
insights_config {
query_insights_enabled = true
# Optional tuning – set thresholds to match your policy:
# Maximum length of recorded query strings (1–4500, default 1024)
query_string_length = 1024
# Enable if you want client address recorded
record_client_address = true
# Enable if you want application tags (e.g. from PG labels) recorded
record_application_tags = true
# For Cloud SQL for PostgreSQL only: limit of query plans captured per minute (0–20)
# query_plans_per_minute = 5
}
}
}
Enabling Query Insights via insights_config.query_insights_enabled = true does not force replacement of the Cloud SQL instance; Terraform will show an in‑place update on the settings.insights_config block in terraform plan.