@@ -2,6 +2,7 @@ package clientconfig
22
33import (
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 }
0 commit comments