|
|
|
|
@ -3,11 +3,14 @@ package openstack
|
|
|
|
|
import (
|
|
|
|
|
"crypto/tls"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
|
|
|
|
|
|
"crypto/x509"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
|
|
"github.com/gophercloud/gophercloud"
|
|
|
|
|
"github.com/gophercloud/gophercloud/openstack"
|
|
|
|
|
"github.com/hashicorp/go-cleanhttp"
|
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@ -24,6 +27,9 @@ type AccessConfig struct {
|
|
|
|
|
Insecure bool `mapstructure:"insecure"`
|
|
|
|
|
Region string `mapstructure:"region"`
|
|
|
|
|
EndpointType string `mapstructure:"endpoint_type"`
|
|
|
|
|
CACertFile string `mapstructure:"cacert"`
|
|
|
|
|
ClientCertFile string `mapstructure:"cert"`
|
|
|
|
|
ClientKeyFile string `mapstructure:"key"`
|
|
|
|
|
|
|
|
|
|
osClient *gophercloud.ProviderClient
|
|
|
|
|
}
|
|
|
|
|
@ -53,6 +59,15 @@ func (c *AccessConfig) Prepare(ctx *interpolate.Context) []error {
|
|
|
|
|
if c.Username == "" {
|
|
|
|
|
c.Username = os.Getenv("SDK_USERNAME")
|
|
|
|
|
}
|
|
|
|
|
if c.CACertFile == "" {
|
|
|
|
|
c.CACertFile = os.Getenv("OS_CACERT")
|
|
|
|
|
}
|
|
|
|
|
if c.ClientCertFile == "" {
|
|
|
|
|
c.ClientCertFile = os.Getenv("OS_CERT")
|
|
|
|
|
}
|
|
|
|
|
if c.ClientKeyFile == "" {
|
|
|
|
|
c.ClientKeyFile = os.Getenv("OS_KEY")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get as much as possible from the end
|
|
|
|
|
ao, _ := openstack.AuthOptionsFromEnv()
|
|
|
|
|
@ -85,14 +100,37 @@ func (c *AccessConfig) Prepare(ctx *interpolate.Context) []error {
|
|
|
|
|
return []error{err}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tls_config := &tls.Config{}
|
|
|
|
|
|
|
|
|
|
if c.CACertFile != "" {
|
|
|
|
|
caCert, err := ioutil.ReadFile(c.CACertFile)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return []error{err}
|
|
|
|
|
}
|
|
|
|
|
caCertPool := x509.NewCertPool()
|
|
|
|
|
caCertPool.AppendCertsFromPEM(caCert)
|
|
|
|
|
tls_config.RootCAs = caCertPool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we have insecure set, then create a custom HTTP client that
|
|
|
|
|
// ignores SSL errors.
|
|
|
|
|
if c.Insecure {
|
|
|
|
|
config := &tls.Config{InsecureSkipVerify: true}
|
|
|
|
|
transport := &http.Transport{TLSClientConfig: config}
|
|
|
|
|
client.HTTPClient.Transport = transport
|
|
|
|
|
tls_config.InsecureSkipVerify = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if c.ClientCertFile != "" && c.ClientKeyFile != "" {
|
|
|
|
|
cert, err := tls.LoadX509KeyPair(c.ClientCertFile, c.ClientKeyFile)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return []error{err}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tls_config.Certificates = []tls.Certificate{cert}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
transport := cleanhttp.DefaultTransport()
|
|
|
|
|
transport.TLSClientConfig = tls_config
|
|
|
|
|
client.HTTPClient.Transport = transport
|
|
|
|
|
|
|
|
|
|
// Auth
|
|
|
|
|
err = openstack.Authenticate(client, ao)
|
|
|
|
|
if err != nil {
|
|
|
|
|
|