-
Notifications
You must be signed in to change notification settings - Fork 113
/
gateway.go
141 lines (119 loc) · 4.5 KB
/
gateway.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
// Copyright 2018-2020 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 gateway
import (
"fmt"
"net/url"
"strings"
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
"github.com/cs3org/reva/pkg/rgrpc"
"github.com/cs3org/reva/pkg/token"
"github.com/cs3org/reva/pkg/token/manager/registry"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"google.golang.org/grpc"
)
func init() {
rgrpc.Register("gateway", New)
}
type config struct {
AuthRegistryEndpoint string `mapstructure:"authregistrysvc"`
StorageRegistryEndpoint string `mapstructure:"storageregistrysvc"`
AppRegistryEndpoint string `mapstructure:"appregistrysvc"`
PreferencesEndpoint string `mapstructure:"preferencessvc"`
UserShareProviderEndpoint string `mapstructure:"usershareprovidersvc"`
PublicShareProviderEndpoint string `mapstructure:"publicshareprovidersvc"`
OCMShareProviderEndpoint string `mapstructure:"ocmshareprovidersvc"`
OCMInviteManagerEndpoint string `mapstructure:"ocminvitemanagersvc"`
OCMProviderAuthorizerEndpoint string `mapstructure:"ocmproviderauthorizersvc"`
OCMCoreEndpoint string `mapstructure:"ocmcoresvc"`
UserProviderEndpoint string `mapstructure:"userprovidersvc"`
CommitShareToStorageGrant bool `mapstructure:"commit_share_to_storage_grant"`
CommitShareToStorageRef bool `mapstructure:"commit_share_to_storage_ref"`
DisableHomeCreationOnLogin bool `mapstructure:"disable_home_creation_on_login"`
DataGatewayEndpoint string `mapstructure:"datagateway"`
TransferSharedSecret string `mapstructure:"transfer_shared_secret"`
TranserExpires int64 `mapstructure:"transfer_expires"`
TokenManager string `mapstructure:"token_manager"`
// ShareFolder is the location where to create shares in the recipient's storage provider.
ShareFolder string `mapstructure:"share_folder"`
TokenManagers map[string]map[string]interface{} `mapstructure:"token_managers"`
}
type svc struct {
c *config
dataGatewayURL url.URL
tokenmgr token.Manager
}
// New creates a new gateway svc that acts as a proxy for any grpc operation.
// The gateway is responsible for high-level controls: rate-limiting, coordination between svcs
// like sharing and storage acls, asynchronous transactions, ...
func New(m map[string]interface{}, ss *grpc.Server) (rgrpc.Service, error) {
c, err := parseConfig(m)
if err != nil {
return nil, err
}
// set defaults
if c.ShareFolder == "" {
c.ShareFolder = "MyShares"
}
c.ShareFolder = strings.Trim(c.ShareFolder, "/")
if c.TokenManager == "" {
c.TokenManager = "jwt"
}
// ensure DataGatewayEndpoint is a valid URI
if c.DataGatewayEndpoint == "" {
return nil, errors.New("datagateway is not defined")
}
u, err := url.Parse(c.DataGatewayEndpoint)
if err != nil {
return nil, err
}
tokenManager, err := getTokenManager(c.TokenManager, c.TokenManagers)
if err != nil {
return nil, err
}
s := &svc{
c: c,
dataGatewayURL: *u,
tokenmgr: tokenManager,
}
return s, nil
}
func (s *svc) Register(ss *grpc.Server) {
gateway.RegisterGatewayAPIServer(ss, s)
}
func (s *svc) Close() error {
return nil
}
func (s *svc) UnprotectedEndpoints() []string {
return []string{"/cs3.gateway.v1beta1.GatewayAPI"}
}
func parseConfig(m map[string]interface{}) (*config, error) {
c := &config{}
if err := mapstructure.Decode(m, c); err != nil {
err = errors.Wrap(err, "gateway: error decoding conf")
return nil, err
}
return c, nil
}
func getTokenManager(manager string, m map[string]map[string]interface{}) (token.Manager, error) {
if f, ok := registry.NewFuncs[manager]; ok {
return f(m[manager])
}
return nil, fmt.Errorf("driver %s not found for token manager", manager)
}