From 1cf77bf9e9857009cbb0223bb748b22e0e8921fb Mon Sep 17 00:00:00 2001 From: Anthony Lapenna Date: Tue, 24 Jul 2018 11:11:47 +0200 Subject: [PATCH] fix(libcompose): fix an issue with TLS enabled endpoints (#2071) --- api/libcompose/compose_stack.go | 38 +++++++++++++++++---------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/api/libcompose/compose_stack.go b/api/libcompose/compose_stack.go index a3a6a9e71..2759f01cf 100644 --- a/api/libcompose/compose_stack.go +++ b/api/libcompose/compose_stack.go @@ -27,17 +27,27 @@ func NewComposeStackManager(dataPath string) *ComposeStackManager { } } +func createClient(endpoint *portainer.Endpoint) (client.Factory, error) { + clientOpts := client.Options{ + Host: endpoint.URL, + APIVersion: portainer.SupportedDockerAPIVersion, + } + + if endpoint.TLSConfig.TLS { + clientOpts.TLS = endpoint.TLSConfig.TLS + clientOpts.TLSVerify = !endpoint.TLSConfig.TLSSkipVerify + clientOpts.TLSCAFile = endpoint.TLSConfig.TLSCACertPath + clientOpts.TLSCertFile = endpoint.TLSConfig.TLSCertPath + clientOpts.TLSKeyFile = endpoint.TLSConfig.TLSKeyPath + } + + return client.NewDefaultFactory(clientOpts) +} + // Up will deploy a compose stack (equivalent of docker-compose up) func (manager *ComposeStackManager) Up(stack *portainer.Stack, endpoint *portainer.Endpoint) error { - clientFactory, err := client.NewDefaultFactory(client.Options{ - TLS: endpoint.TLSConfig.TLS, - TLSVerify: endpoint.TLSConfig.TLSSkipVerify, - Host: endpoint.URL, - TLSCAFile: endpoint.TLSCACertPath, - TLSCertFile: endpoint.TLSCertPath, - TLSKeyFile: endpoint.TLSKeyPath, - APIVersion: portainer.SupportedDockerAPIVersion, - }) + + clientFactory, err := createClient(endpoint) if err != nil { return err } @@ -75,15 +85,7 @@ func (manager *ComposeStackManager) Up(stack *portainer.Stack, endpoint *portain // Down will shutdown a compose stack (equivalent of docker-compose down) func (manager *ComposeStackManager) Down(stack *portainer.Stack, endpoint *portainer.Endpoint) error { - clientFactory, err := client.NewDefaultFactory(client.Options{ - TLS: endpoint.TLSConfig.TLS, - TLSVerify: endpoint.TLSConfig.TLSSkipVerify, - Host: endpoint.URL, - TLSCAFile: endpoint.TLSCACertPath, - TLSCertFile: endpoint.TLSCertPath, - TLSKeyFile: endpoint.TLSKeyPath, - APIVersion: portainer.SupportedDockerAPIVersion, - }) + clientFactory, err := createClient(endpoint) if err != nil { return err }