# Configure Certificate Authority Service for Managed
Cloud Service Mesh

Integrating [Certificate Authority Service](https://docs.cloud.google.com/certificate-authority-service/docs.md.txt)
with managed Cloud Service Mesh lets you issue workload certificates using
private CAs instead of the default [Cloud Service Mesh certificate authority](https://docs.cloud.google.com/service-mesh/docs/security/security-overview.md.txt#features).
This setup helps you back signing keys in [Cloud HSM](https://docs.cloud.google.com/kms/docs/hsm.md.txt),
chain certificates to a custom enterprise root, and meet regulatory compliance
requirements across multiple clusters.

Unlike the default mesh CA, CA Service carries an
[explicit SLA](https://cloud.google.com/certificate-authority-service/sla) and
is [charged separately](https://cloud.google.com/certificate-authority-service/pricing).
Because CA Service isn't supported under
[fleet-default configuration](https://docs.cloud.google.com/service-mesh/docs/onboarding/provision-control-plane.md.txt#configure_your_fleet)
during cluster creation, you must
[configure Cloud Service Mesh per cluster](https://docs.cloud.google.com/service-mesh/docs/onboarding/provision-control-plane.md.txt#configure_per_cluster).
For in-cluster control planes, see
[Install default features and Certificate Authority (CA) Service](https://docs.cloud.google.com/service-mesh/docs/unified-install/install-anthos-service-mesh#install_ca_service).

## Prerequisites

As a starting point, this guide assumes that you have:

- [A Cloud project](https://docs.cloud.google.com/resource-manager/docs/creating-managing-projects.md.txt)
- [A Cloud Billing account](https://docs.cloud.google.com/billing/docs/how-to/manage-billing-account.md.txt)
- Obtained the [required permissions](https://docs.cloud.google.com/service-mesh/docs/onboarding/provision-control-plane.md.txt#roles-required) to provision Cloud Service Mesh
- Enabled [Workload Identity Federation for GKE](https://docs.cloud.google.com/kubernetes-engine/docs/how-to/workload-identity.md.txt) on your clusters and node pools.
-
  One or more clusters with a
  [supported version of GKE](https://docs.cloud.google.com/service-mesh/docs/managed/supported-features-mcp#environments),
  in one of the supported [regions](https://docs.cloud.google.com/service-mesh/docs/managed/supported-features-mcp#regions).
  To learn how to create a Google Kubernetes Engine cluster with Cloud Service Mesh enabled, see the per cluster configuration in the
  [Provision a managed Cloud Service Mesh control plane on Google Kubernetes Engine](https://docs.cloud.google.com/service-mesh/docs/onboarding/provision-control-plane.md.txt#configure_per_cluster) document.
  Enabling managed Cloud Service Mesh as a default configuration for your fleet and registering clusters to the fleet during cluster creation only supports
  [Mesh CA](https://docs.cloud.google.com/service-mesh/docs/security/security-overview.md.txt#features). You must configure Cloud Service Mesh per cluster and enable CA Service
  explicitly for each cluster.

## Requirements

Enable the required API in the project where the CA pool will be configured.

```bash
 gcloud services enable privateca.googleapis.com \
      --project=CA_PROJECT_ID
```

## Configure CA Service

1. Create the [CA pool](https://docs.cloud.google.com/certificate-authority-service/docs/creating-ca-pool.md.txt) in the tier `DevOps` and in the same region as the cluster that it serves to avoid excessive latency issues or potential cross-region outages. For more information, see [Workload-optimized tiers](https://docs.cloud.google.com/certificate-authority-service/docs/tiers.md.txt).
2. [Create the CA](https://docs.cloud.google.com/certificate-authority-service/docs/creating-certificate-authorities) to have at least one active certificate authority in the CA pool in the same project as the GKE cluster. Use subordinate CA's to sign Cloud Service Mesh workload certificates. Note down the [CA pool](https://docs.cloud.google.com/certificate-authority-service/docs/ca-pool.md.txt) corresponding to the subordinate CA.
3. If it is meant to only service certificates for Cloud Service Mesh workloads, set
   up the following issuance policy for the CA pool:

   ### policy.yaml

   ```
   baselineValues:
     keyUsage:
       baseKeyUsage:
         digitalSignature: true
         keyEncipherment: true
       extendedKeyUsage:
         serverAuth: true
         clientAuth: true
     caOptions:
       isCa: false
   identityConstraints:
     allowSubjectPassthrough: false
     allowSubjectAltNamesPassthrough: true
     celExpression:
       expression: subject_alt_names.all(san, san.type == URI && san.value.startsWith("spiffe://PROJECT_ID.svc.id.goog/ns/") )
   ```

   > [!NOTE]
   > **Note:** As a best practice for multi-cluster meshes, set one subordinate CA pool per unique cluster region. All the subordinate CA pools should chain to the same root CA pool.

4. To update the CA pool's issuance policy, use the following command:

   ```
   gcloud privateca pools update CA_POOL --location ca_region --issuance-policy policy.yaml
   ```

   For information on setting a policy on a pool, see [Using a certificate issuance policy](https://docs.cloud.google.com/certificate-authority-service/docs/use-issuance-policy.md.txt).
5. If you are using a [certificate template](https://docs.cloud.google.com/certificate-authority-service/docs/certificate-template), then configure it now. For more information,
   follow the [CA Service guide](https://docs.cloud.google.com/certificate-authority-service/docs/creating-certificate-template.md.txt#workload-identity) for workload identity certificates.
   Ensure the certificate template is created in the same region as the CA pool. If there are multiple regions for CA pools, then create a certificate
   template per region.

### Roles required to use CA service

For this integration, all workloads in Cloud Service Mesh require the following
[IAM roles](https://docs.cloud.google.com/certificate-authority-service/docs/reference/permissions-and-roles).
These roles-bindings need to be explicitly applied for Cloud Service Mesh workloads:

        WORKLOAD_IDENTITY="FLEET_PROJECT_ID.svc.id.goog:/allAuthenticatedUsers/"

        gcloud privateca pools add-iam-policy-binding CA_POOL \
          --project FLEET_PROJECT_ID \
          --location ca_region \
          --member "group:${WORKLOAD_IDENTITY}" \
          --role "roles/privateca.workloadCertificateRequester"

        gcloud privateca pools add-iam-policy-binding CA_POOL \
          --project FLEET_PROJECT_ID \
          --location ca_region \
          --member "group:${WORKLOAD_IDENTITY}" \
          --role "roles/privateca.auditor"

If using certificate templates:

        gcloud privateca templates add-iam-policy-binding CERT_TEMPLATE_ID \
            --member "group:${WORKLOAD_IDENTITY}" \
            --role "roles/privateca.templateUser"

## Limitations

- Configure and pick the CA before provisioning your Cloud Service Mesh control plane. Changing CA is not supported.

## Configure managed Cloud Service Mesh to use CA Service

> [!NOTE]
> **Note:** Make sure to complete all the following steps before you [enable automatic management](https://docs.cloud.google.com/service-mesh/docs/onboarding/provision-control-plane.md.txt#enable_automatic_management).

1. Verify that the `istio-system` namespace exists, or create it if it is
   missing:

         kubectl create ns istio-system

2. Check if `asm-options` configmap exists in the `istio-system` namespace:

         kubectl get configmap/asm-options -n istio-system

3. Create the configmap if it does not exist:

         kubectl create configmap -n istio-system asm-options

4. Patch the configmap to add CAS configuration:

         kubectl patch configmap/asm-options -n istio-system --type merge \
         -p '{"data":{"ASM_OPTS": "CA=PRIVATECA;CAAddr=projects/CA_PROJECT_ID/locations/ca_region/caPools/CA_POOL"}}'

   If a certificate template is needed, append the template ID to the CA Pool
   address using `:` as a separator:

         kubectl patch configmap/asm-options -n istio-system --type merge \
         -p '{"data":{"ASM_OPTS": "CA=PRIVATECA;CAAddr=projects/CA_PROJECT_ID/locations/ca_region/caPools/CA_POOL:projects/PROJECT_ID/locations/ca_region/certificateTemplates/CERT_TEMPLATE_ID"}}'

After completing the configuration steps, continue the installation of managed Cloud Service Mesh by [enabling automatic management](https://docs.cloud.google.com/service-mesh/docs/onboarding/provision-control-plane.md.txt#enable_automatic_management).