2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
package consul
|
2015-04-03 23:44:32 +00:00
|
|
|
|
|
|
|
import (
|
2022-09-13 17:03:19 +00:00
|
|
|
"bytes"
|
2019-05-01 17:47:41 +00:00
|
|
|
"context"
|
2022-09-13 17:03:19 +00:00
|
|
|
"encoding/hex"
|
2015-04-03 23:44:32 +00:00
|
|
|
"fmt"
|
2016-04-28 18:04:49 +00:00
|
|
|
"math/rand"
|
Teach Vault how to register with Consul
Vault will now register itself with Consul. The active node can be found using `active.vault.service.consul`. All standby vaults are available via `standby.vault.service.consul`. All unsealed vaults are considered healthy and available via `vault.service.consul`. Change in status and registration is event driven and should happen at the speed of a write to Consul (~network RTT + ~1x fsync(2)).
Healthy/active:
```
curl -X GET 'http://127.0.0.1:8500/v1/health/service/vault?pretty' && echo;
[
{
"Node": {
"Node": "vm1",
"Address": "127.0.0.1",
"TaggedAddresses": {
"wan": "127.0.0.1"
},
"CreateIndex": 3,
"ModifyIndex": 20
},
"Service": {
"ID": "vault:127.0.0.1:8200",
"Service": "vault",
"Tags": [
"active"
],
"Address": "127.0.0.1",
"Port": 8200,
"EnableTagOverride": false,
"CreateIndex": 17,
"ModifyIndex": 20
},
"Checks": [
{
"Node": "vm1",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm1",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "passing",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "",
"ServiceID": "vault:127.0.0.1:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 19
}
]
}
]
```
Healthy/standby:
```
[snip]
"Service": {
"ID": "vault:127.0.0.2:8200",
"Service": "vault",
"Tags": [
"standby"
],
"Address": "127.0.0.2",
"Port": 8200,
"EnableTagOverride": false,
"CreateIndex": 17,
"ModifyIndex": 20
},
"Checks": [
{
"Node": "vm2",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm2",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "passing",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "",
"ServiceID": "vault:127.0.0.2:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 19
}
]
}
]
```
Sealed:
```
"Checks": [
{
"Node": "vm2",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm2",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "critical",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "Vault Sealed",
"ServiceID": "vault:127.0.0.2:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 38
}
]
```
2016-04-24 00:15:05 +00:00
|
|
|
"reflect"
|
2019-05-01 17:47:41 +00:00
|
|
|
"strings"
|
2015-04-03 23:44:32 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/api"
|
2019-04-22 16:26:10 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/hashicorp/vault/helper/testhelpers/consul"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/logging"
|
|
|
|
"github.com/hashicorp/vault/sdk/physical"
|
2015-04-03 23:44:32 +00:00
|
|
|
)
|
|
|
|
|
Teach Vault how to register with Consul
Vault will now register itself with Consul. The active node can be found using `active.vault.service.consul`. All standby vaults are available via `standby.vault.service.consul`. All unsealed vaults are considered healthy and available via `vault.service.consul`. Change in status and registration is event driven and should happen at the speed of a write to Consul (~network RTT + ~1x fsync(2)).
Healthy/active:
```
curl -X GET 'http://127.0.0.1:8500/v1/health/service/vault?pretty' && echo;
[
{
"Node": {
"Node": "vm1",
"Address": "127.0.0.1",
"TaggedAddresses": {
"wan": "127.0.0.1"
},
"CreateIndex": 3,
"ModifyIndex": 20
},
"Service": {
"ID": "vault:127.0.0.1:8200",
"Service": "vault",
"Tags": [
"active"
],
"Address": "127.0.0.1",
"Port": 8200,
"EnableTagOverride": false,
"CreateIndex": 17,
"ModifyIndex": 20
},
"Checks": [
{
"Node": "vm1",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm1",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "passing",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "",
"ServiceID": "vault:127.0.0.1:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 19
}
]
}
]
```
Healthy/standby:
```
[snip]
"Service": {
"ID": "vault:127.0.0.2:8200",
"Service": "vault",
"Tags": [
"standby"
],
"Address": "127.0.0.2",
"Port": 8200,
"EnableTagOverride": false,
"CreateIndex": 17,
"ModifyIndex": 20
},
"Checks": [
{
"Node": "vm2",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm2",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "passing",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "",
"ServiceID": "vault:127.0.0.2:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 19
}
]
}
]
```
Sealed:
```
"Checks": [
{
"Node": "vm2",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm2",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "critical",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "Vault Sealed",
"ServiceID": "vault:127.0.0.2:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 38
}
]
```
2016-04-24 00:15:05 +00:00
|
|
|
func TestConsul_newConsulBackend(t *testing.T) {
|
|
|
|
tests := []struct {
|
2017-01-19 22:36:33 +00:00
|
|
|
name string
|
|
|
|
consulConfig map[string]string
|
|
|
|
fail bool
|
|
|
|
redirectAddr string
|
|
|
|
checkTimeout time.Duration
|
|
|
|
path string
|
|
|
|
service string
|
|
|
|
address string
|
|
|
|
scheme string
|
|
|
|
token string
|
|
|
|
max_parallel int
|
|
|
|
disableReg bool
|
|
|
|
consistencyMode string
|
Teach Vault how to register with Consul
Vault will now register itself with Consul. The active node can be found using `active.vault.service.consul`. All standby vaults are available via `standby.vault.service.consul`. All unsealed vaults are considered healthy and available via `vault.service.consul`. Change in status and registration is event driven and should happen at the speed of a write to Consul (~network RTT + ~1x fsync(2)).
Healthy/active:
```
curl -X GET 'http://127.0.0.1:8500/v1/health/service/vault?pretty' && echo;
[
{
"Node": {
"Node": "vm1",
"Address": "127.0.0.1",
"TaggedAddresses": {
"wan": "127.0.0.1"
},
"CreateIndex": 3,
"ModifyIndex": 20
},
"Service": {
"ID": "vault:127.0.0.1:8200",
"Service": "vault",
"Tags": [
"active"
],
"Address": "127.0.0.1",
"Port": 8200,
"EnableTagOverride": false,
"CreateIndex": 17,
"ModifyIndex": 20
},
"Checks": [
{
"Node": "vm1",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm1",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "passing",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "",
"ServiceID": "vault:127.0.0.1:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 19
}
]
}
]
```
Healthy/standby:
```
[snip]
"Service": {
"ID": "vault:127.0.0.2:8200",
"Service": "vault",
"Tags": [
"standby"
],
"Address": "127.0.0.2",
"Port": 8200,
"EnableTagOverride": false,
"CreateIndex": 17,
"ModifyIndex": 20
},
"Checks": [
{
"Node": "vm2",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm2",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "passing",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "",
"ServiceID": "vault:127.0.0.2:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 19
}
]
}
]
```
Sealed:
```
"Checks": [
{
"Node": "vm2",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm2",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "critical",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "Vault Sealed",
"ServiceID": "vault:127.0.0.2:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 38
}
]
```
2016-04-24 00:15:05 +00:00
|
|
|
}{
|
|
|
|
{
|
2017-01-19 22:36:33 +00:00
|
|
|
name: "Valid default config",
|
|
|
|
consulConfig: map[string]string{},
|
|
|
|
checkTimeout: 5 * time.Second,
|
|
|
|
redirectAddr: "http://127.0.0.1:8200",
|
|
|
|
path: "vault/",
|
|
|
|
service: "vault",
|
|
|
|
address: "127.0.0.1:8500",
|
|
|
|
scheme: "http",
|
|
|
|
token: "",
|
|
|
|
max_parallel: 4,
|
|
|
|
disableReg: false,
|
|
|
|
consistencyMode: "default",
|
Teach Vault how to register with Consul
Vault will now register itself with Consul. The active node can be found using `active.vault.service.consul`. All standby vaults are available via `standby.vault.service.consul`. All unsealed vaults are considered healthy and available via `vault.service.consul`. Change in status and registration is event driven and should happen at the speed of a write to Consul (~network RTT + ~1x fsync(2)).
Healthy/active:
```
curl -X GET 'http://127.0.0.1:8500/v1/health/service/vault?pretty' && echo;
[
{
"Node": {
"Node": "vm1",
"Address": "127.0.0.1",
"TaggedAddresses": {
"wan": "127.0.0.1"
},
"CreateIndex": 3,
"ModifyIndex": 20
},
"Service": {
"ID": "vault:127.0.0.1:8200",
"Service": "vault",
"Tags": [
"active"
],
"Address": "127.0.0.1",
"Port": 8200,
"EnableTagOverride": false,
"CreateIndex": 17,
"ModifyIndex": 20
},
"Checks": [
{
"Node": "vm1",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm1",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "passing",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "",
"ServiceID": "vault:127.0.0.1:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 19
}
]
}
]
```
Healthy/standby:
```
[snip]
"Service": {
"ID": "vault:127.0.0.2:8200",
"Service": "vault",
"Tags": [
"standby"
],
"Address": "127.0.0.2",
"Port": 8200,
"EnableTagOverride": false,
"CreateIndex": 17,
"ModifyIndex": 20
},
"Checks": [
{
"Node": "vm2",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm2",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "passing",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "",
"ServiceID": "vault:127.0.0.2:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 19
}
]
}
]
```
Sealed:
```
"Checks": [
{
"Node": "vm2",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm2",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "critical",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "Vault Sealed",
"ServiceID": "vault:127.0.0.2:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 38
}
]
```
2016-04-24 00:15:05 +00:00
|
|
|
},
|
|
|
|
{
|
2016-04-24 02:53:21 +00:00
|
|
|
name: "Valid modified config",
|
|
|
|
consulConfig: map[string]string{
|
|
|
|
"path": "seaTech/",
|
|
|
|
"service": "astronomy",
|
2016-08-15 13:42:42 +00:00
|
|
|
"redirect_addr": "http://127.0.0.2:8200",
|
2016-04-24 02:53:21 +00:00
|
|
|
"check_timeout": "6s",
|
|
|
|
"address": "127.0.0.2",
|
|
|
|
"scheme": "https",
|
|
|
|
"token": "deadbeef-cafeefac-deadc0de-feedface",
|
|
|
|
"max_parallel": "4",
|
|
|
|
"disable_registration": "false",
|
2017-01-19 22:36:33 +00:00
|
|
|
"consistency_mode": "strong",
|
Teach Vault how to register with Consul
Vault will now register itself with Consul. The active node can be found using `active.vault.service.consul`. All standby vaults are available via `standby.vault.service.consul`. All unsealed vaults are considered healthy and available via `vault.service.consul`. Change in status and registration is event driven and should happen at the speed of a write to Consul (~network RTT + ~1x fsync(2)).
Healthy/active:
```
curl -X GET 'http://127.0.0.1:8500/v1/health/service/vault?pretty' && echo;
[
{
"Node": {
"Node": "vm1",
"Address": "127.0.0.1",
"TaggedAddresses": {
"wan": "127.0.0.1"
},
"CreateIndex": 3,
"ModifyIndex": 20
},
"Service": {
"ID": "vault:127.0.0.1:8200",
"Service": "vault",
"Tags": [
"active"
],
"Address": "127.0.0.1",
"Port": 8200,
"EnableTagOverride": false,
"CreateIndex": 17,
"ModifyIndex": 20
},
"Checks": [
{
"Node": "vm1",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm1",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "passing",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "",
"ServiceID": "vault:127.0.0.1:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 19
}
]
}
]
```
Healthy/standby:
```
[snip]
"Service": {
"ID": "vault:127.0.0.2:8200",
"Service": "vault",
"Tags": [
"standby"
],
"Address": "127.0.0.2",
"Port": 8200,
"EnableTagOverride": false,
"CreateIndex": 17,
"ModifyIndex": 20
},
"Checks": [
{
"Node": "vm2",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm2",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "passing",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "",
"ServiceID": "vault:127.0.0.2:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 19
}
]
}
]
```
Sealed:
```
"Checks": [
{
"Node": "vm2",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm2",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "critical",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "Vault Sealed",
"ServiceID": "vault:127.0.0.2:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 38
}
]
```
2016-04-24 00:15:05 +00:00
|
|
|
},
|
2017-01-19 22:36:33 +00:00
|
|
|
checkTimeout: 6 * time.Second,
|
|
|
|
path: "seaTech/",
|
|
|
|
service: "astronomy",
|
|
|
|
redirectAddr: "http://127.0.0.2:8200",
|
|
|
|
address: "127.0.0.2",
|
|
|
|
scheme: "https",
|
|
|
|
token: "deadbeef-cafeefac-deadc0de-feedface",
|
|
|
|
max_parallel: 4,
|
|
|
|
consistencyMode: "strong",
|
Teach Vault how to register with Consul
Vault will now register itself with Consul. The active node can be found using `active.vault.service.consul`. All standby vaults are available via `standby.vault.service.consul`. All unsealed vaults are considered healthy and available via `vault.service.consul`. Change in status and registration is event driven and should happen at the speed of a write to Consul (~network RTT + ~1x fsync(2)).
Healthy/active:
```
curl -X GET 'http://127.0.0.1:8500/v1/health/service/vault?pretty' && echo;
[
{
"Node": {
"Node": "vm1",
"Address": "127.0.0.1",
"TaggedAddresses": {
"wan": "127.0.0.1"
},
"CreateIndex": 3,
"ModifyIndex": 20
},
"Service": {
"ID": "vault:127.0.0.1:8200",
"Service": "vault",
"Tags": [
"active"
],
"Address": "127.0.0.1",
"Port": 8200,
"EnableTagOverride": false,
"CreateIndex": 17,
"ModifyIndex": 20
},
"Checks": [
{
"Node": "vm1",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm1",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "passing",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "",
"ServiceID": "vault:127.0.0.1:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 19
}
]
}
]
```
Healthy/standby:
```
[snip]
"Service": {
"ID": "vault:127.0.0.2:8200",
"Service": "vault",
"Tags": [
"standby"
],
"Address": "127.0.0.2",
"Port": 8200,
"EnableTagOverride": false,
"CreateIndex": 17,
"ModifyIndex": 20
},
"Checks": [
{
"Node": "vm2",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm2",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "passing",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "",
"ServiceID": "vault:127.0.0.2:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 19
}
]
}
]
```
Sealed:
```
"Checks": [
{
"Node": "vm2",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm2",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "critical",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "Vault Sealed",
"ServiceID": "vault:127.0.0.2:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 38
}
]
```
2016-04-24 00:15:05 +00:00
|
|
|
},
|
2019-06-10 18:59:13 +00:00
|
|
|
{
|
|
|
|
name: "Unix socket",
|
|
|
|
consulConfig: map[string]string{
|
|
|
|
"address": "unix:///tmp/.consul.http.sock",
|
|
|
|
},
|
|
|
|
address: "/tmp/.consul.http.sock",
|
|
|
|
scheme: "http", // Default, not overridden?
|
|
|
|
|
|
|
|
// Defaults
|
|
|
|
checkTimeout: 5 * time.Second,
|
|
|
|
redirectAddr: "http://127.0.0.1:8200",
|
|
|
|
path: "vault/",
|
|
|
|
service: "vault",
|
|
|
|
token: "",
|
|
|
|
max_parallel: 4,
|
|
|
|
disableReg: false,
|
|
|
|
consistencyMode: "default",
|
|
|
|
},
|
2019-05-08 16:48:31 +00:00
|
|
|
{
|
|
|
|
name: "Scheme in address",
|
|
|
|
consulConfig: map[string]string{
|
|
|
|
"address": "https://127.0.0.2:5000",
|
|
|
|
},
|
|
|
|
address: "127.0.0.2:5000",
|
|
|
|
scheme: "https",
|
|
|
|
|
|
|
|
// Defaults
|
|
|
|
checkTimeout: 5 * time.Second,
|
|
|
|
redirectAddr: "http://127.0.0.1:8200",
|
|
|
|
path: "vault/",
|
|
|
|
service: "vault",
|
|
|
|
token: "",
|
|
|
|
max_parallel: 4,
|
|
|
|
disableReg: false,
|
|
|
|
consistencyMode: "default",
|
|
|
|
},
|
Teach Vault how to register with Consul
Vault will now register itself with Consul. The active node can be found using `active.vault.service.consul`. All standby vaults are available via `standby.vault.service.consul`. All unsealed vaults are considered healthy and available via `vault.service.consul`. Change in status and registration is event driven and should happen at the speed of a write to Consul (~network RTT + ~1x fsync(2)).
Healthy/active:
```
curl -X GET 'http://127.0.0.1:8500/v1/health/service/vault?pretty' && echo;
[
{
"Node": {
"Node": "vm1",
"Address": "127.0.0.1",
"TaggedAddresses": {
"wan": "127.0.0.1"
},
"CreateIndex": 3,
"ModifyIndex": 20
},
"Service": {
"ID": "vault:127.0.0.1:8200",
"Service": "vault",
"Tags": [
"active"
],
"Address": "127.0.0.1",
"Port": 8200,
"EnableTagOverride": false,
"CreateIndex": 17,
"ModifyIndex": 20
},
"Checks": [
{
"Node": "vm1",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm1",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "passing",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "",
"ServiceID": "vault:127.0.0.1:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 19
}
]
}
]
```
Healthy/standby:
```
[snip]
"Service": {
"ID": "vault:127.0.0.2:8200",
"Service": "vault",
"Tags": [
"standby"
],
"Address": "127.0.0.2",
"Port": 8200,
"EnableTagOverride": false,
"CreateIndex": 17,
"ModifyIndex": 20
},
"Checks": [
{
"Node": "vm2",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm2",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "passing",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "",
"ServiceID": "vault:127.0.0.2:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 19
}
]
}
]
```
Sealed:
```
"Checks": [
{
"Node": "vm2",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm2",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "critical",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "Vault Sealed",
"ServiceID": "vault:127.0.0.2:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 38
}
]
```
2016-04-24 00:15:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2018-04-03 00:46:59 +00:00
|
|
|
logger := logging.NewVaultLogger(log.Debug)
|
2016-08-19 20:45:17 +00:00
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
be, err := NewConsulBackend(test.consulConfig, logger)
|
2016-04-24 02:53:21 +00:00
|
|
|
if test.fail {
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf(`Expected config "%s" to fail`, test.name)
|
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else if !test.fail && err != nil {
|
|
|
|
t.Fatalf("Expected config %s to not fail: %v", test.name, err)
|
Teach Vault how to register with Consul
Vault will now register itself with Consul. The active node can be found using `active.vault.service.consul`. All standby vaults are available via `standby.vault.service.consul`. All unsealed vaults are considered healthy and available via `vault.service.consul`. Change in status and registration is event driven and should happen at the speed of a write to Consul (~network RTT + ~1x fsync(2)).
Healthy/active:
```
curl -X GET 'http://127.0.0.1:8500/v1/health/service/vault?pretty' && echo;
[
{
"Node": {
"Node": "vm1",
"Address": "127.0.0.1",
"TaggedAddresses": {
"wan": "127.0.0.1"
},
"CreateIndex": 3,
"ModifyIndex": 20
},
"Service": {
"ID": "vault:127.0.0.1:8200",
"Service": "vault",
"Tags": [
"active"
],
"Address": "127.0.0.1",
"Port": 8200,
"EnableTagOverride": false,
"CreateIndex": 17,
"ModifyIndex": 20
},
"Checks": [
{
"Node": "vm1",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm1",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "passing",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "",
"ServiceID": "vault:127.0.0.1:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 19
}
]
}
]
```
Healthy/standby:
```
[snip]
"Service": {
"ID": "vault:127.0.0.2:8200",
"Service": "vault",
"Tags": [
"standby"
],
"Address": "127.0.0.2",
"Port": 8200,
"EnableTagOverride": false,
"CreateIndex": 17,
"ModifyIndex": 20
},
"Checks": [
{
"Node": "vm2",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm2",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "passing",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "",
"ServiceID": "vault:127.0.0.2:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 19
}
]
}
]
```
Sealed:
```
"Checks": [
{
"Node": "vm2",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm2",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "critical",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "Vault Sealed",
"ServiceID": "vault:127.0.0.2:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 38
}
]
```
2016-04-24 00:15:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c, ok := be.(*ConsulBackend)
|
|
|
|
if !ok {
|
2016-04-24 02:53:21 +00:00
|
|
|
t.Fatalf("Expected ConsulBackend: %s", test.name)
|
|
|
|
}
|
2016-04-28 18:04:49 +00:00
|
|
|
|
Teach Vault how to register with Consul
Vault will now register itself with Consul. The active node can be found using `active.vault.service.consul`. All standby vaults are available via `standby.vault.service.consul`. All unsealed vaults are considered healthy and available via `vault.service.consul`. Change in status and registration is event driven and should happen at the speed of a write to Consul (~network RTT + ~1x fsync(2)).
Healthy/active:
```
curl -X GET 'http://127.0.0.1:8500/v1/health/service/vault?pretty' && echo;
[
{
"Node": {
"Node": "vm1",
"Address": "127.0.0.1",
"TaggedAddresses": {
"wan": "127.0.0.1"
},
"CreateIndex": 3,
"ModifyIndex": 20
},
"Service": {
"ID": "vault:127.0.0.1:8200",
"Service": "vault",
"Tags": [
"active"
],
"Address": "127.0.0.1",
"Port": 8200,
"EnableTagOverride": false,
"CreateIndex": 17,
"ModifyIndex": 20
},
"Checks": [
{
"Node": "vm1",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm1",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "passing",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "",
"ServiceID": "vault:127.0.0.1:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 19
}
]
}
]
```
Healthy/standby:
```
[snip]
"Service": {
"ID": "vault:127.0.0.2:8200",
"Service": "vault",
"Tags": [
"standby"
],
"Address": "127.0.0.2",
"Port": 8200,
"EnableTagOverride": false,
"CreateIndex": 17,
"ModifyIndex": 20
},
"Checks": [
{
"Node": "vm2",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm2",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "passing",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "",
"ServiceID": "vault:127.0.0.2:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 19
}
]
}
]
```
Sealed:
```
"Checks": [
{
"Node": "vm2",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm2",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "critical",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "Vault Sealed",
"ServiceID": "vault:127.0.0.2:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 38
}
]
```
2016-04-24 00:15:05 +00:00
|
|
|
if test.path != c.path {
|
2016-04-24 02:53:21 +00:00
|
|
|
t.Errorf("bad: %s %v != %v", test.name, test.path, c.path)
|
Teach Vault how to register with Consul
Vault will now register itself with Consul. The active node can be found using `active.vault.service.consul`. All standby vaults are available via `standby.vault.service.consul`. All unsealed vaults are considered healthy and available via `vault.service.consul`. Change in status and registration is event driven and should happen at the speed of a write to Consul (~network RTT + ~1x fsync(2)).
Healthy/active:
```
curl -X GET 'http://127.0.0.1:8500/v1/health/service/vault?pretty' && echo;
[
{
"Node": {
"Node": "vm1",
"Address": "127.0.0.1",
"TaggedAddresses": {
"wan": "127.0.0.1"
},
"CreateIndex": 3,
"ModifyIndex": 20
},
"Service": {
"ID": "vault:127.0.0.1:8200",
"Service": "vault",
"Tags": [
"active"
],
"Address": "127.0.0.1",
"Port": 8200,
"EnableTagOverride": false,
"CreateIndex": 17,
"ModifyIndex": 20
},
"Checks": [
{
"Node": "vm1",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm1",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "passing",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "",
"ServiceID": "vault:127.0.0.1:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 19
}
]
}
]
```
Healthy/standby:
```
[snip]
"Service": {
"ID": "vault:127.0.0.2:8200",
"Service": "vault",
"Tags": [
"standby"
],
"Address": "127.0.0.2",
"Port": 8200,
"EnableTagOverride": false,
"CreateIndex": 17,
"ModifyIndex": 20
},
"Checks": [
{
"Node": "vm2",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm2",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "passing",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "",
"ServiceID": "vault:127.0.0.2:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 19
}
]
}
]
```
Sealed:
```
"Checks": [
{
"Node": "vm2",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm2",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "critical",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "Vault Sealed",
"ServiceID": "vault:127.0.0.2:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 38
}
]
```
2016-04-24 00:15:05 +00:00
|
|
|
}
|
|
|
|
|
2017-01-19 22:36:33 +00:00
|
|
|
if test.consistencyMode != c.consistencyMode {
|
|
|
|
t.Errorf("bad consistency_mode value: %v != %v", test.consistencyMode, c.consistencyMode)
|
2017-01-13 18:24:40 +00:00
|
|
|
}
|
|
|
|
|
2019-05-08 16:48:31 +00:00
|
|
|
// The configuration stored in the Consul "client" object is not exported, so
|
|
|
|
// we either have to skip validating it, or add a method to export it, or use reflection.
|
2020-01-24 17:42:03 +00:00
|
|
|
consulConfig := reflect.Indirect(reflect.ValueOf(c.client)).FieldByName("config")
|
2019-05-08 16:48:31 +00:00
|
|
|
consulConfigScheme := consulConfig.FieldByName("Scheme").String()
|
|
|
|
consulConfigAddress := consulConfig.FieldByName("Address").String()
|
|
|
|
|
|
|
|
if test.scheme != consulConfigScheme {
|
|
|
|
t.Errorf("bad scheme value: %v != %v", test.scheme, consulConfigScheme)
|
|
|
|
}
|
|
|
|
|
|
|
|
if test.address != consulConfigAddress {
|
|
|
|
t.Errorf("bad address value: %v != %v", test.address, consulConfigAddress)
|
|
|
|
}
|
|
|
|
|
Teach Vault how to register with Consul
Vault will now register itself with Consul. The active node can be found using `active.vault.service.consul`. All standby vaults are available via `standby.vault.service.consul`. All unsealed vaults are considered healthy and available via `vault.service.consul`. Change in status and registration is event driven and should happen at the speed of a write to Consul (~network RTT + ~1x fsync(2)).
Healthy/active:
```
curl -X GET 'http://127.0.0.1:8500/v1/health/service/vault?pretty' && echo;
[
{
"Node": {
"Node": "vm1",
"Address": "127.0.0.1",
"TaggedAddresses": {
"wan": "127.0.0.1"
},
"CreateIndex": 3,
"ModifyIndex": 20
},
"Service": {
"ID": "vault:127.0.0.1:8200",
"Service": "vault",
"Tags": [
"active"
],
"Address": "127.0.0.1",
"Port": 8200,
"EnableTagOverride": false,
"CreateIndex": 17,
"ModifyIndex": 20
},
"Checks": [
{
"Node": "vm1",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm1",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "passing",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "",
"ServiceID": "vault:127.0.0.1:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 19
}
]
}
]
```
Healthy/standby:
```
[snip]
"Service": {
"ID": "vault:127.0.0.2:8200",
"Service": "vault",
"Tags": [
"standby"
],
"Address": "127.0.0.2",
"Port": 8200,
"EnableTagOverride": false,
"CreateIndex": 17,
"ModifyIndex": 20
},
"Checks": [
{
"Node": "vm2",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm2",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "passing",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "",
"ServiceID": "vault:127.0.0.2:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 19
}
]
}
]
```
Sealed:
```
"Checks": [
{
"Node": "vm2",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"CreateIndex": 3,
"ModifyIndex": 3
},
{
"Node": "vm2",
"CheckID": "vault-sealed-check",
"Name": "Vault Sealed Status",
"Status": "critical",
"Notes": "Vault service is healthy when Vault is in an unsealed status and can become an active Vault server",
"Output": "Vault Sealed",
"ServiceID": "vault:127.0.0.2:8200",
"ServiceName": "vault",
"CreateIndex": 19,
"ModifyIndex": 38
}
]
```
2016-04-24 00:15:05 +00:00
|
|
|
// FIXME(sean@): Unable to test max_parallel
|
|
|
|
// if test.max_parallel != cap(c.permitPool) {
|
|
|
|
// t.Errorf("bad: %v != %v", test.max_parallel, cap(c.permitPool))
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 23:44:32 +00:00
|
|
|
func TestConsulBackend(t *testing.T) {
|
2022-04-20 22:16:15 +00:00
|
|
|
cleanup, config := consul.PrepareTestContainer(t, "1.4.4", false, true)
|
2020-09-15 14:01:26 +00:00
|
|
|
defer cleanup()
|
2015-04-03 23:44:32 +00:00
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
client, err := api.NewClient(config.APIConfig())
|
2015-04-03 23:44:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
randPath := fmt.Sprintf("vault-%d/", time.Now().Unix())
|
|
|
|
defer func() {
|
|
|
|
client.KV().DeleteTree(randPath, nil)
|
|
|
|
}()
|
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
logger := logging.NewVaultLogger(log.Debug)
|
2016-08-19 20:45:17 +00:00
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
b, err := NewConsulBackend(map[string]string{
|
2020-09-15 14:01:26 +00:00
|
|
|
"address": config.Address(),
|
|
|
|
"token": config.Token,
|
2015-11-03 20:26:07 +00:00
|
|
|
"path": randPath,
|
|
|
|
"max_parallel": "256",
|
2017-08-03 17:24:27 +00:00
|
|
|
}, logger)
|
2015-04-03 23:44:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
physical.ExerciseBackend(t, b)
|
|
|
|
physical.ExerciseBackend_ListPrefix(t, b)
|
2015-11-03 16:47:16 +00:00
|
|
|
}
|
|
|
|
|
2019-05-01 17:47:41 +00:00
|
|
|
func TestConsul_TooLarge(t *testing.T) {
|
2022-04-20 22:16:15 +00:00
|
|
|
cleanup, config := consul.PrepareTestContainer(t, "1.4.4", false, true)
|
2020-09-15 14:01:26 +00:00
|
|
|
defer cleanup()
|
2019-05-01 17:47:41 +00:00
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
client, err := api.NewClient(config.APIConfig())
|
2019-05-01 17:47:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
randPath := fmt.Sprintf("vault-%d/", time.Now().Unix())
|
|
|
|
defer func() {
|
|
|
|
client.KV().DeleteTree(randPath, nil)
|
|
|
|
}()
|
|
|
|
|
|
|
|
logger := logging.NewVaultLogger(log.Debug)
|
|
|
|
|
|
|
|
b, err := NewConsulBackend(map[string]string{
|
2020-09-15 14:01:26 +00:00
|
|
|
"address": config.Address(),
|
|
|
|
"token": config.Token,
|
2019-05-01 17:47:41 +00:00
|
|
|
"path": randPath,
|
|
|
|
"max_parallel": "256",
|
|
|
|
}, logger)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-04-20 22:16:15 +00:00
|
|
|
zeros := make([]byte, 600000)
|
2019-05-01 17:47:41 +00:00
|
|
|
n, err := rand.Read(zeros)
|
|
|
|
if n != 600000 {
|
|
|
|
t.Fatalf("expected 500k zeros, read %d", n)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = b.Put(context.Background(), &physical.Entry{
|
|
|
|
Key: "foo",
|
|
|
|
Value: zeros,
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), physical.ErrValueTooLarge) {
|
|
|
|
t.Fatalf("expected value too large error, got %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = b.(physical.Transactional).Transaction(context.Background(), []*physical.TxnEntry{
|
|
|
|
{
|
|
|
|
Operation: physical.PutOperation,
|
|
|
|
Entry: &physical.Entry{
|
|
|
|
Key: "foo",
|
|
|
|
Value: zeros,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), physical.ErrValueTooLarge) {
|
|
|
|
t.Fatalf("expected value too large error, got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-17 21:38:18 +00:00
|
|
|
func TestConsul_ExpandedCapabilitiesAvailable(t *testing.T) {
|
|
|
|
testCases := map[string]bool{
|
|
|
|
"1.13.5": false,
|
|
|
|
"1.14.3": true,
|
|
|
|
}
|
2022-09-13 17:03:19 +00:00
|
|
|
|
2023-01-17 21:38:18 +00:00
|
|
|
for version, shouldBeAvailable := range testCases {
|
|
|
|
t.Run(version, func(t *testing.T) {
|
|
|
|
cleanup, config := consul.PrepareTestContainer(t, version, false, true)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
logger := logging.NewVaultLogger(log.Debug)
|
|
|
|
backendConfig := map[string]string{
|
|
|
|
"address": config.Address(),
|
|
|
|
"token": config.Token,
|
|
|
|
"path": "vault/",
|
|
|
|
"max_parallel": "-1",
|
|
|
|
}
|
|
|
|
|
|
|
|
be, err := NewConsulBackend(backendConfig, logger)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
b := be.(*ConsulBackend)
|
|
|
|
|
|
|
|
isAvailable := b.ExpandedCapabilitiesAvailable(context.Background())
|
|
|
|
if isAvailable != shouldBeAvailable {
|
|
|
|
t.Errorf("%t != %t, version %s\n", isAvailable, shouldBeAvailable, version)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConsul_TransactionalBackend_GetTransactionsForNonExistentValues(t *testing.T) {
|
|
|
|
cleanup, config := consul.PrepareTestContainer(t, "1.14.2", false, true)
|
2022-09-13 17:03:19 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
client, err := api.NewClient(config.APIConfig())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
txns := make([]*physical.TxnEntry, 0)
|
|
|
|
ctx := context.Background()
|
|
|
|
logger := logging.NewVaultLogger(log.Debug)
|
|
|
|
backendConfig := map[string]string{
|
|
|
|
"address": config.Address(),
|
|
|
|
"token": config.Token,
|
|
|
|
"path": "vault/",
|
|
|
|
"max_parallel": "-1",
|
|
|
|
}
|
|
|
|
|
|
|
|
be, err := NewConsulBackend(backendConfig, logger)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
b := be.(*ConsulBackend)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
_, _ = client.KV().DeleteTree("foo/", nil)
|
|
|
|
}()
|
|
|
|
|
|
|
|
txns = append(txns, &physical.TxnEntry{
|
|
|
|
Operation: physical.GetOperation,
|
|
|
|
Entry: &physical.Entry{
|
|
|
|
Key: "foo/bar",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
txns = append(txns, &physical.TxnEntry{
|
|
|
|
Operation: physical.PutOperation,
|
|
|
|
Entry: &physical.Entry{
|
|
|
|
Key: "foo/bar",
|
|
|
|
Value: []byte("baz"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
err = b.Transaction(ctx, txns)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should return nil, because the key foo/bar didn't exist when we ran that transaction, so the get
|
|
|
|
// should return nil, and the put always returns nil
|
|
|
|
for _, txn := range txns {
|
|
|
|
if txn.Operation == physical.GetOperation {
|
|
|
|
if txn.Entry.Value != nil {
|
|
|
|
t.Fatalf("expected txn.entry.value to be nil but it was %q", string(txn.Entry.Value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestConsul_TransactionalBackend_GetTransactions tests that passing a slice of transactions to the
|
|
|
|
// consul backend will populate values for any transactions that are Get operations.
|
|
|
|
func TestConsul_TransactionalBackend_GetTransactions(t *testing.T) {
|
2023-01-17 21:38:18 +00:00
|
|
|
cleanup, config := consul.PrepareTestContainer(t, "1.14.2", false, true)
|
2022-09-13 17:03:19 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
client, err := api.NewClient(config.APIConfig())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
txns := make([]*physical.TxnEntry, 0)
|
|
|
|
ctx := context.Background()
|
|
|
|
logger := logging.NewVaultLogger(log.Debug)
|
|
|
|
backendConfig := map[string]string{
|
|
|
|
"address": config.Address(),
|
|
|
|
"token": config.Token,
|
|
|
|
"path": "vault/",
|
|
|
|
"max_parallel": "-1",
|
|
|
|
}
|
|
|
|
|
|
|
|
be, err := NewConsulBackend(backendConfig, logger)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
b := be.(*ConsulBackend)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
_, _ = client.KV().DeleteTree("foo/", nil)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Add some seed values to consul, and prepare our slice of transactions at the same time
|
|
|
|
for i := 0; i < 64; i++ {
|
|
|
|
key := fmt.Sprintf("foo/lol-%d", i)
|
|
|
|
err := b.Put(ctx, &physical.Entry{Key: key, Value: []byte(fmt.Sprintf("value-%d", i))})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
txns = append(txns, &physical.TxnEntry{
|
|
|
|
Operation: physical.GetOperation,
|
|
|
|
Entry: &physical.Entry{
|
|
|
|
Key: key,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 64; i++ {
|
|
|
|
key := fmt.Sprintf("foo/lol-%d", i)
|
|
|
|
if i%2 == 0 {
|
|
|
|
txns = append(txns, &physical.TxnEntry{
|
|
|
|
Operation: physical.PutOperation,
|
|
|
|
Entry: &physical.Entry{
|
|
|
|
Key: key,
|
|
|
|
Value: []byte("lmao"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
txns = append(txns, &physical.TxnEntry{
|
|
|
|
Operation: physical.DeleteOperation,
|
|
|
|
Entry: &physical.Entry{
|
|
|
|
Key: key,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(txns) != 128 {
|
|
|
|
t.Fatal("wrong number of transactions")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = b.Transaction(ctx, txns)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that our Get operations were populated with their values
|
|
|
|
for i, txn := range txns {
|
|
|
|
if txn.Operation == physical.GetOperation {
|
|
|
|
val := []byte(fmt.Sprintf("value-%d", i))
|
|
|
|
if !bytes.Equal(val, txn.Entry.Value) {
|
|
|
|
t.Fatalf("expected %s to equal %s but it didn't", hex.EncodeToString(val), hex.EncodeToString(txn.Entry.Value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-03 16:47:16 +00:00
|
|
|
func TestConsulHABackend(t *testing.T) {
|
2022-04-20 22:16:15 +00:00
|
|
|
cleanup, config := consul.PrepareTestContainer(t, "1.4.4", false, true)
|
2020-09-15 14:01:26 +00:00
|
|
|
defer cleanup()
|
2015-11-03 16:47:16 +00:00
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
client, err := api.NewClient(config.APIConfig())
|
2015-11-03 16:47:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
randPath := fmt.Sprintf("vault-%d/", time.Now().Unix())
|
|
|
|
defer func() {
|
|
|
|
client.KV().DeleteTree(randPath, nil)
|
|
|
|
}()
|
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
logger := logging.NewVaultLogger(log.Debug)
|
2020-09-15 14:01:26 +00:00
|
|
|
backendConfig := map[string]string{
|
|
|
|
"address": config.Address(),
|
|
|
|
"token": config.Token,
|
2015-11-03 20:26:07 +00:00
|
|
|
"path": randPath,
|
|
|
|
"max_parallel": "-1",
|
2018-11-01 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
b, err := NewConsulBackend(backendConfig, logger)
|
2015-11-03 16:47:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2015-04-14 18:49:46 +00:00
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
b2, err := NewConsulBackend(backendConfig, logger)
|
2018-11-01 17:31:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
2015-04-14 18:49:46 +00:00
|
|
|
}
|
2018-11-01 17:31:09 +00:00
|
|
|
|
|
|
|
physical.ExerciseHABackend(t, b.(physical.HABackend), b2.(physical.HABackend))
|
2015-05-02 22:34:39 +00:00
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
detect, ok := b.(physical.RedirectDetect)
|
2015-05-02 22:34:39 +00:00
|
|
|
if !ok {
|
2016-08-15 13:42:42 +00:00
|
|
|
t.Fatalf("consul does not implement RedirectDetect")
|
2015-05-02 22:34:39 +00:00
|
|
|
}
|
|
|
|
host, err := detect.DetectHostAddr()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if host == "" {
|
|
|
|
t.Fatalf("bad addr: %v", host)
|
|
|
|
}
|
2015-04-03 23:44:32 +00:00
|
|
|
}
|