1
0

acme.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright (C) 2019-2022 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package cmd
  15. import (
  16. "os"
  17. "github.com/rs/zerolog"
  18. "github.com/spf13/cobra"
  19. "github.com/drakkan/sftpgo/v2/internal/acme"
  20. "github.com/drakkan/sftpgo/v2/internal/config"
  21. "github.com/drakkan/sftpgo/v2/internal/logger"
  22. "github.com/drakkan/sftpgo/v2/internal/util"
  23. )
  24. var (
  25. acmeCmd = &cobra.Command{
  26. Use: "acme",
  27. Short: "Obtain TLS certificates from ACME-based CAs like Let's Encrypt",
  28. }
  29. acmeRunCmd = &cobra.Command{
  30. Use: "run",
  31. Short: "Register your account and obtain certificates",
  32. Long: `This command must be run to obtain TLS certificates the first time or every
  33. time you add a new domain to your configuration file.
  34. Certificates are saved in the configured "certs_path".
  35. After this initial step, the certificates are automatically checked and
  36. renewed by the SFTPGo service
  37. `,
  38. Run: func(_ *cobra.Command, _ []string) {
  39. logger.DisableLogger()
  40. logger.EnableConsoleLogger(zerolog.DebugLevel)
  41. configDir = util.CleanDirInput(configDir)
  42. err := config.LoadConfig(configDir, configFile)
  43. if err != nil {
  44. logger.ErrorToConsole("Unable to initialize ACME, config load error: %v", err)
  45. return
  46. }
  47. acmeConfig := config.GetACMEConfig()
  48. err = acmeConfig.Initialize(configDir, false)
  49. if err != nil {
  50. logger.ErrorToConsole("Unable to initialize ACME configuration: %v", err)
  51. }
  52. if err = acme.GetCertificates(); err != nil {
  53. logger.ErrorToConsole("Cannot get certificates: %v", err)
  54. os.Exit(1)
  55. }
  56. },
  57. }
  58. )
  59. func init() {
  60. addConfigFlags(acmeRunCmd)
  61. acmeCmd.AddCommand(acmeRunCmd)
  62. rootCmd.AddCommand(acmeCmd)
  63. }