/
snippets.rb
1307 lines (1038 loc) · 42.7 KB
/
snippets.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Snippets contains all the code samples for Cloud KMS.
# rubocop:disable Metrics/ClassLength
class Snippets
def create_key_asymmetric_decrypt project_id:, location_id:, key_ring_id:, id:
# [START kms_create_key_asymmetric_decrypt]
# TODO(developer): uncomment these values before running the sample.
# project_id = "my-project"
# location_id = "us-east1"
# key_ring_id = "my-key-ring"
# id = "my-asymmetric-decrypt-key"
# Require the library.
require "google/cloud/kms"
# Create the client.
client = Google::Cloud::Kms.key_management_service
# Build the parent key ring name.
key_ring_name = client.key_ring_path project: project_id, location: location_id, key_ring: key_ring_id
# Build the key.
key = {
purpose: :ASYMMETRIC_DECRYPT,
version_template: {
algorithm: :RSA_DECRYPT_OAEP_2048_SHA256
},
# Optional: customize how long key versions should be kept before destroying.
destroy_scheduled_duration: {
seconds: 24 * 60 * 60
}
}
# Call the API.
created_key = client.create_crypto_key parent: key_ring_name, crypto_key_id: id, crypto_key: key
puts "Created asymmetric decryption key: #{created_key.name}"
# [END kms_create_key_asymmetric_decrypt]
created_key
end
def create_key_asymmetric_sign project_id:, location_id:, key_ring_id:, id:
# [START kms_create_key_asymmetric_sign]
# TODO(developer): uncomment these values before running the sample.
# project_id = "my-project"
# location_id = "us-east1"
# key_ring_id = "my-key-ring"
# id = "my-asymmetric-signing-key"
# Require the library.
require "google/cloud/kms"
# Create the client.
client = Google::Cloud::Kms.key_management_service
# Build the parent key ring name.
key_ring_name = client.key_ring_path project: project_id, location: location_id, key_ring: key_ring_id
# Build the key.
key = {
purpose: :ASYMMETRIC_SIGN,
version_template: {
algorithm: :RSA_SIGN_PKCS1_2048_SHA256
},
# Optional: customize how long key versions should be kept before destroying.
destroy_scheduled_duration: {
seconds: 24 * 60 * 60
}
}
# Call the API.
created_key = client.create_crypto_key parent: key_ring_name, crypto_key_id: id, crypto_key: key
puts "Created asymmetric signing key: #{created_key.name}"
# [END kms_create_key_asymmetric_sign]
created_key
end
def create_key_hsm project_id:, location_id:, key_ring_id:, id:
# [START kms_create_key_hsm]
# TODO(developer): uncomment these values before running the sample.
# project_id = "my-project"
# location_id = "us-east1"
# key_ring_id = "my-key-ring"
# id = "my-hsm-key"
# Require the library.
require "google/cloud/kms"
# Create the client.
client = Google::Cloud::Kms.key_management_service
# Build the parent key ring name.
key_ring_name = client.key_ring_path project: project_id, location: location_id, key_ring: key_ring_id
# Build the key.
key = {
purpose: :ENCRYPT_DECRYPT,
version_template: {
algorithm: :GOOGLE_SYMMETRIC_ENCRYPTION,
protection_level: :HSM
},
# Optional: customize how long key versions should be kept before destroying.
destroy_scheduled_duration: {
seconds: 24 * 60 * 60
}
}
# Call the API.
created_key = client.create_crypto_key parent: key_ring_name, crypto_key_id: id, crypto_key: key
puts "Created hsm key: #{created_key.name}"
# [END kms_create_key_hsm]
created_key
end
def create_key_labels project_id:, location_id:, key_ring_id:, id:
# [START kms_create_key_labels]
# TODO(developer): uncomment these values before running the sample.
# project_id = "my-project"
# location_id = "us-east1"
# key_ring_id = "my-key-ring"
# id = "my-key-with-labels"
# Require the library.
require "google/cloud/kms"
# Create the client.
client = Google::Cloud::Kms.key_management_service
# Build the parent key ring name.
key_ring_name = client.key_ring_path project: project_id, location: location_id, key_ring: key_ring_id
# Build the key.
key = {
purpose: :ENCRYPT_DECRYPT,
version_template: {
algorithm: :GOOGLE_SYMMETRIC_ENCRYPTION
},
labels: {
"team" => "alpha",
"cost_center" => "cc1234"
}
}
# Call the API.
created_key = client.create_crypto_key parent: key_ring_name, crypto_key_id: id, crypto_key: key
puts "Created labeled key: #{created_key.name}"
# [END kms_create_key_labels]
created_key
end
def create_key_mac project_id:, location_id:, key_ring_id:, id:
# [START kms_create_key_mac]
# TODO(developer): uncomment these values before running the sample.
# project_id = "my-project"
# location_id = "us-east1"
# key_ring_id = "my-key-ring"
# id = "my-mac-key"
# Require the library.
require "google/cloud/kms"
# Create the client.
client = Google::Cloud::Kms.key_management_service
# Build the parent key ring name.
key_ring_name = client.key_ring_path project: project_id, location: location_id, key_ring: key_ring_id
# Build the key.
key = {
purpose: :MAC,
version_template: {
algorithm: :HMAC_SHA256
}
}
# Call the API.
created_key = client.create_crypto_key parent: key_ring_name, crypto_key_id: id, crypto_key: key
puts "Created mac key: #{created_key.name}"
# [END kms_create_key_mac]
created_key
end
def create_key_ring project_id:, location_id:, id:
# [START kms_create_key_ring]
# TODO(developer): uncomment these values before running the sample.
# project_id = "my-project"
# location_id = "us-east1"
# id = "my-key-ring"
# Require the library.
require "google/cloud/kms"
# Create the client.
client = Google::Cloud::Kms.key_management_service
# Build the parent location name.
location_name = client.location_path project: project_id, location: location_id
# Build the key ring.
key_ring = {}
# Call the API.
created_key_ring = client.create_key_ring parent: location_name, key_ring_id: id, key_ring: key_ring
puts "Created key ring: #{created_key_ring.name}"
# [END kms_create_key_ring]
created_key_ring
end
def create_key_rotation_schedule project_id:, location_id:, key_ring_id:, id:
# [START kms_create_key_rotation_schedule]
# TODO(developer): uncomment these values before running the sample.
# project_id = "my-project"
# location_id = "us-east1"
# key_ring_id = "my-key-ring"
# id = "my-key-with-rotation"
# Require the library.
require "google/cloud/kms"
# Create the client.
client = Google::Cloud::Kms.key_management_service
# Build the parent key ring name.
key_ring_name = client.key_ring_path project: project_id, location: location_id, key_ring: key_ring_id
# Build the key.
key = {
purpose: :ENCRYPT_DECRYPT,
version_template: {
algorithm: :GOOGLE_SYMMETRIC_ENCRYPTION
},
# Rotate the key every 30 days.
rotation_period: {
seconds: 60 * 60 * 24 * 30
},
# Start the first rotation in 24 hours.
next_rotation_time: {
seconds: (Time.now + (60 * 60 * 24)).to_i
}
}
# Call the API.
created_key = client.create_crypto_key parent: key_ring_name, crypto_key_id: id, crypto_key: key
puts "Created rotating key: #{created_key.name}"
# [END kms_create_key_rotation_schedule]
created_key
end
def create_key_symmetric_encrypt_decrypt project_id:, location_id:, key_ring_id:, id:
# [START kms_create_key_symmetric_encrypt_decrypt]
# TODO(developer): uncomment these values before running the sample.
# project_id = "my-project"
# location_id = "us-east1"
# key_ring_id = "my-key-ring"
# id = "my-symmetric-key"
# Require the library.
require "google/cloud/kms"
# Create the client.
client = Google::Cloud::Kms.key_management_service
# Build the parent key ring name.
key_ring_name = client.key_ring_path project: project_id, location: location_id, key_ring: key_ring_id
# Build the key.
key = {
purpose: :ENCRYPT_DECRYPT,
version_template: {
algorithm: :GOOGLE_SYMMETRIC_ENCRYPTION
}
}
# Call the API.
created_key = client.create_crypto_key parent: key_ring_name, crypto_key_id: id, crypto_key: key
puts "Created symmetric key: #{created_key.name}"
# [END kms_create_key_symmetric_encrypt_decrypt]
created_key
end
def create_key_version project_id:, location_id:, key_ring_id:, key_id:
# [START kms_create_key_version]
# TODO(developer): uncomment these values before running the sample.
# project_id = "my-project"
# location_id = "us-east1"
# key_ring_id = "my-key-ring"
# key_id = "my-key"
# Require the library.
require "google/cloud/kms"
# Create the client.
client = Google::Cloud::Kms.key_management_service
# Build the parent key name.
key_name = client.crypto_key_path project: project_id,
location: location_id,
key_ring: key_ring_id,
crypto_key: key_id
# Build the version.
version = {}
# Call the API.
created_version = client.create_crypto_key_version parent: key_name, crypto_key_version: version
puts "Created key version: #{created_version.name}"
# [END kms_create_key_version]
created_version
end
def decrypt_asymmetric project_id:, location_id:, key_ring_id:, key_id:, version_id:, ciphertext:
# [START kms_decrypt_asymmetric]
# TODO(developer): uncomment these values before running the sample.
# project_id = "my-project"
# location_id = "us-east1"
# key_ring_id = "my-key-ring"
# key_id = "my-key"
# version_id = "123"
# ciphertext = "..."
# Require the library.
require "google/cloud/kms"
# Create the client.
client = Google::Cloud::Kms.key_management_service
# Build the key version name.
key_version_name = client.crypto_key_version_path project: project_id,
location: location_id,
key_ring: key_ring_id,
crypto_key: key_id,
crypto_key_version: version_id
# Call the API.
response = client.asymmetric_decrypt key_version_name, ciphertext
puts "Plaintext: #{response.plaintext}"
# [END kms_decrypt_asymmetric]
response
end
def decrypt_symmetric project_id:, location_id:, key_ring_id:, key_id:, ciphertext:
# [START kms_decrypt_symmetric]
# TODO(developer): uncomment these values before running the sample.
# project_id = "my-project"
# location_id = "us-east1"
# key_ring_id = "my-key-ring"
# key_id = "my-key"
# ciphertext = "..."
# Require the library.
require "google/cloud/kms"
# Create the client.
client = Google::Cloud::Kms.key_management_service
# Build the parent key name.
key_name = client.crypto_key_path project: project_id,
location: location_id,
key_ring: key_ring_id,
crypto_key: key_id
# Call the API.
response = client.decrypt name: key_name, ciphertext: ciphertext
puts "Plaintext: #{response.plaintext}"
# [END kms_decrypt_symmetric]
response
end
def destroy_key_version project_id:, location_id:, key_ring_id:, key_id:, version_id:
# [START kms_destroy_key_version]
# TODO(developer): uncomment these values before running the sample.
# project_id = "my-project"
# location_id = "us-east1"
# key_ring_id = "my-key-ring"
# key_id = "my-key"
# version_id = "123"
# Require the library.
require "google/cloud/kms"
# Create the client.
client = Google::Cloud::Kms.key_management_service
# Build the key version name.
key_version_name = client.crypto_key_version_path project: project_id,
location: location_id,
key_ring: key_ring_id,
crypto_key: key_id,
crypto_key_version: version_id
# Call the API.
destroyed_version = client.destroy_crypto_key_version name: key_version_name
puts "Destroyed key version: #{destroyed_version.name}"
# [END kms_destroy_key_version]
destroyed_version
end
def disable_key_version project_id:, location_id:, key_ring_id:, key_id:, version_id:
# [START kms_disable_key_version]
# TODO(developer): uncomment these values before running the sample.
# project_id = "my-project"
# location_id = "us-east1"
# key_ring_id = "my-key-ring"
# key_id = "my-key"
# version_id = "123"
# Require the library.
require "google/cloud/kms"
# Create the client.
client = Google::Cloud::Kms.key_management_service
# Build the key version name.
key_version_name = client.crypto_key_version_path project: project_id,
location: location_id,
key_ring: key_ring_id,
crypto_key: key_id,
crypto_key_version: version_id
# Create the updated version.
version = {
name: key_version_name,
state: :DISABLED
}
# Create the field mask.
update_mask = { paths: ["state"] }
# Call the API.
disabled_version = client.update_crypto_key_version crypto_key_version: version, update_mask: update_mask
puts "Disabled key version: #{disabled_version.name}"
# [END kms_disable_key_version]
disabled_version
end
def enable_key_version project_id:, location_id:, key_ring_id:, key_id:, version_id:
# [START kms_enable_key_version]
# TODO(developer): uncomment these values before running the sample.
# project_id = "my-project"
# location_id = "us-east1"
# key_ring_id = "my-key-ring"
# key_id = "my-key"
# version_id = "123"
# Require the library.
require "google/cloud/kms"
# Create the client.
client = Google::Cloud::Kms.key_management_service
# Build the key version name.
key_version_name = client.crypto_key_version_path project: project_id,
location: location_id,
key_ring: key_ring_id,
crypto_key: key_id,
crypto_key_version: version_id
# Create the updated version.
version = {
name: key_version_name,
state: :ENABLED
}
# Create the field mask.
update_mask = { paths: ["state"] }
# Call the API.
enabled_version = client.update_crypto_key_version crypto_key_version: version, update_mask: update_mask
puts "Enabled key version: #{enabled_version.name}"
# [END kms_enable_key_version]
enabled_version
end
def encrypt_asymmetric project_id:, location_id:, key_ring_id:, key_id:, version_id:, plaintext:
# [START kms_encrypt_asymmetric]
# Ruby has limited support for asymmetric encryption operations. Specifically,
# public_encrypt() does not allow customizing the MGF hash algorithm. Thus, it
# is not currently possible to use Ruby core for asymmetric encryption
# operations on RSA keys from Cloud KMS.
#
# Third party libraries may provide the required functionality. Google does
# not endorse these external libraries.
# [END kms_encrypt_asymmetric]
_ = project_id, location_id, key_ring_id, key_id, version_id, plaintext
end
def encrypt_symmetric project_id:, location_id:, key_ring_id:, key_id:, plaintext:
# [START kms_encrypt_symmetric]
# TODO(developer): uncomment these values before running the sample.
# project_id = "my-project"
# location_id = "us-east1"
# key_ring_id = "my-key-ring"
# key_id = "my-key"
# plaintext = "..."
# Require the library.
require "google/cloud/kms"
# Create the client.
client = Google::Cloud::Kms.key_management_service
# Build the parent key name.
key_name = client.crypto_key_path project: project_id,
location: location_id,
key_ring: key_ring_id,
crypto_key: key_id
# Call the API.
response = client.encrypt name: key_name, plaintext: plaintext
puts "Ciphertext: #{Base64.strict_encode64 response.ciphertext}"
# [END kms_encrypt_symmetric]
response
end
def generate_random_bytes project_id:, location_id:, num_bytes:
# [START kms_generate_random_bytes]
# TODO(developer): uncomment these values before running the sample.
# project_id = "my-project"
# location_id = "us-east1"
# num_bytes = 256
# Require the library.
require "google/cloud/kms"
# Create the client.
client = Google::Cloud::Kms.key_management_service
# Build the parent location name.
location_name = client.location_path project: project_id,
location: location_id
# Call the API.
response = client.generate_random_bytes location: location_name, length_bytes: num_bytes, protection_level: :HSM
# The data comes back as raw bytes, which may include non-printable
# characters. This base64-encodes the result so it can be printed below.
encoded_data = Base64.strict_encode64 response.data
puts "Random bytes: #{encoded_data}"
# [END kms_generate_random_bytes]
response
end
def get_key_labels project_id:, location_id:, key_ring_id:, key_id:
# [START kms_get_key_labels]
# TODO(developer): uncomment these values before running the sample.
# project_id = "my-project"
# location_id = "us-east1"
# key_ring_id = "my-key-ring"
# key_id = "my-key"
# Require the library.
require "google/cloud/kms"
# Create the client.
client = Google::Cloud::Kms.key_management_service
# Build the parent key name.
key_name = client.crypto_key_path project: project_id,
location: location_id,
key_ring: key_ring_id,
crypto_key: key_id
# Call the API.
key = client.get_crypto_key name: key_name
# Example of iterating over labels.
key.labels.each do |k, v|
puts "#{k} = #{v}"
end
# [END kms_get_key_labels]
key
end
def get_key_version_attestation project_id:, location_id:, key_ring_id:, key_id:, version_id:
# [START kms_get_key_version_attestation]
# TODO(developer): uncomment these values before running the sample.
# project_id = "my-project"
# location_id = "us-east1"
# key_ring_id = "my-key-ring"
# key_id = "my-key"
# version_id = "123"
# Require the library.
require "google/cloud/kms"
# Create the client.
client = Google::Cloud::Kms.key_management_service
# Build the key version name.
key_version_name = client.crypto_key_version_path project: project_id,
location: location_id,
key_ring: key_ring_id,
crypto_key: key_id,
crypto_key_version: version_id
# Call the API.
version = client.get_crypto_key_version name: key_version_name
# Only HSM keys have an attestation. For other key types, the attestion will
# be nil.
attestation = version.attestation
unless attestation
raise "no attestation"
end
puts "Attestation: #{Base64.strict_encode64 attestation.content}"
# [END kms_get_key_version_attestation]
attestation
end
def get_public_key project_id:, location_id:, key_ring_id:, key_id:, version_id:
# [START kms_get_public_key]
# TODO(developer): uncomment these values before running the sample.
# project_id = "my-project"
# location_id = "us-east1"
# key_ring_id = "my-key-ring"
# key_id = "my-key"
# version_id = "123"
# Require the library.
require "google/cloud/kms"
# Create the client.
client = Google::Cloud::Kms.key_management_service
# Build the key version name.
key_version_name = client.crypto_key_version_path project: project_id,
location: location_id,
key_ring: key_ring_id,
crypto_key: key_id,
crypto_key_version: version_id
# Call the API.
public_key = client.get_public_key name: key_version_name
puts "Public key: #{public_key.pem}"
# [END kms_get_public_key]
public_key
end
def iam_add_member project_id:, location_id:, key_ring_id:, key_id:, member:
# [START kms_iam_add_member]
# TODO(developer): uncomment these values before running the sample.
# project_id = "my-project"
# location_id = "us-east1"
# key_ring_id = "my-key-ring"
# key_id = "my-key"
# member = "user:foo@example.com"
# Require the library.
require "google/cloud/kms"
# Create the client.
client = Google::Cloud::Kms.key_management_service
# Build the resource name.
resource_name = client.crypto_key_path project: project_id,
location: location_id,
key_ring: key_ring_id,
crypto_key: key_id
# The resource name could also be a key ring.
# resource_name = client.key_ring_path project: project_id, location: location_id, key_ring: key_ring_id
# Create the IAM client.
iam_client = Google::Cloud::Kms::V1::IAMPolicy::Client.new
# Get the current IAM policy.
policy = iam_client.get_iam_policy resource: resource_name
# Add the member to the policy.
policy.bindings << Google::Iam::V1::Binding.new(
members: [member],
role: "roles/cloudkms.cryptoKeyEncrypterDecrypter"
)
# Save the updated policy.
updated_policy = iam_client.set_iam_policy resource: resource_name, policy: policy
puts "Added #{member}"
# [END kms_iam_add_member]
updated_policy
end
def iam_get_policy project_id:, location_id:, key_ring_id:, key_id:
# [START kms_iam_get_policy]
# TODO(developer): uncomment these values before running the sample.
# project_id = "my-project"
# location_id = "us-east1"
# key_ring_id = "my-key-ring"
# key_id = "my-key"
# Require the library.
require "google/cloud/kms"
# Create the client.
client = Google::Cloud::Kms.key_management_service
# Build the resource name.
resource_name = client.crypto_key_path project: project_id,
location: location_id,
key_ring: key_ring_id,
crypto_key: key_id
# The resource name could also be a key ring.
# resource_name = client.key_ring_path project: project_id, location: location_id, key_ring: key_ring_id
# Create the IAM client.
iam_client = Google::Cloud::Kms::V1::IAMPolicy::Client.new
# Get the current IAM policy.
policy = iam_client.get_iam_policy resource: resource_name
# Print the policy.
puts "Policy for #{resource_name}"
policy.bindings.each do |bind|
puts bind.role
bind.members.each do |member|
puts "- #{member}"
end
end
# [END kms_iam_get_policy]
policy
end
def iam_remove_member project_id:, location_id:, key_ring_id:, key_id:, member:
# [START kms_iam_remove_member]
# TODO(developer): uncomment these values before running the sample.
# project_id = "my-project"
# location_id = "us-east1"
# key_ring_id = "my-key-ring"
# key_id = "my-key"
# member = "user:foo@example.com"
# Require the library.
require "google/cloud/kms"
# Create the client.
client = Google::Cloud::Kms.key_management_service
# Build the resource name.
resource_name = client.crypto_key_path project: project_id,
location: location_id,
key_ring: key_ring_id,
crypto_key: key_id
# The resource name could also be a key ring.
# resource_name = client.key_ring_path project: project_id, location: location_id, key_ring: key_ring_id
# Create the IAM client.
iam_client = Google::Cloud::Kms::V1::IAMPolicy::Client.new
# Get the current IAM policy.
policy = iam_client.get_iam_policy resource: resource_name
# Remove the member from the current bindings
policy.bindings.each do |bind|
if bind.role == "roles/cloudkms.cryptoKeyEncrypterDecrypter"
bind.members.delete member
end
end
# Save the updated policy.
updated_policy = iam_client.set_iam_policy resource: resource_name, policy: policy
puts "Removed #{member}"
# [END kms_iam_remove_member]
updated_policy
end
def quickstart project_id:, location_id:
# [START kms_quickstart]
# TODO(developer): uncomment these values before running the sample.
# project_id = "my-project"
# location_id = "us-east1"
# Require the library.
require "google/cloud/kms"
# Create the client.
client = Google::Cloud::Kms.key_management_service
# Build the parent location name.
location_name = client.location_path project: project_id, location: location_id
# Call the API.
key_rings = client.list_key_rings parent: location_name
# Example of iterating over key rings.
puts "Key rings in #{location_name}"
key_rings.each do |key_ring|
puts key_ring.name
end
# [END kms_quickstart]
key_rings
end
def restore_key_version project_id:, location_id:, key_ring_id:, key_id:, version_id:
# [START kms_restore_key_version]
# TODO(developer): uncomment these values before running the sample.
# project_id = "my-project"
# location_id = "us-east1"
# key_ring_id = "my-key-ring"
# key_id = "my-key"
# version_id = "123"
# Require the library.
require "google/cloud/kms"
# Create the client.
client = Google::Cloud::Kms.key_management_service
# Build the key version name.
key_version_name = client.crypto_key_version_path project: project_id,
location: location_id,
key_ring: key_ring_id,
crypto_key: key_id,
crypto_key_version: version_id
# Call the API.
restored_version = client.restore_crypto_key_version name: key_version_name
puts "Restored key version: #{restored_version.name}"
# [END kms_restore_key_version]
restored_version
end
def sign_asymmetric project_id:, location_id:, key_ring_id:, key_id:, version_id:, message:
# [START kms_sign_asymmetric]
# TODO(developer): uncomment these values before running the sample.
# project_id = "my-project"
# location_id = "us-east1"
# key_ring_id = "my-key-ring"
# key_id = "my-key"
# version_id = "123"
# message = "my message"
# Require the library.
require "google/cloud/kms"
# Require digest.
require "digest"
# Create the client.
client = Google::Cloud::Kms.key_management_service
# Build the key version name.
key_version_name = client.crypto_key_version_path project: project_id,
location: location_id,
key_ring: key_ring_id,
crypto_key: key_id,
crypto_key_version: version_id
# Calculate the hash.
#
# Note: Key algorithms will require a varying hash function. For
# example, EC_SIGN_P384_SHA384 requires SHA-384.
digest = { sha256: Digest::SHA256.digest(message) }
# Call the API.
sign_response = client.asymmetric_sign name: key_version_name, digest: digest
puts "Signature: #{Base64.strict_encode64 sign_response.signature}"
# [END kms_sign_asymmetric]
sign_response
end
def sign_mac project_id:, location_id:, key_ring_id:, key_id:, version_id:, data:
# [START kms_sign_mac]
# TODO(developer): uncomment these values before running the sample.
# project_id = "my-project"
# location_id = "us-east1"
# key_ring_id = "my-key-ring"
# key_id = "my-key"
# version_id = "123"
# data = "my data"
# Require the library.
require "google/cloud/kms"
# Require digest.
require "digest"
# Create the client.
client = Google::Cloud::Kms.key_management_service
# Build the key version name.
key_version_name = client.crypto_key_version_path project: project_id,
location: location_id,
key_ring: key_ring_id,
crypto_key: key_id,
crypto_key_version: version_id
# Call the API.
sign_response = client.mac_sign name: key_version_name, data: data
# The data comes back as raw bytes, which may include non-printable
# characters. This base64-encodes the result so it can be printed below.
encoded_signature = Base64.strict_encode64 sign_response.mac
puts "Signature: #{encoded_signature}"
# [END kms_sign_mac]
sign_response
end
def update_key_add_rotation project_id:, location_id:, key_ring_id:, key_id:
# [START kms_update_key_add_rotation_schedule]
# TODO(developer): uncomment these values before running the sample.
# project_id = "my-project"
# location_id = "us-east1"
# key_ring_id = "my-key-ring"
# key_id = "my-key"
# Require the library.
require "google/cloud/kms"
# Create the client.
client = Google::Cloud::Kms.key_management_service
# Build the parent key name.
key_name = client.crypto_key_path project: project_id,
location: location_id,
key_ring: key_ring_id,
crypto_key: key_id
# Build the key.
key = {
name: key_name,
# Rotate the key every 30 days.
rotation_period: {
seconds: 60 * 60 * 24 * 30
},
# Start the first rotation in 24 hours.
next_rotation_time: {
seconds: (Time.now + (60 * 60 * 24)).to_i
}
}
# Build the field mask.
update_mask = { paths: ["rotation_period", "next_rotation_time"] }
# Call the API.
updated_key = client.update_crypto_key crypto_key: key, update_mask: update_mask
puts "Updated key: #{updated_key.name}"
# [END kms_update_key_add_rotation_schedule]
updated_key
end
def update_key_remove_labels project_id:, location_id:, key_ring_id:, key_id:
# [START kms_update_key_remove_labels]
# TODO(developer): uncomment these values before running the sample.
# project_id = "my-project"
# location_id = "us-east1"
# key_ring_id = "my-key-ring"
# key_id = "my-key"
# Require the library.