> ## 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.

# Cloud CDN Global Backend Services Should Have Session Affinity

### More Info:

Cloud CDN global backend services should have session affinity enabled.

### Risk Level

Medium

### Address

Operational Maturity, Performance Efficiency, Security

### Compliance Standards

CBP

### Triage and Remediation

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

    <AccordionGroup>
      <Accordion title="Using Console" defaultOpen="true">
        To remediate the misconfiguration "Cloud CDN Global Backend Services Should Have Session Affinity" in GCP using GCP console, follow the below steps:

        1. Open the Google Cloud Console and select the project you want to work with.
        2. Go to the Navigation menu and select "Network Services" and then select "Cloud CDN".
        3. In the Cloud CDN page, select the name of the backend service that you want to remediate.
        4. In the backend service page, click on the "Edit" button on the top of the page.
        5. In the "Edit backend service" page, scroll down to the "Session affinity" section.
        6. Select the "Client IP and protocol" option from the drop-down menu for "Session affinity".
        7. Click on the "Save" button to save the changes.

        By following the above steps, you have successfully remediated the misconfiguration "Cloud CDN Global Backend Services Should Have Session Affinity" in GCP using GCP console.

        #
      </Accordion>

      <Accordion title="Using CLI">
        To remediate the misconfiguration "Cloud CDN Global Backend Services Should Have Session Affinity" for GCP using GCP CLI, you can follow these steps:

        1. Open the Cloud Shell in the GCP Console.

        2. Run the following command to list all the backend services in your project:

        ```
        gcloud compute backend-services list
        ```

        3. Identify the backend service(s) that are used by your Cloud CDN.

        4. Run the following command to update the backend service(s) and enable session affinity:

        ```
        gcloud compute backend-services update [BACKEND_SERVICE_NAME] --session-affinity CLIENT_IP
        ```

        Replace \[BACKEND\_SERVICE\_NAME] with the name of the backend service you identified in step 3.

        5. Verify that session affinity is enabled for the backend service(s) by running the following command:

        ```
        gcloud compute backend-services describe [BACKEND_SERVICE_NAME] | grep sessionAffinity
        ```

        This command should return the following output:

        ```
        sessionAffinity: CLIENT_IP
        ```

        This confirms that session affinity has been enabled for the backend service(s) used by your Cloud CDN.

        Note: Enabling session affinity may result in increased latency and decreased availability, depending on your workload. Therefore, it is important to evaluate the impact of this change before implementing it in a production environment.
      </Accordion>

      <Accordion title="Using Python">
        To remediate the misconfiguration "Cloud CDN Global Backend Services Should Have Session Affinity" for GCP using python, you can follow the below steps:

        1. First, you need to authenticate with GCP using the below command:

        ```python theme={null}
        from google.oauth2 import service_account
        from googleapiclient.discovery import build

        credentials = service_account.Credentials.from_service_account_file(
            'path/to/your/credentials.json'
        )

        service = build('compute', 'v1', credentials=credentials)
        ```

        2. Next, you need to get the list of all global backend services using the below command:

        ```python theme={null}
        project = 'your-project-id'
        global_backend_services = service.backendServices().list(project=project).execute()
        ```

        3. For each global backend service, you need to check if session affinity is enabled or not using the below command:

        ```python theme={null}
        for service in global_backend_services['items']:
            service_name = service['name']
            service_info = service['sessionAffinitySettings']['affinityCookieTtlSec']
            if service_info == 0:
                print(f"Session affinity is not enabled for {service_name}")
        ```

        4. If session affinity is not enabled for any global backend service, you can enable it using the below command:

        ```python theme={null}
        for service in global_backend_services['items']:
            service_name = service['name']
            service_info = service['sessionAffinitySettings']['affinityCookieTtlSec']
            if service_info == 0:
                print(f"Enabling session affinity for {service_name}")
                body = {
                    'sessionAffinitySettings': {
                        'affinityType': 'GENERATED_COOKIE',
                        'affinityCookieTtlSec': 3600
                    }
                }
                service = service.backendServices().patch(
                    project=project,
                    backendService=service_name,
                    body=body
                ).execute()
        ```

        This will enable session affinity for all the global backend services which do not have it enabled.
      </Accordion>
    </AccordionGroup>
  </Tab>
</Tabs>

### Additional Reading:

* [https://cloud.google.com/load-balancing/docs/backend-service#session\_affinity](https://cloud.google.com/load-balancing/docs/backend-service#session_affinity)
