ca.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package main
  2. import (
  3. "crypto/ecdsa"
  4. "crypto/ed25519"
  5. "crypto/rsa"
  6. "errors"
  7. "flag"
  8. "net"
  9. "net/http"
  10. "github.com/cloudflare/cfssl/api/info"
  11. "github.com/cloudflare/cfssl/certdb/sql"
  12. "github.com/cloudflare/cfssl/log"
  13. "github.com/cloudflare/cfssl/multiroot/config"
  14. "github.com/cloudflare/cfssl/signer"
  15. "github.com/cloudflare/cfssl/signer/local"
  16. "github.com/cloudflare/cfssl/whitelist"
  17. "github.com/prometheus/client_golang/prometheus/promhttp"
  18. _ "github.com/go-sql-driver/mysql" // import to support MySQL
  19. _ "github.com/lib/pq" // import to support Postgres
  20. _ "github.com/mattn/go-sqlite3" // import to support SQLite
  21. )
  22. func parseSigner(root *config.Root) (signer.Signer, error) {
  23. privateKey := root.PrivateKey
  24. switch priv := privateKey.(type) {
  25. case *rsa.PrivateKey, *ecdsa.PrivateKey, ed25519.PrivateKey:
  26. s, err := local.NewSigner(priv, root.Certificate, signer.DefaultSigAlgo(priv), nil)
  27. if err != nil {
  28. return nil, err
  29. }
  30. s.SetPolicy(root.Config)
  31. if root.DB != nil {
  32. dbAccessor := sql.NewAccessor(root.DB)
  33. s.SetDBAccessor(dbAccessor)
  34. }
  35. return s, nil
  36. default:
  37. return nil, errors.New("unsupported private key type")
  38. }
  39. }
  40. var (
  41. defaultLabel string
  42. signers = map[string]signer.Signer{}
  43. whitelists = map[string]whitelist.NetACL{}
  44. )
  45. func main() {
  46. flagAddr := flag.String("a", ":8888", "listening address")
  47. flagRootFile := flag.String("roots", "", "configuration file specifying root keys")
  48. flagDefaultLabel := flag.String("l", "", "specify a default label")
  49. flagEndpointCert := flag.String("tls-cert", "", "server certificate")
  50. flagEndpointKey := flag.String("tls-key", "", "server private key")
  51. flag.IntVar(&log.Level, "loglevel", log.LevelInfo, "Log level (0 = DEBUG, 5 = FATAL)")
  52. flag.Parse()
  53. if *flagRootFile == "" {
  54. log.Fatal("no root file specified")
  55. }
  56. roots, err := config.Parse(*flagRootFile)
  57. if err != nil {
  58. log.Fatalf("%v", err)
  59. }
  60. for label, root := range roots {
  61. s, err := parseSigner(root)
  62. if err != nil {
  63. log.Criticalf("%v", err)
  64. }
  65. signers[label] = s
  66. if root.ACL != nil {
  67. whitelists[label] = root.ACL
  68. }
  69. log.Info("loaded signer ", label)
  70. }
  71. defaultLabel = *flagDefaultLabel
  72. infoHandler, err := info.NewMultiHandler(signers, defaultLabel)
  73. if err != nil {
  74. log.Criticalf("%v", err)
  75. }
  76. var localhost = whitelist.NewBasic()
  77. localhost.Add(net.ParseIP("127.0.0.1"))
  78. localhost.Add(net.ParseIP("::1"))
  79. http.HandleFunc("/api/v1/cfssl/authsign", dispatchRequest)
  80. http.Handle("/api/v1/cfssl/info", infoHandler)
  81. http.Handle("/metrics", promhttp.Handler())
  82. if *flagEndpointCert == "" && *flagEndpointKey == "" {
  83. log.Info("Now listening on ", *flagAddr)
  84. log.Fatal(http.ListenAndServe(*flagAddr, nil))
  85. } else {
  86. log.Info("Now listening on https:// ", *flagAddr)
  87. log.Fatal(http.ListenAndServeTLS(*flagAddr, *flagEndpointCert, *flagEndpointKey, nil))
  88. }
  89. }