2024-01-01 10:31:45 +00:00
|
|
|
// Copyright (C) 2019 Nicola Murino
|
2022-07-17 18:16:00 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2023-01-03 09:18:30 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-07-17 18:16:00 +00:00
|
|
|
|
2022-05-27 05:39:55 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/acme"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/config"
|
2023-02-23 18:25:20 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/logger"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/util"
|
2022-05-27 05:39:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
acmeCmd = &cobra.Command{
|
|
|
|
Use: "acme",
|
|
|
|
Short: "Obtain TLS certificates from ACME-based CAs like Let's Encrypt",
|
|
|
|
}
|
|
|
|
acmeRunCmd = &cobra.Command{
|
|
|
|
Use: "run",
|
|
|
|
Short: "Register your account and obtain certificates",
|
|
|
|
Long: `This command must be run to obtain TLS certificates the first time or every
|
|
|
|
time you add a new domain to your configuration file.
|
|
|
|
Certificates are saved in the configured "certs_path".
|
|
|
|
After this initial step, the certificates are automatically checked and
|
|
|
|
renewed by the SFTPGo service
|
|
|
|
`,
|
2022-07-19 21:28:33 +00:00
|
|
|
Run: func(_ *cobra.Command, _ []string) {
|
2022-05-27 05:39:55 +00:00
|
|
|
logger.DisableLogger()
|
|
|
|
logger.EnableConsoleLogger(zerolog.DebugLevel)
|
|
|
|
configDir = util.CleanDirInput(configDir)
|
|
|
|
err := config.LoadConfig(configDir, configFile)
|
|
|
|
if err != nil {
|
2022-08-11 18:09:53 +00:00
|
|
|
logger.ErrorToConsole("Unable to initialize ACME, config load error: %v", err)
|
2022-05-27 05:39:55 +00:00
|
|
|
return
|
|
|
|
}
|
2023-02-23 18:25:20 +00:00
|
|
|
kmsConfig := config.GetKMSConfig()
|
|
|
|
err = kmsConfig.Initialize()
|
|
|
|
if err != nil {
|
|
|
|
logger.ErrorToConsole("unable to initialize KMS: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
mfaConfig := config.GetMFAConfig()
|
|
|
|
err = mfaConfig.Initialize()
|
|
|
|
if err != nil {
|
|
|
|
logger.ErrorToConsole("Unable to initialize MFA: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
providerConf := config.GetProviderConf()
|
|
|
|
err = dataprovider.Initialize(providerConf, configDir, false)
|
|
|
|
if err != nil {
|
|
|
|
logger.ErrorToConsole("error initializing data provider: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2022-05-27 05:39:55 +00:00
|
|
|
acmeConfig := config.GetACMEConfig()
|
2023-02-23 18:25:20 +00:00
|
|
|
err = acme.Initialize(acmeConfig, configDir, false)
|
2022-05-27 05:39:55 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.ErrorToConsole("Unable to initialize ACME configuration: %v", err)
|
2023-02-23 18:25:20 +00:00
|
|
|
os.Exit(1)
|
2022-05-27 05:39:55 +00:00
|
|
|
}
|
|
|
|
if err = acme.GetCertificates(); err != nil {
|
|
|
|
logger.ErrorToConsole("Cannot get certificates: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
addConfigFlags(acmeRunCmd)
|
|
|
|
acmeCmd.AddCommand(acmeRunCmd)
|
|
|
|
rootCmd.AddCommand(acmeCmd)
|
|
|
|
}
|