Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when KV store has a zero-length key. #9881

Merged
merged 3 commits into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ BUG FIXES:

* core: Fix resource leak in plugin API (plugin-dependent, not all plugins impacted) [[GH-9557](https://github.com/hashicorp/vault/pull/9557)]
* core: Fix race involved in enabling certain features via a license change
* core: Fix crash when metrics collection encounters zero-length keys in KV store [[GH-9811](https://github.com/hashicorp/vault/pull/9881)]
* replication (enterprise): Only write failover cluster addresses if they've changed
* secrets/database: Fix handling of TLS options in mongodb connection strings [[GH-9519](https://github.com/hashicorp/vault/pull/9519)]
* secrets/gcp: Ensure that the IAM policy version is appropriately set after a roleset's bindings have changed. [[GH-93](https://github.com/hashicorp/vault-plugin-secrets-gcp/pull/93)]
Expand Down
2 changes: 1 addition & 1 deletion vault/core_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ func (c *Core) walkKvMountSecrets(ctx context.Context, m *kvMount) {
return
}
for _, path := range keys {
if path[len(path)-1] == '/' {
if len(path) > 0 && path[len(path)-1] == '/' {
subdirectories = append(subdirectories, currentDirectory+path)
} else {
m.NumSecrets += 1
Expand Down
57 changes: 57 additions & 0 deletions vault/core_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,63 @@ func TestCoreMetrics_KvSecretGauge(t *testing.T) {
}
}

func TestCoreMetrics_KvSecretGauge_BadPath(t *testing.T) {
// Use the real KV implementation instead of Passthrough
AddTestLogicalBackend("kv", logicalKv.Factory)
// Clean up for the next test.
defer func() {
delete(testLogicalBackends, "kv")
}()
core, _, _ := TestCoreUnsealed(t)

me := &MountEntry{
Table: mountTableType,
Path: sanitizePath("kv1"),
Type: "kv",
Options: map[string]string{"version": "1"},
}
ctx := namespace.RootContext(nil)
err := core.mount(ctx, me)
if err != nil {
t.Fatalf("mount error: %v", err)
}

// I don't think there's any remaining way to create a zero-length
// key via the API, so we'll fake it by talking to the storage layer directly.
fake_entry := &logical.StorageEntry{
Key: "logical/" + me.UUID + "/foo/",
Value: []byte{1},
}
err = core.barrier.Put(ctx, fake_entry)
if err != nil {
t.Fatalf("put error: %v", err)
}

values, err := core.kvSecretGaugeCollector(ctx)
if err != nil {
t.Fatalf("collector error: %v", err)
}
t.Logf("Values: %v", values)
found := false
var count float32 = -1
for _, glv := range values {
for _, l := range glv.Labels {
if l.Name == "mount_point" && l.Value == "kv1/" {
found = true
count = glv.Value
break
}
}
}
if found {
if count != 1.0 {
t.Errorf("bad secret count for kv1/")
}
} else {
t.Errorf("no secret count for kv1/")
}
}

func TestCoreMetrics_KvSecretGaugeError(t *testing.T) {
core, _, _, sink := TestCoreUnsealedWithMetrics(t)
ctx := namespace.RootContext(nil)
Expand Down