This document shows how to use customer-managed encryption keys (CMEK) to
encrypt and control data-at-rest in a cloud service through
[Cloud Key Management Service](https://docs.cloud.google.com/kms/docs.md.txt). CMEK is integrated with
[code customization](https://docs.cloud.google.com/gemini/docs/codeassist/code-customization-overview.md.txt) for
Gemini Code Assist. Gemini Code Assist doesn't support the use
of [Cloud EKM](https://docs.cloud.google.com/kms/docs/ekm.md.txt) keys.

In this document, you do the following:

- Learn how to create a CMEK.
- Grant permissions to the Gemini Code Assist service account.
- Create a code repository index with a CMEK.
- Remove access to a CMEK repository.

By default, code customization for Gemini Code Assist encrypts customer content at
rest. code customization handles encryption for you without any
additional actions on your part. This option is called *Google default encryption*.

If you want to control your encryption keys, then you can use customer-managed encryption keys
(CMEKs) in [Cloud KMS](https://docs.cloud.google.com/kms/docs.md.txt) with CMEK-integrated services including
code customization. Using Cloud KMS keys gives you control over their protection
level, location, rotation schedule, usage and access permissions, and cryptographic boundaries.
Using Cloud KMS also lets
you view audit logs and control key lifecycles.

Instead of Google owning and managing the symmetric
[key encryption keys (KEKs)](https://docs.cloud.google.com/kms/docs/envelope-encryption.md.txt#key_encryption_keys) that protect your data, you control and
manage these keys in Cloud KMS.

After you set up your resources with CMEKs, the experience of accessing your
code customization resources is similar to using Google default encryption.
For more information about your encryption
options, see [Customer-managed encryption keys (CMEK)](https://docs.cloud.google.com/kms/docs/cmek.md.txt).

## Before you begin

1. In the Google Cloud console, activate Cloud Shell.

   [Activate Cloud Shell](https://console.cloud.google.com/?cloudshell=true)
2. In the development environment where you set up the gcloud CLI, run the
   [`gcloud components update` command](https://docs.cloud.google.com/sdk/gcloud/reference/components/update)
   to make sure that you have updated all installed components of the
   [gcloud CLI](https://docs.cloud.google.com/sdk/gcloud.md.txt) to the latest version.

   ```bash
   gcloud components update
   ```

## Create a CMEK and grant permissions

To create a CMEK and grant the Gemini Code Assist service account
permissions on the key, perform the following tasks:

1. In the Google Cloud project where you want to manage your keys, do the
   following:

   1. [Enable the Cloud Key Management Service API](https://console.cloud.google.com/flows/enableapi?apiid=cloudkms.googleapis.com&redirect=https://console.cloud.google.com/).

   2. Create the [key ring](https://docs.cloud.google.com/kms/docs/create-key-ring.md.txt) and
      [key](https://docs.cloud.google.com/kms/docs/create-key.md.txt) directly in Cloud KMS.

2. Grant the [CryptoKey Encrypter/Decrypter IAM role](https://docs.cloud.google.com/iam/docs/roles-permissions/cloudkms.md.txt#cloudkms.cryptoKeyEncrypterDecrypter)
   (`roles/cloudkms.cryptoKeyEncrypterDecrypter`) to the
   Gemini Code Assist service account. Grant this permission on
   the key that you created.

   ### Console

   1. Go to **Key management**.

      [Go to Key management](https://console.cloud.google.com/security/kms)
   2. Select the key that you created.

   3. Grant access to the Gemini Code Assist service account:

      1. Click **Add principal**.
      2. Add the Gemini Code Assist service account. The service account is `service-PROJECT_NUMBER@gcp-sa-cloudaicompanions.iam.gserviceaccount.com`, where <var translate="no">PROJECT_NUMBER</var> is the [project number](https://docs.cloud.google.com/resource-manager/docs/creating-managing-projects.md.txt#identifying_projects) of the Google Cloud project where Gemini Code Assist is enabled.
      3. In **Select a role** , select **Cloud KMS** \> **Cloud KMS CryptoKey Encrypter/Decrypter**.
      4. Click **Save**.
   4. Repeat the previous step to grant access to the account that will
      create the code repository index with a CMEK.

   5. Return to the **[Key management](https://console.cloud.google.com/security/kms)**
      page and select the key again.

   6. Select **Show info panel** . You should see roles in the
      **Role/Member** column.

   ### gcloud CLI

   1. To grant access to the Gemini Code Assist service
      account, in a shell environment, use the
      [`kms keys add-iam-policy-binding`command](https://docs.cloud.google.com/sdk/gcloud/reference/projects/add-iam-policy-binding):

          gcloud kms keys add-iam-policy-binding KEY_NAME \
              --project=PROJECT_ID \
              --location=LOCATION \
              --keyring=KEYRING_NAME \
              --member="serviceAccount:service-PROJECT_NUMBER@gcp-sa-cloudaicompanion.iam.gserviceaccount.com" \
              --role="roles/cloudkms.cryptoKeyEncrypterDecrypter"

      Replace the following:
      - <var translate="no">KEY_NAME</var>: the key name.
      - <var translate="no">PROJECT_ID</var>: the ID of the project that contains the key.
      - <var translate="no">LOCATION</var>: the key location.
      - <var translate="no">KEYRING_NAME</var>: the key ring name.
      - <var translate="no">PROJECT_NUMBER</var>: the [project number](https://docs.cloud.google.com/resource-manager/docs/creating-managing-projects.md.txt#identifying_projects) of the Google Cloud project with Gemini Code Assist enabled.
   2. Repeat the previous step to grant access to the account that will
      create the code repository index with a CMEK.

   For more information about this command, see the
   [`gcloud kms keys add-iam-policy-binding` documentation](https://docs.cloud.google.com/sdk/gcloud/reference/kms/keys/add-iam-policy-binding).

You can now
[create a code repository index with a CMEK](https://docs.cloud.google.com/gemini/docs/codeassist/encrypt-data-cmek.md.txt#create_a_code_repository_index_with_a_cmek)
using the API, and specify the key to use for encryption.

## Create a code repository index with a CMEK

To create a new repository that has CMEK protection, do one of the following:

### gcloud CLI

Use the [`gemini code-repository-indexes create` command](https://docs.cloud.google.com/sdk/gcloud/reference/gemini/code-repository-indexes/create):

    gcloud gemini code-repository-indexes create CODE_REPOSITORY_INDEX_NAME \
        --location=LOCATION \
        --kms-key="projects/KEY_PROJECT_ID/locations/LOCATION/keyRings/KEYRING_NAME/cryptoKeys/KEY_NAME"

Replace the following:

- <var translate="no">CODE_REPOSITORY_INDEX_NAME</var>: the name of the new code repository index that you'll create.
- <var translate="no">LOCATION</var>: the key location.
- <var translate="no">KEY_PROJECT_ID</var>: the key project ID.
- <var translate="no">KEYRING_NAME</var>: the key ring name.
- <var translate="no">KEY_NAME</var>: the key name.

### API

1. Create a JSON file that contains the following information:

   ```
     {
       "kmsKey": "projects/KEY_PROJECT_ID/locations/KEY_LOCATION/keyRings/KEYRING_NAME/cryptoKeys/KEY_NAME"
     }
   ```

   Replace the following:
   - `KEY_PROJECT_ID`: the key project ID
   - `KEY_LOCATION`: the key location
   - `KEYRING_NAME`: the key ring name
   - `KEY_NAME`: the key name
2. Use a [`cURL`](http://curl.haxx.se/) command to call the
   [`projects.locations.codeRepositoryIndexes.create` method](https://docs.cloud.google.com/gemini/docs/api/reference/rest/v1/projects.locations.codeRepositoryIndexes/create.md.txt):

   ```
   curl -X POST --data-binary @JSON_FILE_NAME \
       -H "Authorization: Bearer $(gcloud auth print-access-token)" \
       -H "Content-Type: application/json" \
       "https://cloudaicompanion.googleapis.com/v1/projects/PROJECT_ID/locations/KEY_LOCATION/codeRepositoryIndexes?codeRepositoryIndexId=CODE_REPOSITORY_INDEX_NAME"
   ```

   Replace the following:
   - `JSON_FILE_NAME`: the path for the JSON file that you created in the preceding step.
   - `PROJECT_ID`: the ID of the project to create the repository in.
   - `KEY_LOCATION`: the location to create the repository in, which must match the location where the CMEK exists.
   - `CODE_REPOSITORY_INDEX_NAME`: the name of the new code repository index that you'll create. For example, `zg-btf-0001`.

The response returns a set of log entries.

## Remove access to a CMEK repository

> [!WARNING]
> **Warning:** If you disable the CMEK, Google Cloud removes the instance and the service will no longer be available, even if you re-enable the key.

There are several ways to remove access to a CMEK-encrypted repository:

- Revoke the Cloud KMS CryptoKey Encrypter/Decrypter [role](https://docs.cloud.google.com/kms/docs/reference/permissions-and-roles.md.txt#predefined_roles) from the Gemini Code Assist service account using the [Google Cloud console](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access.md.txt#revoke_access) or the [gcloud CLI](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access.md.txt#revoking-gcloud-manual).
- [Temporarily disable](https://docs.cloud.google.com/kms/docs/enable-disable.md.txt#disable_an_enabled_key_version) the CMEK.
- [Permanently destroy](https://docs.cloud.google.com/kms/docs/destroy-restore.md.txt#schedule_a_key_version_for_destruction_destroy_a_key_version) the CMEK.

We recommend that you revoke the permissions from the
Gemini Code Assist service account before disabling or destroying
a key. Changes to permissions are consistent within seconds, so you can observe
the impacts of disabling or destroying a key.