ca.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package main
  2. import (
  3. "crypto/rand"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "net"
  9. "os"
  10. "strings"
  11. "time"
  12. "github.com/slackhq/nebula/cert"
  13. "golang.org/x/crypto/ed25519"
  14. )
  15. type caFlags struct {
  16. set *flag.FlagSet
  17. name *string
  18. duration *time.Duration
  19. outKeyPath *string
  20. outCertPath *string
  21. groups *string
  22. ips *string
  23. subnets *string
  24. }
  25. func newCaFlags() *caFlags {
  26. cf := caFlags{set: flag.NewFlagSet("ca", flag.ContinueOnError)}
  27. cf.set.Usage = func() {}
  28. cf.name = cf.set.String("name", "", "Required: name of the certificate authority")
  29. cf.duration = cf.set.Duration("duration", time.Duration(time.Hour*8760), "Optional: amount of time the certificate should be valid for. Valid time units are seconds: \"s\", minutes: \"m\", hours: \"h\"")
  30. cf.outKeyPath = cf.set.String("out-key", "ca.key", "Optional: path to write the private key to")
  31. cf.outCertPath = cf.set.String("out-crt", "ca.crt", "Optional: path to write the certificate to")
  32. cf.groups = cf.set.String("groups", "", "Optional: comma separated list of groups. This will limit which groups subordinate certs can use")
  33. cf.ips = cf.set.String("ips", "", "Optional: comma separated list of ip and network in CIDR notation. This will limit which ip addresses and networks subordinate certs can use")
  34. cf.subnets = cf.set.String("subnets", "", "Optional: comma separated list of ip and network in CIDR notation. This will limit which subnet addresses and networks subordinate certs can use")
  35. return &cf
  36. }
  37. func ca(args []string, out io.Writer, errOut io.Writer) error {
  38. cf := newCaFlags()
  39. err := cf.set.Parse(args)
  40. if err != nil {
  41. return err
  42. }
  43. if err := mustFlagString("name", cf.name); err != nil {
  44. return err
  45. }
  46. if err := mustFlagString("out-key", cf.outKeyPath); err != nil {
  47. return err
  48. }
  49. if err := mustFlagString("out-crt", cf.outCertPath); err != nil {
  50. return err
  51. }
  52. if *cf.duration <= 0 {
  53. return &helpError{"-duration must be greater than 0"}
  54. }
  55. var groups []string
  56. if *cf.groups != "" {
  57. for _, rg := range strings.Split(*cf.groups, ",") {
  58. g := strings.TrimSpace(rg)
  59. if g != "" {
  60. groups = append(groups, g)
  61. }
  62. }
  63. }
  64. var ips []*net.IPNet
  65. if *cf.ips != "" {
  66. for _, rs := range strings.Split(*cf.ips, ",") {
  67. rs := strings.Trim(rs, " ")
  68. if rs != "" {
  69. ip, ipNet, err := net.ParseCIDR(rs)
  70. if err != nil {
  71. return newHelpErrorf("invalid ip definition: %s", err)
  72. }
  73. ipNet.IP = ip
  74. ips = append(ips, ipNet)
  75. }
  76. }
  77. }
  78. var subnets []*net.IPNet
  79. if *cf.subnets != "" {
  80. for _, rs := range strings.Split(*cf.subnets, ",") {
  81. rs := strings.Trim(rs, " ")
  82. if rs != "" {
  83. _, s, err := net.ParseCIDR(rs)
  84. if err != nil {
  85. return newHelpErrorf("invalid subnet definition: %s", err)
  86. }
  87. subnets = append(subnets, s)
  88. }
  89. }
  90. }
  91. pub, rawPriv, err := ed25519.GenerateKey(rand.Reader)
  92. if err != nil {
  93. return fmt.Errorf("error while generating ed25519 keys: %s", err)
  94. }
  95. nc := cert.NebulaCertificate{
  96. Details: cert.NebulaCertificateDetails{
  97. Name: *cf.name,
  98. Groups: groups,
  99. Ips: ips,
  100. Subnets: subnets,
  101. NotBefore: time.Now(),
  102. NotAfter: time.Now().Add(*cf.duration),
  103. PublicKey: pub,
  104. IsCA: true,
  105. },
  106. }
  107. if _, err := os.Stat(*cf.outKeyPath); err == nil {
  108. return fmt.Errorf("refusing to overwrite existing CA key: %s", *cf.outKeyPath)
  109. }
  110. if _, err := os.Stat(*cf.outCertPath); err == nil {
  111. return fmt.Errorf("refusing to overwrite existing CA cert: %s", *cf.outCertPath)
  112. }
  113. err = nc.Sign(rawPriv)
  114. if err != nil {
  115. return fmt.Errorf("error while signing: %s", err)
  116. }
  117. err = ioutil.WriteFile(*cf.outKeyPath, cert.MarshalEd25519PrivateKey(rawPriv), 0600)
  118. if err != nil {
  119. return fmt.Errorf("error while writing out-key: %s", err)
  120. }
  121. b, err := nc.MarshalToPEM()
  122. if err != nil {
  123. return fmt.Errorf("error while marshalling certificate: %s", err)
  124. }
  125. err = ioutil.WriteFile(*cf.outCertPath, b, 0600)
  126. if err != nil {
  127. return fmt.Errorf("error while writing out-crt: %s", err)
  128. }
  129. return nil
  130. }
  131. func caSummary() string {
  132. return "ca <flags>: create a self signed certificate authority"
  133. }
  134. func caHelp(out io.Writer) {
  135. cf := newCaFlags()
  136. out.Write([]byte("Usage of " + os.Args[0] + " " + caSummary() + "\n"))
  137. cf.set.SetOutput(out)
  138. cf.set.PrintDefaults()
  139. }