|
| 1 | +package command |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + |
| 6 | + "github.com/rs/zerolog" |
| 7 | + "github.com/urfave/cli/v2" |
| 8 | + |
| 9 | + "github.com/cs3org/reva/v2/pkg/rgrpc/todo/pool" |
| 10 | + "github.com/cs3org/reva/v2/pkg/share/manager/jsoncs3" |
| 11 | + "github.com/cs3org/reva/v2/pkg/share/manager/registry" |
| 12 | + "github.com/cs3org/reva/v2/pkg/utils" |
| 13 | + |
| 14 | + "github.com/owncloud/ocis/v2/ocis-pkg/config" |
| 15 | + "github.com/owncloud/ocis/v2/ocis-pkg/config/configlog" |
| 16 | + "github.com/owncloud/ocis/v2/ocis-pkg/config/parser" |
| 17 | + mregistry "github.com/owncloud/ocis/v2/ocis-pkg/registry" |
| 18 | + "github.com/owncloud/ocis/v2/ocis/pkg/register" |
| 19 | + sharingparser "github.com/owncloud/ocis/v2/services/sharing/pkg/config/parser" |
| 20 | +) |
| 21 | + |
| 22 | +// SharesCommand is the entrypoint for the groups command. |
| 23 | +func SharesCommand(cfg *config.Config) *cli.Command { |
| 24 | + return &cli.Command{ |
| 25 | + Name: "shares", |
| 26 | + Usage: `cli tools to manage entries in the share manager.`, |
| 27 | + Category: "maintenance", |
| 28 | + Before: func(c *cli.Context) error { |
| 29 | + // Parse base config |
| 30 | + if err := parser.ParseConfig(cfg, true); err != nil { |
| 31 | + return configlog.ReturnError(err) |
| 32 | + } |
| 33 | + |
| 34 | + // Parse sharing config |
| 35 | + cfg.Sharing.Commons = cfg.Commons |
| 36 | + return configlog.ReturnError(sharingparser.ParseConfig(cfg.Sharing)) |
| 37 | + }, |
| 38 | + Subcommands: []*cli.Command{ |
| 39 | + cleanupCmd(cfg), |
| 40 | + }, |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func init() { |
| 45 | + register.AddCommand(SharesCommand) |
| 46 | +} |
| 47 | + |
| 48 | +func cleanupCmd(cfg *config.Config) *cli.Command { |
| 49 | + return &cli.Command{ |
| 50 | + Name: "cleanup", |
| 51 | + Usage: `clean up stale entries in the share manager.`, |
| 52 | + Flags: []cli.Flag{ |
| 53 | + &cli.StringFlag{ |
| 54 | + Name: "service-account-id", |
| 55 | + Value: "", |
| 56 | + Usage: "Name of the service account to use for the cleanup", |
| 57 | + EnvVars: []string{"OCIS_SERVICE_ACCOUNT_ID"}, |
| 58 | + Required: true, |
| 59 | + }, |
| 60 | + &cli.StringFlag{ |
| 61 | + Name: "service-account-secret", |
| 62 | + Value: "", |
| 63 | + Usage: "Secret for the service account", |
| 64 | + EnvVars: []string{"OCIS_SERVICE_ACCOUNT_SECRET"}, |
| 65 | + Required: true, |
| 66 | + }, |
| 67 | + }, |
| 68 | + Before: func(c *cli.Context) error { |
| 69 | + // Parse base config |
| 70 | + if err := parser.ParseConfig(cfg, true); err != nil { |
| 71 | + return configlog.ReturnError(err) |
| 72 | + } |
| 73 | + |
| 74 | + // Parse sharing config |
| 75 | + cfg.Sharing.Commons = cfg.Commons |
| 76 | + return configlog.ReturnError(sharingparser.ParseConfig(cfg.Sharing)) |
| 77 | + }, |
| 78 | + Action: func(c *cli.Context) error { |
| 79 | + return cleanup(c, cfg) |
| 80 | + }, |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func cleanup(c *cli.Context, cfg *config.Config) error { |
| 85 | + driver := cfg.Sharing.UserSharingDriver |
| 86 | + // cleanup is only implemented for the jsoncs3 share manager |
| 87 | + if driver != "jsoncs3" { |
| 88 | + return configlog.ReturnError(errors.New("cleanup is only implemented for the jsoncs3 share manager")) |
| 89 | + } |
| 90 | + |
| 91 | + rcfg := revaShareConfig(cfg.Sharing) |
| 92 | + f, ok := registry.NewFuncs[driver] |
| 93 | + if !ok { |
| 94 | + return configlog.ReturnError(errors.New("Unknown share manager type '" + driver + "'")) |
| 95 | + } |
| 96 | + mgr, err := f(rcfg[driver].(map[string]interface{})) |
| 97 | + if err != nil { |
| 98 | + return configlog.ReturnError(err) |
| 99 | + } |
| 100 | + |
| 101 | + // Initialize registry to make service lookup work |
| 102 | + _ = mregistry.GetRegistry() |
| 103 | + |
| 104 | + // get an authenticated context |
| 105 | + gatewaySelector, err := pool.GatewaySelector(cfg.Sharing.Reva.Address) |
| 106 | + if err != nil { |
| 107 | + return configlog.ReturnError(err) |
| 108 | + } |
| 109 | + |
| 110 | + client, err := gatewaySelector.Next() |
| 111 | + if err != nil { |
| 112 | + return configlog.ReturnError(err) |
| 113 | + } |
| 114 | + |
| 115 | + serviceUserCtx, err := utils.GetServiceUserContext(c.String("service-account-id"), client, c.String("service-account-secret")) |
| 116 | + if err != nil { |
| 117 | + return configlog.ReturnError(err) |
| 118 | + } |
| 119 | + |
| 120 | + l := logger() |
| 121 | + |
| 122 | + zerolog.SetGlobalLevel(zerolog.InfoLevel) |
| 123 | + serviceUserCtx = l.WithContext(serviceUserCtx) |
| 124 | + |
| 125 | + mgr.(*jsoncs3.Manager).CleanupStaleShares(serviceUserCtx) |
| 126 | + |
| 127 | + return nil |
| 128 | +} |
0 commit comments