-
Notifications
You must be signed in to change notification settings - Fork 360
/
Copy pathprovider_viper.go
289 lines (233 loc) · 8.08 KB
/
provider_viper.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package configuration
import (
"bytes"
"encoding/json"
"fmt"
"net/url"
"strings"
"time"
"github.com/imdario/mergo"
"github.com/pkg/errors"
"github.com/rs/cors"
"github.com/sirupsen/logrus"
"github.com/ory/gojsonschema"
"github.com/ory/x/jsonx"
"github.com/ory/viper"
"github.com/ory/fosite"
"github.com/ory/x/corsx"
"github.com/ory/x/urlx"
"github.com/ory/x/viperx"
"github.com/ory/oathkeeper/x"
)
var _ Provider = new(ViperProvider)
const (
ViperKeyProxyReadTimeout = "serve.proxy.timeout.read"
ViperKeyProxyWriteTimeout = "serve.proxy.timeout.write"
ViperKeyProxyIdleTimeout = "serve.proxy.timeout.idle"
ViperKeyProxyServeAddressHost = "serve.proxy.host"
ViperKeyProxyServeAddressPort = "serve.proxy.port"
ViperKeyAPIServeAddressHost = "serve.api.host"
ViperKeyAPIServeAddressPort = "serve.api.port"
ViperKeyAccessRuleRepositories = "access_rules.repositories"
)
// Authorizers
const (
ViperKeyAuthorizerAllowIsEnabled = "authorizers.allow.enabled"
ViperKeyAuthorizerDenyIsEnabled = "authorizers.deny.enabled"
ViperKeyAuthorizerKetoEngineACPORYIsEnabled = "authorizers.keto_engine_acp_ory.enabled"
)
// Mutators
const (
ViperKeyMutatorCookieIsEnabled = "mutators.cookie.enabled"
ViperKeyMutatorHeaderIsEnabled = "mutators.header.enabled"
ViperKeyMutatorNoopIsEnabled = "mutators.noop.enabled"
ViperKeyMutatorHydratorIsEnabled = "mutators.hydrator.enabled"
ViperKeyMutatorIDTokenIsEnabled = "mutators.id_token.enabled"
ViperKeyMutatorIDTokenJWKSURL = "mutators.id_token.config.jwks_url"
)
// Authenticators
const (
// anonymous
ViperKeyAuthenticatorAnonymousIsEnabled = "authenticators.anonymous.enabled"
// noop
ViperKeyAuthenticatorNoopIsEnabled = "authenticators.noop.enabled"
// cookie session
ViperKeyAuthenticatorCookieSessionIsEnabled = "authenticators.cookie_session.enabled"
// jwt
ViperKeyAuthenticatorJWTIsEnabled = "authenticators.jwt.enabled"
// oauth2_client_credentials
ViperKeyAuthenticatorOAuth2ClientCredentialsIsEnabled = "authenticators.oauth2_client_credentials.enabled"
// oauth2_token_introspection
ViperKeyAuthenticatorOAuth2TokenIntrospectionIsEnabled = "authenticators.oauth2_introspection.enabled"
// unauthorized
ViperKeyAuthenticatorUnauthorizedIsEnabled = "authenticators.unauthorized.enabled"
)
func BindEnvs() {
if err := viper.BindEnv(
ViperKeyProxyReadTimeout,
ViperKeyProxyWriteTimeout,
ViperKeyProxyIdleTimeout,
ViperKeyProxyServeAddressHost,
ViperKeyProxyServeAddressPort,
ViperKeyAPIServeAddressHost,
ViperKeyAPIServeAddressPort,
ViperKeyAccessRuleRepositories,
ViperKeyAuthorizerAllowIsEnabled,
ViperKeyAuthorizerDenyIsEnabled,
ViperKeyAuthorizerKetoEngineACPORYIsEnabled,
ViperKeyMutatorCookieIsEnabled,
ViperKeyMutatorHeaderIsEnabled,
ViperKeyMutatorNoopIsEnabled,
ViperKeyMutatorHydratorIsEnabled,
ViperKeyMutatorIDTokenIsEnabled,
ViperKeyMutatorIDTokenJWKSURL,
ViperKeyAuthenticatorAnonymousIsEnabled,
ViperKeyAuthenticatorNoopIsEnabled,
ViperKeyAuthenticatorCookieSessionIsEnabled,
ViperKeyAuthenticatorJWTIsEnabled,
ViperKeyAuthenticatorOAuth2ClientCredentialsIsEnabled,
ViperKeyAuthenticatorOAuth2TokenIntrospectionIsEnabled,
ViperKeyAuthenticatorUnauthorizedIsEnabled,
); err != nil {
panic(err.Error())
}
}
type ViperProvider struct {
l logrus.FieldLogger
}
func NewViperProvider(l logrus.FieldLogger) *ViperProvider {
return &ViperProvider{l: l}
}
func (v *ViperProvider) AccessRuleRepositories() []url.URL {
sources := viperx.GetStringSlice(v.l, ViperKeyAccessRuleRepositories, []string{})
repositories := make([]url.URL, len(sources))
for k, source := range sources {
repositories[k] = *urlx.ParseOrFatal(v.l, source)
}
return repositories
}
func (v *ViperProvider) CORSEnabled(iface string) bool {
return corsx.IsEnabled(v.l, "serve."+iface)
}
func (v *ViperProvider) CORSOptions(iface string) cors.Options {
return corsx.ParseOptions(v.l, "serve."+iface)
}
func (v *ViperProvider) ProxyReadTimeout() time.Duration {
return viperx.GetDuration(v.l, ViperKeyProxyReadTimeout, time.Second*5, "PROXY_SERVER_READ_TIMEOUT")
}
func (v *ViperProvider) ProxyWriteTimeout() time.Duration {
return viperx.GetDuration(v.l, ViperKeyProxyWriteTimeout, time.Second*10, "PROXY_SERVER_WRITE_TIMEOUT")
}
func (v *ViperProvider) ProxyIdleTimeout() time.Duration {
return viperx.GetDuration(v.l, ViperKeyProxyIdleTimeout, time.Second*120, "PROXY_SERVER_IDLE_TIMEOUT")
}
func (v *ViperProvider) ProxyServeAddress() string {
return fmt.Sprintf(
"%s:%d",
viperx.GetString(v.l, ViperKeyProxyServeAddressHost, ""),
viperx.GetInt(v.l, ViperKeyProxyServeAddressPort, 4455),
)
}
func (v *ViperProvider) APIServeAddress() string {
return fmt.Sprintf(
"%s:%d",
viperx.GetString(v.l, ViperKeyAPIServeAddressHost, ""),
viperx.GetInt(v.l, ViperKeyAPIServeAddressPort, 4456),
)
}
func (v *ViperProvider) ParseURLs(sources []string) ([]url.URL, error) {
r := make([]url.URL, len(sources))
for k, u := range sources {
p, err := url.Parse(u)
if err != nil {
return nil, err
}
r[k] = *p
}
return r, nil
}
func (v *ViperProvider) getURL(value string, key string) *url.URL {
u, err := url.ParseRequestURI(value)
if err != nil {
v.l.WithError(err).Errorf(`Configuration key "%s" is missing or malformed.`, key)
return nil
}
return u
}
func (v *ViperProvider) ToScopeStrategy(value string, key string) fosite.ScopeStrategy {
switch strings.ToLower(value) {
case "hierarchic":
return fosite.HierarchicScopeStrategy
case "exact":
return fosite.ExactScopeStrategy
case "wildcard":
return fosite.WildcardScopeStrategy
case "none":
return nil
default:
v.l.Errorf(`Configuration key "%s" declares unknown scope strategy "%s", only "hierarchic", "exact", "wildcard", "none" are supported. Falling back to strategy "none".`, key, value)
return nil
}
}
func (v *ViperProvider) pipelineIsEnabled(prefix, id string) bool {
return viperx.GetBool(v.l, fmt.Sprintf("%s.%s.enabled", prefix, id), false)
}
func (v *ViperProvider) PipelineConfig(prefix, id string, override json.RawMessage, dest interface{}) error {
// we need to create a copy for config otherwise we will accidentally override values
config, err := x.Deepcopy(viper.GetStringMap(fmt.Sprintf("%s.%s.config", prefix, id)))
if err != nil {
return errors.WithStack(err)
}
if len(override) != 0 {
var overrideMap map[string]interface{}
if err := json.Unmarshal(override, &overrideMap); err != nil {
return errors.WithStack(err)
}
if err := mergo.Merge(&config, &overrideMap, mergo.WithOverride); err != nil {
return errors.WithStack(err)
}
}
marshalled, err := json.Marshal(config)
if err != nil {
return errors.WithStack(err)
}
if dest != nil {
if err := jsonx.NewStrictDecoder(bytes.NewBuffer(marshalled)).Decode(dest); err != nil {
return errors.WithStack(err)
}
}
schema, err := schemas.Find(fmt.Sprintf("%s.%s.schema.json", prefix, id))
if err != nil {
return errors.WithStack(err)
}
if result, err := gojsonschema.Validate(
gojsonschema.NewBytesLoader(schema),
gojsonschema.NewBytesLoader(marshalled),
); err != nil {
return errors.WithStack(err)
} else if !result.Valid() {
return errors.WithStack(result.Errors())
}
return nil
}
func (v *ViperProvider) AuthenticatorIsEnabled(id string) bool {
return v.pipelineIsEnabled("authenticators", id)
}
func (v *ViperProvider) AuthenticatorConfig(id string, override json.RawMessage, dest interface{}) error {
return v.PipelineConfig("authenticators", id, override, dest)
}
func (v *ViperProvider) AuthorizerIsEnabled(id string) bool {
return v.pipelineIsEnabled("authorizers", id)
}
func (v *ViperProvider) AuthorizerConfig(id string, override json.RawMessage, dest interface{}) error {
return v.PipelineConfig("authorizers", id, override, dest)
}
func (v *ViperProvider) MutatorIsEnabled(id string) bool {
return v.pipelineIsEnabled("mutators", id)
}
func (v *ViperProvider) MutatorConfig(id string, override json.RawMessage, dest interface{}) error {
return v.PipelineConfig("mutators", id, override, dest)
}
func (v *ViperProvider) JSONWebKeyURLs() []string {
return viperx.GetStringSlice(v.l, ViperKeyMutatorIDTokenJWKSURL, []string{})
}