Getting Started with Cloud KMS

Use some advanced features of Google Cloud Security and Privacy APIs, including:

  • Setting up a secure Cloud Storage bucket
  • Managing keys and encrypted data using Key Management Service
  • Viewing Cloud Storage audit logs

You use abridged data from the Enron Corpus, encrypt it, and load it into Cloud Storage. The Enron Corpus is a large database of over 600,000 emails generated by 158 employees of the Enron Corporation. This data has been copied to the Cloud Storage bucket gs://enron_emails/.

How to encrypt data and manage encryption keys using Cloud Key Management Service (KMS).

Create a Cloud Storage bucket

In order to store the data for this lab you need to create your own Cloud Storage bucket.

  1. Pick a name for your Cloud Storage bucket, such as -enron_corpus. For more information on naming buckets, see the Cloud Storage bucket naming guidelines. Run the following command in Cloud Shell to set a variable to your bucket name:
  2. Download one of the source files locally so that you can see what it looks like by running:

This should display the contents of a plaintext mail file. There are two types of files you’ll be looking for: plaintext mail files and image files. If you’re interested, use the same mechanism to check out what is in those other files.

Welcome to Cloud Shell! Type "help" to get started.
Your Cloud Platform project in this session is set to qwiklabs-gcp-01-eaa06ec2a416.
Use “gcloud config set project [PROJECT_ID]” to change to a different project.
student_01_c9f851639dd7@cloudshell:~ (qwiklabs-gcp-01-eaa06ec2a416)$ BUCKET_NAME="qwiklabs-gcp-01-eaa06ec2a416-enron_corpus"
student_01_c9f851639dd7@cloudshell:~ (qwiklabs-gcp-01-eaa06ec2a416)$ gsutil mb gs://${BUCKET_NAME}
Creating gs://qwiklabs-gcp-01-eaa06ec2a416-enron_corpus/...
student_01_c9f851639dd7@cloudshell:~ (qwiklabs-gcp-01-eaa06ec2a416)$ gsutil cp gs://enron_emails/allen-p/inbox/1. .
Copying gs://enron_emails/allen-p/inbox/1....
/ [1 files][  1.7 KiB/  1.7 KiB]                                                
Operation completed over 1 objects/1.7 KiB.                                      
student_01_c9f851639dd7@cloudshell:~ (qwiklabs-gcp-01-eaa06ec2a416)$ tail 1.
Attached is the Delta position for 1/18, 1/31, 6/20, 7/16, 9/24



 << File: west_delta_pos.xls >> 

Let me know if you have any questions.


Heatherstudent_01_c9f851639dd7@cloudshell:~ (qwiklabs-gcp-01-eaa06ec2a416)$ 

Enable Cloud KMS

Cloud KMS is a cryptographic key management service on Google Cloud. Before using KMS you need to enable it in your project. In this lab you have been provisioned KMS should already have been enabled. You can make sure of this by using one of the gcloud CLI commands.

student_01_c9f851639dd7@cloudshell:~ (qwiklabs-gcp-01-eaa06ec2a416)$ gcloud services enable cloudkms.googleapis.com
Operation "operations/acat.p2-956063577171-fba72c4a-544a-45f9-892e-ebe6e5d877d4" finished successfully.
student_01_c9f851639dd7@cloudshell:~ (qwiklabs-gcp-01-eaa06ec2a416)$ 

Cloud KMS is now enabled in your project!

Create a Keyring and Cryptokey

In order to encrypt the data, you need to create a KeyRing and a CryptoKey. KeyRings are useful for grouping keys. Keys can be grouped by environment (like test, staging, and prod) or by some other conceptual grouping. For this lab, your KeyRing will be called test and your CryptoKey will be called qwiklab.

student_01_c9f851639dd7@cloudshell:~ (qwiklabs-gcp-01-eaa06ec2a416)$ KEYRING_NAME=test CRYPTOKEY_NAME=qwiklab
student_01_c9f851639dd7@cloudshell:~ (qwiklabs-gcp-01-eaa06ec2a416)$ gcloud kms keyrings create $KEYRING_NAME --location global
student_01_c9f851639dd7@cloudshell:~ (qwiklabs-gcp-01-eaa06ec2a416)$ gcloud kms keys create $CRYPTOKEY_NAME --location global \
      --keyring $KEYRING_NAME \
      --purpose encryption
student_01_c9f851639dd7@cloudshell:~ (qwiklabs-gcp-01-eaa06ec2a416)$ 

Just like that, you’ve created a KeyRing and CryptoKey!

  1. Open the Key management through the Console by going to the Navigation menu > Security > Key Management.

The Key Management web UI allows you to view and manage your CryptoKeys and KeyRings. You will use this UI later when you manage permissions.

A cryptographic key is a resource that is used for encrypting and decrypting data or for producing and verifying digital signatures.

Encrypt your data

Next, try to encrypt some data!

  1. Take the contents of the email you looked at earlier and base64 encode it by running the following:

Base64 encoding allows binary data to be sent to the API as plaintext. This command works for images, videos, or any other kind of binary data.

