/
manager_redis.go
111 lines (89 loc) · 2.5 KB
/
manager_redis.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
package ladon
import (
"encoding/json"
"github.com/pkg/errors"
"github.com/go-redis/redis"
)
// RedisManager is a redis implementation of Manager to store policies persistently.
type RedisManager struct {
db *redis.Client
keyPrefix string
}
// NewRedisManager initializes a new RedisManager with no policies
func NewRedisManager(db *redis.Client, keyPrefix string) *RedisManager {
return &RedisManager{
db: db,
keyPrefix: keyPrefix,
}
}
const redisPolicies = "ladon:policies"
var (
redisPolicyExists = errors.New("Policy exists")
redisNotFound = errors.New("Not found")
)
func (m *RedisManager) redisPoliciesKey() string {
return m.keyPrefix + redisPolicies
}
// Create a new policy to RedisManager
func (m *RedisManager) Create(policy Policy) error {
payload, err := json.Marshal(policy)
if err != nil {
return errors.Wrap(err, "policy marshal failed")
}
wasKeySet, err := m.db.HSetNX(m.redisPoliciesKey(), policy.GetID(), string(payload)).Result()
if !wasKeySet {
return errors.WithStack(redisPolicyExists)
} else if err != nil {
return errors.Wrap(err, "policy creation failed")
}
return nil
}
// Get retrieves a policy.
func (m *RedisManager) Get(id string) (Policy, error) {
resp, err := m.db.HGet(m.redisPoliciesKey(), id).Bytes()
if err == redis.Nil {
return nil, redisNotFound
} else if err != nil {
return nil, errors.WithStack(err)
}
return redisUnmarshalPolicy(resp)
}
// Delete removes a policy.
func (m *RedisManager) Delete(id string) error {
if err := m.db.HDel(m.redisPoliciesKey(), id).Err(); err != nil {
return errors.Wrap(err, "policy deletion failed")
}
return nil
}
// FindPoliciesForSubject finds all policies associated with the subject.
func (m *RedisManager) FindPoliciesForSubject(subject string) (Policies, error) {
var ps Policies
iter := m.db.HScan(m.redisPoliciesKey(), 0, "", 0).Iterator()
for iter.Next() {
if !iter.Next() {
break
}
resp := []byte(iter.Val())
p, err := redisUnmarshalPolicy(resp)
if err != nil {
return nil, err
}
if ok, err := Match(p, p.GetSubjects(), subject); err != nil {
return nil, errors.Wrap(err, "policy subject match failed")
} else if !ok {
continue
}
ps = append(ps, p)
}
if err := iter.Err(); err != nil {
return nil, errors.WithStack(err)
}
return ps, nil
}
func redisUnmarshalPolicy(policy []byte) (Policy, error) {
var p *DefaultPolicy
if err := json.Unmarshal(policy, &p); err != nil {
return nil, errors.Wrap(err, "policy unmarshal failed")
}
return p, nil
}