Add prefixing to couch to fix the error that was exposed

This commit is contained in:
Jeff Mitchell 2017-10-20 18:24:15 -04:00
parent 425b781fc8
commit 7e32ac15ec
4 changed files with 67 additions and 2 deletions

View File

@ -22,6 +22,7 @@ import (
// CouchDBBackend allows the management of couchdb users
type CouchDBBackend struct {
logger log.Logger
prefixed bool
client *couchDBClient
permitPool *physical.PermitPool
}
@ -158,6 +159,19 @@ func buildCouchDBBackend(conf map[string]string, logger log.Logger) (*CouchDBBac
username = conf["username"]
}
prefixed := true
prefixedStr := os.Getenv("COUCHDB_PREFIXED")
if prefixedStr == "" {
prefixedStr = conf["prefixed"]
}
if prefixedStr != "" {
var err error
prefixed, err = strconv.ParseBool(prefixedStr)
if err != nil {
return nil, err
}
}
password := os.Getenv("COUCHDB_PASSWORD")
if password == "" {
password = conf["password"]
@ -183,6 +197,7 @@ func buildCouchDBBackend(conf map[string]string, logger log.Logger) (*CouchDBBac
password: password,
Client: cleanhttp.DefaultPooledClient(),
},
prefixed: prefixed,
logger: logger,
permitPool: physical.NewPermitPool(maxParInt),
}, nil
@ -227,6 +242,10 @@ func (m *CouchDBBackend) Delete(key string) error {
func (m *CouchDBBackend) List(prefix string) ([]string, error) {
defer metrics.MeasureSince([]string{"couchdb", "list"}, time.Now())
if m.prefixed {
prefix = "$" + prefix
}
m.permitPool.Acquire()
defer m.permitPool.Release()
@ -275,6 +294,10 @@ func NewTransactionalCouchDBBackend(conf map[string]string, logger log.Logger) (
func (m *CouchDBBackend) GetInternal(key string) (*physical.Entry, error) {
defer metrics.MeasureSince([]string{"couchdb", "get"}, time.Now())
if m.prefixed {
key = "$" + key
}
return m.client.get(key)
}
@ -282,12 +305,17 @@ func (m *CouchDBBackend) GetInternal(key string) (*physical.Entry, error) {
func (m *CouchDBBackend) PutInternal(entry *physical.Entry) error {
defer metrics.MeasureSince([]string{"couchdb", "put"}, time.Now())
revision, _ := m.client.rev(url.PathEscape(entry.Key))
key := entry.Key
if m.prefixed {
key = "$" + entry.Key
}
revision, _ := m.client.rev(url.PathEscape(key))
return m.client.put(couchDBEntry{
Entry: entry,
Rev: revision,
ID: url.PathEscape(entry.Key),
ID: url.PathEscape(key),
})
}
@ -295,6 +323,10 @@ func (m *CouchDBBackend) PutInternal(entry *physical.Entry) error {
func (m *CouchDBBackend) DeleteInternal(key string) error {
defer metrics.MeasureSince([]string{"couchdb", "delete"}, time.Now())
if m.prefixed {
key = "$" + key
}
revision, _ := m.client.rev(url.PathEscape(key))
deleted := true
return m.client.put(couchDBEntry{

View File

@ -30,6 +30,12 @@ storage "couchdb" {
## `couchdb` Parameters
- `prefixed` `(string: "true")` Specifies whether each value written to
CouchDB should be prefixed with `$`. If turned off, Vault may run into error
conditions if values are written that begin with an underscore, since it is a
reserved prefix in CouchDB. This can also be provided via the environment
variable `COUCHDB_PREFIXED`.
- `endpoint` `(string: "")` Specifies your CouchDB endpoint. This can also be
provided via the environment variable `COUCHDB_ENDPOINT`.

View File

@ -0,0 +1,24 @@
---
layout: "guides"
page_title: "Upgrading to Vault 0.9.0 - Guides"
sidebar_current: "guides-upgrading-to-0.9.0"
description: |-
This page contains the list of deprecations and important or breaking changes
for Vault 0.9.0. Please read it carefully.
---
# Overview
This page contains the list of deprecations and important or breaking changes
for Vault 0.9.0 compared to the most recent release. Please read it carefully.
## CouchDB Storage Changes
Vault may write values to storage that start with an underscore (`_`)
character. This is a reserved character in CouchDB, which can cause breakage.
As a result, this backend now stores each value prefixed with a `$` character.
If you are upgrading from existing CouchDB usage, you can turn off this
behavior by setting the `"prefixed"` configuration value to `"false"`.
Alternately, if you need to handle underscores at the start of keys, you can
rewrite your existing keys to start with a `$` character.

View File

@ -53,6 +53,9 @@
<li<%= sidebar_current("guides-upgrading-to-0.8.0") %>>
<a href="/guides/upgrading/upgrade-to-0.8.0.html">Upgrade to 0.8.0</a>
</li>
<li<%= sidebar_current("guides-upgrading-to-0.9.0") %>>
<a href="/guides/upgrading/upgrade-to-0.9.0.html">Upgrade to 0.9.0</a>
</li>
</ul>
</li>
</ul>