tlsconf.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package control
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. log "github.com/Sirupsen/logrus"
  8. "github.com/codegangsta/cli"
  9. machineUtil "github.com/docker/machine/utils"
  10. "github.com/rancherio/os/config"
  11. )
  12. const (
  13. NAME string = "rancher"
  14. BITS int = 2048
  15. )
  16. func tlsConfCommands() []cli.Command {
  17. return []cli.Command{
  18. {
  19. Name: "generate",
  20. Usage: "generates new set of TLS configuration certs",
  21. Action: tlsConfCreate,
  22. Flags: []cli.Flag{
  23. cli.StringSliceFlag{
  24. Name: "hostname",
  25. Usage: "the hostname for which you want to generate the certificate",
  26. Value: &cli.StringSlice{"localhost"},
  27. },
  28. cli.BoolFlag{
  29. Name: "server, s",
  30. Usage: "generate the server keys instead of client keys",
  31. },
  32. cli.StringFlag{
  33. Name: "dir, d",
  34. Usage: "the directory to save/read the certs to/from",
  35. Value: "",
  36. },
  37. },
  38. },
  39. }
  40. }
  41. func writeCerts(generateServer bool, hostname []string, cfg *config.Config, certPath, keyPath, caCertPath, caKeyPath string) error {
  42. if !generateServer {
  43. return machineUtil.GenerateCert([]string{""}, certPath, keyPath, caCertPath, caKeyPath, NAME, BITS)
  44. }
  45. if cfg.UserDocker.ServerKey == "" || cfg.UserDocker.ServerCert == "" {
  46. err := machineUtil.GenerateCert(hostname, certPath, keyPath, caCertPath, caKeyPath, NAME, BITS)
  47. if err != nil {
  48. return err
  49. }
  50. cert, err := ioutil.ReadFile(certPath)
  51. if err != nil {
  52. return err
  53. }
  54. key, err := ioutil.ReadFile(keyPath)
  55. if err != nil {
  56. return err
  57. }
  58. return cfg.SetConfig(&config.Config{
  59. UserDocker: config.DockerConfig{
  60. CAKey: cfg.UserDocker.CAKey,
  61. CACert: cfg.UserDocker.CACert,
  62. ServerCert: string(cert),
  63. ServerKey: string(key),
  64. },
  65. })
  66. }
  67. if err := ioutil.WriteFile(certPath, []byte(cfg.UserDocker.ServerCert), 0400); err != nil {
  68. return err
  69. }
  70. return ioutil.WriteFile(keyPath, []byte(cfg.UserDocker.ServerKey), 0400)
  71. }
  72. func writeCaCerts(cfg *config.Config, caCertPath, caKeyPath string) error {
  73. if cfg.UserDocker.CACert == "" {
  74. if err := machineUtil.GenerateCACertificate(caCertPath, caKeyPath, NAME, BITS); err != nil {
  75. return err
  76. }
  77. caCert, err := ioutil.ReadFile(caCertPath)
  78. if err != nil {
  79. return err
  80. }
  81. caKey, err := ioutil.ReadFile(caKeyPath)
  82. if err != nil {
  83. return err
  84. }
  85. err = cfg.SetConfig(&config.Config{
  86. UserDocker: config.DockerConfig{
  87. CAKey: string(caKey),
  88. CACert: string(caCert),
  89. },
  90. })
  91. if err != nil {
  92. return err
  93. }
  94. return nil
  95. }
  96. if err := ioutil.WriteFile(caCertPath, []byte(cfg.UserDocker.CACert), 0400); err != nil {
  97. return err
  98. }
  99. return ioutil.WriteFile(caKeyPath, []byte(cfg.UserDocker.CAKey), 0400)
  100. }
  101. func tlsConfCreate(c *cli.Context) {
  102. err := generate(c)
  103. if err != nil {
  104. log.Fatal(err)
  105. }
  106. }
  107. func generate(c *cli.Context) error {
  108. cfg, err := config.LoadConfig()
  109. if err != nil {
  110. return err
  111. }
  112. generateServer := c.Bool("server")
  113. outDir := c.String("dir")
  114. if outDir == "" {
  115. return fmt.Errorf("out directory (-d, --dir) not specified")
  116. }
  117. caCertPath := filepath.Join(outDir, "ca.pem")
  118. caKeyPath := filepath.Join(outDir, "ca-key.pem")
  119. certPath := filepath.Join(outDir, "cert.pem")
  120. keyPath := filepath.Join(outDir, "key.pem")
  121. if generateServer {
  122. certPath = filepath.Join(outDir, "server-cert.pem")
  123. keyPath = filepath.Join(outDir, "server-key.pem")
  124. }
  125. if _, err := os.Stat(outDir); os.IsNotExist(err) {
  126. if err := os.MkdirAll(outDir, 0700); err != nil {
  127. return err
  128. }
  129. }
  130. if err := writeCaCerts(cfg, caCertPath, caKeyPath); err != nil {
  131. return err
  132. }
  133. hostnames := c.StringSlice("hostname")
  134. return writeCerts(generateServer, hostnames, cfg, certPath, keyPath, caCertPath, caKeyPath)
  135. }