From 4216c17072edf726465bbd0fbc8082dea77e69ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20F=20Bj=C3=B6rklund?= Date: Wed, 9 Jan 2019 23:34:17 +0100 Subject: [PATCH] Remove the connection checker, use SSH instead Also update "config", to output ssh parameters --- commands/config.go | 33 +++++++++------- libmachine/check/check.go | 70 ---------------------------------- libmachine/check/check_test.go | 60 ----------------------------- libmachine/libmachine.go | 11 ++++-- 4 files changed, 28 insertions(+), 146 deletions(-) delete mode 100644 libmachine/check/check.go delete mode 100644 libmachine/check/check_test.go diff --git a/commands/config.go b/commands/config.go index bc338a2..978c3c1 100644 --- a/commands/config.go +++ b/commands/config.go @@ -3,11 +3,8 @@ package commands import ( "fmt" "os" - "path/filepath" - "github.com/boot2podman/machine/commands/mcndirs" "github.com/boot2podman/machine/libmachine" - "github.com/boot2podman/machine/libmachine/check" "github.com/boot2podman/machine/libmachine/log" ) @@ -26,21 +23,31 @@ func cmdConfig(c CommandLine, api libmachine.API) error { return err } - podmanHost, _, err := check.DefaultConnChecker.Check(host) + if host.Driver == nil { + return err + } + + user := host.Driver.GetSSHUsername() + + addr, err := host.Driver.GetSSHHostname() + if err != nil { + return err + } + + port, err := host.Driver.GetSSHPort() if err != nil { - return fmt.Errorf("Error running connection boilerplate: %s", err) + return err } - log.Debug(podmanHost) + key := host.Driver.GetSSHKeyPath() - tlsCACert := filepath.Join(mcndirs.GetMachineDir(), host.Name, "ca.pem") - tlsCert := filepath.Join(mcndirs.GetMachineDir(), host.Name, "cert.pem") - tlsKey := filepath.Join(mcndirs.GetMachineDir(), host.Name, "key.pem") + if addr != "" { + // always use root@ for socket + user = "root" + } - // TODO(nathanleclaire): These magic strings for the certificate file - // names should be cross-package constants. - fmt.Printf("--tlsverify\n--tlscacert=%q\n--tlscert=%q\n--tlskey=%q\n-H=%s\n", - tlsCACert, tlsCert, tlsKey, podmanHost) + fmt.Printf("--username=%s\n--host=%s\n--port=%d\n--identity-file=%s\n", + user, addr, port, key) return nil } diff --git a/libmachine/check/check.go b/libmachine/check/check.go deleted file mode 100644 index c5bc4f6..0000000 --- a/libmachine/check/check.go +++ /dev/null @@ -1,70 +0,0 @@ -package check - -import ( - "fmt" - "net/url" - - "github.com/boot2podman/machine/libmachine/auth" - "github.com/boot2podman/machine/libmachine/cert" - "github.com/boot2podman/machine/libmachine/host" -) - -var ( - DefaultConnChecker ConnChecker -) - -func init() { - DefaultConnChecker = &MachineConnChecker{} -} - -// ErrCertInvalid for when the cert is computed to be invalid. -type ErrCertInvalid struct { - wrappedErr error - hostURL string -} - -func (e ErrCertInvalid) Error() string { - return fmt.Sprintf(`There was an error validating certificates for host %q: %s -You can attempt to regenerate them using 'podman-machine regenerate-certs [name]'. -`, e.hostURL, e.wrappedErr) -} - -type ConnChecker interface { - Check(*host.Host) (podmanHost string, authOptions *auth.Options, err error) -} - -type MachineConnChecker struct{} - -func (mcc *MachineConnChecker) Check(h *host.Host) (string, *auth.Options, error) { - podmanHost, err := h.Driver.GetURL() - if err != nil { - return "", &auth.Options{}, err - } - - podmanURL := podmanHost - - u, err := url.Parse(podmanURL) - if err != nil { - return "", &auth.Options{}, fmt.Errorf("Error parsing URL: %s", err) - } - - authOptions := h.AuthOptions() - - if err := checkCert(u.Host, authOptions); err != nil { - return "", &auth.Options{}, fmt.Errorf("Error checking and/or regenerating the certs: %s", err) - } - - return podmanURL, authOptions, nil -} - -func checkCert(hostURL string, authOptions *auth.Options) error { - valid, err := cert.ValidateCertificate(hostURL, authOptions) - if !valid || err != nil { - return ErrCertInvalid{ - wrappedErr: err, - hostURL: hostURL, - } - } - - return nil -} diff --git a/libmachine/check/check_test.go b/libmachine/check/check_test.go deleted file mode 100644 index 47828df..0000000 --- a/libmachine/check/check_test.go +++ /dev/null @@ -1,60 +0,0 @@ -package check - -import ( - "errors" - "testing" - - "crypto/tls" - - "github.com/boot2podman/machine/libmachine/auth" - "github.com/boot2podman/machine/libmachine/cert" - "github.com/stretchr/testify/assert" -) - -type FakeValidateCertificate struct { - IsValid bool - Err error -} - -type FakeCertGenerator struct { - fakeValidateCertificate *FakeValidateCertificate -} - -func (fcg FakeCertGenerator) GenerateCACertificate(certFile, keyFile, org string, bits int) error { - return nil -} - -func (fcg FakeCertGenerator) GenerateCert(opts *cert.Options) error { - return nil -} - -func (fcg FakeCertGenerator) ValidateCertificate(addr string, authOptions *auth.Options) (bool, error) { - return fcg.fakeValidateCertificate.IsValid, fcg.fakeValidateCertificate.Err -} - -func (fcg FakeCertGenerator) ReadTLSConfig(addr string, authOptions *auth.Options) (*tls.Config, error) { - return nil, nil -} - -func TestCheckCert(t *testing.T) { - errCertsExpired := errors.New("Certs have expired") - - cases := []struct { - hostURL string - authOptions *auth.Options - valid bool - checkErr error - expectedErr error - }{ - {"192.168.99.100", &auth.Options{}, true, nil, nil}, - {"192.168.99.100", &auth.Options{}, false, nil, ErrCertInvalid{wrappedErr: nil, hostURL: "192.168.99.100"}}, - {"192.168.99.100", &auth.Options{}, false, errCertsExpired, ErrCertInvalid{wrappedErr: errCertsExpired, hostURL: "192.168.99.100"}}, - } - - for _, c := range cases { - fcg := FakeCertGenerator{fakeValidateCertificate: &FakeValidateCertificate{c.valid, c.checkErr}} - cert.SetCertGenerator(fcg) - err := checkCert(c.hostURL, c.authOptions) - assert.Equal(t, c.expectedErr, err) - } -} diff --git a/libmachine/libmachine.go b/libmachine/libmachine.go index d8fd589..77abd01 100644 --- a/libmachine/libmachine.go +++ b/libmachine/libmachine.go @@ -9,7 +9,6 @@ import ( "github.com/boot2podman/machine/drivers/errdriver" "github.com/boot2podman/machine/libmachine/auth" "github.com/boot2podman/machine/libmachine/cert" - "github.com/boot2podman/machine/libmachine/check" "github.com/boot2podman/machine/libmachine/drivers" "github.com/boot2podman/machine/libmachine/drivers/plugin/localbinary" "github.com/boot2podman/machine/libmachine/drivers/rpc" @@ -169,9 +168,15 @@ func (api *Client) performCreate(h *host.Host) error { // We should check the connection to podman here log.Info("Checking connection to Podman...") - if _, _, err = check.DefaultConnChecker.Check(h); err != nil { - return fmt.Errorf("Error checking the host: %s", err) + client, err := h.CreateSSHClient() + if err != nil { + return fmt.Errorf("Error creating SSH client: %s", err) + } + version, err := client.Output("podman --version") + if err != nil { + return fmt.Errorf("Error getting podman version: %s", err) } + log.Debugf("%s", version) log.Info("Podman is up and running!") return nil