mirror of
https://github.com/rancher/os.git
synced 2025-06-22 13:07:04 +00:00
Read files cloud-config.d in alphanumeric order, then cloud-config.yml `ros config` writes to cloud-config.yml (and cloud-config.d/private.yml - only private keys) Add (c *CloudConfig) Save() method, use it to save the changed config Read and apply metadata as part of LoadConfig() Simplify ros config export logic
177 lines
4.0 KiB
Go
177 lines
4.0 KiB
Go
package control
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
"github.com/codegangsta/cli"
|
|
machineUtil "github.com/docker/machine/utils"
|
|
"github.com/rancherio/os/config"
|
|
)
|
|
|
|
const (
|
|
NAME string = "rancher"
|
|
BITS int = 2048
|
|
)
|
|
|
|
func tlsConfCommands() []cli.Command {
|
|
return []cli.Command{
|
|
{
|
|
Name: "generate",
|
|
Usage: "generates new set of TLS configuration certs",
|
|
Action: tlsConfCreate,
|
|
Flags: []cli.Flag{
|
|
cli.StringSliceFlag{
|
|
Name: "hostname",
|
|
Usage: "the hostname for which you want to generate the certificate",
|
|
Value: &cli.StringSlice{"localhost"},
|
|
},
|
|
cli.BoolFlag{
|
|
Name: "server, s",
|
|
Usage: "generate the server keys instead of client keys",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "dir, d",
|
|
Usage: "the directory to save/read the certs to/from",
|
|
Value: "",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func writeCerts(generateServer bool, hostname []string, cfg *config.CloudConfig, certPath, keyPath, caCertPath, caKeyPath string) error {
|
|
if !generateServer {
|
|
return machineUtil.GenerateCert([]string{""}, certPath, keyPath, caCertPath, caKeyPath, NAME, BITS)
|
|
}
|
|
|
|
if cfg.Rancher.Docker.ServerKey == "" || cfg.Rancher.Docker.ServerCert == "" {
|
|
err := machineUtil.GenerateCert(hostname, certPath, keyPath, caCertPath, caKeyPath, NAME, BITS)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cert, err := ioutil.ReadFile(certPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
key, err := ioutil.ReadFile(keyPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cfg, err = cfg.Merge(map[interface{}]interface{}{
|
|
"rancher": map[interface{}]interface{}{
|
|
"docker": map[interface{}]interface{}{
|
|
"ca_key": cfg.Rancher.Docker.CAKey,
|
|
"ca_cert": cfg.Rancher.Docker.CACert,
|
|
"server_cert": string(cert),
|
|
"server_key": string(key),
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return cfg.Save()
|
|
}
|
|
|
|
if err := ioutil.WriteFile(certPath, []byte(cfg.Rancher.Docker.ServerCert), 0400); err != nil {
|
|
return err
|
|
}
|
|
|
|
return ioutil.WriteFile(keyPath, []byte(cfg.Rancher.Docker.ServerKey), 0400)
|
|
|
|
}
|
|
|
|
func writeCaCerts(cfg *config.CloudConfig, caCertPath, caKeyPath string) error {
|
|
if cfg.Rancher.Docker.CACert == "" {
|
|
if err := machineUtil.GenerateCACertificate(caCertPath, caKeyPath, NAME, BITS); err != nil {
|
|
return err
|
|
}
|
|
|
|
caCert, err := ioutil.ReadFile(caCertPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
caKey, err := ioutil.ReadFile(caKeyPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cfg, err = cfg.Merge(map[interface{}]interface{}{
|
|
"rancher": map[interface{}]interface{}{
|
|
"docker": map[interface{}]interface{}{
|
|
"ca_key": string(caKey),
|
|
"ca_cert": string(caCert),
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return cfg.Save()
|
|
}
|
|
|
|
if err := ioutil.WriteFile(caCertPath, []byte(cfg.Rancher.Docker.CACert), 0400); err != nil {
|
|
return err
|
|
}
|
|
|
|
return ioutil.WriteFile(caKeyPath, []byte(cfg.Rancher.Docker.CAKey), 0400)
|
|
}
|
|
|
|
func tlsConfCreate(c *cli.Context) {
|
|
err := generate(c)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
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 {
|
|
cfg, err := config.LoadConfig()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if outDir == "" {
|
|
return fmt.Errorf("out directory (-d, --dir) not specified")
|
|
}
|
|
caCertPath := filepath.Join(outDir, "ca.pem")
|
|
caKeyPath := filepath.Join(outDir, "ca-key.pem")
|
|
certPath := filepath.Join(outDir, "cert.pem")
|
|
keyPath := filepath.Join(outDir, "key.pem")
|
|
|
|
if generateServer {
|
|
certPath = filepath.Join(outDir, "server-cert.pem")
|
|
keyPath = filepath.Join(outDir, "server-key.pem")
|
|
}
|
|
|
|
if _, err := os.Stat(outDir); os.IsNotExist(err) {
|
|
if err := os.MkdirAll(outDir, 0700); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if err := writeCaCerts(cfg, caCertPath, caKeyPath); err != nil {
|
|
return err
|
|
}
|
|
|
|
return writeCerts(generateServer, hostnames, cfg, certPath, keyPath, caCertPath, caKeyPath)
|
|
}
|