2019-04-22 16:26:10 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2020-09-15 14:01:26 +00:00
|
|
|
"context"
|
2019-04-22 16:26:10 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
consulapi "github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/vault/helper/testhelpers/docker"
|
|
|
|
)
|
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
type Config struct {
|
|
|
|
docker.ServiceHostPort
|
|
|
|
Token string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) APIConfig() *consulapi.Config {
|
|
|
|
apiConfig := consulapi.DefaultConfig()
|
|
|
|
apiConfig.Address = c.Address()
|
|
|
|
apiConfig.Token = c.Token
|
|
|
|
return apiConfig
|
|
|
|
}
|
|
|
|
|
2020-04-30 17:06:24 +00:00
|
|
|
// PrepareTestContainer creates a Consul docker container. If version is empty,
|
|
|
|
// the Consul version used will be given by the environment variable
|
|
|
|
// CONSUL_DOCKER_VERSION, or if that's empty, whatever we've hardcoded as the
|
|
|
|
// the latest Consul version.
|
2020-09-15 14:01:26 +00:00
|
|
|
func PrepareTestContainer(t *testing.T, version string) (func(), *Config) {
|
|
|
|
if retAddress := os.Getenv("CONSUL_HTTP_ADDR"); retAddress != "" {
|
|
|
|
shp, err := docker.NewServiceHostPortParse(retAddress)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return func() {}, &Config{ServiceHostPort: *shp, Token: os.Getenv("CONSUL_HTTP_TOKEN")}
|
2019-04-22 16:26:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
config := `acl { enabled = true default_policy = "deny" }`
|
2020-04-30 17:06:24 +00:00
|
|
|
if version == "" {
|
|
|
|
consulVersion := os.Getenv("CONSUL_DOCKER_VERSION")
|
|
|
|
if consulVersion != "" {
|
|
|
|
version = consulVersion
|
|
|
|
} else {
|
|
|
|
version = "1.7.2" // Latest Consul version, update as new releases come out
|
|
|
|
}
|
|
|
|
}
|
2019-04-22 16:26:10 +00:00
|
|
|
if strings.HasPrefix(version, "1.3") {
|
|
|
|
config = `datacenter = "test" acl_default_policy = "deny" acl_datacenter = "test" acl_master_token = "test"`
|
|
|
|
}
|
|
|
|
|
2020-09-17 17:05:24 +00:00
|
|
|
repo := os.Getenv("CONSUL_DOCKER_REPO")
|
|
|
|
if repo == "" {
|
|
|
|
repo = "consul"
|
|
|
|
}
|
2020-09-15 14:01:26 +00:00
|
|
|
runner, err := docker.NewServiceRunner(docker.RunOptions{
|
2020-09-17 17:05:24 +00:00
|
|
|
ContainerName: "consul",
|
|
|
|
ImageRepo: repo,
|
|
|
|
ImageTag: version,
|
|
|
|
Cmd: []string{"agent", "-dev", "-client", "0.0.0.0", "-hcl", config},
|
|
|
|
Ports: []string{"8500/tcp"},
|
|
|
|
AuthUsername: os.Getenv("CONSUL_DOCKER_USERNAME"),
|
|
|
|
AuthPassword: os.Getenv("CONSUL_DOCKER_PASSWORD"),
|
2020-09-15 14:01:26 +00:00
|
|
|
})
|
2019-04-22 16:26:10 +00:00
|
|
|
if err != nil {
|
2020-09-15 14:01:26 +00:00
|
|
|
t.Fatalf("Could not start docker Consul: %s", err)
|
2019-04-22 16:26:10 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
svc, err := runner.StartService(context.Background(), func(ctx context.Context, host string, port int) (docker.ServiceConfig, error) {
|
|
|
|
shp := docker.NewServiceHostPort(host, port)
|
|
|
|
apiConfig := consulapi.DefaultNonPooledConfig()
|
|
|
|
apiConfig.Address = shp.Address()
|
|
|
|
consul, err := consulapi.NewClient(apiConfig)
|
2019-04-22 16:26:10 +00:00
|
|
|
if err != nil {
|
2020-09-15 14:01:26 +00:00
|
|
|
return nil, err
|
2019-04-22 16:26:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// For version of Consul < 1.4
|
|
|
|
if strings.HasPrefix(version, "1.3") {
|
2020-09-15 14:01:26 +00:00
|
|
|
consulToken := "test"
|
2019-04-22 16:26:10 +00:00
|
|
|
_, err = consul.KV().Put(&consulapi.KVPair{
|
|
|
|
Key: "setuptest",
|
|
|
|
Value: []byte("setuptest"),
|
|
|
|
}, &consulapi.WriteOptions{
|
|
|
|
Token: consulToken,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2020-09-15 14:01:26 +00:00
|
|
|
return nil, err
|
2019-04-22 16:26:10 +00:00
|
|
|
}
|
2020-09-15 14:01:26 +00:00
|
|
|
return &Config{
|
|
|
|
ServiceHostPort: *shp,
|
|
|
|
Token: consulToken,
|
|
|
|
}, nil
|
2019-04-22 16:26:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New default behavior
|
|
|
|
aclbootstrap, _, err := consul.ACL().Bootstrap()
|
|
|
|
if err != nil {
|
2020-09-15 14:01:26 +00:00
|
|
|
return nil, err
|
2019-04-22 16:26:10 +00:00
|
|
|
}
|
2020-09-15 14:01:26 +00:00
|
|
|
consulToken := aclbootstrap.SecretID
|
2019-04-22 16:26:10 +00:00
|
|
|
policy := &consulapi.ACLPolicy{
|
|
|
|
Name: "test",
|
|
|
|
Description: "test",
|
|
|
|
Rules: `node_prefix "" {
|
|
|
|
policy = "write"
|
|
|
|
}
|
|
|
|
|
|
|
|
service_prefix "" {
|
|
|
|
policy = "read"
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
}
|
|
|
|
q := &consulapi.WriteOptions{
|
|
|
|
Token: consulToken,
|
|
|
|
}
|
|
|
|
_, _, err = consul.ACL().PolicyCreate(policy, q)
|
|
|
|
if err != nil {
|
2020-09-15 14:01:26 +00:00
|
|
|
return nil, err
|
2019-04-22 16:26:10 +00:00
|
|
|
}
|
2020-09-15 14:01:26 +00:00
|
|
|
return &Config{
|
|
|
|
ServiceHostPort: *shp,
|
|
|
|
Token: consulToken,
|
|
|
|
}, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not start docker Consul: %s", err)
|
2019-04-22 16:26:10 +00:00
|
|
|
}
|
2020-09-15 14:01:26 +00:00
|
|
|
return svc.Cleanup, svc.Config.(*Config)
|
2019-04-22 16:26:10 +00:00
|
|
|
}
|