Skip to content

Commit

Permalink
Micro ocdav (#2665)
Browse files Browse the repository at this point in the history
* Introduce ocdav NewWithConfig()

Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>

* add go micro service wrapper for ocdav

Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>

* add changelog

Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>

* make jwtsecret configurable

Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>

* add header and missing loader

Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>

* add doc for the ocdav cmd

Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>

* liniting issues

Signed-off-by: jkoberg <jkoberg@owncloud.com>

* minor cleanup

Signed-off-by: jkoberg <jkoberg@owncloud.com>

* tracing

Signed-off-by: jkoberg <jkoberg@owncloud.com>

* Revert "minor cleanup"

This reverts commit f501bd3.

* trying to understand why the test is failing

Signed-off-by: jkoberg <jkoberg@owncloud.com>

* still trying to understand why the test is failing

Signed-off-by: jkoberg <jkoberg@owncloud.com>

* fix the typo

Signed-off-by: jkoberg <jkoberg@owncloud.com>

* reorder middlewares

Signed-off-by: jkoberg <jkoberg@owncloud.com>

Co-authored-by: jkoberg <jkoberg@owncloud.com>
  • Loading branch information
butonic and kobergj authored Apr 11, 2022
1 parent 134ed5a commit 865ba72
Show file tree
Hide file tree
Showing 9 changed files with 481 additions and 5 deletions.
5 changes: 5 additions & 0 deletions changelog/unreleased/micro-ocdav.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Enhancement: Add embeddable ocdav go micro service

The new `pkg/micro/ocdav` package implements a go micro compatible version of the ocdav service.

https://github.com/cs3org/reva/pull/2665
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/ReneKroon/ttlcache/v2 v2.11.0
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
github.com/asim/go-micro/plugins/events/natsjs/v4 v4.0.0-20220311080335-e5a35d38f931
github.com/asim/go-micro/plugins/server/http/v4 v4.0.0-20220317022205-c6d352c83291
github.com/aws/aws-sdk-go v1.42.39
github.com/beevik/etree v1.1.0
github.com/bluele/gcache v0.0.2
Expand Down
31 changes: 31 additions & 0 deletions go.sum

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion internal/http/services/owncloud/ocdav/ocdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func getLockSystem(c *Config) (LockSystem, error) {
return NewCS3LS(client), nil
}

// New returns a new ocdav
// New returns a new ocdav service
func New(m map[string]interface{}, log *zerolog.Logger) (global.Service, error) {
conf := &Config{}
if err := mapstructure.Decode(m, conf); err != nil {
Expand All @@ -151,6 +151,11 @@ func New(m map[string]interface{}, log *zerolog.Logger) (global.Service, error)
return nil, err
}

return NewWith(conf, fm, ls, log)
}

// NewWith returns a new ocdav service
func NewWith(conf *Config, fm favorite.Manager, ls LockSystem, _ *zerolog.Logger) (global.Service, error) {
s := &svc{
c: conf,
webDavHandler: new(WebDavHandler),
Expand Down
8 changes: 4 additions & 4 deletions internal/http/services/owncloud/ocdav/propfind/propfind.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,10 @@ func (p *Handler) statSpace(ctx context.Context, client gateway.GatewayAPIClient
ArbitraryMetadataKeys: metadataKeys,
}
res, err := client.Stat(ctx, req)
if err != nil || res == nil || res.Status == nil || res.Status.Code != rpc.Code_CODE_OK {
return nil, res.Status, err
if err != nil {
return nil, nil, err
}
return res.Info, res.Status, nil
return res.GetInfo(), res.GetStatus(), nil
}

func (p *Handler) getResourceInfos(ctx context.Context, w http.ResponseWriter, r *http.Request, pf XML, spaces []*provider.StorageSpace, requestPath string, spacesPropfind bool, log zerolog.Logger) ([]*provider.ResourceInfo, bool, bool) {
Expand Down Expand Up @@ -391,7 +391,7 @@ func (p *Handler) getResourceInfos(ctx context.Context, w http.ResponseWriter, r
// TODO get mtime, and size from space as well, so we no longer have to stat here?
spaceRef := spacelookup.MakeRelativeReference(space, requestPath, spacesPropfind)
info, status, err := p.statSpace(ctx, client, space, spaceRef, metadataKeys)
if err != nil || status.Code != rpc.Code_CODE_OK {
if err != nil || status.GetCode() != rpc.Code_CODE_OK {
continue
}

Expand Down
48 changes: 48 additions & 0 deletions pkg/micro/ocdav/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2018-2021 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

package main

import (
"os"

"github.com/cs3org/reva/v2/pkg/micro/ocdav"
"github.com/rs/zerolog"
)

// main starts a go micro service that uses the ocdev handler
// It is an example how to use pkg/micro/ocdav Service and leaves out any flag parsing.
func main() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
logger := zerolog.New(os.Stdout).With().Timestamp().Logger()

s, err := ocdav.Service(
ocdav.Logger(logger),
ocdav.GatewaySvc("127.0.0.1:9142"),
ocdav.FilesNamespace("/users/{{.Id.OpaqueId}}"),
ocdav.WebdavNamespace("/users/{{.Id.OpaqueId}}"),
ocdav.SharesNamespace("/Shares"),
)
if err != nil {
logger.Fatal().Err(err).Msg("failed starting ocdav service")
return
}
if err := s.Run(); err != nil {
logger.Fatal().Err(err).Msg("ocdav service exited with error")
}
}
28 changes: 28 additions & 0 deletions pkg/micro/ocdav/loader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2018-2021 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

package ocdav

import (
// initialize reva registries by importing the relevant loader packages
// see cmd/revad/runtime/loader.go for other loaders if a service is not found
_ "github.com/cs3org/reva/v2/internal/http/interceptors/auth/credential/loader"
_ "github.com/cs3org/reva/v2/internal/http/interceptors/auth/token/loader"
_ "github.com/cs3org/reva/v2/internal/http/interceptors/auth/tokenwriter/loader"
_ "github.com/cs3org/reva/v2/pkg/token/manager/loader"
)
185 changes: 185 additions & 0 deletions pkg/micro/ocdav/option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
// Copyright 2018-2021 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

package ocdav

import (
"context"
"crypto/tls"

"github.com/cs3org/reva/v2/internal/http/services/owncloud/ocdav"
"github.com/cs3org/reva/v2/pkg/storage/favorite"
"github.com/rs/zerolog"
)

// Option defines a single option function.
type Option func(o *Options)

// Options defines the available options for this package.
type Options struct {
TLSConfig *tls.Config
Address string
Logger zerolog.Logger
Context context.Context
// Metrics *metrics.Metrics
// Flags []cli.Flag
Name string
JWTSecret string

FavoriteManager favorite.Manager

TracingEnabled bool
TracingCollector string
TracingEndpoint string

// ocdav.* is internal so we need to set config options individually
config ocdav.Config
lockSystem ocdav.LockSystem
}

// newOptions initializes the available default options.
func newOptions(opts ...Option) Options {
opt := Options{}

for _, o := range opts {
o(&opt)
}

return opt
}

// TLSConfig provides a function to set the TLSConfig option.
func TLSConfig(config *tls.Config) Option {
return func(o *Options) {
o.TLSConfig = config
}
}

// Address provides a function to set the address option.
func Address(val string) Option {
return func(o *Options) {
o.Address = val
}
}

// JWTSecret provides a function to set the jwt secret option.
func JWTSecret(s string) Option {
return func(o *Options) {
o.JWTSecret = s
}
}

// Context provides a function to set the context option.
func Context(val context.Context) Option {
return func(o *Options) {
o.Context = val
}
}

// Logger provides a function to set the logger option.
func Logger(val zerolog.Logger) Option {
return func(o *Options) {
o.Logger = val
}
}

// Name provides a function to set the Name option.
func Name(val string) Option {
return func(o *Options) {
o.Name = val
}
}

// Prefix provides a function to set the prefix config option.
func Prefix(val string) Option {
return func(o *Options) {
o.config.Prefix = val
}
}

// FilesNamespace provides a function to set the FilesNamespace config option.
func FilesNamespace(val string) Option {
return func(o *Options) {
o.config.FilesNamespace = val
}
}

// WebdavNamespace provides a function to set the WebdavNamespace config option.
func WebdavNamespace(val string) Option {
return func(o *Options) {
o.config.WebdavNamespace = val
}
}

// SharesNamespace provides a function to set the SharesNamespace config option.
func SharesNamespace(val string) Option {
return func(o *Options) {
o.config.SharesNamespace = val
}
}

// GatewaySvc provides a function to set the GatewaySvc config option.
func GatewaySvc(val string) Option {
return func(o *Options) {
o.config.GatewaySvc = val
}
}

// Timeout provides a function to set the Timeout config option.
func Timeout(val int64) Option {
return func(o *Options) {
o.config.Timeout = val
}
}

// Insecure provides a function to set the Insecure config option.
func Insecure(val bool) Option {
return func(o *Options) {
o.config.Insecure = val
}
}

// PublicURL provides a function to set the PublicURL config option.
func PublicURL(val string) Option {
return func(o *Options) {
o.config.PublicURL = val
}
}

// FavoriteManager provides a function to set the FavoriteManager option.
func FavoriteManager(val favorite.Manager) Option {
return func(o *Options) {
o.FavoriteManager = val
}
}

// LockSystem provides a function to set the LockSystem option.
func LockSystem(val ocdav.LockSystem) Option {
return func(o *Options) {
o.lockSystem = val
}
}

// Tracing enables tracing
func Tracing(trEndpoint string, trCollector string) Option {
return func(o *Options) {
o.TracingEnabled = true
o.TracingEndpoint = trEndpoint
o.TracingCollector = trCollector
}
}
Loading

0 comments on commit 865ba72

Please sign in to comment.