Skip to content

Commit

Permalink
AppProvider: make CodiMD config non hard-coded (#3401)
Browse files Browse the repository at this point in the history
  • Loading branch information
glpatcern authored Nov 3, 2022
1 parent 392b4df commit a2b2109
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 96 deletions.
7 changes: 7 additions & 0 deletions changelog/unreleased/app-tweaks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Enhancement: make WOPI bridged apps (CodiMD) configuration non hard-coded

The configuration of the custom mimetypes has been moved to the AppProvider,
and the given mimetypes are used to configure bridged apps by sharing
the corresponding config item to the drivers.

https://github.com/cs3org/reva/pull/3401
4 changes: 2 additions & 2 deletions examples/nextcloud-integration/custom-mime-types-demo.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
".zmd": "application/compressed-markdown",
".zep": "application/compressed-etherpad"
"zmd": "application/compressed-markdown",
"zep": "application/compressed-etherpad"
}
2 changes: 1 addition & 1 deletion examples/nextcloud-integration/revad.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ driver = "memory"

[grpc.services.appprovider]
driver = "wopi"
custom_mime_types_json = "custom-mime-types-demo.json"

[grpc.services.appprovider.drivers.wopi]
iop_secret = "hello"
wopi_url = "http://0.0.0.0:8880/"
app_name = "Collabora"
app_url = "https://your-collabora-server.org:9980"
custom_mime_types_json = "custom-mime-types-demo.json"

[grpc.services.appregistry]
driver = "static"
Expand Down
19 changes: 19 additions & 0 deletions examples/storage-references/appprovider-codimd.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[shared]
gatewaysvc = "localhost:your-revad-gateway-port"

[grpc]
address = "0.0.0.0:12345"

[grpc.services.appprovider]
driver = "wopi"
custom_mime_types_json = "custom-mime-types-demo.json"
mime_types = ["text/markdown", "application/compressed-markdown", "text/plain"]
app_provider_url = "localhost:12345"
language = "en-GB"

[grpc.services.appprovider.drivers.wopi]
iop_secret = "hello"
wopi_url = "http://0.0.0.0:8880/"
app_name = "CodiMD"
app_url = "https://your-codimd-server.org:3000"
app_int_url = "https://your-codimd-server.org:3000"
4 changes: 0 additions & 4 deletions examples/storage-references/gateway.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ mime_types = [
{"mime_type" = "application/vnd.jupyter", "extension" = "ipynb", "name" = "Jupyter Notebook", "description" = "Jupyter Notebook"}
]

[grpc.services.appprovider]
mime_types = ["text/plain"]
language = "en-GB"

