|
| 1 | +--- |
| 2 | +meta: |
| 3 | + title: Create a data encryption key using the Scaleway API and the Scaleway CLI |
| 4 | + description: Discover how to create a data encryption key using the Scaleway API and the Scaleway CLI. |
| 5 | +content: |
| 6 | + h1: Create a data encryption key using the Scaleway API and the Scaleway CLI |
| 7 | + paragraph: Discover how to create a data encryption key using the Scaleway API and the Scaleway CLI. |
| 8 | +tags: key-management dek data-encryption-key cli sdk api encryption |
| 9 | +categories: |
| 10 | + - identity-and-access-management |
| 11 | +dates: |
| 12 | + validation: 2024-12-09 |
| 13 | + posted: 2024-12-09 |
| 14 | +--- |
| 15 | + |
| 16 | +<Macro id="requirements" /> |
| 17 | + |
| 18 | +- A Scaleway account logged into the [console](https://console.scaleway.com) |
| 19 | +- [Owner](/identity-and-access-management/iam/concepts/#owner) status or [IAM permissions](/identity-and-access-management/iam/concepts/#permission) allowing you to perform actions in the intended Organization |
| 20 | +- Created a key encryption key either from the [Scaleway console](/identity-and-access-management/key-manager/how-to/create-km-key) or the [Key Manager API](https://www.scaleway.com/en/developers/api/key-manager/#path-keys-create-a-key) |
| 21 | +- Retrieved your key encryption key's ID |
| 22 | +- Created an [API key](/identity-and-access-management/iam/how-to/create-api-keys/) |
| 23 | +- Downloaded and configured the [Scaleway CLI](https://github.com/scaleway/scaleway-cli?tab=readme-ov-file#getting-started) |
| 24 | + |
| 25 | +## Generate a DEK using the Scaleway CLI |
| 26 | + |
| 27 | +1. Open a terminal and paste the following commands to export your environment variables. Make sure that you replace the placeholder values with your own. |
| 28 | + ```bash |
| 29 | + export SCW_ACCESS_KEY=<SCALEWAY_API_ACCESS_KEY> |
| 30 | + export SCW_SECRET_KEY=<SCALEWAY_API_SECRET_KEY> |
| 31 | + export SCW_DEFAULT_ORGANIZATION_ID=<SCALEWAY_ORGANIZATION_ID> |
| 32 | + export SCW_PROJECT_ID=<SCALEWAY_PROJECT_ID> |
| 33 | + export SCW_DEFAULT_REGION="fr-par" |
| 34 | + export SCW_API_URL="https://api.scaleway.com" |
| 35 | + ``` |
| 36 | + |
| 37 | +2. Paste the following command to generate a data encryption key via the Scaleway CLI. Make sure that you replace `<your_kek_id>` with the ID of your key encryption key. |
| 38 | + ```bash |
| 39 | + scw keymanager key generate-data-key key-id=<your_kek_id> algorithm=aes_256_gcm |
| 40 | + ``` |
| 41 | + |
| 42 | +An output similar to the following should display: |
| 43 | + ```bash |
| 44 | + KeyID <kek_id> |
| 45 | + Algorithm <algorithm_used_to_encrypt_your_key> |
| 46 | + Ciphertext <your_base64_encrypted_dek> |
| 47 | + Plaintext <your_base64_decrypted_dek> |
| 48 | + CreatedAt <creation_date> |
| 49 | + ``` |
| 50 | + |
| 51 | + |
| 52 | +## Generate a DEK using the API |
| 53 | + |
| 54 | +Paste the following command to create your data encryption key via the Key Manager API. Make sure that you replace the placeholder values with your own. |
| 55 | + ```bash |
| 56 | + curl --location 'https://api.scaleway.com/key-manager/v1alpha1/regions/fr-par/keys/<your_key_id>/generate-data-key' \ |
| 57 | + --header 'Content-Type: application/json' \ |
| 58 | + --header 'X-Auth-Token: <your_secret_key>' \ |
| 59 | + --data '{ |
| 60 | + "algorithm": "aes_256_gcm" |
| 61 | + }' |
| 62 | + ``` |
| 63 | + |
| 64 | +Key Manager also supports the `GenerateDataKey` request without a plaintext operation, which only returns an encrypted data encryption key. |
| 65 | + |
| 66 | +If you need to use your DEK, you can decrypt it using the [Decrypt data operation](https://www.scaleway.com/en/developers/api/key-manager/#path-keys-decrypt-data) specifying the `kek_id` parameter used to encrypt it. |
| 67 | + |
| 68 | +Key Manager **does not allow the use of data encryption keys for data encryption**. |
| 69 | + |
| 70 | +However, you can use the DEK independently from Key Manager, for example with the [Tink extension](/encrypt-decrypt-dek-/#encrypt-and-decrypt-data-with-tink-and-key-manager) or with [OpenSSL](/encrypt-decrypt-dek/#manually-encrypt-and-decrypt-data-with-a-key-manager-dek). |
| 71 | + |
| 72 | + |
| 73 | +!!! info |
| 74 | + |
| 75 | + The way the KEK is generated, its length, and the encryption algorithm used, **cannot be changed or customized after creation**. |
| 76 | + |
| 77 | + However, unlike the KEK, you have the flexibility to choose any encryption algorithm (cipher) you prefer for encrypting and decrypting your data with the DEK. You are not restricted to a specific encryption method for the data itself. |
| 78 | + |
| 79 | + **We highly recommend that you use standard and well-established ciphers (and the proper mode), as well as a library like Tink, that chooses the right cryptosystem according to your use-case.** |
0 commit comments