diff --git a/changelog/unreleased/storage-registry-json.md b/changelog/unreleased/storage-registry-json.md new file mode 100644 index 000000000..4ed10b42a --- /dev/null +++ b/changelog/unreleased/storage-registry-json.md @@ -0,0 +1,3 @@ +Enhancement: Add option to reading registry rules from json file + +https://github.com/owncloud/ocis/pull/1917 diff --git a/storage/pkg/command/frontend.go b/storage/pkg/command/frontend.go index e4d2362c9..928a73fc9 100644 --- a/storage/pkg/command/frontend.go +++ b/storage/pkg/command/frontend.go @@ -156,8 +156,9 @@ func frontendConfigFromStruct(c *cli.Context, cfg *config.Config, filesCfg map[s "public_url": cfg.Reva.Frontend.PublicURL, }, "ocs": map[string]interface{}{ - "share_prefix": cfg.Reva.Frontend.OCSSharePrefix, - "prefix": cfg.Reva.Frontend.OCSPrefix, + "share_prefix": cfg.Reva.Frontend.OCSSharePrefix, + "home_namespace": cfg.Reva.Frontend.OCSHomeNamespace, + "prefix": cfg.Reva.Frontend.OCSPrefix, "config": map[string]interface{}{ "version": "1.8", "website": "reva", diff --git a/storage/pkg/command/gateway.go b/storage/pkg/command/gateway.go index 083b798b5..935f23e20 100644 --- a/storage/pkg/command/gateway.go +++ b/storage/pkg/command/gateway.go @@ -2,7 +2,9 @@ package command import ( "context" + "encoding/json" "flag" + "io/ioutil" "os" "path" "strings" @@ -16,6 +18,7 @@ import ( "github.com/micro/cli/v2" "github.com/oklog/run" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" + "github.com/owncloud/ocis/ocis-pkg/log" "github.com/owncloud/ocis/storage/pkg/config" "github.com/owncloud/ocis/storage/pkg/flagset" "github.com/owncloud/ocis/storage/pkg/server/debug" @@ -46,7 +49,7 @@ func Gateway(cfg *config.Config) *cli.Command { ctx, cancel := context.WithCancel(context.Background()) uuid := uuid.Must(uuid.NewV4()) pidFile := path.Join(os.TempDir(), "revad-"+c.Command.Name+"-"+uuid.String()+".pid") - rcfg := gatewayConfigFromStruct(c, cfg) + rcfg := gatewayConfigFromStruct(c, cfg, logger) defer cancel() gr.Add(func() error { @@ -103,7 +106,7 @@ func Gateway(cfg *config.Config) *cli.Command { } // gatewayConfigFromStruct will adapt an oCIS config struct into a reva mapstructure to start a reva service. -func gatewayConfigFromStruct(c *cli.Context, cfg *config.Config) map[string]interface{} { +func gatewayConfigFromStruct(c *cli.Context, cfg *config.Config, logger log.Logger) map[string]interface{} { rcfg := map[string]interface{}{ "core": map[string]interface{}{ "max_cpus": cfg.Reva.Users.MaxCPUs, @@ -162,7 +165,7 @@ func gatewayConfigFromStruct(c *cli.Context, cfg *config.Config) map[string]inte "drivers": map[string]interface{}{ "static": map[string]interface{}{ "home_provider": cfg.Reva.StorageRegistry.HomeProvider, - "rules": rules(cfg), + "rules": rules(cfg, logger), }, }, }, @@ -172,7 +175,7 @@ func gatewayConfigFromStruct(c *cli.Context, cfg *config.Config) map[string]inte return rcfg } -func rules(cfg *config.Config) map[string]map[string]interface{} { +func rules(cfg *config.Config, logger log.Logger) map[string]map[string]interface{} { // if a list of rules is given it overrides the generated rules from below if len(cfg.Reva.StorageRegistry.Rules) > 0 { @@ -184,6 +187,21 @@ func rules(cfg *config.Config) map[string]map[string]interface{} { return rules } + // check if the rules have to be read from a json file + if cfg.Reva.StorageRegistry.JSON != "" { + data, err := ioutil.ReadFile(cfg.Reva.StorageRegistry.JSON) + if err != nil { + logger.Error().Err(err).Msg("Failed to read storage registry rules from JSON file: " + cfg.Reva.StorageRegistry.JSON) + return nil + } + var rules map[string]map[string]interface{} + if err = json.Unmarshal(data, &rules); err != nil { + logger.Error().Err(err).Msg("Failed to unmarshal storage registry rules") + return nil + } + return rules + } + // generate rules based on default config return map[string]map[string]interface{}{ cfg.Reva.StorageHome.MountPath: {"address": cfg.Reva.StorageHome.Endpoint}, diff --git a/storage/pkg/config/config.go b/storage/pkg/config/config.go index 7f048bb92..506830ef7 100644 --- a/storage/pkg/config/config.go +++ b/storage/pkg/config/config.go @@ -36,6 +36,7 @@ type StorageRegistry struct { // HomeProvider is the path in the global namespace that the static storage registry uses to determine the home storage HomeProvider string Rules []string + JSON string } // Sharing defines the available sharing configuration. @@ -112,6 +113,7 @@ type FrontendPort struct { OCDavPrefix string OCSPrefix string OCSSharePrefix string + OCSHomeNamespace string PublicURL string Middleware Middleware } diff --git a/storage/pkg/flagset/frontend.go b/storage/pkg/flagset/frontend.go index ab09f983e..e872cfdf2 100644 --- a/storage/pkg/flagset/frontend.go +++ b/storage/pkg/flagset/frontend.go @@ -119,6 +119,13 @@ func FrontendWithConfig(cfg *config.Config) []cli.Flag { EnvVars: []string{"STORAGE_FRONTEND_OCS_SHARE_PREFIX"}, Destination: &cfg.Reva.Frontend.OCSSharePrefix, }, + &cli.StringFlag{ + Name: "ocs-home-namespace", + Value: flags.OverrideDefaultString(cfg.Reva.Frontend.OCSHomeNamespace, "/home"), + Usage: "the prefix prepended to the incoming requests in OCS", + EnvVars: []string{"STORAGE_FRONTEND_OCS_HOME_NAMESPACE"}, + Destination: &cfg.Reva.Frontend.OCSHomeNamespace, + }, // Gateway &cli.StringFlag{ diff --git a/storage/pkg/flagset/gateway.go b/storage/pkg/flagset/gateway.go index 6211948a4..af603478f 100644 --- a/storage/pkg/flagset/gateway.go +++ b/storage/pkg/flagset/gateway.go @@ -140,14 +140,20 @@ func GatewayWithConfig(cfg *config.Config) []cli.Flag { Usage: `Replaces the generated storage registry rules with this set: --storage-registry-rule "/eos=localhost:9158" [--storage-registry-rule "1284d238-aa92-42ce-bdc4-0b0000009162=localhost:9162"]`, EnvVars: []string{"STORAGE_STORAGE_REGISTRY_RULES"}, }, - &cli.StringFlag{ Name: "storage-home-provider", Value: flags.OverrideDefaultString(cfg.Reva.StorageRegistry.HomeProvider, "/home"), Usage: "mount point of the storage provider for user homes in the global namespace", - EnvVars: []string{"STORAGE_REGISTRY_HOME_PROVIDER"}, + EnvVars: []string{"STORAGE_STORAGE_REGISTRY_HOME_PROVIDER"}, Destination: &cfg.Reva.StorageRegistry.HomeProvider, }, + &cli.StringFlag{ + Name: "storage-registry-json", + Value: flags.OverrideDefaultString(cfg.Reva.StorageRegistry.JSON, ""), + Usage: "JSON file containing the storage registry rules", + EnvVars: []string{"STORAGE_STORAGE_REGISTRY_JSON"}, + Destination: &cfg.Reva.StorageRegistry.JSON, + }, // please note that STORAGE_FRONTEND_PUBLIC_URL is also defined in // storage/pkg/flagset/frontend.go because this setting may be consumed