2015-04-25 19:10:53 +00:00
|
|
|
---
|
|
|
|
layout: "docs"
|
2017-09-20 20:05:00 +00:00
|
|
|
page_title: "MySQL - Secrets Engines"
|
2015-04-25 19:10:53 +00:00
|
|
|
sidebar_current: "docs-secrets-mysql"
|
|
|
|
description: |-
|
2017-09-20 20:05:00 +00:00
|
|
|
The MySQL secrets engine for Vault generates database credentials to access MySQL.
|
2015-04-25 19:10:53 +00:00
|
|
|
---
|
|
|
|
|
2017-09-20 20:05:00 +00:00
|
|
|
# MySQL Secrets Engine
|
2015-04-25 19:10:53 +00:00
|
|
|
|
|
|
|
Name: `mysql`
|
|
|
|
|
2017-09-20 20:05:00 +00:00
|
|
|
~> **Deprecation Note:** This secrets engine is deprecated in favor of the
|
|
|
|
combined databases secrets engine added in v0.7.1. See the documentation for
|
|
|
|
the new implementation of this secrets engine at
|
|
|
|
[MySQL/MariaDB database plugin](/docs/secrets/databases/mysql-maria.html).
|
2017-06-08 06:45:01 +00:00
|
|
|
|
2017-09-20 20:05:00 +00:00
|
|
|
The MySQL secrets engine for Vault generates database credentials
|
2015-04-25 19:10:53 +00:00
|
|
|
dynamically based on configured roles. This means that services that need
|
|
|
|
to access a database no longer need to hardcode credentials: they can request
|
|
|
|
them from Vault, and use Vault's leasing mechanism to more easily roll keys.
|
|
|
|
|
|
|
|
Additionally, it introduces a new ability: with every service accessing
|
|
|
|
the database with unique credentials, it makes auditing much easier when
|
|
|
|
questionable data access is discovered: you can track it down to the specific
|
|
|
|
instance of a service based on the SQL username.
|
|
|
|
|
|
|
|
Vault makes use of its own internal revocation system to ensure that users
|
|
|
|
become invalid within a reasonable time of the lease expiring.
|
|
|
|
|
2017-09-20 20:05:00 +00:00
|
|
|
This page will show a quick start for this secrets engine. For detailed documentation
|
|
|
|
on every path, use `vault path-help` after mounting the secrets engine.
|
2015-04-25 19:10:53 +00:00
|
|
|
|
|
|
|
## Quick Start
|
|
|
|
|
2017-09-20 20:05:00 +00:00
|
|
|
The first step to using the mysql secrets engine is to mount it. Unlike the `kv`
|
|
|
|
secrets engine, the `mysql` secrets engine is not mounted by default.
|
2015-04-27 03:02:58 +00:00
|
|
|
|
|
|
|
```
|
2017-09-20 20:05:00 +00:00
|
|
|
$ vault secrets enable mysql
|
|
|
|
Success! Enabled the mysql secrets engine at: mysql/
|
2015-04-27 03:02:58 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
Next, we must configure Vault to know how to connect to the MySQL
|
2016-12-16 08:37:54 +00:00
|
|
|
instance. This is done by providing a [DSN (Data Source Name)](https://github.com/go-sql-driver/mysql#dsn-data-source-name):
|
2015-04-27 03:02:58 +00:00
|
|
|
|
|
|
|
```
|
2015-10-12 16:10:22 +00:00
|
|
|
$ vault write mysql/config/connection \
|
2016-03-23 19:18:09 +00:00
|
|
|
connection_url="root:root@tcp(192.168.33.10:3306)/"
|
2015-04-27 03:02:58 +00:00
|
|
|
Success! Data written to: mysql/config/connection
|
|
|
|
```
|
|
|
|
|
|
|
|
In this case, we've configured Vault with the user "root" and password "root,
|
|
|
|
connecting to an instance at "192.168.33.10" on port 3306. It is not necessary
|
|
|
|
that Vault has the root user, but the user must have privileges to create
|
|
|
|
other users, namely the `GRANT OPTION` privilege.
|
|
|
|
|
2016-12-16 10:13:35 +00:00
|
|
|
For using UNIX socket use: `root:root@unix(/path/to/socket)/`.
|
|
|
|
|
2015-04-27 03:02:58 +00:00
|
|
|
Optionally, we can configure the lease settings for credentials generated
|
|
|
|
by Vault. This is done by writing to the `config/lease` key:
|
|
|
|
|
|
|
|
```
|
2015-10-12 16:10:22 +00:00
|
|
|
$ vault write mysql/config/lease \
|
|
|
|
lease=1h \
|
|
|
|
lease_max=24h
|
2015-04-27 03:02:58 +00:00
|
|
|
Success! Data written to: mysql/config/lease
|
|
|
|
```
|
|
|
|
|
|
|
|
This restricts each credential to being valid or leased for 1 hour
|
|
|
|
at a time, with a maximum use period of 24 hours. This forces an
|
|
|
|
application to renew their credentials at least hourly, and to recycle
|
|
|
|
them once per day.
|
|
|
|
|
|
|
|
The next step is to configure a role. A role is a logical name that maps
|
2015-09-05 00:05:45 +00:00
|
|
|
to a policy used to generate those credentials. For example, lets create
|
2015-04-27 03:02:58 +00:00
|
|
|
a "readonly" role:
|
|
|
|
|
|
|
|
```
|
2015-10-12 16:10:22 +00:00
|
|
|
$ vault write mysql/roles/readonly \
|
|
|
|
sql="CREATE USER '{{name}}'@'%' IDENTIFIED BY '{{password}}';GRANT SELECT ON *.* TO '{{name}}'@'%';"
|
2015-04-27 03:02:58 +00:00
|
|
|
Success! Data written to: mysql/roles/readonly
|
|
|
|
```
|
|
|
|
|
|
|
|
By writing to the `roles/readonly` path we are defining the `readonly` role.
|
|
|
|
This role will be created by evaluating the given `sql` statements. By
|
|
|
|
default, the `{{name}}` and `{{password}}` fields will be populated by
|
|
|
|
Vault with dynamically generated values. This SQL statement is creating
|
|
|
|
the named user, and then granting it `SELECT` or read-only privileges
|
|
|
|
to tables in the database. More complex `GRANT` queries can be used to
|
2015-04-27 19:16:07 +00:00
|
|
|
customize the privileges of the role. See the [MySQL manual](https://dev.mysql.com/doc/refman/5.7/en/grant.html)
|
|
|
|
for more information.
|
2015-04-27 03:02:58 +00:00
|
|
|
|
|
|
|
To generate a new set of credentials, we simply read from that role:
|
|
|
|
|
|
|
|
```
|
|
|
|
$ vault read mysql/creds/readonly
|
2017-09-20 20:05:00 +00:00
|
|
|
Key Value
|
|
|
|
--- -----
|
|
|
|
lease_id mysql/creds/readonly/bd404e98-0f35-b378-269a-b7770ef01897
|
|
|
|
lease_duration 3600
|
|
|
|
password 132ae3ef-5a64-7499-351e-bfe59f3a2a21
|
|
|
|
username readonly-aefa635a-18
|
2015-04-27 03:02:58 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
By reading from the `creds/readonly` path, Vault has generated a new
|
|
|
|
set of credentials using the `readonly` role configuration. Here we
|
|
|
|
see the dynamically generated username and password, along with a one
|
|
|
|
hour lease.
|
|
|
|
|
2017-09-20 20:05:00 +00:00
|
|
|
Using ACLs, it is possible to restrict using the mysql secrets engine such
|
2015-04-27 03:02:58 +00:00
|
|
|
that trusted operators can manage the role definitions, and both
|
|
|
|
users and applications are restricted in the credentials they are
|
|
|
|
allowed to read.
|
|
|
|
|
2016-07-20 13:36:51 +00:00
|
|
|
Optionally, you may configure both the number of characters from the role name
|
|
|
|
that are truncated to form the display name portion of the mysql username
|
2017-03-09 02:47:35 +00:00
|
|
|
interpolated into the `{{name}}` field: the default is 10.
|
2016-07-20 13:36:51 +00:00
|
|
|
|
|
|
|
You may also configure the total number of characters allowed in the entire
|
2017-02-07 16:55:29 +00:00
|
|
|
generated username (the sum of the display name and uuid portions); the
|
2016-07-20 13:36:51 +00:00
|
|
|
default is 16. Note that versions of MySQL prior to 5.8 have a 16 character
|
|
|
|
total limit on user names, so it is probably not safe to increase this above
|
|
|
|
the default on versions prior to that.
|
2016-07-13 00:05:43 +00:00
|
|
|
|
2015-04-27 18:17:13 +00:00
|
|
|
## API
|
2015-04-27 03:02:58 +00:00
|
|
|
|
2017-09-20 20:05:00 +00:00
|
|
|
The MySQL secrets engine has a full HTTP API. Please see the
|
|
|
|
[MySQL secrets engine API](/api/secret/mysql/index.html) for more
|
2017-03-09 02:47:35 +00:00
|
|
|
details.
|