package command import ( "errors" "github.com/rs/zerolog" "github.com/urfave/cli/v2" "github.com/opencloud-eu/reva/v2/pkg/rgrpc/todo/pool" "github.com/opencloud-eu/reva/v2/pkg/share/manager/jsoncs3" "github.com/opencloud-eu/reva/v2/pkg/share/manager/registry" "github.com/opencloud-eu/reva/v2/pkg/utils" "github.com/opencloud-eu/opencloud/opencloud/pkg/register" "github.com/opencloud-eu/opencloud/pkg/config" "github.com/opencloud-eu/opencloud/pkg/config/configlog" "github.com/opencloud-eu/opencloud/pkg/config/parser" oclog "github.com/opencloud-eu/opencloud/pkg/log" mregistry "github.com/opencloud-eu/opencloud/pkg/registry" sharing "github.com/opencloud-eu/opencloud/services/sharing/pkg/config" sharingparser "github.com/opencloud-eu/opencloud/services/sharing/pkg/config/parser" ) // SharesCommand is the entrypoint for the groups command. func SharesCommand(cfg *config.Config) *cli.Command { return &cli.Command{ Name: "shares", Usage: `cli tools to manage entries in the share manager.`, Category: "maintenance", Before: func(c *cli.Context) error { // Parse base config if err := parser.ParseConfig(cfg, true); err != nil { return configlog.ReturnError(err) } // Parse sharing config cfg.Sharing.Commons = cfg.Commons return configlog.ReturnError(sharingparser.ParseConfig(cfg.Sharing)) }, Subcommands: []*cli.Command{ cleanupCmd(cfg), }, } } func init() { register.AddCommand(SharesCommand) } func cleanupCmd(cfg *config.Config) *cli.Command { return &cli.Command{ Name: "cleanup", Usage: `clean up stale entries in the share manager.`, Flags: []cli.Flag{ &cli.StringFlag{ Name: "service-account-id", Value: "", Usage: "Name of the service account to use for the cleanup", EnvVars: []string{"OC_SERVICE_ACCOUNT_ID"}, Required: true, }, &cli.StringFlag{ Name: "service-account-secret", Value: "", Usage: "Secret for the service account", EnvVars: []string{"OC_SERVICE_ACCOUNT_SECRET"}, Required: true, }, }, Before: func(c *cli.Context) error { // Parse base config if err := parser.ParseConfig(cfg, true); err != nil { return configlog.ReturnError(err) } // Parse sharing config cfg.Sharing.Commons = cfg.Commons return configlog.ReturnError(sharingparser.ParseConfig(cfg.Sharing)) }, Action: func(c *cli.Context) error { return cleanup(c, cfg) }, } } func cleanup(c *cli.Context, cfg *config.Config) error { driver := cfg.Sharing.UserSharingDriver // cleanup is only implemented for the jsoncs3 share manager if driver != "jsoncs3" { return configlog.ReturnError(errors.New("cleanup is only implemented for the jsoncs3 share manager")) } rcfg := revaShareConfig(cfg.Sharing) f, ok := registry.NewFuncs[driver] if !ok { return configlog.ReturnError(errors.New("Unknown share manager type '" + driver + "'")) } mgr, err := f(rcfg[driver].(map[string]interface{})) if err != nil { return configlog.ReturnError(err) } // Initialize registry to make service lookup work _ = mregistry.GetRegistry() // get an authenticated context gatewaySelector, err := pool.GatewaySelector(cfg.Sharing.Reva.Address) if err != nil { return configlog.ReturnError(err) } client, err := gatewaySelector.Next() if err != nil { return configlog.ReturnError(err) } serviceUserCtx, err := utils.GetServiceUserContext(c.String("service-account-id"), client, c.String("service-account-secret")) if err != nil { return configlog.ReturnError(err) } l := logger() zerolog.SetGlobalLevel(zerolog.InfoLevel) serviceUserCtx = l.WithContext(serviceUserCtx) mgr.(*jsoncs3.Manager).CleanupStaleShares(serviceUserCtx) return nil } func revaShareConfig(cfg *sharing.Config) map[string]interface{} { return map[string]interface{}{ "json": map[string]interface{}{ "file": cfg.UserSharingDrivers.JSON.File, "gateway_addr": cfg.Reva.Address, }, "sql": map[string]interface{}{ // cernbox sql "db_username": cfg.UserSharingDrivers.SQL.DBUsername, "db_password": cfg.UserSharingDrivers.SQL.DBPassword, "db_host": cfg.UserSharingDrivers.SQL.DBHost, "db_port": cfg.UserSharingDrivers.SQL.DBPort, "db_name": cfg.UserSharingDrivers.SQL.DBName, "password_hash_cost": cfg.UserSharingDrivers.SQL.PasswordHashCost, "enable_expired_shares_cleanup": cfg.UserSharingDrivers.SQL.EnableExpiredSharesCleanup, "janitor_run_interval": cfg.UserSharingDrivers.SQL.JanitorRunInterval, }, "owncloudsql": map[string]interface{}{ "gateway_addr": cfg.Reva.Address, "storage_mount_id": cfg.UserSharingDrivers.OwnCloudSQL.UserStorageMountID, "db_username": cfg.UserSharingDrivers.OwnCloudSQL.DBUsername, "db_password": cfg.UserSharingDrivers.OwnCloudSQL.DBPassword, "db_host": cfg.UserSharingDrivers.OwnCloudSQL.DBHost, "db_port": cfg.UserSharingDrivers.OwnCloudSQL.DBPort, "db_name": cfg.UserSharingDrivers.OwnCloudSQL.DBName, }, "cs3": map[string]interface{}{ "gateway_addr": cfg.UserSharingDrivers.CS3.ProviderAddr, "provider_addr": cfg.UserSharingDrivers.CS3.ProviderAddr, "service_user_id": cfg.UserSharingDrivers.CS3.SystemUserID, "service_user_idp": cfg.UserSharingDrivers.CS3.SystemUserIDP, "machine_auth_apikey": cfg.UserSharingDrivers.CS3.SystemUserAPIKey, }, "jsoncs3": map[string]interface{}{ "gateway_addr": cfg.Reva.Address, "provider_addr": cfg.UserSharingDrivers.JSONCS3.ProviderAddr, "service_user_id": cfg.UserSharingDrivers.JSONCS3.SystemUserID, "service_user_idp": cfg.UserSharingDrivers.JSONCS3.SystemUserIDP, "machine_auth_apikey": cfg.UserSharingDrivers.JSONCS3.SystemUserAPIKey, }, } } func revaPublicShareConfig(cfg *sharing.Config) map[string]interface{} { return map[string]interface{}{ "json": map[string]interface{}{ "file": cfg.PublicSharingDrivers.JSON.File, "gateway_addr": cfg.Reva.Address, }, "jsoncs3": map[string]interface{}{ "gateway_addr": cfg.Reva.Address, "provider_addr": cfg.PublicSharingDrivers.JSONCS3.ProviderAddr, "service_user_id": cfg.PublicSharingDrivers.JSONCS3.SystemUserID, "service_user_idp": cfg.PublicSharingDrivers.JSONCS3.SystemUserIDP, "machine_auth_apikey": cfg.PublicSharingDrivers.JSONCS3.SystemUserAPIKey, }, "sql": map[string]interface{}{ "db_username": cfg.PublicSharingDrivers.SQL.DBUsername, "db_password": cfg.PublicSharingDrivers.SQL.DBPassword, "db_host": cfg.PublicSharingDrivers.SQL.DBHost, "db_port": cfg.PublicSharingDrivers.SQL.DBPort, "db_name": cfg.PublicSharingDrivers.SQL.DBName, "password_hash_cost": cfg.PublicSharingDrivers.SQL.PasswordHashCost, "enable_expired_shares_cleanup": cfg.PublicSharingDrivers.SQL.EnableExpiredSharesCleanup, "janitor_run_interval": cfg.PublicSharingDrivers.SQL.JanitorRunInterval, }, "cs3": map[string]interface{}{ "gateway_addr": cfg.PublicSharingDrivers.CS3.ProviderAddr, "provider_addr": cfg.PublicSharingDrivers.CS3.ProviderAddr, "service_user_id": cfg.PublicSharingDrivers.CS3.SystemUserID, "service_user_idp": cfg.PublicSharingDrivers.CS3.SystemUserIDP, "machine_auth_apikey": cfg.PublicSharingDrivers.CS3.SystemUserAPIKey, }, } } func logger() *zerolog.Logger { log := oclog.NewLogger( oclog.Name("migrate"), oclog.Level("info"), oclog.Pretty(true), oclog.Color(true)).Logger return &log }