student_01_c9f851639dd7@cloudshell:~ (qwiklabs-g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Vlc2RheSwgRGVjZW1iZXIgMDQsIDIwMDEgMzoxMiBQTQpUbzoJQmVsZGVuLCBUaW07IEFsbGVuLCBQaGlsbGlwIEsuCkNjOglEcmlzY29sbCwgTWljaGFlbCBNLgpTdWJqZWN0OglXZXN0IFBvc2l0aW9uCgoKQXR0YWNoZWQgaXMgdGhlIERlbHRhIHBvc2l0aW9uIGZvciAxLzE4LCAxLzMxLCA2LzIwLCA3LzE2LCA5LzI0CgoKCiA8PCBGaWxlOiB3ZXN0X2RlbHRhX3Bvcy54bHMgPj4gCgpMZXQgbWUga25vdyBpZiB5b3UgaGF2ZSBhbnkgcXVlc3Rpb25zLgoKCkhlYXRoZXI=
student_01_c9f851639dd7@cloudshell:~ (qwiklabs-gcp-01-eaa06ec2a416)$ 

Using the encrypt endpoint, you can send the base64-encoded text you want to encrypt to the specified key.

student_01_c9f851639dd7@cloudshell:~ (qwiklabs-gcp-01-eaa06ec2a416)$ curl -v "https://cloudkms.googleapis.com/v1/projects/$DEVSHELL_PROJECT_ID/locations/global/keyRings/$KEYRING_NAME/cryptoKeys/$CRYPTOKEY_NAME:encrypt" \
  -d "{\"plaintext\":\"$PLAINTEXT\"}" \
  -H "Authorization:Bearer $(gcloud auth application-default print-access-token)"\
  -H "Content-Type: application/json"
*   Trying 74.125.130.95:443...
* Connected to cloudkms.googleapis.com (74.125.130.95) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
*  CAfile: /etc/ssl/certs/ca-certificates.crt
*  CApath: /etc/ssl/certs
* TLSv1.0 (OUT), TLS header, Certificate Status (22):
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS header, Certificate Status (22):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS header, Finished (20):
* TLSv1.2 (IN), TLS header, Supplemental data (23):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.2 (OUT), TLS header, Finished (20):
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server accepted to use h2
* Server certificate:
*  subject: CN=upload.video.google.com
*  start date: Apr 16 04:17:12 2024 GMT
*  expire date: Jul  9 04:17:11 2024 GMT
*  subjectAltName: host "cloudkms.googleapis.com" matched cert's "*.googleapis.com"
*  issuer: C=US; O=Google Trust Services LLC; CN=GTS CA 1C3
*  SSL certificate verify ok.
* Using HTTP2, server supports multiplexing
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
* Using Stream ID: 1 (easy handle 0x5812d38e7eb0)
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
> POST /v1/projects/qwiklabs-gcp-01-eaa06ec2a416/locations/global/keyRings/test/cryptoKeys/qwiklab:encrypt HTTP/2
> Host: cloudkms.googleapis.com
> user-agent: curl/7.81.0
> accept: */*
> authorization:Bearer ya29.a0AXooCgsK14kPyN3KnOwaampnNvDfGn1qV8qf0YMumyBFfF9TRbVL1rSLlc1Bghy1ZcRCAAkaE2MYXlTgKQLdU6nhi5tiPn1Ig1yCsozyAiuXQXp56Dvorq1GXuZAMuFFd17t7F_8vG30IqjHf4-G5gFNq8NZZ0Gmvm9Njxrzj-WG5WndMjB0ncvxMch7QBE8iBjJ-PwjRdhAAJMGNiIX0jVKcWzMZLC-ih5000z2xOeifZvU5mYR5UFKIBxrruRArlSew13sBTfabR2BiOnRhNz6iR-WfBnryeNLrLUnbtN0zERVMpSAK7KwUIB6sPLgWFV72hbRVdM2kdnt3jgYZftln3ZsYET57r13Qihpyi_vL_FgCifbwMBP_OUE5dsiP5fJvtLR1OoGwny6E3Y3yRnuaz8Ng_jNbgaCgYKAUYSARMSFQHGX2MipghQaGtglR1ehE5fQw-e6w0425
> content-type: application/json
> content-length: 2384
> 
* TLSv1.2 (IN), TLS header, Supplemental data (23):
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
* We are completely uploaded and fine
* TLSv1.2 (IN), TLS header, Supplemental data (23):
* TLSv1.2 (IN), TLS header, Supplemental data (23):
< HTTP/2 200 
< content-type: application/json; charset=UTF-8
< vary: X-Origin
< vary: Referer
< vary: Origin,Accept-Encoding
< date: Tue, 07 May 2024 16:39:00 GMT
< server: ESF
< cache-control: private
< x-xss-protection: 0
< x-frame-options: SAMEORIGIN
< x-content-type-options: nosniff
< accept-ranges: none
< 
* TLSv1.2 (IN), TLS header, Supplemental data (23):
{
  "name": "projects/qwiklabs-gcp-01-eaa06ec2a416/locations/global/keyRings/test/cryptoKeys/qwiklab/cryptoKeyVersions/1",
  "ciphertext": "CiQAF4fTeBF1tr+Lh/GbvUT/mbs6A7f9BpFinEzAQbQgIH//iFMSmQ4AIL9QXCaM5QtX9y4pmg3K4E04BZQmRy+1HJ7tEJSbBasXBCgHZg2AO87aKrIINTYoP0szQZqDghF/7aqN6uQ0x7cAtChVdj2hLDTnqszhFb0lRWfPHpAWMNJKmuxoya/5+8IQ68LvGdMytlYZ/IeGWBwD4bF5PDKB4VShHiJkiK6g8tu/wBTgEVQ8y2gg1Qj+FtTYF0XrjLNzYf/m4ncfrZYJ/PFchlpOFrs0/1OCQRWnMPn0wPjepgP7yYSRECkPzkyxJwyU1SENFG0Rzbt9AVX4PYzn1GSmXfeFhi3nk1IGM7taGGaKDn28q3ViDTAGoaF1/HN17LhjiPYK6YmuAV/7az85BL5ZWp4QW2B2HWD0JDtwWHksFjsust3ttOeJF3mun/koi2otRpHb1J4GyuwYV/Nj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4f2OM/+eyABzRD/q6dZiSz+Bxta1Rv6xQJrISFPz60HPglZfA+/ZiqQIVW87dmM6A+NaDgMgadoS5sGHhI3NngjRajnJBmnxpXuSqi/Cf5S544U24wpqM8bP48S1lQE1iwQ/fe13weSooOeO7AWxTSnOuhbBOKtCECGs8JXc/mpns8/ZTVxshs4tyxYAZFxaWU0oFcIHn0Yoztw4g0LSEBj2gv1e3NQEafyn3meLDbaPCF+cjqtiyDuz3GOLO884hWyFtsx+cfNwEviw6t0GJVMlEPM8GYhTUoqYtf+IQYyN1aILw2XlMqgGCNpzzlgf/xSr1s4gpDymSU5RXo2jII1tx/CbHaf5kiIsEP2yN1oCAVdRtDmy5M2SDAEvpoFUOvWtrbo9leXZ0wblnWv2qRrb6MzDFdeT6exQ/jkUcG5gvAH3l52K2r1/ssos5B++MidKCLdrOSTe484DCu0I3WdZTA81uQy58WzLsMCq4jQ8s5FDevkcoXTqMGq2+KOjoyLzwK3uhl+pwF5gSYoK8sQ5JqEMcIUwFvb0Xmm9EpuZicJfUIParNK+wQVGpgCbnhtxg61sxfUBfDdawlOmQl378kVph3eMEW6IRn0tpaYEq1nggIY00Tz9wgRPQsKz2tqMaUG4TinWSamss62m4n2Qz3LGEgs2WInuyNmwoYnwOy+CrAOffVL436Ds7WyPI3KeOxp13ld13e7ARpr5GE4H3BNLwWpDtJN77lPQvfGOT09QAGv0KNmDag8MzdTncN7lAtgN3yUqU3VedKmGglrTEWxeQyn+3Cw1/2VjxHEAim4TLqr74gLrunCPYDw18ANwaeXtTbRid5ZxFX8kTtOucX652EeYEOGGxdh8symzrdogbIhIf+bA+w+zr6lZXoxmjSo+URdlZV6kh63e8XeDxyO8liPmFA1qOFwUHb4M7mxB+2ZBNhvhkzfzyO6w==",
  "ciphertextCrc32c": "571844411",
  "protectionLevel": "SOFTWARE"
}
* TLSv1.2 (IN), TLS header, Supplemental data (23):
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
* Connection #0 to host cloudkms.googleapis.com left intact
student_01_c9f851639dd7@cloudshell:~ (qwiklabs-gcp-01-eaa06ec2a416)$ 

The encrypt action will return a different result each time even when using the same text and key.

The response will be a JSON payload containing the encrypted text in the attribute ciphertext.

Now that your data is encrypted, you can save it to a file and upload it to your Cloud Storage bucket. To grab the encrypted text from the JSON response and save it to a file, use the command-line utility jq. The response from the previous call can be piped into jq, which can parse out the ciphertext property to the file 1.encrypted. Run the following:

student_01_c9f851639dd7@cloudshell:~ (qwiklabs-gcp-01-eaa06ec2a416)$ curl -v "https://cloudkms.googleapis.com/v1/projects/$DEVSHELL_PROJECT_ID/locations/global/keyRings/$KEYRING_NAME/cryptoKeys/$CRYPTOKEY_NAME:encrypt" \
  -d "{\"plaintext\":\"$PLAINTEXT\"}" \
  -H "Authorization:Bearer $(gcloud auth application-default print-access-token)"\
  -H "Content-Type:application/json" \
| jq .ciphertext -r > 1.encrypted
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 142.251.12.95:443...
* Connected to cloudkms.googleapis.com (142.251.12.95) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
*  CAfile: /etc/ssl/certs/ca-certificates.crt
*  CApath: /etc/ssl/certs
* TLSv1.0 (OUT), TLS header, Certificate Status (22):
} [5 bytes data]
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
} [512 bytes data]
* TLSv1.2 (IN), TLS header, Certificate Status (22):
{ [5 bytes data]
* TLSv1.3 (IN), TLS handshake, Server hello (2):
{ [122 bytes data]
* TLSv1.2 (IN), TLS header, Finished (20):
{ [5 bytes data]
* TLSv1.2 (IN), TLS header, Supplemental data (23):
{ [5 bytes data]
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
{ [15 bytes data]
* TLSv1.3 (IN), TLS handshake, Certificate (11):
{ [4395 bytes data]
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
{ [79 bytes data]
* TLSv1.3 (IN), TLS handshake, Finished (20):
{ [52 bytes data]
* TLSv1.2 (OUT), TLS header, Finished (20):
} [5 bytes data]
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
} [1 bytes data]
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
} [5 bytes data]
* TLSv1.3 (OUT), TLS handshake, Finished (20):
} [52 bytes data]
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server accepted to use h2
* Server certificate:
*  subject: CN=upload.video.google.com
*  start date: Apr 16 04:17:12 2024 GMT
*  expire date: Jul  9 04:17:11 2024 GMT
*  subjectAltName: host "cloudkms.googleapis.com" matched cert's "*.googleapis.com"
*  issuer: C=US; O=Google Trust Services LLC; CN=GTS CA 1C3
*  SSL certificate verify ok.
* Using HTTP2, server supports multiplexing
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
} [5 bytes data]
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
} [5 bytes data]
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
} [5 bytes data]
* Using Stream ID: 1 (easy handle 0x59dd1d420eb0)
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
} [5 bytes data]
> POST /v1/projects/qwiklabs-gcp-01-eaa06ec2a416/locations/global/keyRings/test/cryptoKeys/qwiklab:encrypt HTTP/2
> Host: cloudkms.googleapis.com
> user-agent: curl/7.81.0
> accept: */*
> authorization:Bearer ya29.a0AXooCgsK14kPyN3KnOwaampnNvDfGn1qV8qf0YMumyBFfF9TRbVL1rSLlc1Bghy1ZcRCAAkaE2MYXlTgKQLdU6nhi5tiPn1Ig1yCsozyAiuXQXp56Dvorq1GXuZAMuFFd17t7F_8vG30IqjHf4-G5gFNq8NZZ0Gmvm9Njxrzj-WG5WndMjB0ncvxMch7QBE8iBjJ-PwjRdhAAJMGNiIX0jVKcWzMZLC-ih5000z2xOeifZvU5mYR5UFKIBxrruRArlSew13sBTfabR2BiOnRhNz6iR-WfBnryeNLrLUnbtN0zERVMpSAK7KwUIB6sPLgWFV72hbRVdM2kdnt3jgYZftln3ZsYET57r13Qihpyi_vL_FgCifbwMBP_OUE5dsiP5fJvtLR1OoGwny6E3Y3yRnuaz8Ng_jNbgaCgYKAUYSARMSFQHGX2MipghQaGtglR1ehE5fQw-e6w0425
> content-type:application/json
> content-length: 2384
> 
* TLSv1.2 (IN), TLS header, Supplemental data (23):
{ [5 bytes data]
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
} [5 bytes data]
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
} [5 bytes data]
* We are completely uploaded and fine
* TLSv1.2 (IN), TLS header, Supplemental data (23):
{ [5 bytes data]
* TLSv1.2 (IN), TLS header, Supplemental data (23):
{ [5 bytes data]
< HTTP/2 200 
< content-type: application/json; charset=UTF-8
< vary: X-Origin
< vary: Referer
< vary: Origin,Accept-Encoding
< date: Tue, 07 May 2024 16:40:20 GMT
< server: ESF
< cache-control: private
< x-xss-protection: 0
< x-frame-options: SAMEORIGIN
< x-content-type-options: nosniff
< accept-ranges: none
< 
* TLSv1.2 (IN), TLS header, Supplemental data (23):
{ [5 bytes data]
* TLSv1.2 (IN), TLS header, Supplemental data (23):
{ [5 bytes data]
* TLSv1.2 (IN), TLS header, Supplemental data (23):
{ [5 bytes data]
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
} [5 bytes data]
100  5077    0  2693  100  2384   9366   8291 --:--:-- --:--:-- --:--:-- 17689
* Connection #0 to host cloudkms.googleapis.com left intact
student_01_c9f851639dd7@cloudshell:~ (qwiklabs-gcp-01-eaa06ec2a416)$ 

To verify the encrypted data can be decrypted, call the decrypt endpoint to verify the decrypted text matches the original email. The encrypted data has information on which CryptoKey version was used to encrypt it, so the specific version is never supplied to the decrypt endpoint. Run the following:

student_01_c9f851639dd7@cloudshell:~ (qwiklabs-gcp-01-eaa06ec2a416)$ curl -v "https://cloudkms.googleapis.com/v1/projects/$DEVSHELL_PROJECT_ID/locations/global/keyRings/$KEYRING_NAME/cryptoKeys/$CRYPTOKEY_NAME:decrypt" \
  -d "{\"ciphertext\":\"$(cat 1.encrypted)\"}" \
  -H "Authorization:Bearer $(gcloud auth application-default print-access-token)"\
  -H "Content-Type:application/json" \
| jq .plaintext -r | base64 -d
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 142.251.175.95:443...
* Connected to cloudkms.googleapis.com (142.251.175.95) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
*  CAfile: /etc/ssl/certs/ca-certificates.crt
*  CApath: /etc/ssl/certs
* TLSv1.0 (OUT), TLS header, Certificate Status (22):
} [5 bytes data]
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
} [512 bytes data]
* TLSv1.2 (IN), TLS header, Certificate Status (22):
{ [5 bytes data]
* TLSv1.3 (IN), TLS handshake, Server hello (2):
{ [122 bytes data]
* TLSv1.2 (IN), TLS header, Finished (20):
{ [5 bytes data]
* TLSv1.2 (IN), TLS header, Supplemental data (23):
{ [5 bytes data]
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
{ [15 bytes data]
* TLSv1.3 (IN), TLS handshake, Certificate (11):
{ [4395 bytes data]
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
{ [78 bytes data]
* TLSv1.3 (IN), TLS handshake, Finished (20):
{ [52 bytes data]
* TLSv1.2 (OUT), TLS header, Finished (20):
} [5 bytes data]
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
} [1 bytes data]
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
} [5 bytes data]
* TLSv1.3 (OUT), TLS handshake, Finished (20):
} [52 bytes data]
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server accepted to use h2
* Server certificate:
*  subject: CN=upload.video.google.com
*  start date: Apr 16 04:17:12 2024 GMT
*  expire date: Jul  9 04:17:11 2024 GMT
*  subjectAltName: host "cloudkms.googleapis.com" matched cert's "*.googleapis.com"
*  issuer: C=US; O=Google Trust Services LLC; CN=GTS CA 1C3
*  SSL certificate verify ok.
* Using HTTP2, server supports multiplexing
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
} [5 bytes data]
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
} [5 bytes data]
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
} [5 bytes data]
* Using Stream ID: 1 (easy handle 0x57ae24446eb0)
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
} [5 bytes data]
> POST /v1/projects/qwiklabs-gcp-01-eaa06ec2a416/locations/global/keyRings/test/cryptoKeys/qwiklab:decrypt HTTP/2
> Host: cloudkms.googleapis.com
> user-agent: curl/7.81.0
> accept: */*
> authorization:Bearer ya29.a0AXooCgsK14kPyN3KnOwaampnNvDfGn1qV8qf0YMumyBFfF9TRbVL1rSLlc1Bghy1ZcRCAAkaE2MYXlTgKQLdU6nhi5tiPn1Ig1yCsozyAiuXQXp56Dvorq1GXuZAMuFFd17t7F_8vG30IqjHf4-G5gFNq8NZZ0Gmvm9Njxrzj-WG5WndMjB0ncvxMch7QBE8iBjJ-PwjRdhAAJMGNiIX0jVKcWzMZLC-ih5000z2xOeifZvU5mYR5UFKIBxrruRArlSew13sBTfabR2BiOnRhNz6iR-WfBnryeNLrLUnbtN0zERVMpSAK7KwUIB6sPLgWFV72hbRVdM2kdnt3jgYZftln3ZsYET57r13Qihpyi_vL_FgCifbwMBP_OUE5dsiP5fJvtLR1OoGwny6E3Y3yRnuaz8Ng_jNbgaCgYKAUYSARMSFQHGX2MipghQaGtglR1ehE5fQw-e6w0425
> content-type:application/json
> content-length: 2497
> 
* TLSv1.2 (IN), TLS header, Supplemental data (23):
{ [5 bytes data]
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
} [5 bytes data]
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
} [5 bytes data]
* We are completely uploaded and fine
* TLSv1.2 (IN), TLS header, Supplemental data (23):
{ [5 bytes data]
* TLSv1.2 (IN), TLS header, Supplemental data (23):
{ [5 bytes data]
< HTTP/2 200 
< content-type: application/json; charset=UTF-8
< vary: X-Origin
< vary: Referer
< vary: Origin,Accept-Encoding
< date: Tue, 07 May 2024 16:41:40 GMT
< server: ESF
< cache-control: private
< x-xss-protection: 0
< x-frame-options: SAMEORIGIN
< x-content-type-options: nosniff
< accept-ranges: none
< 
* TLSv1.2 (IN), TLS header, Supplemental data (23):
{ [5 bytes data]
* TLSv1.2 (IN), TLS header, Supplemental data (23):
{ [5 bytes data]
* TLSv1.2 (IN), TLS header, Supplemental data (23):
{ [5 bytes data]
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
} [5 bytes data]
100  4978    0  2481  100  2497   7879   7930 --:--:-- --:--:-- --:--:-- 15853
* Connection #0 to host cloudkms.googleapis.com left intact
Message-ID: <16159836.1075855377439.JavaMail.evans@thyme>
Date: Fri, 7 Dec 2001 10:06:42 -0800 (PST)
From: heather.dunton@enron.com
To: k..allen@enron.com
Subject: RE: West Position
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-From: Dunton, Heather </O=ENRON/OU=NA/CN=RECIPIENTS/CN=HDUNTON>
X-To: Allen, Phillip K. </O=ENRON/OU=NA/CN=RECIPIENTS/CN=Pallen>
X-cc: 
X-bcc: 
X-Folder: \Phillip_Allen_Jan2002_1\Allen, Phillip K.\Inbox
X-Origin: Allen-P
X-FileName: pallen (Non-Privileged).pst

 
Please let me know if you still need Curve Shift.

Thanks,
Heather
 -----Original Message-----
From:   Allen, Phillip K.  
Sent:   Friday, December 07, 2001 5:14 AM
To:     Dunton, Heather
Subject:        RE: West Position

Heather,

Did you attach the file to this email?

 -----Original Message-----
From:   Dunton, Heather  
Sent:   Wednesday, December 05, 2001 1:43 PM
To:     Allen, Phillip K.; Belden, Tim
Subject:        FW: West Position

Attached is the Delta position for 1/16, 1/30, 6/19, 7/13, 9/21


 -----Original Message-----
From:   Allen, Phillip K.  
Sent:   Wednesday, December 05, 2001 6:41 AM
To:     Dunton, Heather
Subject:        RE: West Position

Heather,

This is exactly what we need.  Would it possible to add the prior day for each of the dates below to the pivot table.  In order to validate the curve shift on the dates below we also need the prior days ending positions.

Thank you,

Phillip Allen

 -----Original Message-----
From:   Dunton, Heather  
Sent:   Tuesday, December 04, 2001 3:12 PM
To:     Belden, Tim; Allen, Phillip K.
Cc:     Driscoll, Michael M.
Subject:        West Position


Attached is the Delta position for 1/18, 1/31, 6/20, 7/16, 9/24



 << File: west_delta_pos.xls >> 

Let me know if you have any questions.


Heatherstudent_01_c9f851639dd7@cloudshell:~ (qwiklabs-gcp-01-eaa06ec2a416)$ 

Now that you have verified the text has been encrypted successfully, upload the encrypted file to your Cloud Storage bucket.

student_01_c9f851639dd7@cloudshell:~ (qwiklabs-gcp-01-eaa06ec2a416)$ ls
1.  1.encrypted  README-cloudshell.txt
student_01_c9f851639dd7@cloudshell:~ (qwiklabs-gcp-01-eaa06ec2a416)$ gsutil cp 1.encrypted gs://${BUCKET_NAME}
Copying file://1.encrypted [Content-Type=application/octet-stream]...
- [1 files][  2.4 KiB/  2.4 KiB]                                                
Operation completed over 1 objects/2.4 KiB.                                      
student_01_c9f851639dd7@cloudshell:~ (qwiklabs-gcp-01-eaa06ec2a416)$ 

Configure IAM permissions

In KMS, there are two major permissions to focus on. One permissions allows a user or service account to manage KMS resources, the other allows a user or service account to use keys to encrypt and decrypt data.

The permission to manage keys is cloudkms.admin, and allows anyone with the permission to create KeyRings and create, modify, disable, and destroy CryptoKeys. The permission to encrypt and decrypt is cloudkms.cryptoKeyEncrypterDecrypter, and is used to call the encrypt and decrypt API endpoints.

For this exercise, you will use the current authorized user to assign IAM permissions.

To get the current authorized user, run the command below:

Next, assign that user the ability to manage KMS resources. Run the following gcloud command to assign the IAM permission to manage the KeyRing you just created

student_01_c9f851639dd7@cloudshell:~ (qwiklabs-gcp-01-eaa06ec2a416)$ USER_EMAIL=$(gcloud auth list --limit=1 2>/dev/null | grep '@' | awk '{print $2}')
student_01_c9f851639dd7@cloudshell:~ (qwiklabs-gcp-01-eaa06ec2a416)$ gcloud kms keyrings add-iam-policy-binding $KEYRING_NAME \
    --location global \
    --member user:$USER_EMAIL \
    --role roles/cloudkms.admin
Updated IAM policy for keyring [test].
bindings:
- members:
  - user:student-01-c9f851639dd7@qwiklabs.net
  role: roles/cloudkms.admin
etag: BwYX3-Znm0w=
version: 1
student_01_c9f851639dd7@cloudshell:~ (qwiklabs-gcp-01-eaa06ec2a416)$ 

Since CryptoKeys belong to KeyRings, and KeyRings belong to Projects, a user with a specific role or permission at a higher level in that hierarchy inherits the same permissions on the child resources. For example, a user who has the role of Owner on a Project is also an Owner on all the KeyRings and CryptoKeys in that project. Similarly, if a user is granted the cloudkms.admin role on a KeyRing, they have the associated permissions on the CryptoKeys in that KeyRing.

Without the cloudkms.cryptoKeyEncrypterDecrypter permission, the authorized user will not be able to use the keys to encrypt or decrypt data.

  1. Run the following gcloud command to assign the IAM permission to encrypt and decrypt data for any CryptoKey under the KeyRing you created:
student_01_c9f851639dd7@cloudshell:~ (qwiklabs-gcp-01-eaa06ec2a416)$ gcloud kms keyrings add-iam-policy-binding $KEYRING_NAME \
    --location global \
    --member user:$USER_EMAIL \
    --role roles/cloudkms.cryptoKeyEncrypterDecrypter
Updated IAM policy for keyring [test].
bindings:
- members:
  - user:student-01-c9f851639dd7@qwiklabs.net
  role: roles/cloudkms.admin
- members:
  - user:student-01-c9f851639dd7@qwiklabs.net
  role: roles/cloudkms.cryptoKeyEncrypterDecrypter
etag: BwYX3-qYM7A=
version: 1
student_01_c9f851639dd7@cloudshell:~ (qwiklabs-gcp-01-eaa06ec2a416)$ 

Back up data on the command line

Now that you have an understanding of how to encrypt a single file, and have permission to do so, you can run a script to backup all files in a directory. For this example, copy all emails for allen-p, encrypt them, and upload them to a Cloud Storage bucket.

  1. First, copy all emails for allen-p into your current working directory:
student_01_c9f851639dd7@cloudshell:~ (qwiklabs-gcp-01-eaa06ec2a416)$ gsutil -m cp -r gs://enron_emails/allen-p .
Copying gs://enron_emails/allen-p/inbox/1....
Copying gs://enron_emails/allen-p/inbox/15....                                  
Copying gs://enron_emails/allen-p/inbox/16....                                  
Copying gs://enron_emails/allen-p/inbox/10....                                  
Copying gs://enron_emails/allen-p/inbox/14....                                  
Copying gs://enron_emails/allen-p/inbox/17....                                  
Copying gs://enron_emails/allen-p/inbox/11....                                  
Copying gs://enron_emails/allen-p/inbox/13....                                  
Copying gs://enron_emails/allen-p/inbox/12....
Copying gs://enron_emails/allen-p/inbox/18....                                  
Copying gs://enron_emails/allen-p/inbox/19....                                  
Copying gs://enron_emails/allen-p/inbox/2....                                   
Copying gs://enron_emails/allen-p/inbox/20....                                  
Copying gs://enron_emails/allen-p/inbox/21....
Copying gs://enron_emails/allen-p/inbox/22....                                  
Copying gs://enron_emails/allen-p/inbox/23....                                  
Copying gs://enron_emails/allen-p/inbox/24....                                  
Copying gs://enron_emails/allen-p/inbox/25....                                  
Copying gs://enron_emails/allen-p/inbox/26....                                  
Copying gs://enron_emails/allen-p/inbox/27....                                  
Copying gs://enron_emails/allen-p/inbox/28....                                  
Copying gs://enron_emails/allen-p/inbox/29....                                  
Copying gs://enron_emails/allen-p/inbox/3....e                                  
Copying gs://enron_emails/allen-p/inbox/30....                                  
Copying gs://enron_emails/allen-p/inbox/4....e                                  
Copying gs://enron_emails/allen-p/inbox/5....e                                  
Copying gs://enron_emails/allen-p/inbox/6....e                                  
Copying gs://enron_emails/allen-p/inbox/7....e                                  
Copying gs://enron_emails/allen-p/inbox/8....e                                  
Copying gs://enron_emails/allen-p/inbox/9....e                                  
/ [30/30 files][ 64.5 KiB/ 64.5 KiB] 100% Done                                  
Operation completed over 30 objects/64.5 KiB.                                    
student_01_c9f851639dd7@cloudshell:~ (qwiklabs-gcp-01-eaa06ec2a416)$ 

Now copy and paste the following into Cloud Shell to back up and encrypt all the files in the allen-p directory to your Cloud Storage bucket.

This script loops over all the files in a given directory, encrypts them using the KMS API, and uploads them to Cloud Storage.

MYDIR=allen-p
FILES=$(find $MYDIR -type f -not -name "*.encrypted")
for file in $FILES; do
  PLAINTEXT=$(cat $file | base64 -w0)
  curl -v "https://cloudkms.googleapis.com/v1/projects/$DEVSHELL_PROJECT_ID/locations/global/keyRings/$KEYRING_NAME/cryptoKeys/$CRYPTOKEY_NAME:encrypt" \
    -d "{\"plaintext\":\"$PLAINTEXT\"}" \
    -H "Authorization:Bearer $(gcloud auth application-default print-access-token)" \
    -H "Content-Type:application/json" \
  | jq .ciphertext -r > $file.encrypted
done
gsutil -m cp allen-p/inbox/*.encrypted gs://${BUCKET_NAME}/allen-p/inbox
{snip}
< content-type: application/json; charset=UTF-8
< vary: X-Origin
< vary: Referer
< vary: Origin,Accept-Encoding
< date: Tue, 07 May 2024 16:50:48 GMT
< server: ESF
< cache-control: private
< x-xss-protection: 0
< x-frame-options: SAMEORIGIN
< x-content-type-options: nosniff
< accept-ranges: none
< 
* TLSv1.2 (IN), TLS header, Supplemental data (23):
{ [5 bytes data]
* TLSv1.2 (IN), TLS header, Supplemental data (23):
{ [5 bytes data]
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
} [5 bytes data]
100  2037    0  1173  100   864   4306   3172 --:--:-- --:--:-- --:--:--  7461
* Connection #0 to host cloudkms.googleapis.com left intact
Copying file://allen-p/inbox/10..encrypted [Content-Type=application/octet-stream]...
Copying file://allen-p/inbox/11..encrypted [Content-Type=application/octet-stream]...
Copying file://allen-p/inbox/13..encrypted [Content-Type=application/octet-stream]...
Copying file://allen-p/inbox/15..encrypted [Content-Type=application/octet-stream]...
Copying file://allen-p/inbox/16..encrypted [Content-Type=application/octet-stream]...
Copying file://allen-p/inbox/14..encrypted [Content-Type=application/octet-stream]...
Copying file://allen-p/inbox/12..encrypted [Content-Type=application/octet-stream]...
Copying file://allen-p/inbox/17..encrypted [Content-Type=application/octet-stream]...
Copying file://allen-p/inbox/18..encrypted [Content-Type=application/octet-stream]...
Copying file://allen-p/inbox/19..encrypted [Content-Type=application/octet-stream]...
Copying file://allen-p/inbox/1..encrypted [Content-Type=application/octet-stream]...
Copying file://allen-p/inbox/20..encrypted [Content-Type=application/octet-stream]...
Copying file://allen-p/inbox/21..encrypted [Content-Type=application/octet-stream]...
Copying file://allen-p/inbox/22..encrypted [Content-Type=application/octet-stream]...
Copying file://allen-p/inbox/23..encrypted [Content-Type=application/octet-stream]...
Copying file://allen-p/inbox/24..encrypted [Content-Type=application/octet-stream]...
Copying file://allen-p/inbox/25..encrypted [Content-Type=application/octet-stream]...
Copying file://allen-p/inbox/26..encrypted [Content-Type=application/octet-stream]...
Copying file://allen-p/inbox/27..encrypted [Content-Type=application/octet-stream]...
Copying file://allen-p/inbox/28..encrypted [Content-Type=application/octet-stream]...
Copying file://allen-p/inbox/29..encrypted [Content-Type=application/octet-stream]...
Copying file://allen-p/inbox/2..encrypted [Content-Type=application/octet-stream]...
Copying file://allen-p/inbox/30..encrypted [Content-Type=application/octet-stream]...
Copying file://allen-p/inbox/3..encrypted [Content-Type=application/octet-stream]...
Copying file://allen-p/inbox/4..encrypted [Content-Type=application/octet-stream]...
Copying file://allen-p/inbox/5..encrypted [Content-Type=application/octet-stream]...
Copying file://allen-p/inbox/6..encrypted [Content-Type=application/octet-stream]...
Copying file://allen-p/inbox/7..encrypted [Content-Type=application/octet-stream]...
Copying file://allen-p/inbox/8..encrypted [Content-Type=application/octet-stream]...
Copying file://allen-p/inbox/9..encrypted [Content-Type=application/octet-stream]...
- [30/30 files][ 89.4 KiB/ 89.4 KiB] 100% Done                                  
Operation completed over 30 objects/89.4 KiB.                                    
student_01_c9f851639dd7@cloudshell:~ (qwiklabs-gcp-01-eaa06ec2a416)$ 

After the script completes, you can view the encrypted files when you click Storage from the Console’s left menu.

  1. To find the files, go to Navigation menu > Cloud Storage > Buckets > YOUR_BUCKET > allen-p > inbox.

Cloud Storage supports Server Side Encryption, which supports key rotation of your data and is the recommended way to encrypt data in Cloud Storage. The above example is for demonstration purposes only.

View Cloud Audit logs

Google Cloud Audit Logging consists of two log streams, Admin Activity and Data Access, which are generated by Google Cloud services to help you answer the question “who did what, where, and when?” within your Google Cloud projects.

  • To view the activity for any resource in KMS, go to Navigation menu > Cloud Overview > Activity tab. This will take you to the Cloud Activity UI and then click on View Log Explorer, Select Cloud KMS Key Ring as the Resource Type and you should see the creation and all modifications made to the KeyRing.
(logName = "projects/qwiklabs-gcp-01-eaa06ec2a416/logs/cloudaudit.googleapis.com%2Factivity" OR logName = "projects/qwiklabs-gcp-01-eaa06ec2a416/logs/cloudaudit.googleapis.com%2Fdata_access" OR labels.activity_type_name:*)
resource.type="cloudkms_keyring"

You’ve now encrypted and uploaded data using KMS and Cloud Storage!

Summary

student_01_c9f851639dd7@cloudshell:~ (qwiklabs-gcp-01-eaa06ec2a416)$ history 
    1  gcloud compute ssh source-instance --zone=$ZONE
    2  BUCKET_NAME="qwiklabs-gcp-01-eaa06ec2a416-enron_corpus"
    3  gsutil mb gs://${BUCKET_NAME}
    4  gsutil cp gs://enron_emails/allen-p/inbox/1. .
    5  tail 1.
    6  gcloud services enable cloudkms.googleapis.com
    7  KEYRING_NAME=test CRYPTOKEY_NAME=qwiklab
    8  gcloud kms keyrings create $KEYRING_NAME --location global
    9  gcloud kms keys create $CRYPTOKEY_NAME --location global       --keyring $KEYRING_NAME       --purpose encryption
   10  PLAINTEXT=$(cat 1. | base64 -w0)
   11  echo PLAINTEXT
   12  echo $PLAINTEXT
   13  curl -v "https://cloudkms.googleapis.com/v1/projects/$DEVSHELL_PROJECT_ID/locations/global/keyRings/$KEYRING_NAME/cryptoKeys/$CRYPTOKEY_NAME:encrypt"   -d "{\"plaintext\":\"$PLAINTEXT\"}"   -H "Authorization:Bearer $(gcloud auth application-default print-access-token)"  -H "Content-Type: application/json"
   14  curl -v "https://cloudkms.googleapis.com/v1/projects/$DEVSHELL_PROJECT_ID/locations/global/keyRings/$KEYRING_NAME/cryptoKeys/$CRYPTOKEY_NAME:encrypt"   -d "{\"plaintext\":\"$PLAINTEXT\"}"   -H "Authorization:Bearer $(gcloud auth application-default print-access-token)"  -H "Content-Type:application/json" | jq .ciphertext -r > 1.encrypted
   15  curl -v "https://cloudkms.googleapis.com/v1/projects/$DEVSHELL_PROJECT_ID/locations/global/keyRings/$KEYRING_NAME/cryptoKeys/$CRYPTOKEY_NAME:decrypt"   -d "{\"ciphertext\":\"$(cat 1.encrypted)\"}"   -H "Authorization:Bearer $(gcloud auth application-default print-access-token)"  -H "Content-Type:application/json" | jq .plaintext -r | base64 -d
   16  ls
   17  gsutil cp 1.encrypted gs://${BUCKET_NAME}
   18  USER_EMAIL=$(gcloud auth list --limit=1 2>/dev/null | grep '@' | awk '{print $2}')
   19  gcloud kms keyrings add-iam-policy-binding $KEYRING_NAME     --location global     --member user:$USER_EMAIL     --role roles/cloudkms.admin
   20  gcloud kms keyrings add-iam-policy-binding $KEYRING_NAME     --location global     --member user:$USER_EMAIL     --role roles/cloudkms.cryptoKeyEncrypterDecrypter
   21  gsutil -m cp -r gs://enron_emails/allen-p .
   22  MYDIR=allen-p
   23  FILES=$(find $MYDIR -type f -not -name "*.encrypted")
   24  for file in $FILES; do   PLAINTEXT=$(cat $file | base64 -w0);   curl -v "https://cloudkms.googleapis.com/v1/projects/$DEVSHELL_PROJECT_ID/locations/global/keyRings/$KEYRING_NAME/cryptoKeys/$CRYPTOKEY_NAME:encrypt"     -d "{\"plaintext\":\"$PLAINTEXT\"}"     -H "Authorization:Bearer $(gcloud auth application-default print-access-token)"     -H "Content-Type:application/json"   | jq .ciphertext -r > $file.encrypted; done
   25  gsutil -m cp allen-p/inbox/*.encrypted gs://${BUCKET_NAME}/allen-p/inbox
   26  history 
student_01_c9f851639dd7@cloudshell:~ (qwiklabs-gcp-01-eaa06ec2a416)$ history 
    1  gcloud compute ssh source-instance --zone=$ZONE
    2  BUCKET_NAME="qwiklabs-gcp-01-eaa06ec2a416-enron_corpus"
    3  gsutil mb gs://${BUCKET_NAME}
    4  gsutil cp gs://enron_emails/allen-p/inbox/1. .
    5  tail 1.
    6  gcloud services enable cloudkms.googleapis.com
    7  KEYRING_NAME=test CRYPTOKEY_NAME=qwiklab
    8  gcloud kms keyrings create $KEYRING_NAME --location global
    9  gcloud kms keys create $CRYPTOKEY_NAME --location global       --keyring $KEYRING_NAME       --purpose encryption
   10  PLAINTEXT=$(cat 1. | base64 -w0)
   11  echo PLAINTEXT
   12  echo $PLAINTEXT
   13  curl -v "https://cloudkms.googleapis.com/v1/projects/$DEVSHELL_PROJECT_ID/locations/global/keyRings/$KEYRING_NAME/cryptoKeys/$CRYPTOKEY_NAME:encrypt"   -d "{\"plaintext\":\"$PLAINTEXT\"}"   -H "Authorization:Bearer $(gcloud auth application-default print-access-token)"  -H "Content-Type: application/json"
   14  curl -v "https://cloudkms.googleapis.com/v1/projects/$DEVSHELL_PROJECT_ID/locations/global/keyRings/$KEYRING_NAME/cryptoKeys/$CRYPTOKEY_NAME:encrypt"   -d "{\"plaintext\":\"$PLAINTEXT\"}"   -H "Authorization:Bearer $(gcloud auth application-default print-access-token)"  -H "Content-Type:application/json" | jq .ciphertext -r > 1.encrypted
   15  curl -v "https://cloudkms.googleapis.com/v1/projects/$DEVSHELL_PROJECT_ID/locations/global/keyRings/$KEYRING_NAME/cryptoKeys/$CRYPTOKEY_NAME:decrypt"   -d "{\"ciphertext\":\"$(cat 1.encrypted)\"}"   -H "Authorization:Bearer $(gcloud auth application-default print-access-token)"  -H "Content-Type:application/json" | jq .plaintext -r | base64 -d
   16  ls
   17  gsutil cp 1.encrypted gs://${BUCKET_NAME}
   18  USER_EMAIL=$(gcloud auth list --limit=1 2>/dev/null | grep '@' | awk '{print $2}')
   19  gcloud kms keyrings add-iam-policy-binding $KEYRING_NAME     --location global     --member user:$USER_EMAIL     --role roles/cloudkms.admin
   20  gcloud kms keyrings add-iam-policy-binding $KEYRING_NAME     --location global     --member user:$USER_EMAIL     --role roles/cloudkms.cryptoKeyEncrypterDecrypter
   21  gsutil -m cp -r gs://enron_emails/allen-p .
   22  MYDIR=allen-p
   23  FILES=$(find $MYDIR -type f -not -name "*.encrypted")
   24  for file in $FILES; do   PLAINTEXT=$(cat $file | base64 -w0);   curl -v "https://cloudkms.googleapis.com/v1/projects/$DEVSHELL_PROJECT_ID/locations/global/keyRings/$KEYRING_NAME/cryptoKeys/$CRYPTOKEY_NAME:encrypt"     -d "{\"plaintext\":\"$PLAINTEXT\"}"     -H "Authorization:Bearer $(gcloud auth application-default print-access-token)"     -H "Content-Type:application/json"   | jq .ciphertext -r > $file.encrypted; done
   25  gsutil -m cp allen-p/inbox/*.encrypted gs://${BUCKET_NAME}/allen-p/inbox
   26  history 
   27  history 
student_01_c9f851639dd7@cloudshell:~ (qwiklabs-gcp-01-eaa06ec2a416)$ 

Tags:

Categories:

Updated:

Back to Top ↑