Files
ocis/glauth/pkg/command/server.go
T

187 lines
4.8 KiB
Go
Raw Normal View History

package command
import (
"context"
2021-10-21 07:35:10 +02:00
glauthcfg "github.com/glauth/glauth/v2/pkg/config"
"github.com/oklog/run"
2020-09-18 14:55:36 +02:00
accounts "github.com/owncloud/ocis/accounts/pkg/proto/v0"
2020-09-18 12:46:58 +02:00
"github.com/owncloud/ocis/glauth/pkg/config"
"github.com/owncloud/ocis/glauth/pkg/config/parser"
"github.com/owncloud/ocis/glauth/pkg/logging"
2021-03-18 12:05:08 +01:00
"github.com/owncloud/ocis/glauth/pkg/metrics"
2020-09-18 12:46:58 +02:00
"github.com/owncloud/ocis/glauth/pkg/server/debug"
"github.com/owncloud/ocis/glauth/pkg/server/glauth"
2021-03-18 12:05:08 +01:00
"github.com/owncloud/ocis/glauth/pkg/tracing"
2021-06-07 17:11:29 +02:00
pkgcrypto "github.com/owncloud/ocis/ocis-pkg/crypto"
2021-03-18 12:05:08 +01:00
"github.com/owncloud/ocis/ocis-pkg/service/grpc"
"github.com/urfave/cli/v2"
)
// Server is the entrypoint for the server command.
func Server(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "server",
Usage: "Start integrated server",
Before: func(ctx *cli.Context) error {
2021-11-04 15:56:01 +01:00
if err := parser.ParseConfig(cfg); err != nil {
2021-11-04 15:56:01 +01:00
return err
}
2021-11-04 15:56:01 +01:00
return nil
},
Action: func(c *cli.Context) error {
logger := logging.Configure(cfg.Service.Name, cfg.Log)
err := tracing.Configure(cfg)
if err != nil {
return err
}
2021-03-18 12:05:08 +01:00
gr := run.Group{}
ctx, cancel := func() (context.Context, context.CancelFunc) {
if cfg.Context == nil {
return context.WithCancel(context.Background())
}
2021-03-18 12:05:08 +01:00
return context.WithCancel(cfg.Context)
}()
metrics := metrics.New()
defer cancel()
metrics.BuildInfo.WithLabelValues(cfg.Service.Version).Set(1)
2020-09-25 13:15:20 +02:00
{
2020-10-05 17:12:40 +02:00
lcfg := glauthcfg.LDAP{
Enabled: cfg.Ldap.Enabled,
2021-08-18 17:22:52 +02:00
Listen: cfg.Ldap.Addr,
2020-10-05 17:12:40 +02:00
}
lscfg := glauthcfg.LDAPS{
Enabled: cfg.Ldaps.Enabled,
2021-08-18 17:22:52 +02:00
Listen: cfg.Ldaps.Addr,
2020-10-05 17:12:40 +02:00
Cert: cfg.Ldaps.Cert,
Key: cfg.Ldaps.Key,
}
bcfg := glauthcfg.Config{
LDAP: lcfg, // TODO remove LDAP from the backend config upstream
LDAPS: lscfg, // TODO remove LDAP from the backend config upstream
Backend: glauthcfg.Backend{
2020-09-24 11:40:51 +02:00
Datastore: cfg.Backend.Datastore,
BaseDN: cfg.Backend.BaseDN,
Insecure: cfg.Backend.Insecure,
NameFormat: cfg.Backend.NameFormat,
GroupFormat: cfg.Backend.GroupFormat,
2020-09-24 11:40:51 +02:00
Servers: cfg.Backend.Servers,
SSHKeyAttr: cfg.Backend.SSHKeyAttr,
2020-09-24 11:40:51 +02:00
UseGraphAPI: cfg.Backend.UseGraphAPI,
},
}
2020-10-05 17:12:40 +02:00
fcfg := glauthcfg.Config{
LDAP: lcfg, // TODO remove LDAP from the backend config upstream
LDAPS: lscfg, // TODO remove LDAP from the backend config upstream
Backend: glauthcfg.Backend{
Datastore: cfg.Fallback.Datastore,
BaseDN: cfg.Fallback.BaseDN,
Insecure: cfg.Fallback.Insecure,
NameFormat: cfg.Fallback.NameFormat,
GroupFormat: cfg.Fallback.GroupFormat,
Servers: cfg.Fallback.Servers,
SSHKeyAttr: cfg.Fallback.SSHKeyAttr,
UseGraphAPI: cfg.Fallback.UseGraphAPI,
},
}
2020-10-05 17:12:40 +02:00
if lscfg.Enabled {
2021-06-07 17:11:29 +02:00
if err := pkgcrypto.GenCert(cfg.Ldaps.Cert, cfg.Ldaps.Key, logger); err != nil {
logger.Fatal().Err(err).Msgf("Could not generate test-certificate")
}
}
as, gs := getAccountsServices()
server, err := glauth.Server(
glauth.AccountsService(as),
glauth.GroupsService(gs),
glauth.Logger(logger),
2020-10-05 17:12:40 +02:00
glauth.LDAP(&lcfg),
glauth.LDAPS(&lscfg),
glauth.Backend(&bcfg),
glauth.Fallback(&fcfg),
2020-11-02 13:57:37 +01:00
glauth.RoleBundleUUID(cfg.RoleBundleUUID),
)
if err != nil {
logger.Info().
Err(err).
Str("transport", "ldap").
Msg("Failed to initialize server")
return err
}
gr.Add(func() error {
err := make(chan error)
select {
case <-ctx.Done():
return nil
case err <- server.ListenAndServe():
return <-err
}
}, func(_ error) {
logger.Info().
Str("transport", "ldap").
Msg("Shutting down server")
server.Shutdown()
cancel()
})
gr.Add(func() error {
err := make(chan error)
select {
case <-ctx.Done():
return nil
case err <- server.ListenAndServeTLS():
return <-err
}
}, func(_ error) {
logger.Info().
Str("transport", "ldaps").
Msg("Shutting down server")
server.Shutdown()
cancel()
})
}
{
server, err := debug.Server(
debug.Logger(logger),
debug.Context(ctx),
debug.Config(cfg),
)
if err != nil {
logger.Info().Err(err).Str("transport", "debug").Msg("Failed to initialize server")
return err
}
2021-03-10 11:38:21 +01:00
gr.Add(server.ListenAndServe, func(_ error) {
_ = server.Shutdown(ctx)
cancel()
})
}
return gr.Run()
},
}
}
// getAccountsServices returns an ocis-accounts service
func getAccountsServices() (accounts.AccountsService, accounts.GroupsService) {
2020-11-11 11:50:53 +01:00
return accounts.NewAccountsService("com.owncloud.api.accounts", grpc.DefaultClient),
accounts.NewGroupsService("com.owncloud.api.accounts", grpc.DefaultClient)
}