acme.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package cmd
  2. import (
  3. "os"
  4. "github.com/rs/zerolog"
  5. "github.com/spf13/cobra"
  6. "github.com/drakkan/sftpgo/v2/acme"
  7. "github.com/drakkan/sftpgo/v2/config"
  8. "github.com/drakkan/sftpgo/v2/logger"
  9. "github.com/drakkan/sftpgo/v2/util"
  10. )
  11. var (
  12. acmeCmd = &cobra.Command{
  13. Use: "acme",
  14. Short: "Obtain TLS certificates from ACME-based CAs like Let's Encrypt",
  15. }
  16. acmeRunCmd = &cobra.Command{
  17. Use: "run",
  18. Short: "Register your account and obtain certificates",
  19. Long: `This command must be run to obtain TLS certificates the first time or every
  20. time you add a new domain to your configuration file.
  21. Certificates are saved in the configured "certs_path".
  22. After this initial step, the certificates are automatically checked and
  23. renewed by the SFTPGo service
  24. `,
  25. Run: func(cmd *cobra.Command, args []string) {
  26. logger.DisableLogger()
  27. logger.EnableConsoleLogger(zerolog.DebugLevel)
  28. configDir = util.CleanDirInput(configDir)
  29. err := config.LoadConfig(configDir, configFile)
  30. if err != nil {
  31. logger.ErrorToConsole("Unable to initialize data provider, config load error: %v", err)
  32. return
  33. }
  34. acmeConfig := config.GetACMEConfig()
  35. err = acmeConfig.Initialize(configDir, false)
  36. if err != nil {
  37. logger.ErrorToConsole("Unable to initialize ACME configuration: %v", err)
  38. }
  39. if err = acme.GetCertificates(); err != nil {
  40. logger.ErrorToConsole("Cannot get certificates: %v", err)
  41. os.Exit(1)
  42. }
  43. },
  44. }
  45. )
  46. func init() {
  47. addConfigFlags(acmeRunCmd)
  48. acmeCmd.AddCommand(acmeRunCmd)
  49. rootCmd.AddCommand(acmeCmd)
  50. }