1*e7b1675dSTing-Kang Chang# Python Google Cloud Storage (GCS) client-side encryption example 2*e7b1675dSTing-Kang Chang 3*e7b1675dSTing-Kang ChangThis example shows how to encrypt/decrypt GCS blobs with Tink using 4*e7b1675dSTing-Kang Chang[Envelope Encryption](https://cloud.google.com/kms/docs/envelope-encryption). 5*e7b1675dSTing-Kang Chang 6*e7b1675dSTing-Kang ChangIt shows how you can use Tink to encrypt data with a newly generated *data 7*e7b1675dSTing-Kang Changencryption key* (DEK) which is wrapped with a KMS key. The data will be 8*e7b1675dSTing-Kang Changencrypted with AES256 GCM using the DEK and the DEK will be encrypted with the 9*e7b1675dSTing-Kang ChangKMS key and stored alongside the ciphertext in GCS. 10*e7b1675dSTing-Kang Chang 11*e7b1675dSTing-Kang ChangThe CLI takes the following required arguments: 12*e7b1675dSTing-Kang Chang 13*e7b1675dSTing-Kang Chang* `--mode`: Either `encrypt` or `decrypt` to indicate if you want to encrypt 14*e7b1675dSTing-Kang Chang or decrypt. 15*e7b1675dSTing-Kang Chang* `--kek_uri`: The URI for the Cloud KMS key to be used for envelope encryption. 16*e7b1675dSTing-Kang Chang* `--gcp_credential_path`: Name of the file with the Google Cloud Platform (GCP) 17*e7b1675dSTing-Kang Chang credentials (in JSON format) that can access the Cloud KMS key and the GCS 18*e7b1675dSTing-Kang Chang input/output blobs. 19*e7b1675dSTing-Kang Chang* `--gcp_project_id`: The ID of the GCP project hosting the GCS blobs that you 20*e7b1675dSTing-Kang Chang want to encrypt or decrypt. 21*e7b1675dSTing-Kang Chang* `--local_path`: 22*e7b1675dSTing-Kang Chang * When `--mode encrypt`, read the plaintext from this local file. 23*e7b1675dSTing-Kang Chang * When `--mode decrypt`, write the decryption result to this local file. 24*e7b1675dSTing-Kang Chang* `--gcs_blob_path`: 25*e7b1675dSTing-Kang Chang * Format: `gs://my-bucket-name/my-object-name` 26*e7b1675dSTing-Kang Chang * When `--mode encrypt`, write the encryption result to this blob in GCS. 27*e7b1675dSTing-Kang Chang The encryption result is bound to the location of this blob. That is, if 28*e7b1675dSTing-Kang Chang you rename or move it to a different bucket, decryption will fail. 29*e7b1675dSTing-Kang Chang * When `--mode decrypt`, read the ciphertext from this blob in GCS. 30*e7b1675dSTing-Kang Chang 31*e7b1675dSTing-Kang Chang## Build and run 32*e7b1675dSTing-Kang Chang 33*e7b1675dSTing-Kang Chang### Prequisite 34*e7b1675dSTing-Kang Chang 35*e7b1675dSTing-Kang ChangThis envelope encryption example uses a Cloud KMS key as a key-encryption key 36*e7b1675dSTing-Kang Chang(KEK). In order to run it, you need to: 37*e7b1675dSTing-Kang Chang 38*e7b1675dSTing-Kang Chang* Create a symmetric key on Cloud KMS. Copy the key URI which is in this 39*e7b1675dSTing-Kang Chang format: 40*e7b1675dSTing-Kang Chang `projects/<my-project>/locations/global/keyRings/<my-key-ring>/cryptoKeys/<my-key>` 41*e7b1675dSTing-Kang Chang 42*e7b1675dSTing-Kang Chang* Create a bucket on GCS. 43*e7b1675dSTing-Kang Chang 44*e7b1675dSTing-Kang Chang* Create a service account that is allowed to encrypt and decrypt with the 45*e7b1675dSTing-Kang Chang Cloud KMS key, and read/write to the GCS bucket. Then download the JSON 46*e7b1675dSTing-Kang Chang credentials file. 47*e7b1675dSTing-Kang Chang 48*e7b1675dSTing-Kang Chang### Bazel 49*e7b1675dSTing-Kang Chang 50*e7b1675dSTing-Kang ChangBuild the examples: 51*e7b1675dSTing-Kang Chang 52*e7b1675dSTing-Kang Chang```shell 53*e7b1675dSTing-Kang Chang$ git clone https://github.com/google/tink 54*e7b1675dSTing-Kang Chang$ cd tink/python/examples 55*e7b1675dSTing-Kang Chang$ bazel build ... 56*e7b1675dSTing-Kang Chang``` 57*e7b1675dSTing-Kang Chang 58*e7b1675dSTing-Kang ChangYou can then encrypt a file and upload the result to GCS: 59*e7b1675dSTing-Kang Chang 60*e7b1675dSTing-Kang Chang```shell 61*e7b1675dSTing-Kang Chang$ echo "some data" > testdata.txt 62*e7b1675dSTing-Kang Chang$ ./bazel-bin/gcs/gcs_envelope_aead \ 63*e7b1675dSTing-Kang Chang --mode encrypt \ 64*e7b1675dSTing-Kang Chang --kek_uri gcp-kms://my-cloud-kms-key-uri \ 65*e7b1675dSTing-Kang Chang --gcp_credential_path my-service-account.json \ 66*e7b1675dSTing-Kang Chang --gcp_project_id my-gcp-project-id \ 67*e7b1675dSTing-Kang Chang --local_path testdata.txt \ 68*e7b1675dSTing-Kang Chang --gcs_blob_path gs://my-bucket-name/my-blob-name 69*e7b1675dSTing-Kang Chang``` 70*e7b1675dSTing-Kang Chang 71*e7b1675dSTing-Kang ChangOr download a file from GCS and decrypt it: 72*e7b1675dSTing-Kang Chang 73*e7b1675dSTing-Kang Chang```shell 74*e7b1675dSTing-Kang Chang$ ./bazel-bin/gcs/gcs_envelope_aead 75*e7b1675dSTing-Kang Chang --mode decrypt \ 76*e7b1675dSTing-Kang Chang --kek_uri gcp-kms://my-key-uri \ 77*e7b1675dSTing-Kang Chang --gcp_credential_path my-service-account.json \ 78*e7b1675dSTing-Kang Chang --gcp_project_id my-gcp-project-id \ 79*e7b1675dSTing-Kang Chang --gcs_blob_path gs://my-bucket-name/my-blob-name \ 80*e7b1675dSTing-Kang Chang --local_path testdata.txt.decrypted 81*e7b1675dSTing-Kang Chang``` 82