-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
config.go
321 lines (289 loc) · 10.5 KB
/
config.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright 2022 Paul Greenberg greenpau@outlook.com
//
// 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.
package authcrunch
import (
"fmt"
"github.com/greenpau/go-authcrunch/pkg/authn"
"github.com/greenpau/go-authcrunch/pkg/authz"
"github.com/greenpau/go-authcrunch/pkg/credentials"
"github.com/greenpau/go-authcrunch/pkg/errors"
"github.com/greenpau/go-authcrunch/pkg/idp"
"github.com/greenpau/go-authcrunch/pkg/ids"
"github.com/greenpau/go-authcrunch/pkg/messaging"
"github.com/greenpau/go-authcrunch/pkg/registry"
"github.com/greenpau/go-authcrunch/pkg/sso"
)
// Config is a configuration of Server.
type Config struct {
Credentials *credentials.Config `json:"credentials,omitempty" xml:"credentials,omitempty" yaml:"credentials,omitempty"`
Messaging *messaging.Config `json:"messaging,omitempty" xml:"messaging,omitempty" yaml:"messaging,omitempty"`
AuthenticationPortals []*authn.PortalConfig `json:"authentication_portals,omitempty" xml:"authentication_portals,omitempty" yaml:"authentication_portals,omitempty"`
AuthorizationPolicies []*authz.PolicyConfig `json:"authorization_policies,omitempty" xml:"authorization_policies,omitempty" yaml:"authorization_policies,omitempty"`
IdentityStores []*ids.IdentityStoreConfig `json:"identity_stores,omitempty" xml:"identity_stores,omitempty" yaml:"identity_stores,omitempty"`
IdentityProviders []*idp.IdentityProviderConfig `json:"identity_providers,omitempty" xml:"identity_providers,omitempty" yaml:"identity_providers,omitempty"`
SingleSignOnProviders []*sso.SingleSignOnProviderConfig `json:"sso_providers,omitempty" xml:"sso_providers,omitempty" yaml:"sso_providers,omitempty"`
disabledIdentityStores map[string]interface{}
disabledIdentityProviders map[string]interface{}
UserRegistries []*registry.UserRegistryConfig `json:"user_registries,omitempty" xml:"user_registries,omitempty" yaml:"user_registries,omitempty"`
}
// NewConfig returns an instance of Config.
func NewConfig() *Config {
return &Config{}
}
// AddCredential adds a credential configuration.
func (cfg *Config) AddCredential(c credentials.Credential) error {
if cfg.Credentials == nil {
cfg.Credentials = &credentials.Config{}
}
return cfg.Credentials.Add(c)
}
// AddMessagingProvider adds a messaging provider configuration.
func (cfg *Config) AddMessagingProvider(p messaging.Provider) error {
if cfg.Messaging == nil {
cfg.Messaging = &messaging.Config{}
}
return cfg.Messaging.Add(p)
}
// AddIdentityStore adds an identity store configuration.
func (cfg *Config) AddIdentityStore(name, kind string, data map[string]interface{}) error {
store, err := ids.NewIdentityStoreConfig(name, kind, data)
if err != nil {
return err
}
cfg.IdentityStores = append(cfg.IdentityStores, store)
return nil
}
// AddIdentityProvider adds an identity provider configuration.
func (cfg *Config) AddIdentityProvider(name, kind string, data map[string]interface{}) error {
provider, err := idp.NewIdentityProviderConfig(name, kind, data)
if err != nil {
return err
}
cfg.IdentityProviders = append(cfg.IdentityProviders, provider)
return nil
}
// AddSingleSignOnProvider adds a single sign-on provider configuration.
func (cfg *Config) AddSingleSignOnProvider(data map[string]interface{}) error {
provider, err := sso.NewSingleSignOnProviderConfig(data)
if err != nil {
return err
}
cfg.SingleSignOnProviders = append(cfg.SingleSignOnProviders, provider)
return nil
}
// AddAuthenticationPortal adds an authentication portal configuration.
func (cfg *Config) AddAuthenticationPortal(p *authn.PortalConfig) error {
if err := p.Validate(); err != nil {
return err
}
cfg.AuthenticationPortals = append(cfg.AuthenticationPortals, p)
return nil
}
// AddAuthorizationPolicy adds an authorization policy configuration.
func (cfg *Config) AddAuthorizationPolicy(p *authz.PolicyConfig) error {
if err := p.Validate(); err != nil {
return err
}
cfg.AuthorizationPolicies = append(cfg.AuthorizationPolicies, p)
return nil
}
// Validate validates Config.
func (cfg *Config) Validate() error {
if len(cfg.AuthenticationPortals) < 1 && len(cfg.AuthorizationPolicies) < 1 {
return fmt.Errorf("no portals and gatekeepers found")
}
identityStoreUserRegistry := make(map[string]string)
for _, userRegistry := range cfg.UserRegistries {
userRegistry.SetCredentials(cfg.Credentials)
userRegistry.SetMessaging(cfg.Messaging)
if err := userRegistry.ValidateMessaging(); err != nil {
return err
}
var identityStoreFound bool
for _, identityStore := range cfg.IdentityStores {
if identityStore.Name == userRegistry.IdentityStore {
identityStoreFound = true
identityStoreUserRegistry[identityStore.Name] = userRegistry.IdentityStore
break
}
}
if !identityStoreFound {
return fmt.Errorf(
"identity store %q referenced in %q user registry not found",
userRegistry.IdentityStore, userRegistry.Name,
)
}
}
// Validate auth portal configurations.
for _, portalCfg := range cfg.AuthenticationPortals {
// If there are no excplicitly specified identity stores and providers in a portal, add all of them.
if len(portalCfg.IdentityStores) == 0 && len(portalCfg.IdentityProviders) == 0 {
for _, entry := range cfg.IdentityStores {
portalCfg.IdentityStores = append(portalCfg.IdentityStores, entry.Name)
}
for _, entry := range cfg.IdentityProviders {
portalCfg.IdentityProviders = append(portalCfg.IdentityProviders, entry.Name)
}
}
if len(portalCfg.IdentityStores) == 0 && len(portalCfg.IdentityProviders) == 0 {
return errors.ErrPortalConfigBackendsNotFound
}
// Filter out disabled identity store names.
portalCfg.IdentityStores = cfg.filterDisabledIdentityStores(portalCfg.IdentityStores)
// Vealidate that there are no duplicate or overlapping identity store and providers.
authByName := make(map[string]string)
authByRealm := make(map[string]string)
for _, storeName := range portalCfg.IdentityStores {
if v, exists := authByName[storeName]; exists {
return fmt.Errorf(
"identity store %q has the same name as %s",
storeName, v,
)
}
authByName[storeName] = "another identity store"
var storeConfig *ids.IdentityStoreConfig
for _, entry := range cfg.IdentityStores {
if entry.Name == storeName {
storeConfig = entry
break
}
}
if storeConfig == nil {
continue
}
if storeConfig.Params == nil {
continue
}
if v, exists := storeConfig.Params["realm"]; exists {
realmName := v.(string)
if prevStoreName, exists := authByRealm[realmName]; exists {
return fmt.Errorf(
"identity provider %q has the same %q realm as %q",
storeName, realmName, prevStoreName,
)
}
authByRealm[realmName] = storeName
authByName[storeName] = "identity store in " + realmName + " realm"
}
// Add registry store if configured.
if v, exists := identityStoreUserRegistry[storeName]; exists {
storeConfig.Params["registration_enabled"] = true
portalCfg.UserRegistries = append(portalCfg.UserRegistries, v)
}
}
// Filter out disabled identity store names.
portalCfg.IdentityProviders = cfg.filterDisabledIdentityProviders(portalCfg.IdentityProviders)
for _, providerName := range portalCfg.IdentityProviders {
if v, exists := authByName[providerName]; exists {
return fmt.Errorf(
"identity provider %q has the same name as %s",
providerName, v,
)
}
authByName[providerName] = "another identity provider"
var providerConfig *idp.IdentityProviderConfig
for _, entry := range cfg.IdentityProviders {
providerConfig = entry
if entry.Name == providerName {
break
}
}
if providerConfig == nil {
continue
}
if providerConfig.Params == nil {
continue
}
if v, exists := providerConfig.Params["realm"]; exists {
realmName := v.(string)
if prevProviderName, exists := authByRealm[realmName]; exists {
return fmt.Errorf(
"identity provider %q has the same %q realm as %q",
providerName, realmName, prevProviderName,
)
}
authByRealm[realmName] = providerName
authByName[providerName] = "identity provider in " + realmName + " realm"
}
}
// Iterate over SSO providers.
for _, providerName := range portalCfg.SingleSignOnProviders {
var providerFound bool
for _, entry := range cfg.SingleSignOnProviders {
if providerName == entry.Name {
providerFound = true
break
}
}
if !providerFound {
return fmt.Errorf("sso provider %q configuration not found", providerName)
}
}
}
return nil
}
// AddDisabledIdentityStore adds the names of disabled identity stores.
func (cfg *Config) AddDisabledIdentityStore(s string) {
if cfg.disabledIdentityStores == nil {
cfg.disabledIdentityStores = map[string]interface{}{
s: true,
}
return
}
cfg.disabledIdentityStores[s] = true
}
// AddDisabledIdentityProvider adds the names of disabled identity providers.
func (cfg *Config) AddDisabledIdentityProvider(s string) {
if cfg.disabledIdentityProviders == nil {
cfg.disabledIdentityProviders = map[string]interface{}{
s: true,
}
return
}
cfg.disabledIdentityProviders[s] = true
}
func (cfg *Config) filterDisabledIdentityStores(arr []string) []string {
var output []string
if len(arr) == 0 || cfg.disabledIdentityStores == nil {
return arr
}
for _, s := range arr {
if _, exists := cfg.disabledIdentityStores[s]; exists {
continue
}
output = append(output, s)
}
return output
}
func (cfg *Config) filterDisabledIdentityProviders(arr []string) []string {
var output []string
if len(arr) == 0 || cfg.disabledIdentityProviders == nil {
return arr
}
for _, s := range arr {
if _, exists := cfg.disabledIdentityProviders[s]; exists {
continue
}
output = append(output, s)
}
return output
}
// AddUserRegistry adds a user registry configuration.
func (cfg *Config) AddUserRegistry(r *registry.UserRegistryConfig) error {
if err := r.Validate(); err != nil {
return err
}
cfg.UserRegistries = append(cfg.UserRegistries, r)
return nil
}