Skip to content

Commit 5ce6042

Browse files
committed
Refactor TLS configuration for testability
Move TLS configuration into a PrepareTLSConfig helper function so that we can test the support for OS_INSECURE added in the previous commit. Signed-off-by: Lars Kellogg-Stedman <lars@redhat.com>
1 parent 1675998 commit 5ce6042

2 files changed

Lines changed: 111 additions & 38 deletions

File tree

openstack/clientconfig/requests.go

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package clientconfig
22

33
import (
44
"context"
5+
"crypto/tls"
56
"errors"
67
"fmt"
78
"net/http"
@@ -738,43 +739,10 @@ func AuthenticatedClient(ctx context.Context, opts *ClientOpts) (*gophercloud.Pr
738739
return openstack.AuthenticatedClient(ctx, *ao)
739740
}
740741

741-
// NewServiceClient is a convenience function to get a new service client.
742-
func NewServiceClient(ctx context.Context, service string, opts *ClientOpts) (*gophercloud.ServiceClient, error) {
743-
cloud := new(Cloud)
744-
745-
// If no opts were passed in, create an empty ClientOpts.
746-
if opts == nil {
747-
opts = new(ClientOpts)
748-
}
749-
750-
// Determine if a clouds.yaml entry should be retrieved.
751-
// Start by figuring out the cloud name.
752-
// First check if one was explicitly specified in opts.
753-
var cloudName string
754-
if opts.Cloud != "" {
755-
cloudName = opts.Cloud
756-
}
757-
758-
// Next see if a cloud name was specified as an environment variable.
759-
envPrefix := "OS_"
760-
if opts.EnvPrefix != "" {
761-
envPrefix = opts.EnvPrefix
762-
}
763-
764-
if v := env.Getenv(envPrefix + "CLOUD"); v != "" {
765-
cloudName = v
766-
}
767-
768-
// If a cloud name was determined, try to look it up in clouds.yaml.
769-
if cloudName != "" {
770-
// Get the requested cloud.
771-
var err error
772-
cloud, err = GetCloudFromYAML(opts)
773-
if err != nil {
774-
return nil, err
775-
}
776-
}
777-
742+
// PrepareTLSConfig builds a *tls.Config from environment variables and cloud
743+
// configuration. Environment variables are checked first; cloud entry values
744+
// override if set.
745+
func PrepareTLSConfig(envPrefix string, cloud *Cloud) (*tls.Config, error) {
778746
// Check if a custom CA cert was provided.
779747
// First, check if the CACERT environment variable is set.
780748
var caCertPath string
@@ -824,7 +792,47 @@ func NewServiceClient(ctx context.Context, service string, opts *ClientOpts) (*g
824792
insecurePtr = &insecure
825793
}
826794

827-
tlsConfig, err := internal.PrepareTLSConfig(caCertPath, clientCertPath, clientKeyPath, insecurePtr)
795+
return internal.PrepareTLSConfig(caCertPath, clientCertPath, clientKeyPath, insecurePtr)
796+
}
797+
798+
// NewServiceClient is a convenience function to get a new service client.
799+
func NewServiceClient(ctx context.Context, service string, opts *ClientOpts) (*gophercloud.ServiceClient, error) {
800+
cloud := new(Cloud)
801+
802+
// If no opts were passed in, create an empty ClientOpts.
803+
if opts == nil {
804+
opts = new(ClientOpts)
805+
}
806+
807+
// Determine if a clouds.yaml entry should be retrieved.
808+
// Start by figuring out the cloud name.
809+
// First check if one was explicitly specified in opts.
810+
var cloudName string
811+
if opts.Cloud != "" {
812+
cloudName = opts.Cloud
813+
}
814+
815+
// Next see if a cloud name was specified as an environment variable.
816+
envPrefix := "OS_"
817+
if opts.EnvPrefix != "" {
818+
envPrefix = opts.EnvPrefix
819+
}
820+
821+
if v := env.Getenv(envPrefix + "CLOUD"); v != "" {
822+
cloudName = v
823+
}
824+
825+
// If a cloud name was determined, try to look it up in clouds.yaml.
826+
if cloudName != "" {
827+
// Get the requested cloud.
828+
var err error
829+
cloud, err = GetCloudFromYAML(opts)
830+
if err != nil {
831+
return nil, err
832+
}
833+
}
834+
835+
tlsConfig, err := PrepareTLSConfig(envPrefix, cloud)
828836
if err != nil {
829837
return nil, err
830838
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package testing
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/gophercloud/utils/v2/openstack/clientconfig"
8+
9+
th "github.com/gophercloud/gophercloud/v2/testhelper"
10+
)
11+
12+
func TestPrepareTLSConfigInsecureEnv(t *testing.T) {
13+
t.Run("OS_INSECURE=true", func(t *testing.T) {
14+
os.Setenv("OS_INSECURE", "true")
15+
defer os.Unsetenv("OS_INSECURE")
16+
17+
tlsConfig, err := clientconfig.PrepareTLSConfig("OS_", &clientconfig.Cloud{})
18+
th.AssertNoErr(t, err)
19+
th.AssertEquals(t, true, tlsConfig.InsecureSkipVerify)
20+
})
21+
22+
t.Run("OS_INSECURE=false", func(t *testing.T) {
23+
os.Setenv("OS_INSECURE", "false")
24+
defer os.Unsetenv("OS_INSECURE")
25+
26+
tlsConfig, err := clientconfig.PrepareTLSConfig("OS_", &clientconfig.Cloud{})
27+
th.AssertNoErr(t, err)
28+
th.AssertEquals(t, false, tlsConfig.InsecureSkipVerify)
29+
})
30+
31+
t.Run("OS_INSECURE unset", func(t *testing.T) {
32+
os.Unsetenv("OS_INSECURE")
33+
34+
tlsConfig, err := clientconfig.PrepareTLSConfig("OS_", &clientconfig.Cloud{})
35+
th.AssertNoErr(t, err)
36+
th.AssertEquals(t, false, tlsConfig.InsecureSkipVerify)
37+
})
38+
39+
t.Run("OS_INSECURE=invalid", func(t *testing.T) {
40+
os.Setenv("OS_INSECURE", "invalid")
41+
defer os.Unsetenv("OS_INSECURE")
42+
43+
_, err := clientconfig.PrepareTLSConfig("OS_", &clientconfig.Cloud{})
44+
th.AssertErr(t, err)
45+
})
46+
47+
t.Run("cloud.Verify overrides OS_INSECURE", func(t *testing.T) {
48+
os.Setenv("OS_INSECURE", "true")
49+
defer os.Unsetenv("OS_INSECURE")
50+
51+
cloud := &clientconfig.Cloud{Verify: &iTrue}
52+
tlsConfig, err := clientconfig.PrepareTLSConfig("OS_", cloud)
53+
th.AssertNoErr(t, err)
54+
th.AssertEquals(t, false, tlsConfig.InsecureSkipVerify)
55+
})
56+
57+
t.Run("custom env prefix", func(t *testing.T) {
58+
os.Setenv("FOO_INSECURE", "true")
59+
defer os.Unsetenv("FOO_INSECURE")
60+
61+
tlsConfig, err := clientconfig.PrepareTLSConfig("FOO_", &clientconfig.Cloud{})
62+
th.AssertNoErr(t, err)
63+
th.AssertEquals(t, true, tlsConfig.InsecureSkipVerify)
64+
})
65+
}

0 commit comments

Comments
 (0)