-
Notifications
You must be signed in to change notification settings - Fork 112
/
utils.go
473 lines (415 loc) · 14.4 KB
/
utils.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// 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 utils
import (
"encoding/json"
"errors"
"math/rand"
"net"
"net/http"
"net/url"
"os"
"os/user"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
grouppb "github.com/cs3org/go-cs3apis/cs3/identity/group/v1beta1"
userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
types "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
"github.com/cs3org/reva/v2/pkg/registry"
"github.com/cs3org/reva/v2/pkg/registry/memory"
"github.com/golang/protobuf/proto"
"google.golang.org/protobuf/encoding/protojson"
)
var (
matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
matchEmail = regexp.MustCompile(`^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$`)
// GlobalRegistry configures a service registry globally accessible. It defaults to a memory registry. The usage of
// globals is not encouraged, and this is a workaround until the PR is out of a draft state.
GlobalRegistry registry.Registry = memory.New(map[string]interface{}{})
// ShareStorageProviderID is the provider id used by the sharestorageprovider
ShareStorageProviderID = "a0ca6a90-a365-4782-871e-d44447bbc668"
// ShareStorageSpaceID is the space id used by the sharestorageprovider share jail space
ShareStorageSpaceID = "a0ca6a90-a365-4782-871e-d44447bbc668"
// PublicStorageProviderID is the storage id used by the sharestorageprovider
PublicStorageProviderID = "7993447f-687f-490d-875c-ac95e89a62a4"
// PublicStorageSpaceID is the space id used by the sharestorageprovider
PublicStorageSpaceID = "7993447f-687f-490d-875c-ac95e89a62a4"
// SpaceGrant is used to signal the storageprovider that the grant is on a space
SpaceGrant struct{}
)
// Skip evaluates whether a source endpoint contains any of the prefixes.
// i.e: /a/b/c/d/e contains prefix /a/b/c
func Skip(source string, prefixes []string) bool {
for i := range prefixes {
if strings.HasPrefix(source, prefixes[i]) {
return true
}
}
return false
}
// GetClientIP retrieves the client IP from incoming requests
func GetClientIP(r *http.Request) (string, error) {
var clientIP string
forwarded := r.Header.Get("X-FORWARDED-FOR")
if forwarded != "" {
clientIP = forwarded
} else {
if ip, _, err := net.SplitHostPort(r.RemoteAddr); err != nil {
ipObj := net.ParseIP(r.RemoteAddr)
if ipObj == nil {
return "", err
}
clientIP = ipObj.String()
} else {
clientIP = ip
}
}
return clientIP, nil
}
// ToSnakeCase converts a CamelCase string to a snake_case string.
func ToSnakeCase(str string) string {
snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}")
snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}")
return strings.ToLower(snake)
}
// ResolvePath converts relative local paths to absolute paths
func ResolvePath(path string) (string, error) {
usr, err := user.Current()
if err != nil {
return "", err
}
homeDir := usr.HomeDir
if path == "~" {
path = homeDir
} else if strings.HasPrefix(path, "~/") {
path = filepath.Join(homeDir, path[2:])
}
return filepath.Abs(path)
}
// RandString is a helper to create tokens.
func RandString(n int) string {
rand.Seed(time.Now().UTC().UnixNano())
var l = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
b := make([]rune, n)
for i := range b {
b[i] = l[rand.Intn(len(l))]
}
return string(b)
}
// TSToUnixNano converts a protobuf Timestamp to uint64
// with nanoseconds resolution.
func TSToUnixNano(ts *types.Timestamp) uint64 {
if ts == nil {
return 0
}
return uint64(time.Unix(int64(ts.Seconds), int64(ts.Nanos)).UnixNano())
}
// TSToTime converts a protobuf Timestamp to Go's time.Time.
func TSToTime(ts *types.Timestamp) time.Time {
return time.Unix(int64(ts.Seconds), int64(ts.Nanos))
}
// TimeToTS converts Go's time.Time to a protobuf Timestamp.
func TimeToTS(t time.Time) *types.Timestamp {
return &types.Timestamp{
Seconds: uint64(t.Unix()), // implicitly returns UTC
Nanos: uint32(t.Nanosecond()),
}
}
// LaterTS returns the timestamp which occurs later.
func LaterTS(t1 *types.Timestamp, t2 *types.Timestamp) *types.Timestamp {
if TSToUnixNano(t1) > TSToUnixNano(t2) {
return t1
}
return t2
}
// TSNow returns the current UTC timestamp
func TSNow() *types.Timestamp {
t := time.Now().UTC()
return &types.Timestamp{
Seconds: uint64(t.Unix()),
Nanos: uint32(t.Nanosecond()),
}
}
// MTimeToTS converts a string in the form "<unix>.<nanoseconds>" into a CS3 Timestamp
func MTimeToTS(v string) (ts types.Timestamp, err error) {
p := strings.SplitN(v, ".", 2)
var sec, nsec uint64
if sec, err = strconv.ParseUint(p[0], 10, 64); err == nil {
if len(p) > 1 {
nsec, err = strconv.ParseUint(p[1], 10, 32)
}
}
return types.Timestamp{Seconds: sec, Nanos: uint32(nsec)}, err
}
// ExtractGranteeID returns the ID, user or group, set in the GranteeId object
func ExtractGranteeID(grantee *provider.Grantee) (*userpb.UserId, *grouppb.GroupId) {
switch t := grantee.Id.(type) {
case *provider.Grantee_UserId:
return t.UserId, nil
case *provider.Grantee_GroupId:
return nil, t.GroupId
default:
return nil, nil
}
}
// UserEqual returns whether two users have the same field values.
func UserEqual(u, v *userpb.UserId) bool {
return u != nil && v != nil && u.Idp == v.Idp && u.OpaqueId == v.OpaqueId
}
// UserIDEqual returns whether two users have the same opaqueid values. The idp is ignored
func UserIDEqual(u, v *userpb.UserId) bool {
return u != nil && v != nil && u.OpaqueId == v.OpaqueId
}
// GroupEqual returns whether two groups have the same field values.
func GroupEqual(u, v *grouppb.GroupId) bool {
return u != nil && v != nil && u.Idp == v.Idp && u.OpaqueId == v.OpaqueId
}
// ResourceIDEqual returns whether two resources have the same field values.
func ResourceIDEqual(u, v *provider.ResourceId) bool {
return u != nil && v != nil && u.StorageId == v.StorageId && u.OpaqueId == v.OpaqueId && u.SpaceId == v.SpaceId
}
// ResourceEqual returns whether two resources have the same field values.
func ResourceEqual(u, v *provider.Reference) bool {
return u != nil && v != nil && u.Path == v.Path && ((u.ResourceId == nil && v.ResourceId == nil) || (ResourceIDEqual(u.ResourceId, v.ResourceId)))
}
// GranteeEqual returns whether two grantees have the same field values.
func GranteeEqual(u, v *provider.Grantee) bool {
if u == nil || v == nil {
return false
}
uu, ug := ExtractGranteeID(u)
vu, vg := ExtractGranteeID(v)
return u.Type == v.Type && (UserEqual(uu, vu) || GroupEqual(ug, vg))
}
// IsEmailValid checks whether the provided email has a valid format.
func IsEmailValid(e string) bool {
if len(e) < 3 || len(e) > 254 {
return false
}
return matchEmail.MatchString(e)
}
// IsValidWebAddress checks whether the provided address is a valid URL.
func IsValidWebAddress(address string) bool {
_, err := url.ParseRequestURI(address)
return err == nil
}
// IsValidPhoneNumber checks whether the provided phone number has a valid format.
func IsValidPhoneNumber(number string) bool {
re := regexp.MustCompile(`^(?:(?:\(?(?:00|\+)([1-4]\d\d|[1-9]\d?)\)?)?[\-\.\ \\\/]?)?((?:\(?\d{1,}\)?[\-\.\ \\\/]?){0,})(?:[\-\.\ \\\/]?(?:#|ext\.?|extension|x)[\-\.\ \\\/]?(\d+))?$`)
return re.MatchString(number)
}
// IsValidName cheks if the given name doesn't contain any non-alpha, space or dash characters.
func IsValidName(name string) bool {
re := regexp.MustCompile(`^[A-Za-z\s\-]*$`)
return re.MatchString(name)
}
// MarshalProtoV1ToJSON marshals a proto V1 message to a JSON byte array
// TODO: update this once we start using V2 in CS3APIs
func MarshalProtoV1ToJSON(m proto.Message) ([]byte, error) {
mV2 := proto.MessageV2(m)
return protojson.Marshal(mV2)
}
// UnmarshalJSONToProtoV1 decodes a JSON byte array to a specified proto message type
// TODO: update this once we start using V2 in CS3APIs
func UnmarshalJSONToProtoV1(b []byte, m proto.Message) error {
mV2 := proto.MessageV2(m)
if err := protojson.Unmarshal(b, mV2); err != nil {
return err
}
return nil
}
// IsRelativeReference returns true if the given reference qualifies as relative
// when the resource id is set and the path starts with a .
//
// TODO(corby): Currently if the path begins with a dot, the ResourceId is set but has empty storageId and OpaqueId
// then the reference is still being viewed as relative. We need to check if we want that because in some
// places we might not want to set both StorageId and OpaqueId so we can't do a hard check if they are set.
func IsRelativeReference(ref *provider.Reference) bool {
return ref.ResourceId != nil && strings.HasPrefix(ref.Path, ".")
}
// IsAbsoluteReference returns true if the given reference qualifies as absolute
// when either only the resource id is set or only the path is set and starts with /
//
// TODO(corby): Currently if the path is empty, the ResourceId is set but has empty storageId and OpaqueId
// then the reference is still being viewed as absolute. We need to check if we want that because in some
// places we might not want to set both StorageId and OpaqueId so we can't do a hard check if they are set.
func IsAbsoluteReference(ref *provider.Reference) bool {
return (ref.ResourceId != nil && ref.Path == "") || (ref.ResourceId == nil) && strings.HasPrefix(ref.Path, "/")
}
// IsAbsolutePathReference returns true if the given reference qualifies as a global path
// when only the path is set and starts with /
func IsAbsolutePathReference(ref *provider.Reference) bool {
return ref.ResourceId == nil && strings.HasPrefix(ref.Path, "/")
}
// MakeRelativePath prefixes the path with a . to use it in a relative reference
func MakeRelativePath(p string) string {
p = path.Join("/", p)
if p == "/" {
return "."
}
return "." + p
}
// UserTypeMap translates account type string to CS3 UserType
func UserTypeMap(accountType string) userpb.UserType {
var t userpb.UserType
switch accountType {
case "primary":
t = userpb.UserType_USER_TYPE_PRIMARY
case "secondary":
t = userpb.UserType_USER_TYPE_SECONDARY
case "service":
t = userpb.UserType_USER_TYPE_SERVICE
case "application":
t = userpb.UserType_USER_TYPE_APPLICATION
case "guest":
t = userpb.UserType_USER_TYPE_GUEST
case "federated":
t = userpb.UserType_USER_TYPE_FEDERATED
case "lightweight":
t = userpb.UserType_USER_TYPE_LIGHTWEIGHT
// FIXME new user type
case "spaceowner":
t = 8
}
return t
}
// UserTypeToString translates CS3 UserType to user-readable string
func UserTypeToString(accountType userpb.UserType) string {
var t string
switch accountType {
case userpb.UserType_USER_TYPE_PRIMARY:
t = "primary"
case userpb.UserType_USER_TYPE_SECONDARY:
t = "secondary"
case userpb.UserType_USER_TYPE_SERVICE:
t = "service"
case userpb.UserType_USER_TYPE_APPLICATION:
t = "application"
case userpb.UserType_USER_TYPE_GUEST:
t = "guest"
case userpb.UserType_USER_TYPE_FEDERATED:
t = "federated"
case userpb.UserType_USER_TYPE_LIGHTWEIGHT:
t = "lightweight"
// FIXME new user type
case 8:
t = "spaceowner"
}
return t
}
// GetViewMode converts a human-readable string to a view mode for opening a resource in an app.
func GetViewMode(viewMode string) gateway.OpenInAppRequest_ViewMode {
switch viewMode {
case "view":
return gateway.OpenInAppRequest_VIEW_MODE_VIEW_ONLY
case "read":
return gateway.OpenInAppRequest_VIEW_MODE_READ_ONLY
case "write":
return gateway.OpenInAppRequest_VIEW_MODE_READ_WRITE
default:
return gateway.OpenInAppRequest_VIEW_MODE_INVALID
}
}
// AppendPlainToOpaque adds a new key value pair as a plain string on the given opaque and returns it
func AppendPlainToOpaque(o *types.Opaque, key, value string) *types.Opaque {
o = ensureOpaque(o)
o.Map[key] = &types.OpaqueEntry{
Decoder: "plain",
Value: []byte(value),
}
return o
}
// AppendJSONToOpaque adds a new key value pair as a json on the given opaque and returns it. Ignores errors
func AppendJSONToOpaque(o *types.Opaque, key string, value interface{}) *types.Opaque {
o = ensureOpaque(o)
b, _ := json.Marshal(value)
o.Map[key] = &types.OpaqueEntry{
Decoder: "json",
Value: b,
}
return o
}
// ReadPlainFromOpaque reads a plain string from the given opaque map
func ReadPlainFromOpaque(o *types.Opaque, key string) string {
if o.GetMap() == nil {
return ""
}
if e, ok := o.Map[key]; ok && e.Decoder == "plain" {
return string(e.Value)
}
return ""
}
// ReadJSONFromOpaque reads and unmarshals a value from the opaque in the given interface{} (Make sure it's a pointer!)
func ReadJSONFromOpaque(o *types.Opaque, key string, valptr interface{}) error {
if o.GetMap() == nil {
return errors.New("not found")
}
e, ok := o.Map[key]
if !ok || e.Decoder != "json" {
return errors.New("not found")
}
return json.Unmarshal(e.Value, valptr)
}
// ExistsInOpaque returns true if the key exists in the opaque (ignoring the value)
func ExistsInOpaque(o *types.Opaque, key string) bool {
if o.GetMap() == nil {
return false
}
_, ok := o.Map[key]
return ok
}
// MergeOpaques will merge the opaques. If a key exists in both opaques
// the values from the first opaque will be taken
func MergeOpaques(o *types.Opaque, p *types.Opaque) *types.Opaque {
p = ensureOpaque(p)
for k, v := range o.GetMap() {
p.Map[k] = v
}
return p
}
// ensures the opaque is initialized
func ensureOpaque(o *types.Opaque) *types.Opaque {
if o == nil {
o = &types.Opaque{}
}
if o.Map == nil {
o.Map = map[string]*types.OpaqueEntry{}
}
return o
}
// RemoveItem removes the given item, its children and all empty parent folders
func RemoveItem(path string) error {
if err := os.RemoveAll(path); err != nil {
return err
}
for {
path = filepath.Dir(path)
if err := os.Remove(path); err != nil {
// remove will fail when the dir is not empty.
// We can exit in that case
return nil
}
}
}