[http.services.datagateway]
[http.services.prometheus]
[http.services.ocmd]
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ require (
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/glpatcern/go-mime v0.0.0-20221026162842-2a8d71ad17a9 // indirect
github.com/go-asn1-ber/asn1-ber v1.5.4 // indirect
github.com/go-kit/log v0.2.0 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ github.com/gdexlab/go-render v1.0.1/go.mod h1:wRi5nW2qfjiGj4mPukH4UV0IknS1cHD4Vg
github.com/getkin/kin-openapi v0.13.0/go.mod h1:WGRs2ZMM1Q8LR1QBEwUxC6RJEfaBcD0s+pcEVXFuAjw=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
github.com/glpatcern/go-mime v0.0.0-20221026162842-2a8d71ad17a9 h1:3um08ooi0/lyRmK2eE1XTKmRQHDzPu0IvpCPMljyMZ8=
github.com/glpatcern/go-mime v0.0.0-20221026162842-2a8d71ad17a9/go.mod h1:EJaddanP+JfU3UkVvn0rYYF3b/gD7eZRejbTHqiQExA=
github.com/go-acme/lego/v4 v4.4.0/go.mod h1:l3+tFUFZb590dWcqhWZegynUthtaHJbG2fevUpoOOE0=
github.com/go-asn1-ber/asn1-ber v1.5.4 h1:vXT6d/FNDiELJnLb6hGNa309LMsrCoYFvpwHDF0+Y1A=
github.com/go-asn1-ber/asn1-ber v1.5.4/go.mod h1:hEBeB/ic+5LoWskz+yKT7vGhhPYkProFKoKdwZRWMe0=
Expand Down
56 changes: 46 additions & 10 deletions internal/grpc/services/appprovider/appprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ package appprovider

import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"strconv"
"time"
Expand All @@ -33,6 +36,7 @@ import (
"github.com/cs3org/reva/pkg/app/provider/registry"
"github.com/cs3org/reva/pkg/errtypes"
"github.com/cs3org/reva/pkg/logger"
"github.com/cs3org/reva/pkg/mime"
"github.com/cs3org/reva/pkg/rgrpc"
"github.com/cs3org/reva/pkg/rgrpc/status"
"github.com/cs3org/reva/pkg/rgrpc/todo/pool"
Expand All @@ -52,13 +56,14 @@ type service struct {
}

type config struct {
Driver string `mapstructure:"driver"`
Drivers map[string]map[string]interface{} `mapstructure:"drivers"`
AppProviderURL string `mapstructure:"app_provider_url"`
GatewaySvc string `mapstructure:"gatewaysvc"`
MimeTypes []string `mapstructure:"mime_types"`
Priority uint64 `mapstructure:"priority"`
Language string `mapstructure:"language"`
Driver string `mapstructure:"driver"`
Drivers map[string]map[string]interface{} `mapstructure:"drivers"`
AppProviderURL string `mapstructure:"app_provider_url"`
GatewaySvc string `mapstructure:"gatewaysvc"`
MimeTypes []string `mapstructure:"mime_types" docs:"nil;A list of mime types supported by this app."`
CustomMimeTypesJSON string `mapstructure:"custom_mime_types_json" docs:"nil;An optional mapping file with the list of supported custom file extensions and corresponding mime types."`
Priority uint64 `mapstructure:"priority"`
Language string `mapstructure:"language"`
}

func (c *config) init() {
Expand All @@ -85,6 +90,12 @@ func New(m map[string]interface{}, ss *grpc.Server) (rgrpc.Service, error) {
return nil, err
}

// read and register custom mime types if configured
err = registerMimeTypes(c.CustomMimeTypesJSON)
if err != nil {
return nil, err
}

provider, err := getProvider(c)
if err != nil {
return nil, err
Expand All @@ -99,9 +110,31 @@ func New(m map[string]interface{}, ss *grpc.Server) (rgrpc.Service, error) {
return service, nil
}

func registerMimeTypes(mappingFile string) error {
// TODO(lopresti) this function also exists in the storage provider, to be seen if we want to factor it out, though a
// fileext <-> mimetype "service" would have to be served by the gateway for it to be accessible both by storage providers and app providers.
if mappingFile != "" {
f, err := ioutil.ReadFile(mappingFile)
if err != nil {
return fmt.Errorf("appprovider: error reading the custom mime types file: +%v", err)
}
mimeTypes := map[string]string{}
err = json.Unmarshal(f, &mimeTypes)
if err != nil {
return fmt.Errorf("appprovider: error unmarshalling the custom mime types file: +%v", err)
}
// register all mime types that were read
for e, m := range mimeTypes {
mime.RegisterMime(e, m)
}
}
return nil
}

func (s *service) registerProvider() {
// Give the appregistry service time to come up
time.Sleep(2 * time.Second)
// TODO(lopresti) we should register the appproviders after all other microservices
time.Sleep(3 * time.Second)

ctx := context.Background()
log := logger.New().With().Int("pid", os.Getpid()).Logger()
Expand All @@ -119,6 +152,7 @@ func (s *service) registerProvider() {
mimeTypes = append(mimeTypes, m.(string))
}
pInfo.MimeTypes = mimeTypes
log.Info().Str("appprovider", s.conf.AppProviderURL).Interface("mimetypes", mimeTypes).Msg("appprovider supported mimetypes")
}

client, err := pool.GetGatewayServiceClient(pool.Endpoint(s.conf.GatewaySvc))
Expand Down Expand Up @@ -165,7 +199,10 @@ func (s *service) Register(ss *grpc.Server) {

func getProvider(c *config) (app.Provider, error) {
if f, ok := registry.NewFuncs[c.Driver]; ok {
return f(c.Drivers[c.Driver])
driverConf := c.Drivers[c.Driver]
// share the mime_types config entry to the drivers
driverConf["mime_types"] = c.MimeTypes
return f(driverConf)
}
return nil, errtypes.NotFound("driver not found: " + c.Driver)
}
Expand All @@ -183,5 +220,4 @@ func (s *service) OpenInApp(ctx context.Context, req *providerpb.OpenInAppReques
AppUrl: appURL,
}
return res, nil

}
7 changes: 4 additions & 3 deletions internal/http/services/appprovider/appprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,9 @@ func (s *svc) handleOpen(w http.ResponseWriter, r *http.Request) {
return
}

viewMode := getViewMode(statRes.Info, r.Form.Get("view_mode"))
viewMode := resolveViewMode(statRes.Info, r.Form.Get("view_mode"))
if viewMode == gateway.OpenInAppRequest_VIEW_MODE_INVALID {
writeError(w, r, appErrorInvalidParameter, "invalid view mode", err)
writeError(w, r, appErrorUnauthenticated, "permission denied when accessing the file", err)
return
}

Expand Down Expand Up @@ -436,7 +436,7 @@ func filterAppsByUserAgent(mimeTypes []*appregistry.MimeTypeInfo, userAgent stri
return res
}

func getViewMode(res *provider.ResourceInfo, vm string) gateway.OpenInAppRequest_ViewMode {
func resolveViewMode(res *provider.ResourceInfo, vm string) gateway.OpenInAppRequest_ViewMode {
if vm != "" {
return utils.GetViewMode(vm)
}
Expand All @@ -451,6 +451,7 @@ func getViewMode(res *provider.ResourceInfo, vm string) gateway.OpenInAppRequest
case canView:
viewMode = gateway.OpenInAppRequest_VIEW_MODE_READ_ONLY
default:
// no permissions, will return access denied
viewMode = gateway.OpenInAppRequest_VIEW_MODE_INVALID
}
return viewMode
Expand Down
Loading

0 comments on commit a2b2109

Please sign in to comment.