Skip to content

Commit

Permalink
Merge pull request #10712 from aduffeck/cleanup-stale-shares
Browse files Browse the repository at this point in the history
Cleanup stale shares
  • Loading branch information
butonic authored Dec 3, 2024
2 parents 92e6a9e + a925d09 commit d3619d0
Show file tree
Hide file tree
Showing 32 changed files with 597 additions and 60 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/coreos/go-oidc/v3 v3.11.0
github.com/cs3org/go-cs3apis v0.0.0-20241105092511-3ad35d174fc1
github.com/cs3org/reva/v2 v2.26.7
github.com/cs3org/reva/v2 v2.26.8-0.20241203081301-17f339546533
github.com/davidbyttow/govips/v2 v2.15.0
github.com/dhowden/tag v0.0.0-20240417053706-3d75831295e8
github.com/dutchcoders/go-clamd v0.0.0-20170520113014-b970184f4d9e
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ github.com/cs3org/go-cs3apis v0.0.0-20241105092511-3ad35d174fc1 h1:RU6LT6mkD16xZ
github.com/cs3org/go-cs3apis v0.0.0-20241105092511-3ad35d174fc1/go.mod h1:DedpcqXl193qF/08Y04IO0PpxyyMu8+GrkD6kWK2MEQ=
github.com/cs3org/reva/v2 v2.26.7 h1:E5b1+H5ZsnmDgWWS/u3t4PtdmiMaY1bEEYVI/vE9xo8=
github.com/cs3org/reva/v2 v2.26.7/go.mod h1:xC5N2XOrCRim/W55uyMsew8RwwFZbQ4hIaKshIbyToo=
github.com/cs3org/reva/v2 v2.26.8-0.20241203081301-17f339546533 h1:QshDjljk44ASolJwlHxE9e7u+Slgdi/VfPKYvbfFu2g=
github.com/cs3org/reva/v2 v2.26.8-0.20241203081301-17f339546533/go.mod h1:fJWmn7EkttWOWphZfiKdFOcHuthcUsU55aSN1VeTOhU=
github.com/cyberdelia/templates v0.0.0-20141128023046-ca7fffd4298c/go.mod h1:GyV+0YP4qX0UQ7r2MoYZ+AvYDp12OF5yg4q8rGnyNh4=
github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53EtKeQYTC3kyg=
github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4=
Expand Down
2 changes: 1 addition & 1 deletion ocis/pkg/command/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ func revaShareConfig(cfg *sharing.Config) map[string]interface{} {
"machine_auth_apikey": cfg.UserSharingDrivers.CS3.SystemUserAPIKey,
},
"jsoncs3": map[string]interface{}{
"gateway_addr": cfg.UserSharingDrivers.JSONCS3.ProviderAddr,
"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,
Expand Down
128 changes: 128 additions & 0 deletions ocis/pkg/command/shares.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package command

import (
"errors"

"github.com/rs/zerolog"
"github.com/urfave/cli/v2"

"github.com/cs3org/reva/v2/pkg/rgrpc/todo/pool"
"github.com/cs3org/reva/v2/pkg/share/manager/jsoncs3"
"github.com/cs3org/reva/v2/pkg/share/manager/registry"
"github.com/cs3org/reva/v2/pkg/utils"

"github.com/owncloud/ocis/v2/ocis-pkg/config"
"github.com/owncloud/ocis/v2/ocis-pkg/config/configlog"
"github.com/owncloud/ocis/v2/ocis-pkg/config/parser"
mregistry "github.com/owncloud/ocis/v2/ocis-pkg/registry"
"github.com/owncloud/ocis/v2/ocis/pkg/register"
sharingparser "github.com/owncloud/ocis/v2/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{"OCIS_SERVICE_ACCOUNT_ID"},
Required: true,
},
&cli.StringFlag{
Name: "service-account-secret",
Value: "",
Usage: "Secret for the service account",
EnvVars: []string{"OCIS_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
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d3619d0

Please sign in to comment.