acme.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright (C) 2019-2023 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/dataprovider"
  22. "github.com/drakkan/sftpgo/v2/internal/logger"
  23. "github.com/drakkan/sftpgo/v2/internal/util"
  24. )
  25. var (
  26. acmeCmd = &cobra.Command{
  27. Use: "acme",
  28. Short: "Obtain TLS certificates from ACME-based CAs like Let's Encrypt",
  29. }
  30. acmeRunCmd = &cobra.Command{
  31. Use: "run",
  32. Short: "Register your account and obtain certificates",
  33. Long: `This command must be run to obtain TLS certificates the first time or every
  34. time you add a new domain to your configuration file.
  35. Certificates are saved in the configured "certs_path".
  36. After this initial step, the certificates are automatically checked and
  37. renewed by the SFTPGo service
  38. `,
  39. Run: func(_ *cobra.Command, _ []string) {
  40. logger.DisableLogger()
  41. logger.EnableConsoleLogger(zerolog.DebugLevel)
  42. configDir = util.CleanDirInput(configDir)
  43. err := config.LoadConfig(configDir, configFile)
  44. if err != nil {
  45. logger.ErrorToConsole("Unable to initialize ACME, config load error: %v", err)
  46. return
  47. }
  48. kmsConfig := config.GetKMSConfig()
  49. err = kmsConfig.Initialize()
  50. if err != nil {
  51. logger.ErrorToConsole("unable to initialize KMS: %v", err)
  52. os.Exit(1)
  53. }
  54. mfaConfig := config.GetMFAConfig()
  55. err = mfaConfig.Initialize()
  56. if err != nil {
  57. logger.ErrorToConsole("Unable to initialize MFA: %v", err)
  58. os.Exit(1)
  59. }
  60. providerConf := config.GetProviderConf()
  61. err = dataprovider.Initialize(providerConf, configDir, false)
  62. if err != nil {
  63. logger.ErrorToConsole("error initializing data provider: %v", err)
  64. os.Exit(1)
  65. }
  66. acmeConfig := config.GetACMEConfig()
  67. err = acme.Initialize(acmeConfig, configDir, false)
  68. if err != nil {
  69. logger.ErrorToConsole("Unable to initialize ACME configuration: %v", err)
  70. os.Exit(1)
  71. }
  72. if err = acme.GetCertificates(); err != nil {
  73. logger.ErrorToConsole("Cannot get certificates: %v", err)
  74. os.Exit(1)
  75. }
  76. },
  77. }
  78. )
  79. func init() {
  80. addConfigFlags(acmeRunCmd)
  81. acmeCmd.AddCommand(acmeRunCmd)
  82. rootCmd.AddCommand(acmeCmd)
  83. }