1
0
mirror of https://github.com/rancher/os.git synced 2025-08-31 06:11:12 +00:00
Files
os/cmd/control/tlsconf.go

184 lines
4.4 KiB
Go
Raw Normal View History

2015-02-23 12:00:33 -07:00
package control
2015-02-19 13:48:10 -07:00
import (
2015-03-16 13:50:30 -07:00
"io/ioutil"
2015-02-19 13:48:10 -07:00
"os"
"path/filepath"
"github.com/rancher/os/log"
2015-03-16 13:50:30 -07:00
2015-02-21 13:31:10 -08:00
"github.com/codegangsta/cli"
2015-02-19 13:48:10 -07:00
machineUtil "github.com/docker/machine/utils"
"github.com/rancher/os/config"
2016-06-02 14:32:26 -07:00
"github.com/rancher/os/util"
2015-03-16 13:50:30 -07:00
)
const (
NAME string = "rancher"
BITS int = 2048
2016-11-28 00:06:00 -08:00
ServerTLSPath string = "/etc/docker/tls"
ClientTLSPath string = "/home/rancher/.docker"
Cert string = "cert.pem"
Key string = "key.pem"
ServerCert string = "server-cert.pem"
ServerKey string = "server-key.pem"
CaCert string = "ca.pem"
CaKey string = "ca-key.pem"
2015-02-19 13:48:10 -07:00
)
2015-02-21 13:31:10 -08:00
func tlsConfCommands() []cli.Command {
2015-02-23 12:00:33 -07:00
return []cli.Command{
2015-02-21 13:31:10 -08:00
{
Name: "generate",
ShortName: "gen",
Usage: "generates new set of TLS configuration certs",
Action: tlsConfCreate,
2015-02-23 12:00:33 -07:00
Flags: []cli.Flag{
2015-03-16 13:50:30 -07:00
cli.StringSliceFlag{
Name: "hostname, H",
2015-03-16 13:50:30 -07:00
Usage: "the hostname for which you want to generate the certificate",
Value: &cli.StringSlice{"localhost"},
2015-02-21 13:31:10 -08:00
},
2015-02-23 12:00:33 -07:00
cli.BoolFlag{
2015-03-16 13:50:30 -07:00
Name: "server, s",
Usage: "generate the server keys instead of client keys",
2015-02-21 13:31:10 -08:00
},
2015-02-23 12:00:33 -07:00
cli.StringFlag{
2015-03-16 13:50:30 -07:00
Name: "dir, d",
Usage: "the directory to save/read the certs to/from",
Value: "",
2015-02-21 13:31:10 -08:00
},
},
},
}
2015-02-23 12:00:33 -07:00
}
2015-02-21 13:31:10 -08:00
func writeCerts(generateServer bool, hostname []string, certPath, keyPath, caCertPath, caKeyPath string) error {
2015-03-16 13:50:30 -07:00
if !generateServer {
return machineUtil.GenerateCert([]string{""}, certPath, keyPath, caCertPath, caKeyPath, NAME, BITS)
}
2015-02-19 13:48:10 -07:00
if err := machineUtil.GenerateCert(hostname, certPath, keyPath, caCertPath, caKeyPath, NAME, BITS); err != nil {
return err
2016-05-31 14:34:04 -07:00
}
cert, err := ioutil.ReadFile(certPath)
if err != nil {
return err
}
2015-02-21 13:31:10 -08:00
key, err := ioutil.ReadFile(keyPath)
if err != nil {
2015-03-16 13:50:30 -07:00
return err
2015-02-23 12:00:33 -07:00
}
2015-02-19 13:48:10 -07:00
// certPath, keyPath are already written to by machineUtil.GenerateCert()
if err := config.Set("rancher.docker.server_cert", string(cert)); err != nil {
return err
}
if err := config.Set("rancher.docker.server_key", string(key)); err != nil {
return err
}
2015-02-19 13:48:10 -07:00
return nil
2015-03-16 13:50:30 -07:00
}
2015-02-21 13:31:10 -08:00
2016-05-31 14:34:04 -07:00
func writeCaCerts(cfg *config.CloudConfig, caCertPath, caKeyPath string) error {
if cfg.Rancher.Docker.CACert == "" {
2015-03-16 13:50:30 -07:00
if err := machineUtil.GenerateCACertificate(caCertPath, caKeyPath, NAME, BITS); err != nil {
2016-05-31 14:34:04 -07:00
return err
2015-02-19 13:48:10 -07:00
}
2015-03-16 13:50:30 -07:00
caCert, err := ioutil.ReadFile(caCertPath)
if err != nil {
2016-05-31 14:34:04 -07:00
return err
2015-03-16 13:50:30 -07:00
}
2015-02-19 13:48:10 -07:00
2015-03-16 13:50:30 -07:00
caKey, err := ioutil.ReadFile(caKeyPath)
if err != nil {
2016-05-31 14:34:04 -07:00
return err
2015-02-19 13:48:10 -07:00
}
2016-05-31 14:34:04 -07:00
// caCertPath, caKeyPath are already written to by machineUtil.GenerateCACertificate()
if err := config.Set("rancher.docker.ca_cert", string(caCert)); err != nil {
return err
}
2016-05-31 14:34:04 -07:00
if err := config.Set("rancher.docker.ca_key", string(caKey)); err != nil {
return err
2015-02-19 13:48:10 -07:00
}
} else {
cfg = config.LoadConfig()
2015-03-16 13:50:30 -07:00
if err := util.WriteFileAtomic(caCertPath, []byte(cfg.Rancher.Docker.CACert), 0400); err != nil {
return err
}
2015-03-16 13:50:30 -07:00
if err := util.WriteFileAtomic(caKeyPath, []byte(cfg.Rancher.Docker.CAKey), 0400); err != nil {
return err
}
}
2016-05-31 14:34:04 -07:00
return nil
2015-03-16 13:50:30 -07:00
}
func tlsConfCreate(c *cli.Context) error {
2015-03-16 13:50:30 -07:00
err := generate(c)
if err != nil {
log.Fatal(err)
}
return nil
2015-03-16 13:50:30 -07:00
}
func generate(c *cli.Context) error {
generateServer := c.Bool("server")
outDir := c.String("dir")
hostnames := c.StringSlice("hostname")
return Generate(generateServer, outDir, hostnames)
}
func Generate(generateServer bool, outDir string, hostnames []string) error {
if outDir == "" {
if generateServer {
2016-11-28 00:06:00 -08:00
outDir = ServerTLSPath
} else {
2016-11-28 00:06:00 -08:00
outDir = ClientTLSPath
}
log.Infof("Out directory (-d, --dir) not specified, using default: %s", outDir)
}
caCertPath := filepath.Join(outDir, CaCert)
caKeyPath := filepath.Join(outDir, CaKey)
certPath := filepath.Join(outDir, Cert)
keyPath := filepath.Join(outDir, Key)
2015-02-19 13:48:10 -07:00
2015-03-16 13:50:30 -07:00
if generateServer {
certPath = filepath.Join(outDir, ServerCert)
keyPath = filepath.Join(outDir, ServerKey)
2015-02-19 13:48:10 -07:00
}
2015-03-16 13:50:30 -07:00
if _, err := os.Stat(outDir); os.IsNotExist(err) {
if err := os.MkdirAll(outDir, 0700); err != nil {
return err
}
}
2016-06-01 18:41:55 -07:00
cfg := config.LoadConfig()
if err := writeCaCerts(cfg, caCertPath, caKeyPath); err != nil {
2015-03-16 13:50:30 -07:00
return err
}
if err := writeCerts(generateServer, hostnames, certPath, keyPath, caCertPath, caKeyPath); err != nil {
return err
}
if !generateServer {
if err := filepath.Walk(outDir, func(path string, info os.FileInfo, err error) error {
return os.Chown(path, 1100, 1100) // rancher:rancher
}); err != nil {
return err
}
}
2015-03-16 13:50:30 -07:00
return nil
2015-02-19 13:48:10 -07:00
}