commands: initialize plugins if we have a KMS

this is necessary to be able to read KMS secrets stored within
the data provider

Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
This commit is contained in:
Nicola Murino
2025-04-20 18:48:19 +02:00
parent 513cbe3a77
commit c5e76f303a
8 changed files with 146 additions and 0 deletions

View File

@@ -24,6 +24,7 @@ import (
"github.com/drakkan/sftpgo/v2/internal/config"
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
"github.com/drakkan/sftpgo/v2/internal/logger"
"github.com/drakkan/sftpgo/v2/internal/plugin"
"github.com/drakkan/sftpgo/v2/internal/util"
)
@@ -56,6 +57,15 @@ renewed by the SFTPGo service
logger.ErrorToConsole("unable to initialize KMS: %v", err)
os.Exit(1)
}
if config.HasKMSPlugin() {
if err := plugin.Initialize(config.GetPluginsConfig(), "debug"); err != nil {
logger.ErrorToConsole("unable to initialize plugin system: %v", err)
os.Exit(1)
}
registerSignals()
defer plugin.Handler.Cleanup()
}
mfaConfig := config.GetMFAConfig()
err = mfaConfig.Initialize()
if err != nil {

View File

@@ -24,6 +24,7 @@ import (
"github.com/drakkan/sftpgo/v2/internal/config"
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
"github.com/drakkan/sftpgo/v2/internal/logger"
"github.com/drakkan/sftpgo/v2/internal/plugin"
"github.com/drakkan/sftpgo/v2/internal/service"
"github.com/drakkan/sftpgo/v2/internal/util"
)
@@ -65,6 +66,15 @@ Please take a look at the usage below to customize the options.`,
logger.ErrorToConsole("Unable to initialize KMS: %v", err)
os.Exit(1)
}
if config.HasKMSPlugin() {
if err := plugin.Initialize(config.GetPluginsConfig(), "debug"); err != nil {
logger.ErrorToConsole("unable to initialize plugin system: %v", err)
os.Exit(1)
}
registerSignals()
defer plugin.Handler.Cleanup()
}
mfaConfig := config.GetMFAConfig()
err = mfaConfig.Initialize()
if err != nil {

View File

@@ -27,6 +27,7 @@ import (
"github.com/drakkan/sftpgo/v2/internal/config"
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
"github.com/drakkan/sftpgo/v2/internal/logger"
"github.com/drakkan/sftpgo/v2/internal/plugin"
"github.com/drakkan/sftpgo/v2/internal/util"
)
@@ -58,6 +59,15 @@ Please take a look at the usage below to customize the options.`,
logger.ErrorToConsole("unable to initialize KMS: %v", err)
os.Exit(1)
}
if config.HasKMSPlugin() {
if err := plugin.Initialize(config.GetPluginsConfig(), "debug"); err != nil {
logger.ErrorToConsole("unable to initialize plugin system: %v", err)
os.Exit(1)
}
registerSignals()
defer plugin.Handler.Cleanup()
}
mfaConfig := config.GetMFAConfig()
err = mfaConfig.Initialize()
if err != nil {

View File

@@ -24,6 +24,7 @@ import (
"github.com/drakkan/sftpgo/v2/internal/config"
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
"github.com/drakkan/sftpgo/v2/internal/logger"
"github.com/drakkan/sftpgo/v2/internal/plugin"
"github.com/drakkan/sftpgo/v2/internal/util"
)
@@ -56,6 +57,21 @@ Please take a look at the usage below to customize the options.`,
logger.ErrorToConsole("unable to initialize KMS: %v", err)
os.Exit(1)
}
if config.HasKMSPlugin() {
if err := plugin.Initialize(config.GetPluginsConfig(), "debug"); err != nil {
logger.ErrorToConsole("unable to initialize plugin system: %v", err)
os.Exit(1)
}
registerSignals()
defer plugin.Handler.Cleanup()
}
mfaConfig := config.GetMFAConfig()
err = mfaConfig.Initialize()
if err != nil {
logger.ErrorToConsole("Unable to initialize MFA: %v", err)
os.Exit(1)
}
providerConf := config.GetProviderConf()
logger.InfoToConsole("Reverting provider: %q config file: %q target version %d", providerConf.Driver,
viper.ConfigFileUsed(), revertProviderTargetVersion)

View File

@@ -0,0 +1,41 @@
// Copyright (C) 2025 Nicola Murino
//
// 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
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//go:build !windows
package cmd
import (
"os"
"os/signal"
"syscall"
"github.com/drakkan/sftpgo/v2/internal/logger"
"github.com/drakkan/sftpgo/v2/internal/plugin"
)
func registerSignals() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
go func() {
for sig := range c {
switch sig {
case syscall.SIGINT, syscall.SIGTERM:
logger.DebugToConsole("Received interrupt request")
plugin.Handler.Cleanup()
os.Exit(0)
}
}
}()
}

View File

@@ -0,0 +1,36 @@
// Copyright (C) 2025 Nicola Murino
//
// 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
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package cmd
import (
"os"
"os/signal"
"github.com/drakkan/sftpgo/v2/internal/logger"
"github.com/drakkan/sftpgo/v2/internal/plugin"
)
func registerSignals() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for range c {
logger.DebugToConsole("Received interrupt request")
plugin.Handler.Cleanup()
os.Exit(0)
}
}()
}

View File

@@ -24,6 +24,7 @@ import (
"strconv"
"strings"
kmsplugin "github.com/sftpgo/sdk/plugin/kms"
"github.com/spf13/viper"
"github.com/subosito/gotenv"
@@ -588,6 +589,16 @@ func SetPluginsConfig(config []plugin.Config) {
globalConf.PluginsConfig = config
}
// HasKMSPlugin returns true if at least one KMS plugin is configured.
func HasKMSPlugin() bool {
for _, c := range globalConf.PluginsConfig {
if c.Type == kmsplugin.PluginName {
return true
}
}
return false
}
// GetMFAConfig returns multi-factor authentication config
func GetMFAConfig() mfa.Config {
return globalConf.MFAConfig

View File

@@ -343,6 +343,18 @@ func TestSetGetConfig(t *testing.T) {
if assert.Len(t, config.GetPluginsConfig(), 1) {
assert.Equal(t, pluginConf[0].Type, config.GetPluginsConfig()[0].Type)
}
assert.False(t, config.HasKMSPlugin())
pluginConf = []plugin.Config{
{
Type: "notifier",
},
{
Type: "kms",
},
}
config.SetPluginsConfig(pluginConf)
assert.Len(t, config.GetPluginsConfig(), 2)
assert.True(t, config.HasKMSPlugin())
}
func TestServiceToStart(t *testing.T) {