Bucket encryption
In Object Storage, you can encrypt objects in a bucket using KMS keys in any of the following ways:
- Set up encryption for the bucket to encrypt all new objects with the specified key.
- Specify an encryption key when uploading an object using the API.
Alert
Object Storage employs envelope encryption to secure data. Deleting a key is the same as deleting all data encrypted with that key.
To work with objects in an encrypted bucket, a user or service account must have the following roles for the encryption key in addition to the storage.configurer role:
kms.keys.encrypter: To read the key, encrypt and upload objects.kms.keys.decrypter: To read the key, decrypt and download objects.kms.keys.encrypterDecrypter: This role includes thekms.keys.encrypterandkms.keys.decrypterpermissions.
For more information, see Key Management Service service roles.
Setting up encryption for a bucket
To add a KMS key:
-
In the management console
, select a folder. -
Go to Object Storage.
-
Select the bucket you want to configure encryption for.
-
In the left-hand panel, select
Security. -
Select the Encryption tab.
-
In the KMS Key field, select an existing key or create a new one:
- If the folder does not contain any keys yet, click Create key. If the folder contains keys but they are not suitable, click Create.
- Enter a name for the key.
- Select an encryption algorithm and a rotation period.
- Click Create.
-
Click Save.
Note
Terraform uses a service account to interact with Object Storage. Assign to the service account the required role, e.g., storage.admin, for the folder where you are going to create resources.
If you do not have Terraform yet, install it and configure the Yandex Cloud provider.
Before you start, get an IAM token for your service account and save it to a file.
-
In the configuration file, describe the properties of resources you want to create:
provider "yandex" { cloud_id = "<cloud_ID>" folder_id = "<folder_ID>" zone = "ru-central1-a" service_account_key_file = "key.json" } resource "yandex_iam_service_account" "sa" { name = "<service_account_name>" } // Assigning a role to a service account resource "yandex_resourcemanager_folder_iam_member" "sa-admin" { folder_id = "<folder_ID>" role = "storage.admin" member = "serviceAccount:${yandex_iam_service_account.sa.id}" } // Creating a static access key resource "yandex_iam_service_account_static_access_key" "sa-static-key" { service_account_id = yandex_iam_service_account.sa.id description = "static access key for object storage" } resource "yandex_kms_symmetric_key" "key-a" { name = "<key_name>" description = "<key_description>" default_algorithm = "AES_128" rotation_period = "8760h" // 1 year } resource "yandex_storage_bucket" "test" { bucket = "<bucket_name>" access_key = yandex_iam_service_account_static_access_key.sa-static-key.access_key secret_key = yandex_iam_service_account_static_access_key.sa-static-key.secret_key server_side_encryption_configuration { rule { apply_server_side_encryption_by_default { kms_master_key_id = yandex_kms_symmetric_key.key-a.id sse_algorithm = "aws:kms" } } } }Where:
service_account_key_file: Path to the file with your service account's IAM token (or the file contents).default_algorithm: Encryption algorithm to use with a new key version. A new version of the key is generated with each key rotation. The default value isAES_128.rotation_period: Rotation period. To disable automatic rotation, skip this parameter.apply_server_side_encryption_by_default: Default encryption settings on the server side:kms_master_key_id: ID of the KMS master key used for encryption.sse_algorithm: Encryption algorithm used on the server side. The only supported value isaws:kms.
-
Make sure the configuration files are correct.
- In the command line, navigate to the directory where you created the configuration file.
- Run a check using this command:
terraform plan
If the configuration description is correct, the terminal will display a list of the resources being created and their settings. If the configuration contains any errors, Terraform will point them out.
-
Deploy the cloud resources.
- If the configuration does not contain any errors, run this command:
terraform apply- Confirm creating the resources.
This will create all the resources you need in the specified folder. You can check the new resources and their configuration using the management console
.
Removing bucket encryption
To remove encryption, delete the KMS key:
- In the management console
, select a folder. - Go to Object Storage.
- Select the bucket you want to remove encryption for.
- In the left-hand panel, select
Security. - Select the Encryption tab.
- In the KMS Key field, select Not selected.
- Click Save.
With Terraform
Terraform is distributed under the Business Source License
For more information about the provider resources, see the relevant documentation on the Terraform
If you do not have Terraform yet, install it and configure the Yandex Cloud provider.
To remove encryption for a bucket created using Terraform:
-
Open the Terraform configuration file and delete the
server_side_encryption_configurationsection from the bucket description.Example of a bucket description in Terraform configuration
... resource "yandex_storage_bucket" "test" { bucket = "my-bucket" access_key = "123JE02jKxusn********" secret_key = "ExamP1eSecReTKeykdo********" server_side_encryption_configuration { // Delete this section to disable encryption rule { apply_server_side_encryption_by_default { kms_master_key_id = "abjbeb2bgg4l********" sse_algorithm = "aws:kms" } } } } ... -
In the command line, navigate to the directory with the Terraform configuration file.
-
Check the configuration using this command:
terraform validateIf the configuration is correct, you will get this message:
Success! The configuration is valid. -
Run this command:
terraform planYou will see a detailed list of resources. No changes will be made at this step. If the configuration contains any errors, Terraform will show them.
-
Apply the changes:
terraform apply -
Confirm the changes: type
yesinto the terminal and press Enter.You can check the update using the management console
.