Melora Hugues
64e48a5689
Because polyculeconnect is a OIDC proxy, we need to know which auth backend to use. This is provided using a query param or a form, so we need to get it from our own middleware. This commit adds the following elements: - basic DB storage for the backends - support for DB migrations and a first test migration (not definitive) - middleware to get the backend from the request and put it in the context - test that the backend exists in the auth flow
121 lines
3.5 KiB
Go
121 lines
3.5 KiB
Go
package serve
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"os"
|
|
"os/signal"
|
|
"time"
|
|
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd"
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd/utils"
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/db"
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/middlewares"
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/storage"
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/logger"
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/server"
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/services"
|
|
"github.com/spf13/cobra"
|
|
"github.com/zitadel/oidc/v3/pkg/op"
|
|
"go.uber.org/zap/exp/zapslog"
|
|
)
|
|
|
|
var configPath string
|
|
|
|
const stopTimeout = 10 * time.Second
|
|
|
|
// serveCmd represents the serve command
|
|
var serveCmd = &cobra.Command{
|
|
Use: "serve",
|
|
Short: "Start the web server",
|
|
Long: `Start the PolyculeConnect web server using the configuration defined through environment
|
|
variables`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
serve()
|
|
},
|
|
}
|
|
|
|
func serve() {
|
|
mainCtx, cancel := context.WithCancel(context.Background())
|
|
|
|
conf := utils.InitConfig(configPath)
|
|
logger.Init(conf.LogLevel)
|
|
logger.L.Infof("Initialized logger with level %v", conf.LogLevel)
|
|
|
|
storageType := utils.InitStorage(conf)
|
|
logger.L.Infof("Initialized storage backend %q", conf.StorageType)
|
|
|
|
logger.L.Info("Initializing authentication backends")
|
|
|
|
// dex_server.ConnectorsConfig[connector.TypeRefuseAll] = func() dex_server.ConnectorConfig { return new(connector.RefuseAllConfig) }
|
|
// connectors, err := dexConf.Storage.ListConnectors()
|
|
// if err != nil {
|
|
// logger.L.Fatalf("Failed to get existing connectors: %s", err.Error())
|
|
// }
|
|
// var connectorIDs []string
|
|
// for _, conn := range connectors {
|
|
// connectorIDs = append(connectorIDs, conn.ID)
|
|
// }
|
|
|
|
userDB, err := db.New(*conf)
|
|
if err != nil {
|
|
utils.Failf("failed to init user DB: %s", err.Error())
|
|
}
|
|
|
|
st := storage.Storage{LocalStorage: userDB}
|
|
opConf := op.Config{}
|
|
slogger := slog.New(zapslog.NewHandler(logger.L.Desugar().Core(), nil))
|
|
// slogger :=
|
|
options := []op.Option{
|
|
op.WithAllowInsecure(),
|
|
op.WithLogger(slogger),
|
|
op.WithHttpInterceptors(middlewares.WithBackendFromRequestMiddleware),
|
|
}
|
|
provider, err := op.NewProvider(&opConf, &st, op.StaticIssuer(conf.Issuer), options...)
|
|
if err != nil {
|
|
utils.Failf("failed to init OIDC provider: %s", err.Error())
|
|
}
|
|
|
|
if err := services.AddDefaultBackend(storageType); err != nil {
|
|
logger.L.Errorf("Failed to add connector for backend RefuseAll to stage: %s", err.Error())
|
|
}
|
|
|
|
logger.L.Info("Initializing server")
|
|
s, err := server.New(conf, provider, logger.L)
|
|
if err != nil {
|
|
logger.L.Fatalf("Failed to initialize server: %s", err.Error())
|
|
}
|
|
|
|
go s.Run(mainCtx)
|
|
|
|
c := make(chan os.Signal, 1)
|
|
signal.Notify(c, os.Interrupt)
|
|
|
|
logger.L.Info("Application successfully started")
|
|
|
|
logger.L.Debug("Waiting for stop signal")
|
|
select {
|
|
case <-s.Done():
|
|
logger.L.Fatal("Unexpected exit from server")
|
|
case <-c:
|
|
logger.L.Info("Stopping main application")
|
|
cancel()
|
|
}
|
|
|
|
logger.L.Debugf("Waiting %v for all daemons to stop", stopTimeout)
|
|
select {
|
|
case <-time.After(stopTimeout):
|
|
logger.L.Fatalf("Failed to stop all daemons in the expected time")
|
|
case <-s.Done():
|
|
logger.L.Info("web server successfully stopped")
|
|
}
|
|
|
|
logger.L.Info("Application successfully stopped")
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
func init() {
|
|
cmd.RootCmd.AddCommand(serveCmd)
|
|
serveCmd.Flags().StringVarP(&configPath, "config", "c", "config.json", "Path to the JSON configuration file")
|
|
}
|