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
PubSub Subscriptions Should Not Be Detached From Topics
More Info:
Ensure that PubSub Subscriptions are not detached from topics
Risk Level
Low
Address
Operational Excellence
Compliance Standards
CBP
Triage and Remediation
Remediation
To remediate the PubSub subscription detachment from topics misconfiguration in GCP using GCP console, follow the below steps:
-
Open the Google Cloud Console and select your project.
-
Navigate to the Pub/Sub section from the left-hand side menu.
-
Select the Subscription tab from the top menu.
-
Identify the detached subscription(s) and select the checkbox beside them.
-
Click on the Delete button above the list of subscriptions.
-
Confirm the deletion by clicking on the Delete button in the confirmation dialog box.
-
Next, navigate to the Topics tab from the top menu.
-
Select the topic that the subscription was previously attached to.
-
Click on the Add Subscription button above the list of subscriptions.
-
In the Add Subscription dialog box, select the subscription(s) that you want to attach to the topic.
-
Click on the Create button to attach the subscription(s) to the topic.
-
Verify that the subscription(s) are now attached to the topic.
By following these steps, you can remediate the PubSub subscription detachment from topics misconfiguration in GCP using GCP console.
To remediate the misconfiguration “PubSub Subscriptions Should Not Be Detached From Topics” for GCP using GCP CLI, follow the steps below:
-
Open the Cloud Shell in your GCP console.
-
Run the following command to list all the subscriptions that are detached from topics:
gcloud pubsub subscriptions list --filter="topic:projects/*/topics/* AND NOT topic:projects/*/topics/*"
-
Identify the subscription that is detached from a topic and note down its subscription name.
-
Run the following command to delete the subscription:
gcloud pubsub subscriptions delete [SUBSCRIPTION_NAME]
Replace [SUBSCRIPTION_NAME] with the name of the subscription that you want to delete.
-
Repeat steps 3 and 4 for all the subscriptions that are detached from topics.
-
Finally, run the following command to verify that all the subscriptions are attached to a topic:
gcloud pubsub subscriptions list --filter="topic:projects/*/topics/*"
This command will list all the subscriptions that are attached to a topic.
By following these steps, you can remediate the misconfiguration “PubSub Subscriptions Should Not Be Detached From Topics” for GCP using GCP CLI.
To remediate the misconfiguration “PubSub Subscriptions Should Not Be Detached From Topics” for GCP using python, follow these steps:
- First, you need to identify the detached subscriptions in GCP. You can use the following code to get the list of all detached subscriptions:
from google.cloud import pubsub_v1
project_id = "your-project-id"
client = pubsub_v1.SubscriberClient()
project_path = f"projects/{project_id}"
response = client.list_subscriptions(project_path)
for subscription in response:
if not subscription.topic:
print(f"Detached subscription: {subscription.name}")
- Once you have identified the detached subscriptions, you can re-attach them to their respective topics using the following code:
from google.cloud import pubsub_v1
project_id = "your-project-id"
client = pubsub_v1.SubscriberClient()
project_path = f"projects/{project_id}"
response = client.list_subscriptions(project_path)
for subscription in response:
if not subscription.topic:
print(f"Re-attaching subscription {subscription.name} to topic {subscription.topic}")
topic_path = f"projects/{project_id}/topics/{subscription.topic.split('/')[-1]}"
client.modify_push_config(subscription.name, topic_path=topic_path)
This code will loop through all the subscriptions in your project and re-attach the detached subscriptions to their respective topics.