-
-
Notifications
You must be signed in to change notification settings - Fork 348
/
repository_memory.go
127 lines (103 loc) · 2.97 KB
/
repository_memory.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
/*
* Copyright © 2017-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
*
* 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.
*
* @author Aeneas Rekkas <aeneas+oss@aeneas.io>
* @copyright 2017-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
* @license Apache-2.0
*/
package rule
import (
"context"
"net/url"
"sync"
"github.com/pkg/errors"
"github.com/ory/x/viperx"
"github.com/ory/oathkeeper/helper"
"github.com/ory/oathkeeper/x"
"github.com/ory/x/pagination"
)
var _ Repository = new(RepositoryMemory)
type repositoryMemoryRegistry interface {
RuleValidator() Validator
x.RegistryLogger
}
type RepositoryMemory struct {
sync.RWMutex
rules []Rule
r repositoryMemoryRegistry
}
func NewRepositoryMemory(r repositoryMemoryRegistry) *RepositoryMemory {
return &RepositoryMemory{
r: r,
rules: make([]Rule, 0),
}
}
// WithRules sets rules without validation. For testing only.
func (m *RepositoryMemory) WithRules(rules []Rule) {
m.Lock()
m.rules = rules
m.Unlock()
}
func (m *RepositoryMemory) Count(ctx context.Context) (int, error) {
m.RLock()
defer m.RUnlock()
return len(m.rules), nil
}
func (m *RepositoryMemory) List(ctx context.Context, limit, offset int) ([]Rule, error) {
m.RLock()
defer m.RUnlock()
start, end := pagination.Index(limit, offset, len(m.rules))
return m.rules[start:end], nil
}
func (m *RepositoryMemory) Get(ctx context.Context, id string) (*Rule, error) {
m.RLock()
defer m.RUnlock()
for _, r := range m.rules {
if r.ID == id {
return &r, nil
}
}
return nil, errors.WithStack(helper.ErrResourceNotFound)
}
func (m *RepositoryMemory) Set(ctx context.Context, rules []Rule) error {
for _, check := range rules {
if err := m.r.RuleValidator().Validate(&check); err != nil {
viperx.LoggerWithValidationErrorFields(m.r.Logger(), err).WithError(err).
Errorf("A rule uses a malformed configuration and all URLs matching this rule will not work. You should resolve this issue now.")
}
}
m.Lock()
m.rules = rules
m.Unlock()
return nil
}
func (m *RepositoryMemory) Match(ctx context.Context, method string, u *url.URL) (*Rule, error) {
m.Lock()
defer m.Unlock()
var rules []Rule
for k := range m.rules {
r := &m.rules[k]
if err := r.IsMatching(method, u); err == nil {
rules = append(rules, *r)
}
m.rules[k] = *r
}
if len(rules) == 0 {
return nil, errors.WithStack(helper.ErrMatchesNoRule)
} else if len(rules) != 1 {
return nil, errors.WithStack(helper.ErrMatchesMoreThanOneRule)
}
return &rules[0], nil
}