[Yandex Cloud documentation](../../index.md) > [Yandex Key Management Service](../index.md) > [Step-by-step guides](index.md) > Symmetric encryption > Encryption key access permissions

# Configuring access permissions for a symmetric encryption key

You can grant access to a [symmetric key](../concepts/key.md) to a user, service account, or user group. To do this, assign [roles](../../iam/concepts/access-control/roles.md) for the key. To choose the ones you need, [learn](../security/index.md#roles-list) about the service's roles.

## Assigning a role {#add-access-binding}

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), select the folder where the secret is stored.
  1. Navigate to **Key Management Service**.
  1. In the left-hand panel, select ![image](../../_assets/console-icons/key.svg) **Symmetric keys**.
  1. Click the name of the key you need.
  1. Navigate to ![image](../../_assets/console-icons/persons.svg) **Access bindings** and click **Assign roles**.
  1. Select the group, user, or service account you want to grant access to the key.
  1. Click ![image](../../_assets/console-icons/plus.svg) **Add role** and select the required roles.
  1. Click **Save**.

- CLI {#cli}

  If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../cli/quickstart.md#install).

  The folder used by default is the one specified when [creating](../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

  To assign a role for a symmetric key:

  1. View the description of the CLI command for assigning roles:

     ```bash
     yc kms symmetric-key add-access-binding --help
     ```

  1. Get a list of symmetric keys with their IDs:

     ```bash
     yc kms symmetric-key list
     ```

  1. Get the [ID of the user](../../organization/operations/users-get.md), [service account](../../iam/operations/sa/get-id.md), or user group you are assigning a role to.
  1. Use one of these commands to assign a role:

     * To a user:

        ```bash
        yc kms symmetric-key add-access-binding \
          --id <key_ID> \
          --role <role> \
          --user-account-id <user_ID>
        ```

     * To a federated user:

        ```bash
        yc kms symmetric-key add-access-binding \
          --id <key_ID> \
          --role <role> \
          --subject federatedUser:<user_ID>
        ```

     * To a service account:

        ```bash
        yc kms symmetric-key add-access-binding \
          --id <key_ID> \
          --role <role> \
          --service-account-id <service_account_ID>
        ```

     * To a user group:

        ```bash
        yc kms symmetric-key add-access-binding \
          --id <key_ID> \
          --role <role> \
          --subject group:<group_ID>
        ```

- Terraform {#tf}

  With [Terraform](https://www.terraform.io/), you can quickly create a cloud infrastructure in Yandex Cloud and manage it using configuration files. These files store the infrastructure description written in HashiCorp Configuration Language (HCL). If you change the configuration files, Terraform automatically detects which part of your configuration is already deployed, and what should be added or removed.
  
  Terraform is distributed under the [Business Source License](https://github.com/hashicorp/terraform/blob/main/LICENSE). The [Yandex Cloud provider for Terraform](https://github.com/yandex-cloud/terraform-provider-yandex) is distributed under the [MPL-2.0](https://www.mozilla.org/en-US/MPL/2.0/) license.
  
  For more information about the provider resources, see the guides on the [Terraform](https://www.terraform.io/docs/providers/yandex/index.html) website or [its mirror](../../terraform/index.md).

  If you do not have Terraform yet, [install it and configure the Yandex Cloud provider](../../tutorials/infrastructure-management/terraform-quickstart.md#install-terraform).
  
  
  To manage infrastructure using Terraform under a service account or user accounts (a Yandex account, a federated account, or a local user), [authenticate](../../terraform/authentication.md) using the appropriate method.

  To assign a role for a symmetric encryption key using Terraform:

  1. In the Terraform configuration file, describe the resources you want to create:

      ```hcl
      resource "yandex_kms_symmetric_key_iam_member" "key-viewers" {
        symmetric_encryption_key_id = "<key_ID>"

        role   = "<role>"
        member = "<subject_type>:<subject_ID>"
      }
      ```

      Where:

      * `symmetric_encryption_key_id`: ID of the symmetric encryption key.
      * `role`: [Role](../security/index.md#roles-list) to assign.
      * `member`: Type and ID of the [subject](../../iam/concepts/access-control/index.md#subject) the role is assigned to. Specify it as `userAccount:<user_ID>` or `serviceAccount:<service_account_ID>`.

      For more information about the `yandex_kms_symmetric_key_iam_member` resource properties, see [this provider guide](../../terraform/resources/kms_symmetric_key_iam_member.md).

  1. Create the resources:

      1. In the terminal, navigate to the configuration file directory.
      1. Make sure the configuration is correct using this command:
      
         ```bash
         terraform validate
         ```
      
         If the configuration is valid, you will get this message:
      
         ```bash
         Success! The configuration is valid.
         ```
      
      1. Run this command:
      
         ```bash
         terraform plan
         ```
      
         You will see a list of resources and their properties. No changes will be made at this step. Terraform will show any errors in the configuration.
      1. Apply the configuration changes:
      
         ```bash
         terraform apply
         ```
      
      1. Type `yes` and press **Enter** to confirm the changes.

      Terraform will create all the required resources. You can check the new resources using this [CLI](../../cli/index.md) command:

      ```bash
      yc kms symmetric-key list-access-bindings <key_ID>
      ```

- API {#api}

  Use the [updateAccessBindings](../api-ref/SymmetricKey/updateAccessBindings.md) method for the [SymmetricKey](../api-ref/SymmetricKey/index.md) resource or the [SymmetricKeyService/UpdateAccessBindings](../api-ref/grpc/SymmetricKey/updateAccessBindings.md) gRPC API call and provide the following in the request:

  * `ADD` value in the `accessBindingDeltas[].action` parameter to add a role.
  * Role in the `accessBindingDeltas[].accessBinding.roleId` parameter.
  * ID of the subject you are assigning the role to in the `accessBindingDeltas[].accessBinding.subject.id` parameter.
  * Type of the subject you are assigning the role to in the `accessBindingDeltas[].accessBinding.subject.type` parameter.

{% endlist %}

## Assigning multiple roles {#set-access-bindings}

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), select the folder where the secret is stored.
  1. Navigate to **Key Management Service**.
  1. In the left-hand panel, select ![image](../../_assets/console-icons/key.svg) **Symmetric keys**.
  1. Click the name of the key you need.
  1. Navigate to ![image](../../_assets/console-icons/persons.svg) **Access bindings** and click **Assign roles**.
  1. Select the group, user, or service account you want to grant access to the key.
  1. Click ![image](../../_assets/console-icons/plus.svg) **Add role** and select the required roles.
  1. Click **Save**.

- CLI {#cli}

  {% note alert %}
  
  The `set-access-bindings` command for assigning multiple roles completely rewrites access permissions for the resource. All current resource roles will be deleted.
  
  {% endnote %}

  If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../cli/quickstart.md#install).

  The folder used by default is the one specified when [creating](../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

  To assign multiple roles for a symmetric encryption key:

  1. Make sure the symmetric key has no roles assigned that you would not want to lose:

     ```bash
     yc kms symmetric-key list-access-bindings \
       --id <key_ID>
     ```

  1. View the description of the CLI command for assigning roles:

     ```bash
     yc kms symmetric-key set-access-bindings --help
     ```

  1. Get a list of symmetric encryption keys with their IDs:

     ```bash
     yc kms symmetric-key list
     ```

  1. Get the [ID of the user](../../organization/operations/users-get.md), [service account](../../iam/operations/sa/get-id.md), or user group you are assigning roles to.
  1. Use one of the commands below to assign roles:

     * To a Yandex account user or local user:

        ```bash
        yc kms symmetric-key set-access-bindings \
          --id <key_ID> \
          --access-binding role=<role>,user-account-id=<user_ID>
        ```

     * To a federated user:

        ```bash
        yc kms symmetric-key set-access-bindings \
          --id <key_ID> \
          --access-binding role=<role>,subject=federatedUser:<user_ID>
        ```

     * To a service account:

        ```bash
        yc kms symmetric-key set-access-bindings \
          --id <key_ID> \
          --access-binding role=<role>,service-account-id=<service_account_ID>
        ```

     * To a user group:

        ```bash
        yc kms symmetric-key set-access-bindings \
          --id <key_ID> \
          --access-binding role=<role>,subject=group:<group_ID>
        ```

     Provide a separate `--access-binding` parameter for each role. Here is an example:

      ```bash
      yc kms symmetric-key set-access-bindings \
        --id <key_ID> \
        --access-binding role=<role_1>,service-account-id=<service_account_ID> \
        --access-binding role=<role_2>,service-account-id=<service_account_ID> \
        --access-binding role=<role_3>,service-account-id=<service_account_ID>
      ```

- Terraform {#tf}

  With [Terraform](https://www.terraform.io/), you can quickly create a cloud infrastructure in Yandex Cloud and manage it using configuration files. These files store the infrastructure description written in HashiCorp Configuration Language (HCL). If you change the configuration files, Terraform automatically detects which part of your configuration is already deployed, and what should be added or removed.
  
  Terraform is distributed under the [Business Source License](https://github.com/hashicorp/terraform/blob/main/LICENSE). The [Yandex Cloud provider for Terraform](https://github.com/yandex-cloud/terraform-provider-yandex) is distributed under the [MPL-2.0](https://www.mozilla.org/en-US/MPL/2.0/) license.
  
  For more information about the provider resources, see the guides on the [Terraform](https://www.terraform.io/docs/providers/yandex/index.html) website or [its mirror](../../terraform/index.md).

  If you do not have Terraform yet, [install it and configure the Yandex Cloud provider](../../tutorials/infrastructure-management/terraform-quickstart.md#install-terraform).
  
  
  To manage infrastructure using Terraform under a service account or user accounts (a Yandex account, a federated account, or a local user), [authenticate](../../terraform/authentication.md) using the appropriate method.

  To assign multiple roles for a symmetric encryption key using Terraform:

  1. In the Terraform configuration file, describe the resources you want to create:

      ```hcl
      # Role 1
      resource "yandex_kms_symmetric_key_iam_member" "key-viewers" {
        symmetric_encryption_key_id = "<key_ID>"

        role   = "<role_1>"
        member = "<subject_type>:<subject_ID>"
      }

      # Role 2
      resource "yandex_kms_symmetric_key_iam_member" "key-editors" {
        symmetric_encryption_key_id = "<key_ID>"

        role   = "<role_2>"
        member = "<subject_type>:<subject_ID>"
      }
      ```

      Where:

      * `symmetric_encryption_key_id`: ID of the symmetric encryption key.
      * `role`: [Role](../security/index.md#roles-list) to assign.
      * `member`: Type and ID of the [subject](../../iam/concepts/access-control/index.md#subject) the role is assigned to. Specify it as `userAccount:<user_ID>` or `serviceAccount:<service_account_ID>`.

      For more information about the `yandex_kms_symmetric_key_iam_member` resource properties, see [this provider guide](../../terraform/resources/kms_symmetric_key_iam_member.md).

  1. Create the resources:

      1. In the terminal, navigate to the configuration file directory.
      1. Make sure the configuration is correct using this command:
      
         ```bash
         terraform validate
         ```
      
         If the configuration is valid, you will get this message:
      
         ```bash
         Success! The configuration is valid.
         ```
      
      1. Run this command:
      
         ```bash
         terraform plan
         ```
      
         You will see a list of resources and their properties. No changes will be made at this step. Terraform will show any errors in the configuration.
      1. Apply the configuration changes:
      
         ```bash
         terraform apply
         ```
      
      1. Type `yes` and press **Enter** to confirm the changes.

      Terraform will create all the required resources. You can check the new resources using this [CLI](../../cli/index.md) command:

      ```bash
      yc kms symmetric-key list-access-bindings <key_ID>
      ```

- API {#api}

  {% note alert %}
  
  The `setAccessBindings` method for assigning multiple roles completely rewrites access permissions for the resource. All current resource roles will be deleted.
  
  {% endnote %}

  Use the [SetAccessBindings](../api-ref/SymmetricKey/setAccessBindings.md) method for the [SymmetricKey](../api-ref/SymmetricKey/index.md) resource or the [SymmetricKeyService/SetAccessBindings](../api-ref/grpc/SymmetricKey/setAccessBindings.md) gRPC API call. In your request, provide an array of objects, each one matching a particular role and containing the following data:

  * Role in the `accessBindings[].roleId` parameter.
  * ID of the subject getting the roles in the `accessBindings[].subject.id` parameter.
  * Type of the subject getting the roles in the `accessBindings[].subject.type` parameter.

{% endlist %}