|
| 1 | +--- |
| 2 | +meta: |
| 3 | + title: Setting up and configuring Tink |
| 4 | + description: Follow this guide to learn how to configure your environment and dependencies before using Tink in your projects. |
| 5 | +content: |
| 6 | + h1: Setting up and configuring Tink |
| 7 | + paragraph: Follow this guide to learn how to configure your environment and dependencies before using Tink in your projects. |
| 8 | +tags: key-management dek data-encryption-key cli sdk api encryption |
| 9 | +dates: |
| 10 | + validation: 2025-02-03 |
| 11 | + posted: 2025-02-03 |
| 12 | +--- |
| 13 | + |
| 14 | +This page shows you how to set up and configure Tink for encrypting and decrypting data with Scaleway's Key Manager. |
| 15 | + |
| 16 | +We recommend using Tink with Scaleway’s Key Manager, especially for Go-based applications. |
| 17 | + |
| 18 | +<Macro id="requirements" /> |
| 19 | + |
| 20 | +- A Scaleway account logged into the [console](https://console.scaleway.com) |
| 21 | +- [Owner](/iam/concepts/#owner) status or [IAM permissions](/iam/concepts/#permission) allowing you to perform actions in the intended Organization |
| 22 | +- Downloaded and installed [Go](https://go.dev/doc/install) |
| 23 | +- Installed the [Scaleway Go SDK](https://github.com/scaleway/scaleway-sdk-go) with [valid credentials](/scaleway-sdk/go-sdk/) |
| 24 | +- [Created a Key Manager key](/key-manager/how-to/create-km-key) |
| 25 | + |
| 26 | + |
| 27 | +Tink is a library that helps you perform encryption (securing data) and manage encryption keys. It can work with various key management services (KMS), including Scaleway's Key Manager. |
| 28 | + |
| 29 | +The Scaleway Tink extension generates a unique data encryption key for each piece of data that it encrypts. This method follows the cryptography best practices of using unique data encryption keys for each encryption operation. |
| 30 | +To use Tink with Scaleway Key Manager, you need to install dependencies that let Tink interact with Key Manager. |
| 31 | + |
| 32 | + |
| 33 | +## Setting up environment variables |
| 34 | + |
| 35 | +Open a terminal and export the following environment variables. Make sure that you replace the placeholder values with your own. |
| 36 | + |
| 37 | + ```bash |
| 38 | + export SCW_ACCESS_KEY="<access-key>" |
| 39 | + export SCW_SECRET_KEY="<secret-key>" |
| 40 | + export SCW_DEFAULT_ORGANIZATION_ID="<organization-id>" |
| 41 | + export SCW_PROJECT_ID="<project-id>" |
| 42 | + export SCW_DEFAULT_REGION="<region>" |
| 43 | + export SCW_API_URL="<api-url>" |
| 44 | + export SCW_KM_KEY_ID="<key-id>" |
| 45 | + ``` |
| 46 | + |
| 47 | +## Setting up Tink |
| 48 | + |
| 49 | +1. Open a terminal and access your project directory: |
| 50 | + |
| 51 | + ```shell |
| 52 | + cd <your-project-directory> |
| 53 | + ``` |
| 54 | + |
| 55 | +2. Initialize a Go module in your project directory: |
| 56 | + ```shell |
| 57 | + go mod init <your-project-directory> |
| 58 | + ``` |
| 59 | + |
| 60 | +3. Run the following commands to install the Tink library and the Scaleway Tink Provider for Go: |
| 61 | + |
| 62 | + ```shell |
| 63 | + # Install Tink for Go |
| 64 | + go get -u github.com/tink-crypto/tink-go/v2 |
| 65 | + |
| 66 | + # Install the Scaleway Tink extension |
| 67 | + go get -u github.com/scaleway/tink-go-scwkms |
| 68 | + go mod tidy |
| 69 | + ``` |
| 70 | + |
| 71 | +## Configuring Tink |
| 72 | + |
| 73 | +In order to use Tink for data encryption, you need to provide it with a key URI and a configuration file: |
| 74 | + |
| 75 | +- The key URI points to your key encryption key (KEK) in Scaleway Key Manager. |
| 76 | + |
| 77 | +- The configuration file grants Tink the necessary permissions to access and use the KEK identified by the Key URI. |
| 78 | + |
| 79 | +Tink generates a data encryption key (DEK) which will be used to encrypt your data. The DEK itself is then encrypted using the KEK from Key Manager. This ensures that the DEK is securely protected and can only be decrypted by someone with access to the KEK. |
| 80 | + |
| 81 | +The final output is a single ciphertext that includes both the encrypted data and the encrypted DEK (wrapped DEK). This means that the DEK and the data are both securely packaged together. |
| 82 | + |
| 83 | +Scaleway supports the **Go Tink provider**. |
| 84 | + |
| 85 | +1. [Retrieve the ID](/key-manager/how-to/retrieve-km-key-id) (UUIDv4 format) of your Key Manager's key (KEK). |
| 86 | +2. Copy the following template and paste it into a `.go` file: |
| 87 | + |
| 88 | + ```go |
| 89 | + import ( |
| 90 | + "github.com/scaleway/scaleway-sdk-go/scw" // Library that helps your Go program interact with Scaleway |
| 91 | + "github.com/tink-crypto/tink-go/v2/aead" // Tink library |
| 92 | + "github.com/scaleway/tink-go-scwkms/integration/scwkms" // Scaleway's Tink extension |
| 93 | + ) |
| 94 | + |
| 95 | + const region = "<REGION>" // Replace the placeholder with the region where your key is created |
| 96 | + const keyID = "7f967268-bebb-43b0-9fe2-e13bd23bf421" // Replace the placeholder with the unique ID of your key encryption key |
| 97 | + |
| 98 | + keyURIPrefix := "scw-kms://regions/" + region + "/keys/" |
| 99 | + keyURI := keyURIPrefix + keyID |
| 100 | + |
| 101 | + // Set up a connection to Scaleway |
| 102 | + config, _ := scw.LoadConfig() // Loads your Scaleway configuration |
| 103 | + profile, _ := config.GetActiveProfile() // Gets the active profile (your account settings) |
| 104 | + |
| 105 | + // Set up the connection to use your key in Key Manager |
| 106 | + kms, _ := scwkms.NewClientWithOptions( |
| 107 | + keyURIPrefix, |
| 108 | + scw.WithProfile(profile), // Uses your account profile |
| 109 | + scw.WithEnv(), // Uses environment settings |
| 110 | + ) |
| 111 | + // Prepare the key for encryption and decryption |
| 112 | + kekAEAD, _ := kms.GetAEAD(keyURI) |
| 113 | + ``` |
| 114 | +3. Replace the placeholder values with your own. |
| 115 | + |
| 116 | + <Message type="note"> |
| 117 | + The `kekAEAD` object represents the key in Scaleway’s Key Manager. It allows you to encrypt payloads and decrypt ciphertexts. |
| 118 | + </Message> |
| 119 | + |
| 120 | +Find out how to encrypt and decrypt data with Tink in the [dedicated documentation](/key-manager/api-cli/encrypt-decrypt-data-with-km-dek/). |
0 commit comments