Skip to content

Commit

Permalink
Add support for custom CA certificate and insecure mode
Browse files Browse the repository at this point in the history
  • Loading branch information
MatMaul authored and akutz committed Jun 11, 2018
1 parent dbc39f9 commit 14aa30e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
8 changes: 8 additions & 0 deletions libstorage/drivers/storage/cinder/cinder.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ const (

// ConfigSnapshotTimeout is the config key for the snapshot timeout
ConfigSnapshotTimeout = Name + ".snapshotTimeout"

// ConfigCACert is the config key for custom CA certificate (usually for self signed use case)
ConfigCACert = Name + ".CACert"

// ConfigInsecure is the config ky to disable TLS verification of the server identity
ConfigInsecure = Name + ".insecure"
)

func init() {
Expand All @@ -77,5 +83,7 @@ func init() {
r.Key(gofig.String, "", "10m", "", ConfigDeleteTimeout)
r.Key(gofig.String, "", "10m", "", ConfigCreateTimeout)
r.Key(gofig.String, "", "10m", "", ConfigSnapshotTimeout)
r.Key(gofig.String, "", "", "", ConfigCACert)
r.Key(gofig.Bool, "", false, "", ConfigInsecure)
gofigCore.Register(r)
}
43 changes: 43 additions & 0 deletions libstorage/drivers/storage/cinder/storage/cinder_storage.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package storage

import (
"crypto/tls"
"crypto/x509"
"errors"
"io/ioutil"
"net/http"
"time"

gofig "github.com/akutz/gofig/types"
Expand Down Expand Up @@ -114,11 +119,19 @@ func (d *driver) Init(context types.Context, config gofig.Config) error {
fields["trustId"] = hiddenText
}

fields["caCert"] = d.caCert()
fields["insecure"] = d.insecure()

d.provider, err = openstack.NewClient(authOpts.IdentityEndpoint)
if err != nil {
return goof.WithFieldsE(fields, "error creating Keystone client", err)
}

d.provider.HTTPClient, err = openstackHTTPClient(d.caCert(), d.insecure())
if err != nil {
return goof.WithFieldsE(fields, "error overriding Gophercloud HTTP client", err)
}

if trustID != "" {
authOptionsExt := trusts.AuthOptsExt{
TrustID: trustID,
Expand Down Expand Up @@ -152,6 +165,28 @@ func (d *driver) Init(context types.Context, config gofig.Config) error {
return nil
}

func openstackHTTPClient(caCert string, insecure bool) (http.Client, error) {
if caCert == "" {
return http.Client{}, nil
}

caCertPool := x509.NewCertPool()
caCertContent, err := ioutil.ReadFile(caCert)
if err != nil {
return http.Client{}, errors.New("Can't read certificate file")
}
caCertPool.AppendCertsFromPEM(caCertContent)

tlsConfig := &tls.Config{
RootCAs: caCertPool,
InsecureSkipVerify: insecure,
}
tlsConfig.BuildNameToCertificate()
transport := &http.Transport{TLSClientConfig: tlsConfig}

return http.Client{Transport: transport}, nil
}

// InstanceInspect returns an instance.
func (d *driver) InstanceInspect(
ctx types.Context,
Expand Down Expand Up @@ -851,3 +886,11 @@ func (d *driver) snapshotTimeout() time.Duration {
}
return val
}

func (d *driver) caCert() string {
return d.config.GetString(cinder.ConfigCACert)
}

func (d *driver) insecure() bool {
return d.config.GetBool(cinder.ConfigInsecure)
}

0 comments on commit 14aa30e

Please sign in to comment.