More Info:

Ensure that SQL Instances have Query Insights enabled.

Risk Level

Low

Address

Operational Maturity

Compliance Standards

CBP

Triage and Remediation

Remediation

Using Console

To remediate the misconfiguration “SQL Query Insights should be Enabled” for GCP using the GCP Console, follow the below steps:
  1. Open the GCP Console and go to the Cloud SQL Instances page.
  2. Select the instance for which you want to enable SQL Query Insights.
  3. Click on the “Edit” button at the top of the page.
  4. Scroll down to the “Flags” section and click on the “Add item” button.
  5. In the “Name” field, enter “query-insights-enabled”.
  6. In the “Value” field, enter “on”.
  7. 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.

To remediate the SQL Query Insights misconfiguration for GCP using GCP CLI, follow these steps:
  1. Open the Cloud Shell in the GCP Console.
  2. 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=on
    
    Replace [INSTANCE_NAME] with the name of your Cloud SQL instance.
  3. Verify that SQL Query Insights is enabled by running the following command:
    gcloud sql instances describe [INSTANCE_NAME] | grep queryLog
    
    If 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.
To remediate the misconfiguration “SQL Query Insights should be Enabled” in GCP using Python, follow these steps:
  1. Import the necessary libraries:
from google.cloud import bigquery
from google.cloud.bigquery import QueryJobConfig
  1. Create a client object for the BigQuery API:
client = bigquery.Client()
  1. Define the project ID and dataset ID where the query insights should be enabled:
project_id = 'your-project-id'
dataset_id = 'your-dataset-id'
  1. 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.