Skip to content

Commit

Permalink
Merge pull request #113 from jtopjian/lxd-nohelpers
Browse files Browse the repository at this point in the history
discovery/lxd: Remove lxdhelpers
  • Loading branch information
yyyar authored Oct 20, 2017
2 parents 8d7b4da + c731906 commit fafe58e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 12 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ env:

before_install:
- sudo apt-get -qq update
- sudo apt-get install -y libacl1-dev


install:
- make deps

Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ deps: clean-deps
github.com/gin-contrib/cors \
github.com/lxc/lxd/client \
github.com/lxc/lxd/lxc/config \
github.com/jtopjian/lxdhelpers \
github.com/lxc/lxd/shared \
github.com/lxc/lxd/shared/api \
github.com/pires/go-proxyproto \
golang.org/x/crypto/acme/autocert

Expand Down
77 changes: 68 additions & 9 deletions src/discovery/lxd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
package discovery

import (
"encoding/pem"
"fmt"
"os"
"strings"
"time"

Expand All @@ -16,11 +18,10 @@ import (
"../logging"
"../utils"

"github.com/jtopjian/lxdhelpers"

lxd "github.com/lxc/lxd/client"
lxd_config "github.com/lxc/lxd/lxc/config"
"github.com/lxc/lxd/shared"
lxd_api "github.com/lxc/lxd/shared/api"
)

const (
Expand Down Expand Up @@ -167,8 +168,11 @@ func lxdBuildClient(cfg config.DiscoveryConfig) (lxd.ContainerServer, error) {
if strings.HasPrefix(cfg.LXDServerAddress, "https:") {

/* Validate or generate certificates on the client side (gobetween) */
if err := lxdhelpers.ValidateClientCertificates(lxdConfig, cfg.LXDGenerateClientCerts); err != nil {
return nil, err
if cfg.LXDGenerateClientCerts {
log.Debug("Generating LXD client certificates")
if err := lxdConfig.GenerateClientCertificate(); err != nil {
return nil, err
}
}

/* Validate or accept certificates on the server side (LXD) */
Expand All @@ -184,10 +188,10 @@ func lxdBuildClient(cfg config.DiscoveryConfig) (lxd.ContainerServer, error) {
if err != nil {
/* If there was an error, then gobetween will try to download the server's cert. */
if cfg.LXDAcceptServerCert {
var err error
client, err = lxdhelpers.GetRemoteCertificate(lxdConfig, cfg.LXDServerRemoteName)
log.Debug("Retrieving LXD server certificate")
err := lxdGetRemoteCertificate(lxdConfig, cfg.LXDServerRemoteName)
if err != nil {
return nil, fmt.Errorf("Could not add the LXD server: %s", err)
return nil, fmt.Errorf("Could obtain LXD server certificate: %s", err)
}
} else {
err := fmt.Errorf("Unable to communicate with LXD server. Either set " +
Expand All @@ -210,8 +214,8 @@ func lxdBuildClient(cfg config.DiscoveryConfig) (lxd.ContainerServer, error) {
return nil, err
}

log.Info("Attempting to authenticate")
err = lxdhelpers.ValidateRemoteConnection(client, cfg.LXDServerRemoteName, cfg.LXDServerRemotePassword)
log.Info("Authenticating to LXD server")
err = lxdAuthenticateToServer(client, cfg.LXDServerRemoteName, cfg.LXDServerRemotePassword)
if err != nil {
log.Info("Authentication unsuccessful")
return nil, err
Expand Down Expand Up @@ -255,6 +259,61 @@ func lxdBuildConfig(cfg config.DiscoveryConfig) (*lxd_config.Config, error) {
return config, nil
}

/**
* lxdGetRemoteCertificate will attempt to retrieve a remote LXD server's
certificate and save it to the servercert's path.
*/
func lxdGetRemoteCertificate(config *lxd_config.Config, remote string) error {
addr := config.Remotes[remote]
certificate, err := shared.GetRemoteCertificate(addr.Addr)
if err != nil {
return err
}

serverCertDir := config.ConfigPath("servercerts")
if err := os.MkdirAll(serverCertDir, 0750); err != nil {
return fmt.Errorf("Could not create server cert dir: %s", err)
}

certf := fmt.Sprintf("%s/%s.crt", serverCertDir, remote)
certOut, err := os.Create(certf)
if err != nil {
return err
}

pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: certificate.Raw})
certOut.Close()

return nil
}

/**
* lxdAuthenticateToServer authenticates to an LXD Server
*/
func lxdAuthenticateToServer(client lxd.ContainerServer, remote string, password string) error {
srv, _, err := client.GetServer()
if srv.Auth == "trusted" {
return nil
}

req := lxd_api.CertificatesPost{
Password: password,
}
req.Type = "client"

err = client.CreateCertificate(req)
if err != nil {
return fmt.Errorf("Unable to authenticate with remote server: %s", err)
}

_, _, err = client.GetServer()
if err != nil {
return err
}

return nil
}

/**
* Get container IP address depending on network interface and address type
*/
Expand Down

0 comments on commit fafe58e

Please sign in to comment.