2015-06-08 10:32:44 +00:00
|
|
|
package physical
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2015-06-12 05:56:25 +00:00
|
|
|
"fmt"
|
2015-06-08 10:32:44 +00:00
|
|
|
"sort"
|
2015-06-12 17:31:46 +00:00
|
|
|
"strings"
|
2015-06-08 10:32:44 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/armon/go-metrics"
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MySQLBackend is a physical backend that stores data
|
|
|
|
// within MySQL database.
|
|
|
|
type MySQLBackend struct {
|
2015-06-18 21:31:00 +00:00
|
|
|
dbTable string
|
2015-06-12 05:56:25 +00:00
|
|
|
client *sql.DB
|
|
|
|
statements map[string]*sql.Stmt
|
2015-06-08 10:32:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newMySQLBackend constructs a MySQL backend using the given API client and
|
|
|
|
// server address and credential for accessing mysql database.
|
|
|
|
func newMySQLBackend(conf map[string]string) (Backend, error) {
|
2015-06-18 21:31:00 +00:00
|
|
|
// Get the MySQL credentials to perform read/write operations.
|
|
|
|
username, ok := conf["username"]
|
|
|
|
if !ok || username == "" {
|
|
|
|
return nil, fmt.Errorf("missing username")
|
|
|
|
}
|
|
|
|
password, ok := conf["password"]
|
|
|
|
if !ok || username == "" {
|
|
|
|
return nil, fmt.Errorf("missing password")
|
|
|
|
}
|
|
|
|
|
2015-06-08 10:32:44 +00:00
|
|
|
// Get or set MySQL server address. Defaults to localhost and default port(3306)
|
|
|
|
address, ok := conf["address"]
|
|
|
|
if !ok {
|
|
|
|
address = "127.0.0.1:3306"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the MySQL database and table details.
|
|
|
|
database, ok := conf["database"]
|
|
|
|
if !ok {
|
2015-06-18 21:31:00 +00:00
|
|
|
database = "vault"
|
2015-06-08 10:32:44 +00:00
|
|
|
}
|
|
|
|
table, ok := conf["table"]
|
|
|
|
if !ok {
|
2015-06-18 21:31:00 +00:00
|
|
|
table = "vault"
|
2015-06-08 10:32:44 +00:00
|
|
|
}
|
2015-06-18 21:31:00 +00:00
|
|
|
dbTable := database + "." + table
|
2015-06-08 10:32:44 +00:00
|
|
|
|
|
|
|
// Create MySQL handle for the database.
|
2015-06-18 21:31:00 +00:00
|
|
|
dsn := username + ":" + password + "@tcp(" + address + ")/"
|
2015-06-08 10:32:44 +00:00
|
|
|
db, err := sql.Open("mysql", dsn)
|
|
|
|
if err != nil {
|
2015-06-18 21:31:00 +00:00
|
|
|
return nil, fmt.Errorf("failed to connect to mysql: %v", err)
|
2015-06-08 10:32:44 +00:00
|
|
|
}
|
|
|
|
|
2015-06-18 21:31:00 +00:00
|
|
|
// Create the required database if it doesn't exists.
|
|
|
|
if _, err := db.Exec("CREATE DATABASE IF NOT EXISTS " + database); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create mysql database: %v", err)
|
2015-06-08 10:32:44 +00:00
|
|
|
}
|
|
|
|
|
2015-06-18 21:31:00 +00:00
|
|
|
// Create the required table if it doesn't exists.
|
|
|
|
create_query := "CREATE TABLE IF NOT EXISTS " + dbTable +
|
|
|
|
" (vault_key varchar(512), vault_value mediumblob, PRIMARY KEY (vault_key))"
|
|
|
|
if _, err := db.Exec(create_query); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create mysql table: %v", err)
|
2015-06-12 05:56:25 +00:00
|
|
|
}
|
|
|
|
|
2015-06-08 10:32:44 +00:00
|
|
|
// Setup the backend.
|
|
|
|
m := &MySQLBackend{
|
2015-06-18 21:31:00 +00:00
|
|
|
dbTable: dbTable,
|
2015-06-12 05:56:25 +00:00
|
|
|
client: db,
|
2015-06-18 21:31:00 +00:00
|
|
|
statements: make(map[string]*sql.Stmt),
|
2015-06-08 10:32:44 +00:00
|
|
|
}
|
|
|
|
|
2015-06-18 21:31:00 +00:00
|
|
|
// Prepare all the statements required
|
|
|
|
statements := map[string]string{
|
|
|
|
"put": "INSERT INTO " + dbTable +
|
|
|
|
" VALUES( ?, ? ) ON DUPLICATE KEY UPDATE vault_value=VALUES(vault_value)",
|
|
|
|
"get": "SELECT vault_value FROM " + dbTable + " WHERE vault_key = ?",
|
|
|
|
"delete": "DELETE FROM " + dbTable + " WHERE vault_key = ?",
|
|
|
|
"list": "SELECT vault_key FROM " + dbTable + " WHERE vault_key LIKE ?",
|
|
|
|
}
|
|
|
|
for name, query := range statements {
|
|
|
|
if err := m.prepare(name, query); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2015-06-08 10:32:44 +00:00
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2015-06-18 21:31:00 +00:00
|
|
|
// prepare is a helper to prepare a query for future execution
|
|
|
|
func (m *MySQLBackend) prepare(name, query string) error {
|
|
|
|
stmt, err := m.client.Prepare(query)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to prepare '%s': %v", name, err)
|
|
|
|
}
|
|
|
|
m.statements[name] = stmt
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-08 10:32:44 +00:00
|
|
|
// Put is used to insert or update an entry.
|
|
|
|
func (m *MySQLBackend) Put(entry *Entry) error {
|
|
|
|
defer metrics.MeasureSince([]string{"mysql", "put"}, time.Now())
|
|
|
|
|
2015-06-12 05:56:25 +00:00
|
|
|
_, err := m.statements["put"].Exec(entry.Key, entry.Value)
|
2015-06-08 10:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get is used to fetch and entry.
|
|
|
|
func (m *MySQLBackend) Get(key string) (*Entry, error) {
|
|
|
|
defer metrics.MeasureSince([]string{"mysql", "get"}, time.Now())
|
|
|
|
|
|
|
|
var result []byte
|
2015-06-12 05:56:25 +00:00
|
|
|
err := m.statements["get"].QueryRow(key).Scan(&result)
|
2015-06-18 21:31:00 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2015-06-13 02:19:40 +00:00
|
|
|
return nil, nil
|
2015-06-08 10:32:44 +00:00
|
|
|
}
|
2015-06-18 21:31:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-06-12 17:31:46 +00:00
|
|
|
}
|
|
|
|
|
2015-06-08 10:32:44 +00:00
|
|
|
ent := &Entry{
|
|
|
|
Key: key,
|
|
|
|
Value: result,
|
|
|
|
}
|
|
|
|
return ent, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete is used to permanently delete an entry
|
|
|
|
func (m *MySQLBackend) Delete(key string) error {
|
|
|
|
defer metrics.MeasureSince([]string{"mysql", "delete"}, time.Now())
|
|
|
|
|
2015-06-12 05:56:25 +00:00
|
|
|
_, err := m.statements["delete"].Exec(key)
|
2015-06-08 10:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// List is used to list all the keys under a given
|
|
|
|
// prefix, up to the next prefix.
|
|
|
|
func (m *MySQLBackend) List(prefix string) ([]string, error) {
|
|
|
|
defer metrics.MeasureSince([]string{"mysql", "list"}, time.Now())
|
|
|
|
|
2015-06-18 21:31:00 +00:00
|
|
|
// Add the % wildcard to the prefix to do the prefix search
|
|
|
|
likePrefix := prefix + "%"
|
|
|
|
rows, err := m.statements["list"].Query(likePrefix)
|
2015-06-08 10:32:44 +00:00
|
|
|
|
2015-06-18 21:31:00 +00:00
|
|
|
var keys []string
|
2015-06-08 10:32:44 +00:00
|
|
|
for rows.Next() {
|
2015-06-18 21:31:00 +00:00
|
|
|
var key string
|
|
|
|
err = rows.Scan(&key)
|
2015-06-08 10:32:44 +00:00
|
|
|
if err != nil {
|
2015-06-18 21:31:00 +00:00
|
|
|
return nil, fmt.Errorf("failed to scan rows: %v", err)
|
2015-06-08 10:32:44 +00:00
|
|
|
}
|
|
|
|
|
2015-06-18 21:31:00 +00:00
|
|
|
key = strings.TrimPrefix(key, prefix)
|
|
|
|
if i := strings.Index(key, "/"); i == -1 {
|
|
|
|
// Add objects only from the current 'folder'
|
|
|
|
keys = append(keys, key)
|
|
|
|
} else if i != -1 {
|
|
|
|
// Add truncated 'folder' paths
|
|
|
|
keys = appendIfMissing(keys, string(key[:i+1]))
|
2015-06-08 10:32:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(keys)
|
|
|
|
return keys, nil
|
|
|
|
}
|