From 297cc77621b8a7daae6ad89f18af0550b6eac23e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Sun, 28 Jul 2024 21:01:29 +0200 Subject: [PATCH] gateway should talk to itself MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jörn Friedrich Dreyer extract method Signed-off-by: Jörn Friedrich Dreyer --- .../omit-some-lookups-in-gateway.md | 5 ++++ ocis-pkg/config/helpers.go | 24 ++++++++++++++--- services/gateway/pkg/revaconfig/config.go | 7 +++-- .../storage-system/pkg/revaconfig/config.go | 27 ++++++++++--------- 4 files changed, 46 insertions(+), 17 deletions(-) create mode 100644 changelog/unreleased/omit-some-lookups-in-gateway.md diff --git a/changelog/unreleased/omit-some-lookups-in-gateway.md b/changelog/unreleased/omit-some-lookups-in-gateway.md new file mode 100644 index 0000000000..578081008d --- /dev/null +++ b/changelog/unreleased/omit-some-lookups-in-gateway.md @@ -0,0 +1,5 @@ +Enhancement: Gateways should directly talk to themselves + +The CS3 gateway can directly to itself when it wants to talk to the registries running in the same reva runtime. + +https://github.com/owncloud/ocis/pull/9714 diff --git a/ocis-pkg/config/helpers.go b/ocis-pkg/config/helpers.go index d8e4722bb8..1899e3bd46 100644 --- a/ocis-pkg/config/helpers.go +++ b/ocis-pkg/config/helpers.go @@ -1,13 +1,14 @@ package config import ( - gofig "github.com/gookit/config/v2" - gooyaml "github.com/gookit/config/v2/yaml" - "github.com/owncloud/ocis/v2/ocis-pkg/config/defaults" "io/fs" "os" "path" "strings" + + gofig "github.com/gookit/config/v2" + gooyaml "github.com/gookit/config/v2/yaml" + "github.com/owncloud/ocis/v2/ocis-pkg/config/defaults" ) var ( @@ -49,3 +50,20 @@ func bindSourcesToStructs(fileSystem fs.FS, filePath, service string, dst interf return nil } + +// LocalEndpoint returns the local endpoint for a given protocol and address. +// Use it when configuring the reva runtime to get a service endpoint in the same +// runtime, e.g. a gateway talking to an authregistry service. +func LocalEndpoint(protocol, addr string) string { + localEndpoint := addr + switch protocol { + case "tcp": + parts := strings.SplitN(addr, ":", 2) + if len(parts) == 2 { + localEndpoint = "dns:127.0.0.1:" + parts[1] + } + case "unix": + localEndpoint = "unix:" + addr + } + return localEndpoint +} diff --git a/services/gateway/pkg/revaconfig/config.go b/services/gateway/pkg/revaconfig/config.go index 035ae05bf6..f24fd87fc8 100644 --- a/services/gateway/pkg/revaconfig/config.go +++ b/services/gateway/pkg/revaconfig/config.go @@ -6,12 +6,15 @@ import ( "strings" "github.com/cs3org/reva/v2/pkg/utils" + pkgconfig "github.com/owncloud/ocis/v2/ocis-pkg/config" "github.com/owncloud/ocis/v2/ocis-pkg/log" "github.com/owncloud/ocis/v2/services/gateway/pkg/config" ) // GatewayConfigFromStruct will adapt an oCIS config struct into a reva mapstructure to start a reva service. func GatewayConfigFromStruct(cfg *config.Config, logger log.Logger) map[string]interface{} { + localEndpoint := pkgconfig.LocalEndpoint(cfg.GRPC.Protocol, cfg.GRPC.Addr) + rcfg := map[string]interface{}{ "core": map[string]interface{}{ "tracing_enabled": cfg.Tracing.Enabled, @@ -39,8 +42,8 @@ func GatewayConfigFromStruct(cfg *config.Config, logger log.Logger) map[string]i "gateway": map[string]interface{}{ "applicationauthsvc": cfg.AuthAppEndpoint, // registries are located on the gateway - "authregistrysvc": cfg.Reva.Address, - "storageregistrysvc": cfg.Reva.Address, + "authregistrysvc": localEndpoint, + "storageregistrysvc": localEndpoint, "appregistrysvc": cfg.AppRegistryEndpoint, // user metadata is located on the users services "preferencessvc": cfg.UsersEndpoint, diff --git a/services/storage-system/pkg/revaconfig/config.go b/services/storage-system/pkg/revaconfig/config.go index 0dc62e3be5..82373265e8 100644 --- a/services/storage-system/pkg/revaconfig/config.go +++ b/services/storage-system/pkg/revaconfig/config.go @@ -2,11 +2,14 @@ package revaconfig import ( userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" + pkgconfig "github.com/owncloud/ocis/v2/ocis-pkg/config" "github.com/owncloud/ocis/v2/services/storage-system/pkg/config" ) // StorageSystemFromStruct will adapt an oCIS config struct into a reva mapstructure to start a reva service. func StorageSystemFromStruct(cfg *config.Config) map[string]interface{} { + localEndpoint := pkgconfig.LocalEndpoint(cfg.GRPC.Protocol, cfg.GRPC.Addr) + rcfg := map[string]interface{}{ "shared": map[string]interface{}{ "jwt_secret": cfg.TokenManager.JWTSecret, @@ -25,12 +28,12 @@ func StorageSystemFromStruct(cfg *config.Config) map[string]interface{} { "services": map[string]interface{}{ "gateway": map[string]interface{}{ // registries are located on the gateway - "authregistrysvc": "com.owncloud.api.storage-system", - "storageregistrysvc": "com.owncloud.api.storage-system", + "authregistrysvc": localEndpoint, + "storageregistrysvc": localEndpoint, // user metadata is located on the users services - "userprovidersvc": "com.owncloud.api.storage-system", - "groupprovidersvc": "com.owncloud.api.storage-system", - "permissionssvc": "com.owncloud.api.storage-system", + "userprovidersvc": localEndpoint, + "groupprovidersvc": localEndpoint, + "permissionssvc": localEndpoint, // other "disable_home_creation_on_login": true, // metadata manually creates a space // metadata always uses the simple upload, so no transfer secret or datagateway needed @@ -60,7 +63,7 @@ func StorageSystemFromStruct(cfg *config.Config) map[string]interface{} { "drivers": map[string]interface{}{ "static": map[string]interface{}{ "rules": map[string]interface{}{ - "machine": "com.owncloud.api.storage-system", + "machine": localEndpoint, }, }, }, @@ -70,7 +73,7 @@ func StorageSystemFromStruct(cfg *config.Config) map[string]interface{} { "auth_managers": map[string]interface{}{ "machine": map[string]interface{}{ "api_key": cfg.SystemUserAPIKey, - "gateway_addr": "com.owncloud.api.storage-system", + "gateway_addr": localEndpoint, }, }, }, @@ -86,7 +89,7 @@ func StorageSystemFromStruct(cfg *config.Config) map[string]interface{} { "static": map[string]interface{}{ "rules": map[string]interface{}{ "/": map[string]interface{}{ - "address": "com.owncloud.api.storage-system", + "address": localEndpoint, }, }, }, @@ -94,7 +97,7 @@ func StorageSystemFromStruct(cfg *config.Config) map[string]interface{} { }, "storageprovider": map[string]interface{}{ "driver": cfg.Driver, - "drivers": metadataDrivers(cfg), + "drivers": metadataDrivers(localEndpoint, cfg), "data_server_url": cfg.DataServerURL, }, }, @@ -113,7 +116,7 @@ func StorageSystemFromStruct(cfg *config.Config) map[string]interface{} { "dataprovider": map[string]interface{}{ "prefix": "data", "driver": cfg.Driver, - "drivers": metadataDrivers(cfg), + "drivers": metadataDrivers(localEndpoint, cfg), "data_txs": map[string]interface{}{ "simple": map[string]interface{}{ "cache_store": "noop", @@ -144,7 +147,7 @@ func StorageSystemFromStruct(cfg *config.Config) map[string]interface{} { return rcfg } -func metadataDrivers(cfg *config.Config) map[string]interface{} { +func metadataDrivers(localEndpoint string, cfg *config.Config) map[string]interface{} { return map[string]interface{}{ "ocis": map[string]interface{}{ "metadata_backend": cfg.Drivers.OCIS.MetadataBackend, @@ -152,7 +155,7 @@ func metadataDrivers(cfg *config.Config) map[string]interface{} { "user_layout": "{{.Id.OpaqueId}}", "treetime_accounting": false, "treesize_accounting": false, - "permissionssvc": "com.owncloud.api.storage-system", + "permissionssvc": localEndpoint, "max_acquire_lock_cycles": cfg.Drivers.OCIS.MaxAcquireLockCycles, "lock_cycle_duration_factor": cfg.Drivers.OCIS.LockCycleDurationFactor, "disable_versioning": true,