2017-01-19 05:49:01 +01:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-10-08 17:26:05 +02:00
|
|
|
"net"
|
2017-01-19 05:49:01 +01:00
|
|
|
"strings"
|
2017-10-08 17:26:05 +02:00
|
|
|
"time"
|
2017-01-19 05:49:01 +01:00
|
|
|
|
|
|
|
consulapi "github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/terraform/backend"
|
2020-11-18 16:07:30 +01:00
|
|
|
"github.com/hashicorp/terraform/internal/legacy/helper/schema"
|
2017-01-19 05:49:01 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// New creates a new backend for Consul remote state.
|
|
|
|
func New() backend.Backend {
|
2017-03-02 07:15:08 +01:00
|
|
|
s := &schema.Backend{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"path": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
Description: "Path to store state in Consul",
|
|
|
|
},
|
|
|
|
|
|
|
|
"access_token": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Description: "Access token for a Consul ACL",
|
|
|
|
Default: "", // To prevent input
|
|
|
|
},
|
|
|
|
|
|
|
|
"address": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Description: "Address to the Consul Cluster",
|
|
|
|
Default: "", // To prevent input
|
|
|
|
},
|
|
|
|
|
|
|
|
"scheme": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Description: "Scheme to communicate to Consul with",
|
|
|
|
Default: "", // To prevent input
|
|
|
|
},
|
|
|
|
|
|
|
|
"datacenter": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Description: "Datacenter to communicate with",
|
|
|
|
Default: "", // To prevent input
|
|
|
|
},
|
|
|
|
|
|
|
|
"http_auth": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Description: "HTTP Auth in the format of 'username:password'",
|
|
|
|
Default: "", // To prevent input
|
2017-01-19 05:49:01 +01:00
|
|
|
},
|
2017-03-13 08:17:33 +01:00
|
|
|
|
|
|
|
"gzip": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Description: "Compress the state data using gzip",
|
|
|
|
Default: false,
|
|
|
|
},
|
2017-03-15 01:28:42 +01:00
|
|
|
|
|
|
|
"lock": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Description: "Lock state access",
|
|
|
|
Default: true,
|
|
|
|
},
|
2017-08-23 18:47:21 +02:00
|
|
|
|
|
|
|
"ca_file": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Description: "A path to a PEM-encoded certificate authority used to verify the remote agent's certificate.",
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("CONSUL_CACERT", ""),
|
|
|
|
},
|
|
|
|
|
|
|
|
"cert_file": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Description: "A path to a PEM-encoded certificate provided to the remote agent; requires use of key_file.",
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("CONSUL_CLIENT_CERT", ""),
|
|
|
|
},
|
|
|
|
|
|
|
|
"key_file": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Description: "A path to a PEM-encoded private key, required if cert_file is specified.",
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("CONSUL_CLIENT_KEY", ""),
|
|
|
|
},
|
2017-01-19 05:49:01 +01:00
|
|
|
},
|
|
|
|
}
|
2017-03-02 07:15:08 +01:00
|
|
|
|
|
|
|
result := &Backend{Backend: s}
|
|
|
|
result.Backend.ConfigureFunc = result.configure
|
|
|
|
return result
|
2017-01-19 05:49:01 +01:00
|
|
|
}
|
|
|
|
|
2017-03-02 07:15:08 +01:00
|
|
|
type Backend struct {
|
|
|
|
*schema.Backend
|
|
|
|
|
2017-03-15 01:28:42 +01:00
|
|
|
// The fields below are set from configure
|
2017-10-08 17:26:05 +02:00
|
|
|
client *consulapi.Client
|
2017-03-02 07:15:08 +01:00
|
|
|
configData *schema.ResourceData
|
2017-03-15 01:28:42 +01:00
|
|
|
lock bool
|
2017-03-02 07:15:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Backend) configure(ctx context.Context) error {
|
2017-01-19 05:49:01 +01:00
|
|
|
// Grab the resource data
|
2017-03-02 07:15:08 +01:00
|
|
|
b.configData = schema.FromContextBackendConfig(ctx)
|
|
|
|
|
2017-03-15 01:28:42 +01:00
|
|
|
// Store the lock information
|
|
|
|
b.lock = b.configData.Get("lock").(bool)
|
|
|
|
|
2017-03-02 07:15:08 +01:00
|
|
|
data := b.configData
|
2017-01-19 05:49:01 +01:00
|
|
|
|
|
|
|
// Configure the client
|
|
|
|
config := consulapi.DefaultConfig()
|
2017-10-08 17:26:05 +02:00
|
|
|
|
|
|
|
// replace the default Transport Dialer to reduce the KeepAlive
|
2017-10-08 18:57:11 +02:00
|
|
|
config.Transport.DialContext = dialContext
|
2017-10-08 17:26:05 +02:00
|
|
|
|
2017-01-19 05:49:01 +01:00
|
|
|
if v, ok := data.GetOk("access_token"); ok && v.(string) != "" {
|
|
|
|
config.Token = v.(string)
|
|
|
|
}
|
|
|
|
if v, ok := data.GetOk("address"); ok && v.(string) != "" {
|
|
|
|
config.Address = v.(string)
|
|
|
|
}
|
|
|
|
if v, ok := data.GetOk("scheme"); ok && v.(string) != "" {
|
|
|
|
config.Scheme = v.(string)
|
|
|
|
}
|
|
|
|
if v, ok := data.GetOk("datacenter"); ok && v.(string) != "" {
|
|
|
|
config.Datacenter = v.(string)
|
|
|
|
}
|
2017-08-23 18:47:21 +02:00
|
|
|
|
|
|
|
if v, ok := data.GetOk("ca_file"); ok && v.(string) != "" {
|
2017-08-31 09:58:44 +02:00
|
|
|
config.TLSConfig.CAFile = v.(string)
|
2017-08-23 18:47:21 +02:00
|
|
|
}
|
|
|
|
if v, ok := data.GetOk("cert_file"); ok && v.(string) != "" {
|
2017-08-31 09:58:44 +02:00
|
|
|
config.TLSConfig.CertFile = v.(string)
|
2017-08-23 18:47:21 +02:00
|
|
|
}
|
|
|
|
if v, ok := data.GetOk("key_file"); ok && v.(string) != "" {
|
2017-08-31 09:58:44 +02:00
|
|
|
config.TLSConfig.KeyFile = v.(string)
|
2017-08-23 18:47:21 +02:00
|
|
|
}
|
|
|
|
|
2017-01-19 05:49:01 +01:00
|
|
|
if v, ok := data.GetOk("http_auth"); ok && v.(string) != "" {
|
|
|
|
auth := v.(string)
|
|
|
|
|
|
|
|
var username, password string
|
|
|
|
if strings.Contains(auth, ":") {
|
|
|
|
split := strings.SplitN(auth, ":", 2)
|
|
|
|
username = split[0]
|
|
|
|
password = split[1]
|
|
|
|
} else {
|
|
|
|
username = auth
|
|
|
|
}
|
|
|
|
|
|
|
|
config.HttpAuth = &consulapi.HttpBasicAuth{
|
|
|
|
Username: username,
|
|
|
|
Password: password,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-08 17:26:05 +02:00
|
|
|
client, err := consulapi.NewClient(config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
b.client = client
|
|
|
|
return nil
|
2017-01-19 05:49:01 +01:00
|
|
|
}
|
2017-10-08 18:57:11 +02:00
|
|
|
|
|
|
|
// dialContext is the DialContext function for the consul client transport.
|
|
|
|
// This is stored in a package var to inject a different dialer for tests.
|
|
|
|
var dialContext = (&net.Dialer{
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
KeepAlive: 17 * time.Second,
|
|
|
|
}).DialContext
|