refactor glauth

Signed-off-by: Christian Richter <crichter@owncloud.com>
This commit is contained in:
Christian Richter
2022-04-13 17:04:37 +02:00
parent 2089ac5f7b
commit d4442941a1
36 changed files with 32 additions and 32 deletions
+53
View File
@@ -0,0 +1,53 @@
package command
import (
"fmt"
"net/http"
"github.com/owncloud/ocis/extensions/glauth/pkg/config"
"github.com/owncloud/ocis/extensions/glauth/pkg/config/parser"
"github.com/owncloud/ocis/extensions/glauth/pkg/logging"
"github.com/urfave/cli/v2"
)
// Health is the entrypoint for the health command.
func Health(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "health",
Usage: "check health status",
Category: "info",
Before: func(c *cli.Context) error {
return parser.ParseConfig(cfg)
},
Action: func(c *cli.Context) error {
logger := logging.Configure(cfg.Service.Name, cfg.Log)
resp, err := http.Get(
fmt.Sprintf(
"http://%s/healthz",
cfg.Debug.Addr,
),
)
if err != nil {
logger.Fatal().
Err(err).
Msg("Failed to request health check")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
logger.Fatal().
Int("code", resp.StatusCode).
Msg("Health seems to be in bad state")
}
logger.Debug().
Int("code", resp.StatusCode).
Msg("Health got a good state")
return nil
},
}
}
+64
View File
@@ -0,0 +1,64 @@
package command
import (
"context"
"os"
"github.com/owncloud/ocis/extensions/glauth/pkg/config"
"github.com/owncloud/ocis/ocis-pkg/clihelper"
ociscfg "github.com/owncloud/ocis/ocis-pkg/config"
"github.com/thejerf/suture/v4"
"github.com/urfave/cli/v2"
)
// GetCommands provides all commands for this service
func GetCommands(cfg *config.Config) cli.Commands {
return []*cli.Command{
// start this service
Server(cfg),
// interaction with this service
// infos about this service
Health(cfg),
Version(cfg),
}
}
// Execute is the entry point for the ocis-glauth command.
func Execute(cfg *config.Config) error {
app := clihelper.DefaultApp(&cli.App{
Name: "ocis-glauth",
Usage: "Serve GLAuth API for oCIS",
Commands: GetCommands(cfg),
})
cli.HelpFlag = &cli.BoolFlag{
Name: "help,h",
Usage: "Show the help",
}
return app.Run(os.Args)
}
// SutureService allows for the glauth command to be embedded and supervised by a suture supervisor tree.
type SutureService struct {
cfg *config.Config
}
// NewSutureService creates a new glauth.SutureService
func NewSutureService(cfg *ociscfg.Config) suture.Service {
cfg.GLAuth.Commons = cfg.Commons
return SutureService{
cfg: cfg.GLAuth,
}
}
func (s SutureService) Serve(ctx context.Context) error {
s.cfg.Context = ctx
if err := Execute(s.cfg); err != nil {
return err
}
return nil
}
+186
View File
@@ -0,0 +1,186 @@
package command
import (
"context"
"fmt"
accountssvc "github.com/owncloud/ocis/protogen/gen/ocis/services/accounts/v0"
glauthcfg "github.com/glauth/glauth/v2/pkg/config"
"github.com/oklog/run"
"github.com/owncloud/ocis/extensions/glauth/pkg/config"
"github.com/owncloud/ocis/extensions/glauth/pkg/config/parser"
"github.com/owncloud/ocis/extensions/glauth/pkg/logging"
"github.com/owncloud/ocis/extensions/glauth/pkg/metrics"
"github.com/owncloud/ocis/extensions/glauth/pkg/server/debug"
"github.com/owncloud/ocis/extensions/glauth/pkg/server/glauth"
"github.com/owncloud/ocis/extensions/glauth/pkg/tracing"
pkgcrypto "github.com/owncloud/ocis/ocis-pkg/crypto"
"github.com/owncloud/ocis/ocis-pkg/service/grpc"
"github.com/owncloud/ocis/ocis-pkg/version"
"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: fmt.Sprintf("start %s extension without runtime (unsupervised mode)", cfg.Service.Name),
Category: "server",
Before: func(c *cli.Context) error {
return parser.ParseConfig(cfg)
},
Action: func(c *cli.Context) error {
logger := logging.Configure(cfg.Service.Name, cfg.Log)
err := tracing.Configure(cfg)
if err != nil {
return err
}
gr := run.Group{}
ctx, cancel := func() (context.Context, context.CancelFunc) {
if cfg.Context == nil {
return context.WithCancel(context.Background())
}
return context.WithCancel(cfg.Context)
}()
metrics := metrics.New()
defer cancel()
metrics.BuildInfo.WithLabelValues(version.String).Set(1)
{
lcfg := glauthcfg.LDAP{
Enabled: cfg.Ldap.Enabled,
Listen: cfg.Ldap.Addr,
}
lscfg := glauthcfg.LDAPS{
Enabled: cfg.Ldaps.Enabled,
Listen: cfg.Ldaps.Addr,
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{
Datastore: cfg.Backend.Datastore,
BaseDN: cfg.Backend.BaseDN,
Insecure: cfg.Backend.Insecure,
NameFormat: cfg.Backend.NameFormat,
GroupFormat: cfg.Backend.GroupFormat,
Servers: cfg.Backend.Servers,
SSHKeyAttr: cfg.Backend.SSHKeyAttr,
UseGraphAPI: cfg.Backend.UseGraphAPI,
},
}
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,
},
}
if lscfg.Enabled {
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),
glauth.LDAP(&lcfg),
glauth.LDAPS(&lscfg),
glauth.Backend(&bcfg),
glauth.Fallback(&fcfg),
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
}
gr.Add(server.ListenAndServe, func(_ error) {
_ = server.Shutdown(ctx)
cancel()
})
}
return gr.Run()
},
}
}
// getAccountsServices returns an ocis-accounts service
func getAccountsServices() (accountssvc.AccountsService, accountssvc.GroupsService) {
return accountssvc.NewAccountsService("com.owncloud.api.accounts", grpc.DefaultClient),
accountssvc.NewGroupsService("com.owncloud.api.accounts", grpc.DefaultClient)
}
+50
View File
@@ -0,0 +1,50 @@
package command
import (
"fmt"
"os"
"github.com/owncloud/ocis/ocis-pkg/registry"
"github.com/owncloud/ocis/ocis-pkg/version"
tw "github.com/olekukonko/tablewriter"
"github.com/owncloud/ocis/extensions/glauth/pkg/config"
"github.com/urfave/cli/v2"
)
// Version prints the service versions of all running instances.
func Version(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "version",
Usage: "print the version of this binary and the running extension instances",
Category: "info",
Action: func(c *cli.Context) error {
fmt.Println("Version: " + version.String)
fmt.Printf("Compiled: %s\n", version.Compiled())
fmt.Println("")
reg := registry.GetRegistry()
services, err := reg.GetService(cfg.Ldap.Namespace + "." + cfg.Service.Name)
if err != nil {
fmt.Println(fmt.Errorf("could not get %s services from the registry: %v", cfg.Service.Name, err))
return err
}
if len(services) == 0 {
fmt.Println("No running " + cfg.Service.Name + " service found.")
return nil
}
table := tw.NewWriter(os.Stdout)
table.SetHeader([]string{"Version", "Address", "Id"})
table.SetAutoFormatHeaders(false)
for _, s := range services {
for _, n := range s.Nodes {
table.Append([]string{s.Version, n.Address, n.Id})
}
}
table.Render()
return nil
},
}
}