Determine SQL Instances Provisioned are of Desired Type
More Info:
Determine if the SQL instances provisioned in the account are only of the type desired by the organization and nothing else.
Risk Level
Medium
Address
Operational Maturity
Compliance Standards
- APRA CPS 234 (Australia)
- BSI C5 (Germany)
- Brazil LGPD
- CCPA / CPRA (California)
- CIS Critical Security Controls v8
- CMMC 2.0
- CSA Cloud Controls Matrix v4
- Cloudanix Best Practice
- DPDPA
- Digital Operational Resilience Act (EU)
- ISO/IEC 27017
- ISO/IEC 27018
- ISO/IEC 27701
- KSA PDPL
- MAS Technology Risk Management (Singapore)
- MITRE ATT&CK (Cloud)
- NIS2 Directive
- NIST SP 800-171
- NYDFS 23 NYCRR 500
- SWIFT Customer Security Controls Framework
- Sarbanes-Oxley IT General Controls
- UK NCSC Cyber Assessment Framework
Triage and Remediation
- Remediation
Remediation
Using Console
The following are the step-by-step instructions to remediate the misconfiguration "Determine SQL Instances Provisioned are of Desired Type" for GCP using GCP console:
-
Open the Google Cloud Console and navigate to the SQL Instances page.
-
Identify the SQL instances that are not of the desired type.
-
Click on the name of the SQL instance that needs to be remediated.
-
In the Instance details page, click on the Edit button at the top of the page.
-
In the Edit page, scroll down to the Configuration Options section.
-
In the Configuration Options section, select the desired SQL type from the drop-down menu.
-
Click on the Save button at the bottom of the page to save the changes.
-
Repeat the above steps for all the SQL instances that are not of the desired type.
-
Verify that all the SQL instances are now of the desired type by checking the Instance details page for each SQL instance.
By following the above steps, you can remediate the misconfiguration "Determine SQL Instances Provisioned are of Desired Type" for GCP using GCP console.
Using CLI
The first step in remediating this misconfiguration is to identify all the SQL instances that are currently provisioned in your GCP environment and verify that they are of the desired type. Follow these steps to remediate this issue using the GCP CLI:
-
Open the Cloud Shell in your GCP account.
-
Run the following command to list all the SQL instances in your project:
gcloud sql instances list -
Review the output from the above command to identify the SQL instances that are currently provisioned in your project.
-
Run the following command to describe a specific SQL instance:
gcloud sql instances describe INSTANCE_NAMEReplace INSTANCE_NAME with the name of the SQL instance that you want to describe.
-
Review the output from the above command to verify that the SQL instance is of the desired type.
If you find that any SQL instances are not of the desired type, you will need to create a new instance of the desired type and migrate your data to the new instance. Here are the steps to create a new SQL instance:
-
Run the following command to create a new SQL instance:
gcloud sql instances create NEW_INSTANCE_NAME --tier=TIER --region=REGIONReplace NEW_INSTANCE_NAME with a unique name for the new SQL instance, TIER with the desired tier (e.g. db-n1-standard-2) and REGION with the desired region (e.g. us-central1).
-
Run the following command to set the root password for the new SQL instance:
gcloud sql users set-password root --host % --instance=NEW_INSTANCE_NAME --password=NEW_PASSWORDReplace NEW_INSTANCE_NAME with the name of the new SQL instance and NEW_PASSWORD with a strong password for the root user.
-
Run the following command to create a new database in the new SQL instance:
gcloud sql databases create NEW_DATABASE_NAME --instance=NEW_INSTANCE_NAMEReplace NEW_INSTANCE_NAME with the name of the new SQL instance and NEW_DATABASE_NAME with a unique name for the new database.
-
Finally, you will need to migrate your data from the old SQL instance to the new SQL instance. You can use the Cloud SQL Admin API or a third-party tool to perform the migration. Once the migration is complete, you can delete the old SQL instance using the following command:
gcloud sql instances delete OLD_INSTANCE_NAMEReplace OLD_INSTANCE_NAME with the name of the old SQL instance.
Using Python
The misconfiguration "Determine SQL Instances Provisioned are of Desired Type" in GCP can be remediated using the following steps in Python:
- Import the necessary libraries:
from google.cloud import bigquery
from google.oauth2 import service_account
- Create a service account and authenticate using the JSON key file:
credentials = service_account.Credentials.from_service_account_file('path/to/key.json')
- Set the project ID and create a BigQuery client:
project_id = 'your-project-id'
client = bigquery.Client(project=project_id, credentials=credentials)
- Define the query to check for SQL instances that are not of the desired type:
query = """
SELECT
project_id,
name,
settings.database_version
FROM
`your-project-id`.region-us-central1.sqladmin.instances
WHERE
settings.database_version != 'MYSQL_5_7'
AND settings.database_version != 'POSTGRES_9_6'
"""
- Run the query and check the results:
query_job = client.query(query)
if query_job.result():
print('There are SQL instances that are not of the desired type.')
for row in query_job:
print(f"Project ID: {row.project_id}, Instance Name: {row.name}, Database Version: {row.settings.database_version}")
else:
print('All SQL instances are of the desired type.')
- If there are instances that are not of the desired type, you can update them using the Cloud SQL API:
from googleapiclient.discovery import build
from google.oauth2 import service_account
credentials = service_account.Credentials.from_service_account_file('path/to/key.json')
service = build('sqladmin', 'v1beta4', credentials=credentials)
project_id = 'your-project-id'
instance_name = 'your-instance-name'
instance_type = 'MYSQL_5_7' # or 'POSTGRES_9_6'
request = service.instances().patch(
project=project_id,
instance=instance_name,
body={
'settings': {
'databaseVersion': instance_type
}
}
)
response = request.execute()
Note: Make sure to replace 'path/to/key.json', 'your-project-id', 'your-instance-name', 'MYSQL_5_7', and 'POSTGRES_9_6' with the correct values for your environment.
Using Terraform
resource "google_sql_database_instance" "PRIMARY_INSTANCE" {
name = "PRIMARY_INSTANCE_NAME" # replace with the Cloud SQL instance name
database_version = "MYSQL_8_0" # replace with desired engine/version
region = "PRIMARY_INSTANCE_REGION" # replace with desired region
settings {
tier = var.desired_sql_instance_tier # must be one of var.allowed_sql_instance_tiers
availability_type = "ZONAL" # or "REGIONAL" as required
# ...other required settings...
}
}
variable "allowed_sql_instance_tiers" {
description = "List of SQL instance tiers that are allowed by the organization."
type = list(string)
# Replace with the exact set of tiers your org permits, e.g.:
# default = ["db-custom-2-7680", "db-custom-4-15360"]
default = [
"ALLOWED_SQL_TIER_1",
"ALLOWED_SQL_TIER_2",
]
}
variable "desired_sql_instance_tier" {
description = "The tier to use for this Cloud SQL instance; must be in allowed_sql_instance_tiers."
type = string
default = "ALLOWED_SQL_TIER_1" # replace with the desired tier for this instance
validation {
condition = contains(var.allowed_sql_instance_tiers, var.desired_sql_instance_tier)
error_message = "The Cloud SQL instance tier must be one of var.allowed_sql_instance_tiers."
}
}
Changing settings.tier updates the instance type; this may cause a brief restart but does not force resource replacement.
To remediate, ensure every google_sql_database_instance you manage sets settings.tier via var.desired_sql_instance_tier, and configure var.allowed_sql_instance_tiers to match the organization-approved types; remove or retype any instances that use disallowed tiers.
Verification: terraform plan should show either no changes (all instances already use allowed tiers) or in-place updates where settings.0.tier is changed from a disallowed value to one of the allowed tiers.