2015-02-23 19:00:33 +00:00
|
|
|
package control
|
2015-02-19 20:48:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2015-02-21 21:31:10 +00:00
|
|
|
"github.com/codegangsta/cli"
|
2015-02-19 20:48:10 +00:00
|
|
|
machineUtil "github.com/docker/machine/utils"
|
|
|
|
)
|
|
|
|
|
2015-02-21 21:31:10 +00:00
|
|
|
func tlsConfCommands() []cli.Command {
|
2015-02-23 19:00:33 +00:00
|
|
|
return []cli.Command{
|
2015-02-21 21:31:10 +00:00
|
|
|
{
|
2015-02-23 19:00:33 +00:00
|
|
|
Name: "create",
|
|
|
|
Usage: "use it to create a new set of tls configuration certs and keys or upload existing ones",
|
2015-02-21 21:31:10 +00:00
|
|
|
Action: tlsConfCreate,
|
2015-02-23 19:00:33 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "cakey",
|
2015-02-21 21:31:10 +00:00
|
|
|
Usage: "path to existing certificate authority key (only use with --generate)",
|
|
|
|
},
|
2015-02-23 19:00:33 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "ca",
|
2015-02-21 21:31:10 +00:00
|
|
|
Usage: "path to existing certificate authority (only use with --genreate)",
|
|
|
|
},
|
2015-02-23 19:00:33 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "generate, g",
|
2015-02-21 21:31:10 +00:00
|
|
|
Usage: "generate the client key and client cert from existing ca and cakey",
|
|
|
|
},
|
2015-02-23 19:00:33 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "outDir, o",
|
2015-02-21 21:31:10 +00:00
|
|
|
Usage: "the output directory to save the generated certs or keys",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2015-02-23 19:00:33 +00:00
|
|
|
}
|
2015-02-21 21:31:10 +00:00
|
|
|
|
|
|
|
func tlsConfCreate(c *cli.Context) {
|
2015-02-19 20:48:10 +00:00
|
|
|
name := "rancher"
|
|
|
|
bits := 2048
|
|
|
|
|
|
|
|
caCertPath := "ca.pem"
|
|
|
|
caKeyPath := "ca-key.pem"
|
|
|
|
outDir := "/etc/docker/tls/"
|
|
|
|
generateCaCerts := true
|
|
|
|
|
|
|
|
inputCaKey := ""
|
|
|
|
inputCaCert := ""
|
|
|
|
|
2015-02-21 21:31:10 +00:00
|
|
|
if val := c.String("outDir"); val != "" {
|
|
|
|
outDir = val
|
|
|
|
}
|
2015-02-23 19:00:33 +00:00
|
|
|
|
2015-02-21 21:31:10 +00:00
|
|
|
if c.Bool("generate") {
|
|
|
|
generateCaCerts = false
|
2015-02-19 20:48:10 +00:00
|
|
|
}
|
2015-02-23 19:00:33 +00:00
|
|
|
|
2015-02-21 21:31:10 +00:00
|
|
|
if val := c.String("cakey"); val != "" {
|
|
|
|
inputCaKey = val
|
|
|
|
}
|
|
|
|
|
|
|
|
if val := c.String("ca"); val != "" {
|
|
|
|
inputCaCert = val
|
2015-02-23 19:00:33 +00:00
|
|
|
}
|
2015-02-19 20:48:10 +00:00
|
|
|
|
|
|
|
caCertPath = filepath.Join(outDir, caCertPath)
|
|
|
|
caKeyPath = filepath.Join(outDir, caKeyPath)
|
|
|
|
|
2015-02-21 21:31:10 +00:00
|
|
|
serverCertPath := "server-cert.pem"
|
|
|
|
serverKeyPath := "server-key.pem"
|
|
|
|
|
2015-02-19 20:48:10 +00:00
|
|
|
if generateCaCerts {
|
|
|
|
if err := machineUtil.GenerateCACertificate(caCertPath, caKeyPath, name, bits); err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if inputCaKey == "" || inputCaCert == "" {
|
|
|
|
fmt.Println("Please specify caKey and CaCert along with -g")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(inputCaKey); err != nil {
|
|
|
|
|
|
|
|
fmt.Printf("ERROR: %s does not exist\n", inputCaKey)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
caKeyPath = inputCaKey
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(inputCaCert); err != nil {
|
|
|
|
fmt.Printf("ERROR: %s does not exist\n", inputCaCert)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
caCertPath = inputCaCert
|
|
|
|
}
|
2015-02-21 21:31:10 +00:00
|
|
|
serverCertPath = "client-cert.pem"
|
|
|
|
serverKeyPath = "client-key.pem"
|
2015-02-19 20:48:10 +00:00
|
|
|
}
|
|
|
|
|
2015-02-21 21:31:10 +00:00
|
|
|
serverCertPath = filepath.Join(outDir, serverCertPath)
|
2015-02-19 20:48:10 +00:00
|
|
|
serverKeyPath = filepath.Join(outDir, serverKeyPath)
|
|
|
|
|
|
|
|
if err := machineUtil.GenerateCert([]string{""}, serverCertPath, serverKeyPath, caCertPath, caKeyPath, name, bits); err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|