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
|
KV - Version 2
==============
.. note::
Every method under the :py:attr:`Kv class's v2 attribute<hvac.api.secrets_engines.Kv>` includes a `mount_point` parameter that can be used to address the KvV2 secret engine under a custom mount path. E.g., If enabling the KvV2 secret engine using Vault's CLI commands via `vault secrets enable -path=my-kvv2 -version=2 kv`", the `mount_point` parameter in :py:meth:`hvac.api.secrets_engines.KvV2` methods would be set to "my-kvv2".
Configuration
-------------
:py:meth:`hvac.api.secrets_engines.KvV2.configure`
Setting the default `max_versions` for a key/value engine version 2 under a path of `kv`:
.. code:: python
import hvac
client = hvac.Client()
client.secrets.kv.v2.configure(
max_versions=20,
mount_point='kv',
)
Setting the default `cas_required` (check-and-set required) flag under the implicit default path of `secret`:
.. code:: python
import hvac
client = hvac.Client()
client.secrets.kv.v2.configure(
cas_required=True,
)
Read Configuration
------------------
:py:meth:`hvac.api.secrets_engines.KvV2.configure`
Reading the configuration of a KV version 2 engine mounted under a path of `kv`:
.. code:: python
import hvac
client = hvac.Client()
kv_configuration = client.secrets.kv.v2.read_configuration(
mount_point='kv',
)
print('Config under path "kv": max_versions set to "{max_ver}"'.format(
max_ver=kv_configuration['data']['max_versions'],
))
print('Config under path "kv": check-and-set require flag set to {cas}'.format(
cas=kv_configuration['data']['cas_required'],
))
Read Secret Versions
--------------------
:py:meth:`hvac.api.secrets_engines.KvV2.read_secret_version`
Read the latest version of a given secret/path ("hvac"):
.. code:: python
import hvac
client = hvac.Client()
secret_version_response = client.secrets.kv.v2.read_secret_version(
path='hvac',
)
print('Latest version of secret under path "hvac" contains the following keys: {data}'.format(
data=secret_version_response['data']['data'].keys(),
))
print('Latest version of secret under path "hvac" created at: {date}'.format(
date=secret_version_response['data']['metadata']['created_time'],
))
print('Latest version of secret under path "hvac" is version #{ver}'.format(
ver=secret_version_response['data']['metadata']['version'],
))
Read specific version (1) of a given secret/path ("hvac"):
.. code:: python
import hvac
client = hvac.Client()
secret_version_response = client.secrets.kv.v2.read_secret_version(
path='hvac',
version=1,
)
print('Version 1 of secret under path "hvac" contains the following keys: {data}'.format(
data=secret_version_response['data']['data'].keys(),
))
print('Version 1 of secret under path "hvac" created at: {date}'.format(
date=secret_version_response['data']['metadata']['created_time'],
))
Create/Update Secret
--------------------
:py:meth:`hvac.api.secrets_engines.KvV2.create_or_update_secret`
.. code:: python
import hvac
client = hvac.Client()
client.secrets.kv.v2.create_or_update_secret(
path='hvac',
secret=dict(pssst='this is secret'),
)
`cas` parameter with an argument that doesn't match the current version:
.. code:: python
import hvac
client = hvac.Client()
# Assuming a current version of "6" for the path "hvac" =>
client.secrets.kv.v2.create_or_update_secret(
path='hvac',
secret=dict(pssst='this is secret'),
cas=5,
) # Raises hvac.exceptions.InvalidRequest
`cas` parameter set to `0` will only succeed if the path hasn't already been written.
.. code:: python
import hvac
client = hvac.Client()
client.secrets.kv.v2.create_or_update_secret(
path='hvac',
secret=dict(pssst='this is secret #1'),
cas=0,
)
client.secrets.kv.v2.create_or_update_secret(
path='hvac',
secret=dict(pssst='this is secret #2'),
cas=0,
) # => Raises hvac.exceptions.InvalidRequest
Patch Existing Secret
---------------------
Method (similar to the Vault CLI command `vault kv patch`) to update an existing path. Either to add a new key/value to the secret and/or update the value for an existing key. Raises an :py:class:`hvac.exceptions.InvalidRequest` if the path hasn't been written to previously.
:py:meth:`hvac.api.secrets_engines.KvV2.patch`
.. code:: python
import hvac
client = hvac.Client()
client.secrets.kv.v2.patch(
path='hvac',
secret=dict(pssst='this is a patched secret'),
)
Delete Latest Version of Secret
-------------------------------
:py:meth:`hvac.api.secrets_engines.KvV2.delete_latest_version_of_secret`
.. code:: python
import hvac
client = hvac.Client()
client.secrets.kv.v2.delete_latest_version_of_secret(
path=hvac,
)
Delete Secret Versions
----------------------
:py:meth:`hvac.api.secrets_engines.KvV2.delete_secret_versions`
Marking the first 3 versions of a secret deleted under path "hvac":
.. code:: python
import hvac
client = hvac.Client()
client.secrets.kv.v2.delete_secret_versions(
path='hvac',
versions=[1, 2, 3],
)
Undelete Secret Versions
------------------------
:py:meth:`hvac.api.secrets_engines.KvV2.undelete_secret_versions`
Marking the last 3 versions of a secret deleted under path "hvac" as "undeleted":
.. code:: python
import hvac
client = hvac.Client()
hvac_path_metadata = client.secrets.kv.v2.read_secret_metadata(
path='hvac',
)
oldest_version = hvac_path_metadata['data']['oldest_version']
current_version = hvac_path_metadata['data']['current_version']
versions_to_undelete = range(max(oldest_version, current_version - 2), current_version + 1)
client.secrets.kv.v2.undelete_secret_versions(
path='hvac',
versions=versions_to_undelete,
)
Destroy Secret Versions
-----------------------
:py:meth:`hvac.api.secrets_engines.KvV2.destroy_secret_versions`
Destroying the first three versions of a secret under path 'hvac':
.. code:: python
import hvac
client = hvac.Client()
client.secrets.kv.v2.destroy_secret_versions(
path='hvac',
versions=[1, 2, 3],
)
List Secrets
------------
:py:meth:`hvac.api.secrets_engines.KvV2.list_secrets`
Listing secrets under the 'hvac' path prefix:
.. code:: python
import hvac
client = hvac.Client()
client.secrets.kv.v2.create_or_update_secret(
path='hvac/big-ole-secret',
secret=dict(pssst='this is a large secret'),
)
client.secrets.kv.v2.create_or_update_secret(
path='hvac/lil-secret',
secret=dict(pssst='this secret... not so big'),
)
list_response = client.secrets.kv.v2.list_secrets(
path='hvac',
)
print('The following paths are available under "hvac" prefix: {keys}'.format(
keys=','.join(list_response['data']['keys']),
))
Read Secret Metadata
--------------------
:py:meth:`hvac.api.secrets_engines.KvV2.read_secret_metadata`
.. code:: python
import hvac
client = hvac.Client()
hvac_path_metadata = client.secrets.kv.v2.read_secret_metadata(
path='hvac',
)
print('Secret under path hvac is on version {cur_ver}, with an oldest version of {old_ver}'.format(
cur_ver=hvac_path_metadata['data']['oldest_version'],
old_ver=hvac_path_metadata['data']['current_version'],
))
Update Metadata
---------------
:py:meth:`hvac.api.secrets_engines.KvV2.update_metadata`
Set max versions for a given path ("hvac") to 3:
.. code:: python
import hvac
client = hvac.Client()
client.secrets.kv.v2.update_metadata(
path='hvac',
max_versions=3,
)
Set cas (check-and-set) parameter as required for a given path ("hvac"):
.. code:: python
import hvac
client = hvac.Client()
client.secrets.kv.v2.update_metadata(
path='hvac',
cas_required=True,
)
Set "delete_version_after" value to 30 minutes for all new versions written to the "hvac" path / key:
.. code:: python
import hvac
client = hvac.Client()
client.secrets.kv.v2.update_metadata(
path='hvac',
delete_version_after="30m",
)
Describe the secret with custom metadata values in ``custom_metadata`` (Vault >= 1.9.0):
.. code:: python
import hvac
client = hvac.Client()
clients.secrets.kv.v2.update_metadata(
path='hvac',
custom_metadata={
"type": "api-token",
"color": "blue",
},
)
Delete Metadata and All Versions
--------------------------------
:py:meth:`hvac.api.secrets_engines.KvV2.delete_metadata_and_all_versions`
Delete all versions and metadata for a given path:
.. code:: python
import hvac
client = hvac.Client()
client.secrets.kv.v2.delete_metadata_and_all_versions(
path='hvac',
)
|