> ## Documentation Index
> Fetch the complete documentation index at: https://cloudanix.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# PubSub Lite Topic Retention Should Be Set

### More Info:

Ensure that topic retention is set for PubSub Lite

### Risk Level

Low

### Address

Operational Maturity, Reliability

### Compliance Standards

CBP

### Triage and Remediation

<Tabs>
  <Tab title="Remediation">
    ### Remediation

    <AccordionGroup>
      <Accordion title="Using Console" defaultOpen="true">
        To remediate the misconfiguration of PubSub Lite Topic Retention using GCP console, you can follow the below steps:

        1. Login to the Google Cloud Console ([https://console.cloud.google.com/](https://console.cloud.google.com/)).
        2. Select the project that contains the PubSub Lite topic that needs to be remediated.
        3. Navigate to the PubSub Lite Topics page by selecting "Pub/Sub Lite" from the left-hand menu and then selecting "Topics".
        4. Select the PubSub Lite topic that needs to be remediated.
        5. Click on the "Edit" button at the top of the page.
        6. Under the "Retention" section, set the retention period for the topic. You can set the retention period to any value between 10 minutes and 7 days.
        7. Click on the "Save" button to save the changes.

        After following these steps, the PubSub Lite Topic Retention will be set for the selected topic.

        #
      </Accordion>

      <Accordion title="Using CLI">
        To remediate the misconfiguration of PubSub Lite Topic Retention for GCP using GCP CLI, follow these steps:

        1. Open the GCP Cloud Shell in your GCP console.

        2. Run the following command to set the retention period for a PubSub Lite topic:

           ```
           gcloud pubsub lite-topics update TOPIC-ID --retention-period PERIOD
           ```

           Replace TOPIC-ID with the ID of the PubSub Lite topic that you want to update, and replace PERIOD with the number of seconds that you want to retain messages in the topic.

           For example, to set the retention period to 7 days (or 604800 seconds), run the following command:

           ```
           gcloud pubsub lite-topics update my-topic --retention-period 604800
           ```

        3. Verify that the retention period has been set correctly by running the following command:

           ```
           gcloud pubsub lite-topics describe TOPIC-ID
           ```

           Replace TOPIC-ID with the ID of the PubSub Lite topic that you updated. The output should include a "retentionConfig" section that shows the retention period that you set.

           For example, the output might look like this:

           ```
           name: projects/my-project/locations/us-central1-a/topics/my-topic
           partitionConfig:
             count: 1
             ...
           retentionConfig:
             perPartitionBytes: 0
             period: 604800s
           state: READY
           ```

           This indicates that the retention period for the topic has been set to 7 days.

        4. Repeat these steps for any other PubSub Lite topics that need to have their retention period set.

        By following these steps, you can remediate the misconfiguration of PubSub Lite Topic Retention for GCP using GCP CLI.
      </Accordion>

      <Accordion title="Using Python">
        To remediate the PubSub Lite Topic Retention misconfiguration in GCP using Python, you can follow the below steps:

        1. Install the Google Cloud Pub/Sub Lite client library for Python using pip:

        ```
        pip install google-cloud-pubsublite
        ```

        2. Import the required libraries:

        ```python theme={null}
        from google.api_core.exceptions import NotFound
        from google.cloud.pubsublite import AdminClient, SubscriptionPath, TopicPath
        from google.cloud.pubsublite.types import Duration
        ```

        3. Set the project ID and location of the Pub/Sub Lite topic:

        ```python theme={null}
        project_id = "your-project-id"
        location = "your-location"
        topic_id = "your-topic-id"
        ```

        4. Create an instance of the `AdminClient` class:

        ```python theme={null}
        admin_client = AdminClient()
        ```

        5. Use the `TopicPath` class to create a `topic_path` object:

        ```python theme={null}
        topic_path = TopicPath(project_id, location, topic_id)
        ```

        6. Use a try-except block to check if the topic exists:

        ```python theme={null}
        try:
            topic = admin_client.get_topic(topic_path)
        except NotFound:
            print(f"Topic '{topic_path}' not found.")
            return
        ```

        7. Set the retention duration for the topic using the `update_topic` method:

        ```python theme={null}
        retention_duration = Duration(seconds=604800) # 1 week in seconds
        topic.retention_config.period = retention_duration
        admin_client.update_topic(topic)
        ```

        8. Verify that the retention duration has been set:

        ```python theme={null}
        print(f"Retention duration set to {topic.retention_config.period.seconds} seconds.")
        ```

        The complete code to remediate the PubSub Lite Topic Retention misconfiguration in GCP using Python is as follows:

        ```python theme={null}
        from google.api_core.exceptions import NotFound
        from google.cloud.pubsublite import AdminClient, SubscriptionPath, TopicPath
        from google.cloud.pubsublite.types import Duration

        project_id = "your-project-id"
        location = "your-location"
        topic_id = "your-topic-id"

        admin_client = AdminClient()
        topic_path = TopicPath(project_id, location, topic_id)

        try:
            topic = admin_client.get_topic(topic_path)
        except NotFound:
            print(f"Topic '{topic_path}' not found.")
            return

        retention_duration = Duration(seconds=604800) # 1 week in seconds
        topic.retention_config.period = retention_duration
        admin_client.update_topic(topic)

        print(f"Retention duration set to {topic.retention_config.period.seconds} seconds.")
        ```
      </Accordion>
    </AccordionGroup>
  </Tab>
</Tabs>
