-
Notifications
You must be signed in to change notification settings - Fork 362
/
Copy pathrepository_memory.go
157 lines (128 loc) · 3.68 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
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
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package rule
import (
"context"
"net/http"
"net/url"
"sync"
"github.com/pkg/errors"
"github.com/ory/oathkeeper/driver/configuration"
"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
invalidRules []Rule
matchingStrategy configuration.MatchingStrategy
r repositoryMemoryRegistry
}
// MatchingStrategy returns current MatchingStrategy.
func (m *RepositoryMemory) MatchingStrategy(_ context.Context) (configuration.MatchingStrategy, error) {
m.RLock()
defer m.RUnlock()
return m.matchingStrategy, nil
}
// SetMatchingStrategy updates MatchingStrategy.
func (m *RepositoryMemory) SetMatchingStrategy(_ context.Context, ms configuration.MatchingStrategy) error {
m.Lock()
defer m.Unlock()
m.matchingStrategy = ms
return nil
}
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 {
m.Lock()
defer m.Unlock()
m.rules = make([]Rule, 0, len(rules))
m.invalidRules = make([]Rule, 0)
for _, check := range rules {
if err := m.r.RuleValidator().Validate(&check); err != nil {
m.r.Logger().WithError(err).WithField("rule_id", check.ID).
Errorf("A Rule uses a malformed configuration and all URLs matching this rule will not work. You should resolve this issue now.")
m.invalidRules = append(m.invalidRules, check)
} else {
m.rules = append(m.rules, check)
}
}
return nil
}
func (m *RepositoryMemory) Match(ctx context.Context, method string, u *url.URL, headers http.Header, protocol Protocol) (*Rule, error) {
if u == nil {
return nil, errors.WithStack(errors.New("nil URL provided"))
}
m.Lock()
defer m.Unlock()
var rules []*Rule
for k := range m.rules {
r := &m.rules[k]
if matched, err := r.IsMatching(m.matchingStrategy, method, u, headers, protocol); err != nil {
return nil, errors.WithStack(err)
} else if matched {
rules = append(rules, r)
}
}
for k := range m.invalidRules {
r := &m.invalidRules[k]
if matched, err := r.IsMatching(m.matchingStrategy, method, u, headers, protocol); err != nil {
return nil, errors.WithStack(err)
} else if matched {
rules = append(rules, 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
}
func (m *RepositoryMemory) ReadyChecker(r *http.Request) error {
c, err := m.Count(r.Context())
if err != nil {
return err
}
if c == 0 {
return errors.WithStack(helper.ErrResourceNotFound.WithReason("No rules found."))
}
return nil
}