File: transit.rst

package info (click to toggle)
python-hvac 2.3.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,800 kB
  • sloc: python: 29,360; makefile: 42; sh: 14
file content (551 lines) | stat: -rw-r--r-- 11,399 bytes parent folder | download | duplicates (2)
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
Transit
=======

.. contents::
   :local:
   :depth: 1


.. testsetup:: transit_secret

    client.sys.enable_secrets_engine(
        backend_type='transit',
    )

.. note:: The following helper method is used various of the examples included here.

.. testcode:: transit_secret

    import sys


    def base64ify(bytes_or_str):
        """Helper method to perform base64 encoding across Python 2.7 and Python 3.X"""
        if isinstance(bytes_or_str, str):
            input_bytes = bytes_or_str.encode('utf8')
        else:
            input_bytes = bytes_or_str

        output_bytes = base64.urlsafe_b64encode(input_bytes)
        return output_bytes.decode('ascii')

Create Key
----------

.. automethod:: hvac.api.secrets_engines.Transit.create_key
   :noindex:

Examples
````````

.. testcode:: transit_secret

    import hvac
    client = hvac.Client(url='https://127.0.0.1:8200')

    client.secrets.transit.create_key(name='hvac-key')

Read Key
--------

.. automethod:: hvac.api.secrets_engines.Transit.read_key
   :noindex:

Examples
````````

.. testcode:: transit_secret

    import hvac
    client = hvac.Client(url='https://127.0.0.1:8200')

    read_key_response = client.secrets.transit.read_key(name='hvac-key')
    latest_version = read_key_response['data']['latest_version']
    print('Latest version for key "hvac-key" is: {ver}'.format(ver=latest_version))

Example output:

.. testoutput:: transit_secret

    Latest version for key "hvac-key" is: 1


List Keys
---------

.. automethod:: hvac.api.secrets_engines.Transit.list_keys
   :noindex:

Examples
````````

.. testcode:: transit_secret

    import hvac
    client = hvac.Client(url='https://127.0.0.1:8200')

    list_keys_response = client.secrets.transit.read_key(name='hvac-key')
    keys = list_keys_response['data']['keys']
    print('Currently configured keys: {keys}'.format(keys=keys))

Example output:

.. testoutput:: transit_secret

    Currently configured keys: {'1': ...}


Delete Key
----------

.. automethod:: hvac.api.secrets_engines.Transit.delete_key
   :noindex:

Examples
````````

.. testcode:: transit_secret

    import hvac
    client = hvac.Client(url='https://127.0.0.1:8200')

    key_name = 'gonna-delete-this-key'

    client.secrets.transit.create_key(
        name=key_name,
    )

    # Update key subsequently to allow deletion...
    client.secrets.transit.update_key_configuration(
        name=key_name,
        deletion_allowed=True,
    )

    # Finally, delete the key
    client.secrets.transit.delete_key(name=key_name)


Update Key Configuration
------------------------

.. automethod:: hvac.api.secrets_engines.Transit.update_key_configuration
   :noindex:

Examples
````````

.. testcode:: transit_secret

    import hvac
    client = hvac.Client(url='https://127.0.0.1:8200')

    # allow key "hvac-key" to be exported in subsequent requests
    client.secrets.transit.update_key_configuration(
        name='hvac-key',
        exportable=True,
    )


Rotate Key
----------

.. automethod:: hvac.api.secrets_engines.Transit.rotate_key
   :noindex:

Examples
````````

.. testcode:: transit_secret

    import hvac
    client = hvac.Client(url='https://127.0.0.1:8200')
    client.secrets.transit.rotate_key(name='hvac-key')

Export Key
----------

.. automethod:: hvac.api.secrets_engines.Transit.export_key
   :noindex:

Examples
````````

.. testcode:: transit_secret

    import hvac
    client = hvac.Client(url='https://127.0.0.1:8200')
    export_key_response = client.secrets.transit.export_key(
        name='hvac-key',
        key_type='hmac-key',
    )

    print('Exported keys: %s' % export_key_response['data']['keys'])

Example output:

