mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-03-02 22:17:21 -05:00
ensure commands for all services
This commit is contained in:
@@ -1,223 +0,0 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/cs3org/reva/v2/cmd/revad/runtime"
|
||||
"github.com/gofrs/uuid"
|
||||
"github.com/oklog/run"
|
||||
"github.com/owncloud/ocis/extensions/auth-basic/pkg/config"
|
||||
"github.com/owncloud/ocis/extensions/auth-basic/pkg/config/parser"
|
||||
"github.com/owncloud/ocis/extensions/auth-basic/pkg/server/debug"
|
||||
ociscfg "github.com/owncloud/ocis/ocis-pkg/config"
|
||||
"github.com/owncloud/ocis/ocis-pkg/ldap"
|
||||
"github.com/owncloud/ocis/ocis-pkg/log"
|
||||
"github.com/owncloud/ocis/ocis-pkg/sync"
|
||||
"github.com/owncloud/ocis/ocis-pkg/tracing"
|
||||
"github.com/thejerf/suture/v4"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
// Command is the entrypoint for the auth-basic command.
|
||||
func AuthBasic(cfg *config.Config) *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "auth-basic",
|
||||
Usage: "start authprovider for basic auth",
|
||||
Before: func(ctx *cli.Context) error {
|
||||
err := parser.ParseConfig(cfg)
|
||||
if err != nil {
|
||||
fmt.Printf("%v", err)
|
||||
}
|
||||
return err
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
logCfg := cfg.Logging
|
||||
logger := log.NewLogger(
|
||||
log.Level(logCfg.Level),
|
||||
log.File(logCfg.File),
|
||||
log.Pretty(logCfg.Pretty),
|
||||
log.Color(logCfg.Color),
|
||||
)
|
||||
tracing.Configure(cfg.Tracing.Enabled, cfg.Tracing.Type, logger)
|
||||
gr := run.Group{}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
// pre-create folders
|
||||
if cfg.AuthProvider == "json" && cfg.AuthProviders.JSON.File != "" {
|
||||
if err := os.MkdirAll(filepath.Dir(cfg.AuthProviders.JSON.File), os.FileMode(0700)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
uuid := uuid.Must(uuid.NewV4())
|
||||
|
||||
pidFile := path.Join(os.TempDir(), "revad-"+c.Command.Name+"-"+uuid.String()+".pid")
|
||||
|
||||
rcfg := authBasicConfigFromStruct(c, cfg)
|
||||
logger.Debug().
|
||||
Str("server", "authbasic").
|
||||
Interface("reva-config", rcfg).
|
||||
Msg("config")
|
||||
|
||||
if cfg.AuthProvider == "ldap" {
|
||||
ldapCfg := cfg.AuthProviders.LDAP
|
||||
if err := ldap.WaitForCA(logger, ldapCfg.Insecure, ldapCfg.CACert); err != nil {
|
||||
logger.Error().Err(err).Msg("The configured LDAP CA cert does not exist")
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
gr.Add(func() error {
|
||||
runtime.RunWithOptions(rcfg, pidFile, runtime.WithLogger(&logger.Logger))
|
||||
return nil
|
||||
}, func(_ error) {
|
||||
logger.Info().
|
||||
Str("server", c.Command.Name).
|
||||
Msg("Shutting down server")
|
||||
|
||||
cancel()
|
||||
})
|
||||
|
||||
debugServer, err := debug.Server(
|
||||
debug.Logger(logger),
|
||||
debug.Context(ctx),
|
||||
debug.Config(cfg),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
logger.Info().Err(err).Str("server", "debug").Msg("Failed to initialize server")
|
||||
return err
|
||||
}
|
||||
|
||||
gr.Add(debugServer.ListenAndServe, func(_ error) {
|
||||
cancel()
|
||||
})
|
||||
|
||||
if !cfg.Supervised {
|
||||
sync.Trap(&gr, cancel)
|
||||
}
|
||||
|
||||
return gr.Run()
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// authBasicConfigFromStruct will adapt an oCIS config struct into a reva mapstructure to start a reva service.
|
||||
func authBasicConfigFromStruct(c *cli.Context, cfg *config.Config) map[string]interface{} {
|
||||
rcfg := map[string]interface{}{
|
||||
"core": map[string]interface{}{
|
||||
"tracing_enabled": cfg.Tracing.Enabled,
|
||||
"tracing_endpoint": cfg.Tracing.Endpoint,
|
||||
"tracing_collector": cfg.Tracing.Collector,
|
||||
"tracing_service_name": c.Command.Name,
|
||||
},
|
||||
"shared": map[string]interface{}{
|
||||
"jwt_secret": cfg.TokenManager.JWTSecret,
|
||||
"gatewaysvc": cfg.Reva.Address,
|
||||
"skip_user_groups_in_token": cfg.SkipUserGroupsInToken,
|
||||
},
|
||||
"grpc": map[string]interface{}{
|
||||
"network": cfg.GRPC.Protocol,
|
||||
"address": cfg.GRPC.Addr,
|
||||
// TODO build services dynamically
|
||||
"services": map[string]interface{}{
|
||||
"authprovider": map[string]interface{}{
|
||||
"auth_manager": cfg.AuthProvider,
|
||||
"auth_managers": map[string]interface{}{
|
||||
"json": map[string]interface{}{
|
||||
"users": cfg.AuthProviders.JSON.File,
|
||||
},
|
||||
"ldap": ldapConfigFromString(cfg.AuthProviders.LDAP),
|
||||
"owncloudsql": map[string]interface{}{
|
||||
"dbusername": cfg.AuthProviders.OwnCloudSQL.DBUsername,
|
||||
"dbpassword": cfg.AuthProviders.OwnCloudSQL.DBPassword,
|
||||
"dbhost": cfg.AuthProviders.OwnCloudSQL.DBHost,
|
||||
"dbport": cfg.AuthProviders.OwnCloudSQL.DBPort,
|
||||
"dbname": cfg.AuthProviders.OwnCloudSQL.DBName,
|
||||
"idp": cfg.AuthProviders.OwnCloudSQL.IDP,
|
||||
"nobody": cfg.AuthProviders.OwnCloudSQL.Nobody,
|
||||
"join_username": cfg.AuthProviders.OwnCloudSQL.JoinUsername,
|
||||
"join_ownclouduuid": cfg.AuthProviders.OwnCloudSQL.JoinOwnCloudUUID,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
return rcfg
|
||||
}
|
||||
|
||||
// AuthBasicSutureService allows for the storage-authbasic command to be embedded and supervised by a suture supervisor tree.
|
||||
type AuthBasicSutureService struct {
|
||||
cfg *config.Config
|
||||
}
|
||||
|
||||
// NewAuthBasicSutureService creates a new store.AuthBasicSutureService
|
||||
func NewAuthBasic(cfg *ociscfg.Config) suture.Service {
|
||||
cfg.AuthBasic.Commons = cfg.Commons
|
||||
return AuthBasicSutureService{
|
||||
cfg: cfg.AuthBasic,
|
||||
}
|
||||
}
|
||||
|
||||
func (s AuthBasicSutureService) Serve(ctx context.Context) error {
|
||||
f := &flag.FlagSet{}
|
||||
cmdFlags := AuthBasic(s.cfg).Flags
|
||||
for k := range cmdFlags {
|
||||
if err := cmdFlags[k].Apply(f); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
cliCtx := cli.NewContext(nil, f, nil)
|
||||
if AuthBasic(s.cfg).Before != nil {
|
||||
if err := AuthBasic(s.cfg).Before(cliCtx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := AuthBasic(s.cfg).Action(cliCtx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func ldapConfigFromString(cfg config.LDAPProvider) map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
"uri": cfg.URI,
|
||||
"cacert": cfg.CACert,
|
||||
"insecure": cfg.Insecure,
|
||||
"bind_username": cfg.BindDN,
|
||||
"bind_password": cfg.BindPassword,
|
||||
"user_base_dn": cfg.UserBaseDN,
|
||||
"group_base_dn": cfg.GroupBaseDN,
|
||||
"user_filter": cfg.UserFilter,
|
||||
"group_filter": cfg.GroupFilter,
|
||||
"user_scope": cfg.UserScope,
|
||||
"group_scope": cfg.GroupScope,
|
||||
"user_objectclass": cfg.UserObjectClass,
|
||||
"group_objectclass": cfg.GroupObjectClass,
|
||||
"login_attributes": cfg.LoginAttributes,
|
||||
"idp": cfg.IDP,
|
||||
"user_schema": map[string]interface{}{
|
||||
"id": cfg.UserSchema.ID,
|
||||
"idIsOctetString": cfg.UserSchema.IDIsOctetString,
|
||||
"mail": cfg.UserSchema.Mail,
|
||||
"displayName": cfg.UserSchema.DisplayName,
|
||||
"userName": cfg.UserSchema.Username,
|
||||
},
|
||||
"group_schema": map[string]interface{}{
|
||||
"id": cfg.GroupSchema.ID,
|
||||
"idIsOctetString": cfg.GroupSchema.IDIsOctetString,
|
||||
"mail": cfg.GroupSchema.Mail,
|
||||
"displayName": cfg.GroupSchema.DisplayName,
|
||||
"groupName": cfg.GroupSchema.Groupname,
|
||||
"member": cfg.GroupSchema.Member,
|
||||
},
|
||||
}
|
||||
}
|
||||
57
extensions/auth-basic/pkg/command/health.go
Normal file
57
extensions/auth-basic/pkg/command/health.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/owncloud/ocis/extensions/auth-basic/pkg/config"
|
||||
"github.com/owncloud/ocis/extensions/auth-basic/pkg/config/parser"
|
||||
"github.com/owncloud/ocis/extensions/auth-basic/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 {
|
||||
err := parser.ParseConfig(cfg)
|
||||
if err != nil {
|
||||
fmt.Printf("%v", err)
|
||||
}
|
||||
return err
|
||||
},
|
||||
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
extensions/auth-basic/pkg/command/root.go
Normal file
64
extensions/auth-basic/pkg/command/root.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
"github.com/owncloud/ocis/extensions/auth-basic/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-auth-basic command.
|
||||
func Execute(cfg *config.Config) error {
|
||||
app := clihelper.DefaultApp(&cli.App{
|
||||
Name: "ocis-auth-basic",
|
||||
Usage: "Provide basic authentication 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 auth-basic command to be embedded and supervised by a suture supervisor tree.
|
||||
type SutureService struct {
|
||||
cfg *config.Config
|
||||
}
|
||||
|
||||
// NewSutureService creates a new auth-basic.SutureService
|
||||
func NewSutureService(cfg *ociscfg.Config) suture.Service {
|
||||
cfg.AuthBasic.Commons = cfg.Commons
|
||||
return SutureService{
|
||||
cfg: cfg.AuthBasic,
|
||||
}
|
||||
}
|
||||
|
||||
func (s SutureService) Serve(ctx context.Context) error {
|
||||
s.cfg.Context = ctx
|
||||
if err := Execute(s.cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
120
extensions/auth-basic/pkg/command/server.go
Normal file
120
extensions/auth-basic/pkg/command/server.go
Normal file
@@ -0,0 +1,120 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/cs3org/reva/v2/cmd/revad/runtime"
|
||||
"github.com/gofrs/uuid"
|
||||
"github.com/oklog/run"
|
||||
"github.com/owncloud/ocis/extensions/auth-basic/pkg/config"
|
||||
"github.com/owncloud/ocis/extensions/auth-basic/pkg/config/parser"
|
||||
"github.com/owncloud/ocis/extensions/auth-basic/pkg/logging"
|
||||
"github.com/owncloud/ocis/extensions/auth-basic/pkg/revaconfig"
|
||||
"github.com/owncloud/ocis/extensions/auth-basic/pkg/server/debug"
|
||||
"github.com/owncloud/ocis/extensions/auth-basic/pkg/tracing"
|
||||
"github.com/owncloud/ocis/ocis-pkg/ldap"
|
||||
"github.com/owncloud/ocis/ocis-pkg/service/external"
|
||||
"github.com/owncloud/ocis/ocis-pkg/sync"
|
||||
"github.com/owncloud/ocis/ocis-pkg/version"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
// Server is the entry point 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 {
|
||||
err := parser.ParseConfig(cfg)
|
||||
if err != nil {
|
||||
fmt.Printf("%v", err)
|
||||
}
|
||||
return err
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
logger := logging.Configure(cfg.Service.Name, cfg.Log)
|
||||
err := tracing.Configure(cfg, logger)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
gr := run.Group{}
|
||||
ctx, cancel := defineContext(cfg)
|
||||
|
||||
defer cancel()
|
||||
|
||||
pidFile := path.Join(os.TempDir(), "revad-"+cfg.Service.Name+"-"+uuid.Must(uuid.NewV4()).String()+".pid")
|
||||
|
||||
rcfg := revaconfig.AuthBasicConfigFromStruct(cfg)
|
||||
|
||||
// the reva runtime calls os.Exit in the case of a failure and there is no way for the oCIS
|
||||
// runtime to catch it and restart a reva service. Therefore we need to ensure the service has
|
||||
// everything it needs, before starting the service.
|
||||
// In this case: CA certificates
|
||||
if cfg.AuthProvider == "ldap" {
|
||||
ldapCfg := cfg.AuthProviders.LDAP
|
||||
if err := ldap.WaitForCA(logger, ldapCfg.Insecure, ldapCfg.CACert); err != nil {
|
||||
logger.Error().Err(err).Msg("The configured LDAP CA cert does not exist")
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
gr.Add(func() error {
|
||||
runtime.RunWithOptions(rcfg, pidFile, runtime.WithLogger(&logger.Logger))
|
||||
return nil
|
||||
}, func(_ error) {
|
||||
logger.Info().
|
||||
Str("server", cfg.Service.Name).
|
||||
Msg("Shutting down server")
|
||||
|
||||
cancel()
|
||||
})
|
||||
|
||||
debugServer, err := debug.Server(
|
||||
debug.Logger(logger),
|
||||
debug.Context(ctx),
|
||||
debug.Config(cfg),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
logger.Info().Err(err).Str("server", "debug").Msg("Failed to initialize server")
|
||||
return err
|
||||
}
|
||||
|
||||
gr.Add(debugServer.ListenAndServe, func(_ error) {
|
||||
cancel()
|
||||
})
|
||||
|
||||
if !cfg.Supervised {
|
||||
sync.Trap(&gr, cancel)
|
||||
}
|
||||
|
||||
if err := external.RegisterGRPCEndpoint(
|
||||
ctx,
|
||||
cfg.GRPC.Namespace+"."+cfg.Service.Name,
|
||||
uuid.Must(uuid.NewV4()).String(),
|
||||
cfg.GRPC.Addr,
|
||||
version.String,
|
||||
logger,
|
||||
); err != nil {
|
||||
logger.Fatal().Err(err).Msg("failed to register the grpc endpoint")
|
||||
}
|
||||
|
||||
return gr.Run()
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// defineContext sets the context for the extension. If there is a context configured it will create a new child from it,
|
||||
// if not, it will create a root context that can be cancelled.
|
||||
func defineContext(cfg *config.Config) (context.Context, context.CancelFunc) {
|
||||
return func() (context.Context, context.CancelFunc) {
|
||||
if cfg.Context == nil {
|
||||
return context.WithCancel(context.Background())
|
||||
}
|
||||
return context.WithCancel(cfg.Context)
|
||||
}()
|
||||
}
|
||||
50
extensions/auth-basic/pkg/command/version.go
Normal file
50
extensions/auth-basic/pkg/command/version.go
Normal 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/auth-basic/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.GRPC.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
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user