The following pull request was submitted through Github. It can be accessed and reviewed at: https://github.com/lxc/lxd/pull/3600
This e-mail was sent by the LXC bot, direct replies will not reach the author unless they happen to be subscribed to this list. === Description (from pull-request) === Signed-off-by: Stéphane Graber <stgra...@ubuntu.com>
From 463703d4db59cb48d7ca6b4e5a36bb6ca61c0147 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgra...@ubuntu.com> Date: Thu, 27 Jul 2017 13:44:20 -0400 Subject: [PATCH] client: Simplify ConnectPublicLXD logic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Graber <stgra...@ubuntu.com> --- client/connection.go | 71 ++++++++++++++++++---------------------------------- lxc/config/remote.go | 14 +++++++++-- 2 files changed, 37 insertions(+), 48 deletions(-) diff --git a/client/connection.go b/client/connection.go index ae13332d3..759ac3d25 100644 --- a/client/connection.go +++ b/client/connection.go @@ -44,33 +44,7 @@ type ConnectionArgs struct { func ConnectLXD(url string, args *ConnectionArgs) (ContainerServer, error) { logger.Infof("Connecting to a remote LXD over HTTPs") - // Use empty args if not specified - if args == nil { - args = &ConnectionArgs{} - } - - // Initialize the client struct - server := ProtocolLXD{ - httpCertificate: args.TLSServerCert, - httpHost: url, - httpProtocol: "https", - httpUserAgent: args.UserAgent, - } - - // Setup the HTTP client - httpClient, err := tlsHTTPClient(args.HTTPClient, args.TLSClientCert, args.TLSClientKey, args.TLSCA, args.TLSServerCert, args.Proxy) - if err != nil { - return nil, err - } - server.http = httpClient - - // Test the connection and seed the server information - _, _, err = server.GetServer() - if err != nil { - return nil, err - } - - return &server, nil + return httpsLXD(url, args) } // ConnectLXDUnix lets you connect to a remote LXD daemon over a local unix socket. @@ -127,17 +101,25 @@ func ConnectLXDUnix(path string, args *ConnectionArgs) (ContainerServer, error) func ConnectPublicLXD(url string, args *ConnectionArgs) (ImageServer, error) { logger.Infof("Connecting to a remote public LXD over HTTPs") + return httpsLXD(url, args) +} + +// ConnectSimpleStreams lets you connect to a remote SimpleStreams image server over HTTPs. +// +// Unless the remote server is trusted by the system CA, the remote certificate must be provided (TLSServerCert). +func ConnectSimpleStreams(url string, args *ConnectionArgs) (ImageServer, error) { + logger.Infof("Connecting to a remote simplestreams server") + // Use empty args if not specified if args == nil { args = &ConnectionArgs{} } // Initialize the client struct - server := ProtocolLXD{ - httpCertificate: args.TLSServerCert, + server := ProtocolSimpleStreams{ httpHost: url, - httpProtocol: "https", httpUserAgent: args.UserAgent, + httpCertificate: args.TLSServerCert, } // Setup the HTTP client @@ -147,31 +129,26 @@ func ConnectPublicLXD(url string, args *ConnectionArgs) (ImageServer, error) { } server.http = httpClient - // Test the connection and seed the server information - _, _, err = server.GetServer() - if err != nil { - return nil, err - } + // Get simplestreams client + ssClient := simplestreams.NewClient(url, *httpClient, args.UserAgent) + server.ssClient = ssClient return &server, nil } -// ConnectSimpleStreams lets you connect to a remote SimpleStreams image server over HTTPs. -// -// Unless the remote server is trusted by the system CA, the remote certificate must be provided (TLSServerCert). -func ConnectSimpleStreams(url string, args *ConnectionArgs) (ImageServer, error) { - logger.Infof("Connecting to a remote simplestreams server") - +// Internal function called by ConnectLXD and ConnectPublicLXD +func httpsLXD(url string, args *ConnectionArgs) (ContainerServer, error) { // Use empty args if not specified if args == nil { args = &ConnectionArgs{} } // Initialize the client struct - server := ProtocolSimpleStreams{ + server := ProtocolLXD{ + httpCertificate: args.TLSServerCert, httpHost: url, + httpProtocol: "https", httpUserAgent: args.UserAgent, - httpCertificate: args.TLSServerCert, } // Setup the HTTP client @@ -181,9 +158,11 @@ func ConnectSimpleStreams(url string, args *ConnectionArgs) (ImageServer, error) } server.http = httpClient - // Get simplestreams client - ssClient := simplestreams.NewClient(url, *httpClient, args.UserAgent) - server.ssClient = ssClient + // Test the connection and seed the server information + _, _, err = server.GetServer() + if err != nil { + return nil, err + } return &server, nil } diff --git a/lxc/config/remote.go b/lxc/config/remote.go index e5d255e64..aaf2095b0 100644 --- a/lxc/config/remote.go +++ b/lxc/config/remote.go @@ -113,8 +113,18 @@ func (c *Config) GetImageServer(name string) (lxd.ImageServer, error) { return d, nil } - // HTTPs (LXD) - d, err := lxd.ConnectPublicLXD(remote.Addr, args) + // HTTPs (public LXD) + if remote.Public { + d, err := lxd.ConnectPublicLXD(remote.Addr, args) + if err != nil { + return nil, err + } + + return d, nil + } + + // HTTPs (private LXD) + d, err := lxd.ConnectLXD(remote.Addr, args) if err != nil { return nil, err }
_______________________________________________ lxc-devel mailing list lxc-devel@lists.linuxcontainers.org http://lists.linuxcontainers.org/listinfo/lxc-devel