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
|
Database
==============
.. note::
Every method under the :py:attr:`Database class<hvac.api.secrets_engines.Database>` includes a ``mount_point`` parameter that can be used to address the Database secret engine under a custom mount path. E.g., If enabling the Database secret engine using Vault's CLI commands via ``vault secrets enable -path=my-database database``, the ``mount_point`` parameter in :py:meth:`hvac.api.secrets_engines.Database()` methods would be set to ``my-database``.
Enable Database Secrets Engine
------------------------------
.. code:: python
import hvac
client = hvac.Client()
client.sys.enable.secrets_engine(
backend_type='database',
path='my-database'
)
.. note::
Example code below are for configuring and connecting to Postgres. See the `official developer docs`_ for a list of supported database plugins and detailed configuration requirements.
.. _official developer docs: https://developer.hashicorp.com/vault/docs/secrets/databases#database-capabilities
Configuration
-------------
:py:meth:`hvac.api.secrets_engines.Database.configure`
Configures the database engine:
.. code:: python
import hvac
client = hvac.Client()
client.secrets.database.configure(
name='db-connection-name',
plugin_name='postgresql-database-plugin',
allowed_roles='role-name',
connection_url=f'postgresql://{{{{username}}}}:{{{{password}}}}@postgres:5432/postgres?sslmode=disable',
username='db-username',
password='db-password',
)
.. note::
The database needs to be created and available to connect before you can configure the database secrets engine using the above configure method.
Read Configuration
-------------------
:py:meth:`hvac.api.secrets_engines.Database.read_connection`
Returns the configuration settings for a connection mounted under a path of ``my-database``:
.. code:: python
import hvac
client = hvac.Client()
connection_config = client.secrets.database.read_connection(
name='db-connection-name',
mount_point='my-database'
)
List Connections
----------------
:py:meth:`hvac.api.secrets_engines.Database.list_connections`
Returns a list of available connections:
.. code:: python
import hvac
client = hvac.Client()
connections = client.secrets.database.list_connections(
mount_point='my-database'
)
Delete Connection
-----------------
:py:meth:`hvac.api.secrets_engines.Database.delete_connection`
Deletes a connection:
.. code:: python
import hvac
client = hvac.Client()
client.secrets.database.delete_connection(
name='db-connection-name',
mount_point='my-database'
)
Reset Connection
----------------
:py:meth:`hvac.api.secrets_engines.Database.reset_connection`
Closes a connection and its underlying plugin and restarts it with the configuration stored:
.. code:: python
import hvac
client = hvac.Client()
client.secrets.database.reset_connection(
name='db-connection-name',
mount_point='my-database'
)
Create Role
------------
:py:meth:`hvac.api.secrets_engines.Database.create_role`
Creates or updates a role definition:
.. code:: python
import hvac
client = hvac.Client()
# SQL to create a new user with read only role to public schema
creation_statements = [
"CREATE ROLE \"{{name}}\" WITH LOGIN PASSWORD '{{password}}' VALID UNTIL '{{expiration}}';",
"GRANT SELECT ON ALL TABLES IN SCHEMA public TO \"{{name}}\";"
]
# Create a new role for the PostgreSQL connection
client.secrets.database.create_role(
name='role-name',
db_name='db-connection-name',
creation_statements=creation_statements,
default_ttl='1h',
max_ttl='24h',
mount_point='my-database'
)
Read A Role
-----------
:py:meth:`hvac.api.secrets_engines.Database.read_role`
Creates or updates a role definition:
.. code:: python
import hvac
client = hvac.Client()
role = client.secrets.database.read_role(
name='role-name',
mount_point='my-database'
)
List All The Roles
------------------
:py:meth:`hvac.api.secrets_engines.Database.list_roles`
Returns a list of available roles:
.. code:: python
import hvac
client = hvac.Client()
roles = client.secrets.database.list_roles(
mount_point='my-database'
)
Delete A Role
--------------
:py:meth:`hvac.api.secrets_engines.Database.delete_role`
Deletes a role definition:
.. code:: python
import hvac
client = hvac.Client()
client.secrets.database.delete_role(
name='role-name',
mount_point='my-database'
)
Rotate Root Credentials
------------------------
:py:meth:`hvac.api.secrets_engines.Database.rotate_root_credentials()`
Rotates the root credentials stored for the database connection.
This user must have permissions to update its own password.
.. code:: python
import hvac
client = hvac.Client()
client.secrets.database.rotate_root_credentials(
name='db-connection-name',
mount_point='my-database'
)
Generate Credentials
---------------------
:py:meth:`hvac.api.secrets_engines.Database.generate_credentials`
Generates a new set of dynamic credentials based on the named role:
.. code:: python
import hvac
client = hvac.Client()
credentials = client.secrets.database.generate_credentials(
name='role-name',
mount_point='my-database'
)
Get Static Credentials
-----------------------
:py:meth:`hvac.api.secrets_engines.Database.get_static_credentials`
Returns the current credentials based on the named static role:
.. code:: python
import hvac
client = hvac.Client()
credentials = client.secrets.database.get_static_credentials(
name='role-name',
mount_point='my-database'
)
Create Static Role
--------------------
:py:meth:`hvac.api.secrets_engines.Database.create_static_role`
Creates or updates a static role:
.. code:: python
import hvac
client = hvac.Client()
rotation_statement = ["ALTER USER \"{{name}}\" WITH PASSWORD '{{password}}';"]
credentials = client.secrets.database.create_static_role(
name='role-name',
db_name='db-connection-name',
username='static-role-username'
rotation_statements=rotation_statement,
rotation_period=86400,
mount_point='my-database'
)
.. note::
The ``username`` referenced above needs to be pre-created in the database prior to calling this method as Vault will be referencing this username to rotate its password.
Read Static Role
-----------------
:py:meth:`hvac.api.secrets_engines.Database.read_static_role`
Queries a static role definition:
.. code:: python
import hvac
client = hvac.Client()
client.secrets.database.read_static_role(
name='role-name',
mount_point='my-database'
)
List Static Roles
-------------------
:py:meth:`hvac.api.secrets_engines.Database.list_static_roles`
Returns a list of available static roles:
.. code:: python
import hvac
client = hvac.Client()
static_roles = client.secrets.database.list_static_roles(
mount_point='my-database'
)
Rotate Static Role Credentials
------------------------------
:py:meth:`hvac.api.secrets_engines.Database.rotate_static_role_credentials`
This endpoint is used to rotate the Static Role credentials stored for a given role name. While Static Roles are rotated automatically by Vault at configured rotation periods, users can use this endpoint to manually trigger a rotation to change the stored password and reset the TTL of the Static Role's password.
.. code:: python
import hvac
client = hvac.Client()
client.secrets.database.rotate_static_role_credentials(
name='role-name',
mount_point='my-database'
)
|