// getCertPaths returns the cert paths // codegangsta/cli will not set the cert paths if the storage-path // is set to something different so we cannot use the paths // in the global options. le sigh. func getCertPathInfo(c *cli.Context) libmachine.CertPathInfo { // setup cert paths caCertPath := c.GlobalString("tls-ca-cert") caKeyPath := c.GlobalString("tls-ca-key") clientCertPath := c.GlobalString("tls-client-cert") clientKeyPath := c.GlobalString("tls-client-key") if caCertPath == "" { caCertPath = filepath.Join(utils.GetMachineCertDir(), "ca.pem") } if caKeyPath == "" { caKeyPath = filepath.Join(utils.GetMachineCertDir(), "ca-key.pem") } if clientCertPath == "" { clientCertPath = filepath.Join(utils.GetMachineCertDir(), "cert.pem") } if clientKeyPath == "" { clientKeyPath = filepath.Join(utils.GetMachineCertDir(), "key.pem") } return libmachine.CertPathInfo{ CaCertPath: caCertPath, CaKeyPath: caKeyPath, ClientCertPath: clientCertPath, ClientKeyPath: clientKeyPath, } }
func getTLSAuthOptions(opt *options.Options) *AuthOptions { caCertPath := opt.String("tls-ca-cert") caKeyPath := opt.String("tls-ca-key") clientCertPath := opt.String("tls-client-cert") clientKeyPath := opt.String("tls-client-key") if caCertPath == "" { caCertPath = filepath.Join(utils.GetMachineCertDir(), "ca.pem") } if caKeyPath == "" { caKeyPath = filepath.Join(utils.GetMachineCertDir(), "ca-key.pem") } if clientCertPath == "" { clientCertPath = filepath.Join(utils.GetMachineCertDir(), "cert.pem") } if clientKeyPath == "" { clientKeyPath = filepath.Join(utils.GetMachineCertDir(), "key.pem") } return &AuthOptions{ CaCertPath: caCertPath, CaKeyPath: caKeyPath, ClientCertPath: clientCertPath, ClientKeyPath: clientKeyPath, } }
func setupCertificates(caCertPath, caKeyPath, clientCertPath, clientKeyPath string) error { org := utils.GetUsername() bits := 2048 if _, err := os.Stat(utils.GetMachineCertDir()); err != nil { if os.IsNotExist(err) { if err := os.MkdirAll(utils.GetMachineCertDir(), 0700); err != nil { log.Fatalf("Error creating machine config dir: %s", err) } } else { log.Fatal(err) } } if _, err := os.Stat(caCertPath); os.IsNotExist(err) { log.Infof("Creating CA: %s", caCertPath) // check if the key path exists; if so, error if _, err := os.Stat(caKeyPath); err == nil { log.Fatalf("The CA key already exists. Please remove it or specify a different key/cert.") } if err := utils.GenerateCACertificate(caCertPath, caKeyPath, org, bits); err != nil { log.Infof("Error generating CA certificate: %s", err) } } if _, err := os.Stat(clientCertPath); os.IsNotExist(err) { log.Infof("Creating client certificate: %s", clientCertPath) if _, err := os.Stat(utils.GetMachineCertDir()); err != nil { if os.IsNotExist(err) { if err := os.Mkdir(utils.GetMachineCertDir(), 0700); err != nil { log.Fatalf("Error creating machine client cert dir: %s", err) } } else { log.Fatal(err) } } // check if the key path exists; if so, error if _, err := os.Stat(clientKeyPath); err == nil { log.Fatalf("The client key already exists. Please remove it or specify a different key/cert.") } if err := utils.GenerateCert([]string{""}, clientCertPath, clientKeyPath, caCertPath, caKeyPath, org, bits); err != nil { log.Fatalf("Error generating client certificate: %s", err) } } return nil }
func getMachineConfig(c *cli.Context) (*machineConfig, error) { name := c.Args().First() certInfo := getCertPathInfo(c) defaultStore, err := getDefaultStore( c.GlobalString("storage-path"), certInfo.CaCertPath, certInfo.CaKeyPath, ) if err != nil { log.Fatal(err) } provider, err := newProvider(defaultStore) if err != nil { log.Fatal(err) } m, err := provider.Get(name) if err != nil { return nil, err } machineDir := filepath.Join(utils.GetMachineDir(), m.Name) caCert := filepath.Join(machineDir, "ca.pem") caKey := filepath.Join(utils.GetMachineCertDir(), "ca-key.pem") clientCert := filepath.Join(machineDir, "cert.pem") clientKey := filepath.Join(machineDir, "key.pem") serverCert := filepath.Join(machineDir, "server.pem") serverKey := filepath.Join(machineDir, "server-key.pem") machineUrl, err := m.GetURL() if err != nil { if err == drivers.ErrHostIsNotRunning { machineUrl = "" } else { return nil, fmt.Errorf("Unexpected error getting machine url: %s", err) } } return &machineConfig{ machineName: name, machineDir: machineDir, machineUrl: machineUrl, clientKeyPath: clientKey, clientCertPath: clientCert, serverCertPath: serverCert, caKeyPath: caKey, caCertPath: caCert, serverKeyPath: serverKey, AuthOptions: *m.HostOptions.AuthOptions, SwarmOptions: *m.HostOptions.SwarmOptions, }, nil }
func getCertInfoFromHost(h *HostV0) CertPathInfo { // setup cert paths caCertPath := h.CaCertPath caKeyPath := h.PrivateKeyPath clientCertPath := h.ClientCertPath clientKeyPath := h.ClientKeyPath serverCertPath := h.ServerCertPath serverKeyPath := h.ServerKeyPath if caCertPath == "" { caCertPath = filepath.Join(utils.GetMachineCertDir(), "ca.pem") } if caKeyPath == "" { caKeyPath = filepath.Join(utils.GetMachineCertDir(), "ca-key.pem") } if clientCertPath == "" { clientCertPath = filepath.Join(utils.GetMachineCertDir(), "cert.pem") } if clientKeyPath == "" { clientKeyPath = filepath.Join(utils.GetMachineCertDir(), "key.pem") } if serverCertPath == "" { serverCertPath = filepath.Join(utils.GetMachineCertDir(), "server.pem") } if serverKeyPath == "" { serverKeyPath = filepath.Join(utils.GetMachineCertDir(), "server-key.pem") } return CertPathInfo{ CaCertPath: caCertPath, CaKeyPath: caKeyPath, ClientCertPath: clientCertPath, ClientKeyPath: clientKeyPath, ServerCertPath: serverCertPath, ServerKeyPath: serverKeyPath, } }
func (h *Host) ConfigureAuth() error { d := h.Driver if d.DriverName() == "none" { return nil } // copy certs to client dir for docker client machineDir := filepath.Join(utils.GetMachineDir(), h.Name) if err := utils.CopyFile(h.CaCertPath, filepath.Join(machineDir, "ca.pem")); err != nil { log.Fatalf("Error copying ca.pem to machine dir: %s", err) } clientCertPath := filepath.Join(utils.GetMachineCertDir(), "cert.pem") if err := utils.CopyFile(clientCertPath, filepath.Join(machineDir, "cert.pem")); err != nil { log.Fatalf("Error copying cert.pem to machine dir: %s", err) } clientKeyPath := filepath.Join(utils.GetMachineCertDir(), "key.pem") if err := utils.CopyFile(clientKeyPath, filepath.Join(machineDir, "key.pem")); err != nil { log.Fatalf("Error copying key.pem to machine dir: %s", err) } var ( ip = "" ipErr error maxRetries = 4 ) for i := 0; i < maxRetries; i++ { ip, ipErr = h.Driver.GetIP() if ip != "" { break } log.Debugf("waiting for ip: %s", ipErr) time.Sleep(5 * time.Second) } if ipErr != nil { return ipErr } if ip == "" { return fmt.Errorf("unable to get machine IP") } serverCertPath := filepath.Join(h.StorePath, "server.pem") serverKeyPath := filepath.Join(h.StorePath, "server-key.pem") org := h.Name bits := 2048 log.Debugf("generating server cert: %s ca-key=%s private-key=%s org=%s", serverCertPath, h.CaCertPath, h.PrivateKeyPath, org, ) if err := utils.GenerateCert([]string{ip}, serverCertPath, serverKeyPath, h.CaCertPath, h.PrivateKeyPath, org, bits); err != nil { return fmt.Errorf("error generating server cert: %s", err) } if err := h.StopDocker(); err != nil { return err } dockerDir, err := h.GetDockerConfigDir() if err != nil { return err } cmd, err := h.GetSSHCommand(fmt.Sprintf("sudo mkdir -p %s", dockerDir)) if err != nil { return err } if err := cmd.Run(); err != nil { return err } // upload certs and configure TLS auth caCert, err := ioutil.ReadFile(h.CaCertPath) if err != nil { return err } // due to windows clients, we cannot use filepath.Join as the paths // will be mucked on the linux hosts machineCaCertPath := path.Join(dockerDir, "ca.pem") serverCert, err := ioutil.ReadFile(serverCertPath) if err != nil { return err } machineServerCertPath := path.Join(dockerDir, "server.pem") serverKey, err := ioutil.ReadFile(serverKeyPath) if err != nil { return err } machineServerKeyPath := path.Join(dockerDir, "server-key.pem") cmd, err = h.GetSSHCommand(fmt.Sprintf("echo \"%s\" | sudo tee %s", string(caCert), machineCaCertPath)) if err != nil { return err } if err := cmd.Run(); err != nil { return err } cmd, err = h.GetSSHCommand(fmt.Sprintf("echo \"%s\" | sudo tee %s", string(serverKey), machineServerKeyPath)) if err != nil { return err } if err := cmd.Run(); err != nil { return err } cmd, err = h.GetSSHCommand(fmt.Sprintf("echo \"%s\" | sudo tee %s", string(serverCert), machineServerCertPath)) if err != nil { return err } if err := cmd.Run(); err != nil { return err } dockerUrl, err := h.Driver.GetURL() if err != nil { return err } u, err := url.Parse(dockerUrl) if err != nil { return err } dockerPort := 2376 parts := strings.Split(u.Host, ":") if len(parts) == 2 { dPort, err := strconv.Atoi(parts[1]) if err != nil { return err } dockerPort = dPort } cfg, err := h.generateDockerConfig(dockerPort, machineCaCertPath, machineServerKeyPath, machineServerCertPath) if err != nil { return err } cmd, err = h.GetSSHCommand(fmt.Sprintf("echo \"%s\" | sudo tee -a %s", cfg.EngineConfig, cfg.EngineConfigPath)) if err != nil { return err } if err := cmd.Run(); err != nil { return err } if err := h.StartDocker(); err != nil { return err } return nil }