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

Ignore anonymous request failures checking kv status #1231

Merged
merged 2 commits into from
Jul 17, 2019
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
6 changes: 6 additions & 0 deletions dependency/vault_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,12 @@ func isKVv2(client *api.Client, path string) (string, bool, error) {
return "", false, nil
}

// anonymous requests may fail to access /sys/internal/ui path
// Vault v1.1.3 returns 500 status code but may return 4XX in future
if client.Token() == "" {
return "", false, nil
}

return "", false, err
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've considered always suppressing errors, so we fallback to pre-#1180 behavior, but was worried about transient errors. It will not be ideal if clients inconsistently wrap lookups as KVv2 and not, when hitting transient errors in this call; and debugging that will be very hard. Open for better suggestions.

}

Expand Down
60 changes: 60 additions & 0 deletions dependency/vault_read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package dependency
import (
"fmt"
"net/url"
"reflect"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -400,6 +402,64 @@ func TestVaultReadQuery_Fetch_KVv2(t *testing.T) {
}
}

// TestVaultReadQuery_Fetch_PKI_Anonymous asserts that vault.read can fetch a
// pki ca public cert even even when running unauthenticated client.
func TestVaultReadQuery_Fetch_PKI_Anonymous(t *testing.T) {
t.Parallel()

clients, vault := testVaultServer(t)
defer vault.Stop()

err := clients.Vault().Sys().Mount("pki", &api.MountInput{
Type: "pki",
})
if err != nil {
t.Fatal(err)
}

vc := clients.Vault()
_, err = vc.Logical().Write("sys/policies/acl/secrets-only", map[string]interface{}{
"policy": `path "secret/*" { capabilities = ["create", "read"] }`,
})
if err != nil {
t.Fatal(err)
}

_, err = vc.Logical().Write("pki/root/generate/internal", map[string]interface{}{
"common_name": "example.com",
"ttl": "24h",
})

anonClient := NewClientSet()
anonClient.CreateVaultClient(&CreateVaultClientInput{
Address: vault.Address,
Token: "",
})
_, err = anonClient.vault.client.Auth().Token().LookupSelf()
if err == nil || !strings.Contains(err.Error(), "missing client token") {
t.Fatalf("expected a missing client token error but found: %v", err)
}

d, err := NewVaultReadQuery("pki/cert/ca")
if err != nil {
t.Fatal(err)
}

act, _, err := d.Fetch(anonClient, nil)
if err != nil {
t.Fatal(err)
}

sec, ok := act.(*Secret)
if !ok {
t.Fatalf("expected secret but found %v", reflect.TypeOf(act))
}
cert, ok := sec.Data["certificate"].(string)
if !ok || !strings.Contains(cert, "BEGIN") {
t.Fatalf("expected a cert but found: %v", cert)
}
}

func TestVaultReadQuery_String(t *testing.T) {
t.Parallel()

Expand Down