2023-10-29 12:32:09 +00:00
|
|
|
package serve
|
2023-10-28 12:58:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-10-15 17:35:14 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/sha256"
|
2024-08-15 16:25:15 +00:00
|
|
|
"log/slog"
|
2023-10-28 12:58:06 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"time"
|
|
|
|
|
2023-10-29 12:32:09 +00:00
|
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd"
|
|
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd/utils"
|
2024-08-17 12:23:06 +00:00
|
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/client"
|
2024-08-15 16:25:15 +00:00
|
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/db"
|
2024-08-16 09:02:44 +00:00
|
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/middlewares"
|
2024-10-15 17:35:14 +00:00
|
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/model"
|
2024-08-15 16:25:15 +00:00
|
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/storage"
|
2023-10-28 12:58:06 +00:00
|
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/logger"
|
|
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/server"
|
2024-10-15 17:35:14 +00:00
|
|
|
"github.com/go-jose/go-jose/v4"
|
2024-10-06 20:11:58 +00:00
|
|
|
"github.com/google/uuid"
|
2023-10-28 12:58:06 +00:00
|
|
|
"github.com/spf13/cobra"
|
2024-08-15 16:25:15 +00:00
|
|
|
"github.com/zitadel/oidc/v3/pkg/op"
|
|
|
|
"go.uber.org/zap/exp/zapslog"
|
2023-10-28 12:58:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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())
|
|
|
|
|
2023-10-29 12:32:09 +00:00
|
|
|
conf := utils.InitConfig(configPath)
|
2023-10-28 12:58:06 +00:00
|
|
|
logger.Init(conf.LogLevel)
|
|
|
|
logger.L.Infof("Initialized logger with level %v", conf.LogLevel)
|
|
|
|
|
2024-08-15 16:25:15 +00:00
|
|
|
userDB, err := db.New(*conf)
|
2023-10-28 12:58:06 +00:00
|
|
|
if err != nil {
|
2024-08-15 16:25:15 +00:00
|
|
|
utils.Failf("failed to init user DB: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2024-10-06 20:11:58 +00:00
|
|
|
backends := map[uuid.UUID]*client.OIDCClient{}
|
2024-10-15 17:35:14 +00:00
|
|
|
key := sha256.Sum256([]byte("test"))
|
2024-10-06 20:11:58 +00:00
|
|
|
|
2024-10-15 17:35:14 +00:00
|
|
|
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
|
|
if err != nil {
|
|
|
|
utils.Failf("Failed to generate private key: %s", err)
|
|
|
|
}
|
|
|
|
signingKey := model.Key{
|
|
|
|
PrivateKey: privateKey,
|
|
|
|
KeyID: uuid.New(),
|
|
|
|
SigningAlg: jose.RS256,
|
|
|
|
}
|
|
|
|
|
|
|
|
st := storage.Storage{LocalStorage: userDB, InitializedBackends: backends, Key: &signingKey}
|
|
|
|
opConf := op.Config{
|
2024-10-19 14:21:04 +00:00
|
|
|
CryptoKey: key,
|
|
|
|
CodeMethodS256: false,
|
|
|
|
GrantTypeRefreshToken: true,
|
2024-10-15 17:35:14 +00:00
|
|
|
}
|
2024-08-15 16:25:15 +00:00
|
|
|
slogger := slog.New(zapslog.NewHandler(logger.L.Desugar().Core(), nil))
|
|
|
|
// slogger :=
|
|
|
|
options := []op.Option{
|
|
|
|
op.WithAllowInsecure(),
|
|
|
|
op.WithLogger(slogger),
|
2024-08-16 09:02:44 +00:00
|
|
|
op.WithHttpInterceptors(middlewares.WithBackendFromRequestMiddleware),
|
2023-10-28 12:58:06 +00:00
|
|
|
}
|
2024-08-17 12:23:06 +00:00
|
|
|
|
2024-08-15 16:25:15 +00:00
|
|
|
provider, err := op.NewProvider(&opConf, &st, op.StaticIssuer(conf.Issuer), options...)
|
|
|
|
if err != nil {
|
|
|
|
utils.Failf("failed to init OIDC provider: %s", err.Error())
|
2023-10-28 12:58:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.L.Info("Initializing server")
|
2024-10-06 20:11:58 +00:00
|
|
|
s, err := server.New(conf, provider, &st, logger.L)
|
2023-10-28 12:58:06 +00:00
|
|
|
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() {
|
2023-10-29 12:32:09 +00:00
|
|
|
cmd.RootCmd.AddCommand(serveCmd)
|
2023-10-28 12:58:06 +00:00
|
|
|
serveCmd.Flags().StringVarP(&configPath, "config", "c", "config.json", "Path to the JSON configuration file")
|
|
|
|
}
|