.. testoutput:: transit_secret

    Exported keys: {...}

Encrypt Data
------------

.. automethod:: hvac.api.secrets_engines.Transit.encrypt_data
   :noindex:

Examples
````````

.. testcode:: transit_secret

    import base64
    import hvac
    client = hvac.Client(url='https://127.0.0.1:8200')

    encrypt_data_response = client.secrets.transit.encrypt_data(
        name='hvac-key',
        plaintext=base64ify('hi its me hvac'.encode()),
    )
    ciphertext = encrypt_data_response['data']['ciphertext']
    print('Encrypted plaintext ciphertext is: {cipher}'.format(cipher=ciphertext))

Example output:

.. testoutput:: transit_secret

    Encrypted plaintext ciphertext is: vault:...


Decrypt Data
------------

.. automethod:: hvac.api.secrets_engines.Transit.decrypt_data
   :noindex:

Examples
````````

.. testcode:: transit_secret

    import hvac
    client = hvac.Client(url='https://127.0.0.1:8200')

    decrypt_data_response = client.secrets.transit.decrypt_data(
        name='hvac-key',
        ciphertext=ciphertext,
    )
    plaintext = decrypt_data_response['data']['plaintext']
    print('Decrypted plaintext is: {text}'.format(text=plaintext))

Example output:

.. testoutput:: transit_secret

    Decrypted plaintext is: ...


Rewrap Data
-----------

.. automethod:: hvac.api.secrets_engines.Transit.rewrap_data
   :noindex:

Examples
````````

.. testcode:: transit_secret

    import hvac
    client = hvac.Client(url='https://127.0.0.1:8200')

    encrypt_data_response = client.secrets.transit.rewrap_data(
        name='hvac-key',
        ciphertext=ciphertext,
    )
    rewrapped_ciphertext = encrypt_data_response['data']['ciphertext']
    print('Rewrapped ciphertext is: {cipher}'.format(cipher=rewrapped_ciphertext))

Example output:

.. testoutput:: transit_secret

    Rewrapped ciphertext is: vault:...


Generate Data Key
-----------------

.. automethod:: hvac.api.secrets_engines.Transit.generate_data_key
   :noindex:

Examples
````````

.. testcode:: transit_secret

    import hvac
    client = hvac.Client(url='https://127.0.0.1:8200')
    gen_key_response = client.secrets.transit.generate_data_key(
        name='hvac-key',
        key_type='plaintext',
    )
    ciphertext = gen_key_response['data']['ciphertext']
    print('Generated data key ciphertext is: {cipher}'.format(cipher=ciphertext))

Example output:

.. testoutput:: transit_secret


    Generated data key ciphertext is: vault:...

Generate Random Bytes
---------------------

.. automethod:: hvac.api.secrets_engines.Transit.generate_random_bytes
   :noindex:

Examples
````````

.. testcode:: transit_secret

    import hvac
    client = hvac.Client(url='https://127.0.0.1:8200')

    gen_bytes_response = client.secrets.transit.generate_random_bytes(n_bytes=32)
    random_bytes = gen_bytes_response['data']['random_bytes']
    print('Here are some random bytes: {bytes}'.format(bytes=random_bytes))

Example output:

.. testoutput:: transit_secret

    Here are some random bytes: ...


Hash Data
---------

.. automethod:: hvac.api.secrets_engines.Transit.hash_data
   :noindex:

Examples
````````

.. testcode:: transit_secret

    import hvac
    client = hvac.Client(url='https://127.0.0.1:8200')

    hash_data_response = client.secrets.transit.hash_data(
        hash_input=base64ify('hi its me hvac'),
        algorithm='sha2-256',
    )
    sum = hash_data_response['data']['sum']
    print('Hashed data is: {sum}'.format(sum=sum))

Example output:

.. testoutput:: transit_secret

    Hashed data is: ...


Generate Hmac
-------------

.. automethod:: hvac.api.secrets_engines.Transit.generate_hmac
   :noindex:

Examples
````````

