-
Notifications
You must be signed in to change notification settings - Fork 112
/
dataprovider.go
219 lines (187 loc) · 6.07 KB
/
dataprovider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// 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 dataprovider
import (
"bytes"
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"net/http"
"os"
"github.com/cs3org/reva/v2/pkg/appctx"
"github.com/cs3org/reva/v2/pkg/events"
"github.com/cs3org/reva/v2/pkg/events/stream"
datatxregistry "github.com/cs3org/reva/v2/pkg/rhttp/datatx/manager/registry"
"github.com/cs3org/reva/v2/pkg/rhttp/global"
"github.com/cs3org/reva/v2/pkg/rhttp/router"
"github.com/cs3org/reva/v2/pkg/storage"
"github.com/cs3org/reva/v2/pkg/storage/fs/registry"
"github.com/go-micro/plugins/v4/events/natsjs"
"github.com/mitchellh/mapstructure"
"github.com/rs/zerolog"
)
func init() {
global.Register("dataprovider", New)
}
type config struct {
Prefix string `mapstructure:"prefix" docs:"data;The prefix to be used for this HTTP service"`
Driver string `mapstructure:"driver" docs:"localhome;The storage driver to be used."`
Drivers map[string]map[string]interface{} `mapstructure:"drivers" docs:"url:pkg/storage/fs/localhome/localhome.go;The configuration for the storage driver"`
DataTXs map[string]map[string]interface{} `mapstructure:"data_txs" docs:"url:pkg/rhttp/datatx/manager/simple/simple.go;The configuration for the data tx protocols"`
NatsAddress string `mapstructure:"nats_address"`
NatsClusterID string `mapstructure:"nats_clusterID"`
NatsTLSInsecure bool `mapstructure:"nats_tls_insecure"`
NatsRootCACertPath string `mapstructure:"nats_root_ca_cert_path"`
NatsEnableTLS bool `mapstructure:"nats_enable_tls"`
}
func (c *config) init() {
if c.Prefix == "" {
c.Prefix = "data"
}
if c.Driver == "" {
c.Driver = "localhome"
}
}
type svc struct {
conf *config
handler http.Handler
storage storage.FS
dataTXs map[string]http.Handler
}
// New returns a new datasvc
func New(m map[string]interface{}, log *zerolog.Logger) (global.Service, error) {
conf := &config{}
if err := mapstructure.Decode(m, conf); err != nil {
return nil, err
}
conf.init()
var evstream events.Stream
if conf.NatsAddress == "" || conf.NatsClusterID == "" {
log.Warn().Msg("missing or incomplete nats configuration. Events will not be published.")
} else {
var tlsConf *tls.Config
if conf.NatsEnableTLS {
var rootCAPool *x509.CertPool
if conf.NatsRootCACertPath != "" {
f, err := os.Open(conf.NatsRootCACertPath)
if err != nil {
return nil, err
}
var certBytes bytes.Buffer
if _, err := io.Copy(&certBytes, f); err != nil {
return nil, err
}
rootCAPool = x509.NewCertPool()
rootCAPool.AppendCertsFromPEM(certBytes.Bytes())
conf.NatsTLSInsecure = false
}
tlsConf = &tls.Config{
InsecureSkipVerify: conf.NatsTLSInsecure,
RootCAs: rootCAPool,
}
}
s, err := stream.Nats(natsjs.TLSConfig(tlsConf), natsjs.Address(conf.NatsAddress), natsjs.ClusterID(conf.NatsClusterID))
if err != nil {
return nil, err
}
evstream = s
}
fs, err := getFS(conf, evstream)
if err != nil {
return nil, err
}
dataTXs, err := getDataTXs(conf, fs, evstream)
if err != nil {
return nil, err
}
s := &svc{
storage: fs,
conf: conf,
dataTXs: dataTXs,
}
err = s.setHandler()
return s, err
}
func getFS(c *config, stream events.Stream) (storage.FS, error) {
if f, ok := registry.NewFuncs[c.Driver]; ok {
return f(c.Drivers[c.Driver], stream)
}
return nil, fmt.Errorf("driver not found: %s", c.Driver)
}
func getDataTXs(c *config, fs storage.FS, publisher events.Publisher) (map[string]http.Handler, error) {
if c.DataTXs == nil {
c.DataTXs = make(map[string]map[string]interface{})
}
if len(c.DataTXs) == 0 {
c.DataTXs["simple"] = make(map[string]interface{})
c.DataTXs["simple"]["cache_store"] = "noop"
c.DataTXs["simple"]["cache_database"] = "reva"
c.DataTXs["spaces"] = make(map[string]interface{})
c.DataTXs["spaces"]["cache_store"] = "noop"
c.DataTXs["spaces"]["cache_database"] = "reva"
c.DataTXs["tus"] = make(map[string]interface{})
c.DataTXs["tus"]["cache_store"] = "noop"
c.DataTXs["tus"]["cache_database"] = "reva"
}
txs := make(map[string]http.Handler)
for t := range c.DataTXs {
if f, ok := datatxregistry.NewFuncs[t]; ok {
if tx, err := f(c.DataTXs[t], publisher); err == nil {
if handler, err := tx.Handler(fs); err == nil {
txs[t] = handler
}
}
}
}
return txs, nil
}
func (s *svc) Close() error {
return nil
}
func (s *svc) Unprotected() []string {
return []string{
"/tus",
}
}
func (s *svc) Prefix() string {
return s.conf.Prefix
}
func (s *svc) Handler() http.Handler {
return s.handler
}
func (s *svc) setHandler() error {
s.handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log := appctx.GetLogger(r.Context())
log.Debug().Msgf("dataprovider routing: path=%s", r.URL.Path)
head, tail := router.ShiftPath(r.URL.Path)
if handler, ok := s.dataTXs[head]; ok {
r.URL.Path = tail
handler.ServeHTTP(w, r)
return
}
// If we don't find a prefix match for any of the protocols, upload the resource
// through the direct HTTP protocol
if handler, ok := s.dataTXs["simple"]; ok {
handler.ServeHTTP(w, r)
return
}
w.WriteHeader(http.StatusInternalServerError)
})
return nil
}