-
-
Notifications
You must be signed in to change notification settings - Fork 347
/
provider.go
196 lines (159 loc) · 4.17 KB
/
provider.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
package config
import (
"context"
_ "embed"
"encoding/json"
"fmt"
"sync"
"github.com/ory/x/watcherx"
"github.com/ory/keto/internal/namespace"
_ "github.com/ory/jsonschema/v3/httploader"
"github.com/ory/x/configx"
"github.com/pkg/errors"
"github.com/rs/cors"
"github.com/spf13/pflag"
"github.com/ory/x/logrusx"
"github.com/ory/x/tracing"
)
//go:embed config.schema.json
var Schema []byte
const (
KeyDSN = "dsn"
KeyReadAPIHost = "serve.read.host"
KeyReadAPIPort = "serve.read.port"
KeyWriteAPIHost = "serve.write.host"
KeyWriteAPIPort = "serve.write.port"
KeyNamespaces = "namespaces"
DSNMemory = "sqlite://file::memory:?_fk=true&cache=shared"
)
type (
Provider struct {
p *configx.Provider
l *logrusx.Logger
ctx context.Context
nm namespace.Manager
cancelNamespaceManager context.CancelFunc
nmLock sync.RWMutex
}
)
func New(ctx context.Context, flags *pflag.FlagSet, l *logrusx.Logger) (*Provider, error) {
kp := &Provider{
l: l,
ctx: ctx,
}
var err error
kp.p, err = configx.New(
Schema,
configx.WithFlags(flags),
configx.WithStderrValidationReporter(),
configx.WithImmutables(KeyDSN, "serve"),
configx.OmitKeysFromTracing(KeyDSN),
configx.WithLogrusWatcher(kp.l),
configx.WithContext(ctx),
configx.AttachWatcher(func(watcherx.Event, error) {
// TODO this can be optimized to run only on changes related to namespace config
kp.resetNamespaceManager()
}),
)
if err != nil {
return nil, err
}
l.UseConfig(kp.p)
return kp, nil
}
func (k *Provider) Source() *configx.Provider {
return k.p
}
func (k *Provider) resetNamespaceManager() {
k.nmLock.Lock()
defer k.nmLock.Unlock()
if k.nm == nil {
return
}
// cancel and delete old namespace manager
// the next read request will result in a new one being created
k.cancelNamespaceManager()
k.nm = nil
}
func (k *Provider) Set(key string, v interface{}) error {
if err := k.p.Set(key, v); err != nil {
return err
}
if key == KeyNamespaces {
k.resetNamespaceManager()
}
return nil
}
func (k *Provider) ReadAPIListenOn() string {
return fmt.Sprintf(
"%s:%d",
k.p.StringF(KeyReadAPIHost, ""),
k.p.IntF(KeyReadAPIPort, 4466),
)
}
func (k *Provider) WriteAPIListenOn() string {
return fmt.Sprintf(
"%s:%d",
k.p.StringF(KeyWriteAPIHost, ""),
k.p.IntF(KeyWriteAPIPort, 4467),
)
}
func (k *Provider) CORS() (cors.Options, bool) {
return k.p.CORS("serve", cors.Options{
AllowedMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE"},
AllowedHeaders: []string{"Authorization", "Content-Type"},
ExposedHeaders: []string{"Content-Type"},
AllowCredentials: true,
})
}
func (k *Provider) DSN() string {
dsn := k.p.String(KeyDSN)
if dsn == "memory" {
return DSNMemory
}
return dsn
}
func (k *Provider) TracingServiceName() string {
return k.p.StringF("tracing.service_name", "ORY Keto")
}
func (k *Provider) TracingProvider() string {
return k.p.StringF("tracing.provider", "")
}
func (k *Provider) TracingConfig() *tracing.Config {
return k.p.TracingConfig("ORY Keto")
}
func (k *Provider) NamespaceManager() (namespace.Manager, error) {
if k.nm == nil {
k.nmLock.Lock()
defer k.nmLock.Unlock()
var ctx context.Context
ctx, k.cancelNamespaceManager = context.WithCancel(k.ctx)
switch nTyped := k.p.GetF(KeyNamespaces, "file://./keto_namespaces").(type) {
case string:
var err error
k.nm, err = NewNamespaceWatcher(ctx, k.l, nTyped)
if err != nil {
return nil, err
}
case []*namespace.Namespace:
k.nm = NewMemoryNamespaceManager(nTyped...)
case []interface{}:
nEnc, err := json.Marshal(nTyped)
if err != nil {
return nil, errors.WithStack(err)
}
nn := make([]*namespace.Namespace, len(nTyped))
if err := json.Unmarshal(nEnc, &nn); err != nil {
return nil, errors.WithStack(err)
}
k.nm = NewMemoryNamespaceManager(nn...)
default:
return nil, errors.Errorf("could not create namespace manager from %#v, this indicates an error in the JSON schema that should be reported", nTyped)
}
// return here to properly unlock
return k.nm, nil
}
k.nmLock.RLock()
defer k.nmLock.RUnlock()
return k.nm, nil
}