Skip to content

Commit 65d8f9a

Browse files
authored
CERNBox REST driver for groupprovider service (#1434)
1 parent 8160a93 commit 65d8f9a

File tree

5 files changed

+657
-11
lines changed

5 files changed

+657
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Enhancement: CERNBox REST driver for groupprovider service
2+
3+
https://github.com/cs3org/reva/pull/1434

pkg/cbox/group/rest/cache.go

+173
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
// Copyright 2018-2021 CERN
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
// In applying this license, CERN does not waive the privileges and immunities
16+
// granted to it by virtue of its status as an Intergovernmental Organization
17+
// or submit itself to any jurisdiction.
18+
19+
package rest
20+
21+
import (
22+
"encoding/json"
23+
"errors"
24+
"strconv"
25+
"time"
26+
27+
grouppb "github.com/cs3org/go-cs3apis/cs3/identity/group/v1beta1"
28+
userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
29+
"github.com/gomodule/redigo/redis"
30+
)
31+
32+
const (
33+
groupPrefix = "group:"
34+
groupMembersPrefix = "members:"
35+
groupInternalIDPrefix = "internal:"
36+
)
37+
38+
func initRedisPool(address, username, password string) *redis.Pool {
39+
return &redis.Pool{
40+
41+
MaxIdle: 50,
42+
MaxActive: 1000,
43+
IdleTimeout: 240 * time.Second,
44+
45+
Dial: func() (redis.Conn, error) {
46+
var c redis.Conn
47+
var err error
48+
switch {
49+
case username != "":
50+
c, err = redis.Dial("tcp", address,
51+
redis.DialUsername(username),
52+
redis.DialPassword(password),
53+
)
54+
case password != "":
55+
c, err = redis.Dial("tcp", address,
56+
redis.DialPassword(password),
57+
)
58+
default:
59+
c, err = redis.Dial("tcp", address)
60+
}
61+
62+
if err != nil {
63+
return nil, err
64+
}
65+
return c, err
66+
},
67+
68+
TestOnBorrow: func(c redis.Conn, t time.Time) error {
69+
_, err := c.Do("PING")
70+
return err
71+
},
72+
}
73+
}
74+
75+
func (m *manager) setVal(key, val string, expiration int) error {
76+
conn := m.redisPool.Get()
77+
defer conn.Close()
78+
if conn != nil {
79+
if expiration != -1 {
80+
if _, err := conn.Do("SET", key, val, "EX", expiration); err != nil {
81+
return err
82+
}
83+
} else {
84+
if _, err := conn.Do("SET", key, val); err != nil {
85+
return err
86+
}
87+
}
88+
return nil
89+
}
90+
return errors.New("rest: unable to get connection from redis pool")
91+
}
92+
93+
func (m *manager) getVal(key string) (string, error) {
94+
conn := m.redisPool.Get()
95+
defer conn.Close()
96+
if conn != nil {
97+
val, err := redis.String(conn.Do("GET", key))
98+
if err != nil {
99+
return "", err
100+
}
101+
return val, nil
102+
}
103+
return "", errors.New("rest: unable to get connection from redis pool")
104+
}
105+
106+
func (m *manager) fetchCachedInternalID(gid *grouppb.GroupId) (string, error) {
107+
return m.getVal(groupPrefix + groupInternalIDPrefix + gid.OpaqueId)
108+
}
109+
110+
func (m *manager) cacheInternalID(gid *grouppb.GroupId, internalID string) error {
111+
return m.setVal(groupPrefix+groupInternalIDPrefix+gid.OpaqueId, internalID, -1)
112+
}
113+
114+
func (m *manager) fetchCachedGroupDetails(gid *grouppb.GroupId) (*grouppb.Group, error) {
115+
group, err := m.getVal(groupPrefix + gid.OpaqueId)
116+
if err != nil {
117+
return nil, err
118+
}
119+
120+
g := grouppb.Group{}
121+
if err = json.Unmarshal([]byte(group), &g); err != nil {
122+
return nil, err
123+
}
124+
return &g, nil
125+
}
126+
127+
func (m *manager) cacheGroupDetails(g *grouppb.Group) error {
128+
encodedGroup, err := json.Marshal(&g)
129+
if err != nil {
130+
return err
131+
}
132+
if err = m.setVal(groupPrefix+g.Id.OpaqueId, string(encodedGroup), -1); err != nil {
133+
return err
134+
}
135+
136+
if err = m.setVal(groupPrefix+"gid_number:"+strconv.FormatInt(g.GidNumber, 10), g.Id.OpaqueId, -1); err != nil {
137+
return err
138+
}
139+
if err = m.setVal(groupPrefix+"mail:"+g.Mail, g.Id.OpaqueId, -1); err != nil {
140+
return err
141+
}
142+
if err = m.setVal(groupPrefix+"group_name:"+g.GroupName, g.Id.OpaqueId, -1); err != nil {
143+
return err
144+
}
145+
return nil
146+
}
147+
148+
func (m *manager) fetchCachedParam(field, claim string) (string, error) {
149+
return m.getVal(groupPrefix + field + ":" + claim)
150+
}
151+
152+
func (m *manager) fetchCachedGroupMembers(gid *grouppb.GroupId) ([]*userpb.UserId, error) {
153+
members, err := m.getVal(groupPrefix + groupMembersPrefix + gid.OpaqueId)
154+
if err != nil {
155+
return nil, err
156+
}
157+
u := []*userpb.UserId{}
158+
if err = json.Unmarshal([]byte(members), &u); err != nil {
159+
return nil, err
160+
}
161+
return u, nil
162+
}
163+
164+
func (m *manager) cacheGroupMembers(gid *grouppb.GroupId, members []*userpb.UserId) error {
165+
u, err := json.Marshal(&members)
166+
if err != nil {
167+
return err
168+
}
169+
if err = m.setVal(groupPrefix+groupMembersPrefix+gid.OpaqueId, string(u), m.conf.GroupMembersCacheExpiration*60); err != nil {
170+
return err
171+
}
172+
return nil
173+
}

0 commit comments

Comments
 (0)