2023-04-24 20:52:12 +02:00
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2024-03-28 15:53:40 +01:00
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/integration"
|
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/jwk"
|
2023-04-24 20:52:12 +02:00
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/setup"
|
|
|
|
"github.com/pkg/errors"
|
2024-03-28 15:53:40 +01:00
|
|
|
"gitlab.com/wpetit/goweb/logger"
|
2023-04-24 20:52:12 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) initRepositories(ctx context.Context) error {
|
2024-03-26 17:28:38 +01:00
|
|
|
if err := s.initRedisClient(ctx); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2023-04-24 20:52:12 +02:00
|
|
|
if err := s.initLayerRepository(ctx); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.initProxyRepository(ctx); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-26 17:28:38 +01:00
|
|
|
func (s *Server) initRedisClient(ctx context.Context) error {
|
|
|
|
client := setup.NewRedisClient(ctx, s.redisConfig)
|
|
|
|
|
|
|
|
s.redisClient = client
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-24 20:52:12 +02:00
|
|
|
func (s *Server) initLayerRepository(ctx context.Context) error {
|
2024-03-26 17:28:38 +01:00
|
|
|
layerRepository, err := setup.NewLayerRepository(ctx, s.redisClient)
|
2023-04-24 20:52:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.layerRepository = layerRepository
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) initProxyRepository(ctx context.Context) error {
|
2024-03-26 17:28:38 +01:00
|
|
|
proxyRepository, err := setup.NewProxyRepository(ctx, s.redisClient)
|
2023-04-24 20:52:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.proxyRepository = proxyRepository
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-03-28 15:53:40 +01:00
|
|
|
|
|
|
|
func (s *Server) initPrivateKey(ctx context.Context) error {
|
|
|
|
localKey, err := jwk.LoadOrGenerate(string(s.serverConfig.Auth.PrivateKey), jwk.DefaultKeySize)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = integration.WithPrivateKey(ctx, localKey)
|
|
|
|
|
|
|
|
key, err := integration.RunOnKeyLoad(ctx, s.integrations)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if key != nil {
|
|
|
|
s.privateKey = key
|
|
|
|
} else {
|
|
|
|
s.privateKey = localKey
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Info(ctx, "using private key", logger.F("keyID", s.privateKey.KeyID()))
|
|
|
|
|
|
|
|
publicKeys, err := jwk.PublicKeySet(s.privateKey)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.publicKeys = publicKeys
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|