.. testcode:: transit_secret

    import hvac
    client = hvac.Client(url='https://127.0.0.1:8200')

    generate_hmac_response = client.secrets.transit.generate_hmac(
        name='hvac-key',
        hash_input=base64ify('hi its me hvac'),
        algorithm='sha2-256',
    )
    hmac = generate_hmac_response['data']
    print("HMAC'd data is: {hmac}".format(hmac=hmac))

Example output:

.. testoutput:: transit_secret

    HMAC'd data is: {'hmac': 'vault:...'}


Sign Data
---------

.. automethod:: hvac.api.secrets_engines.Transit.sign_data
   :noindex:

Examples
````````

.. testcode:: transit_secret

    import hvac
    client = hvac.Client(url='https://127.0.0.1:8200')

    key_name = 'hvac-signing-key'

    # Note: some key types do no support signing...
    # E.g., "key type aes256-gcm96 does not support verification"
    client.secrets.transit.create_key(
        name=key_name,
        key_type='ed25519',
    )

    sign_data_response = client.secrets.transit.sign_data(
        name=key_name,
        hash_input=base64ify('hi its me hvac'),
    )
    signature = sign_data_response['data']['signature']
    print('Signature is: {signature}'.format(signature=signature))

Example output:

.. testoutput:: transit_secret

    Signature is: vault:...


Verify Signed Data
------------------

.. automethod:: hvac.api.secrets_engines.Transit.verify_signed_data
   :noindex:

Examples
````````

.. testcode:: transit_secret

    import hvac
    client = hvac.Client(url='https://127.0.0.1:8200')

    verify_signed_data_response = client.secrets.transit.verify_signed_data(
        name='hvac-signing-key',
        hash_input=base64ify('hi its me hvac'),
        signature=signature,
    )
    valid = verify_signed_data_response['data']['valid']
    print('Signature is valid?: {valid}'.format(valid=valid))

Example output:

.. testoutput:: transit_secret

    Signature is valid?: True


Backup Key
----------

.. automethod:: hvac.api.secrets_engines.Transit.backup_key
   :noindex:

Examples
````````

.. testcode:: transit_secret

    import hvac
    client = hvac.Client(url='https://127.0.0.1:8200')

    key_name = 'hvac-key'

    # Update the key configuration to allow exporting
    client.secrets.transit.update_key_configuration(
        name=key_name,
        exportable=True,
        allow_plaintext_backup=True,
    )

    backup_key_response = client.secrets.transit.backup_key(
        name=key_name,
    )

    backed_up_key = backup_key_response['data']['backup']
    print('Backed up key: %s' % backed_up_key)

Example output:

.. testoutput:: transit_secret

    Backed up key: ...

Restore Key
-----------

.. automethod:: hvac.api.secrets_engines.Transit.restore_key
   :noindex:

Examples
````````

.. testcode:: transit_secret

    import hvac

    client = hvac.Client(url='https://127.0.0.1:8200')

    client.secrets.transit.update_key_configuration(
        name=key_name,
        deletion_allowed=True,
    )
    delete_resp = client.secrets.transit.delete_key(name=key_name)

    # Restore a key after deletion
    client.secrets.transit.restore_key(backup=backed_up_key)


Trim Key
--------

.. automethod:: hvac.api.secrets_engines.Transit.trim_key
   :noindex:

Examples
````````

.. note:: Transit key trimming was added for Vault versions >=0.11.4.

.. testcode:: transit_secret
    :skipif: test_utils.vault_version_lt('0.11.4')

    import hvac
    client = hvac.Client(url='https://127.0.0.1:8200')

    key_name = 'hvac-key'

    for _ in range(0, 10):
        # Rotate the key a bunch...
        client.secrets.transit.rotate_key(
            name=key_name,
        )

    # Set a minimum encryption version
    client.secrets.transit.update_key_configuration(
        name=key_name,
        min_decryption_version=3,
        min_encryption_version=5,
    )

    # Trim any unneeded versions remaining of the key...
    client.secrets.transit.trim_key(
        name='hvac-key',
        min_version=3,
    )

.. testcleanup:: transit_secret

    client.sys.disable_secrets_engine('transit')