-
-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathapp.go
More file actions
271 lines (235 loc) 路 8.3 KB
/
Copy pathapp.go
File metadata and controls
271 lines (235 loc) 路 8.3 KB
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
// 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 security
import (
"context"
"encoding/json"
"fmt"
"sync"
"github.com/caddyserver/caddy/v2"
"github.com/greenpau/go-authcrunch"
"github.com/greenpau/go-authcrunch/pkg/authn"
"github.com/greenpau/go-authcrunch/pkg/authz"
"go.uber.org/zap"
)
var (
appName = "security"
// Interface guards
_ caddy.Provisioner = (*App)(nil)
_ caddy.Module = (*App)(nil)
_ caddy.App = (*App)(nil)
_ caddy.CleanerUpper = (*App)(nil)
)
func init() {
caddy.RegisterModule(&App{})
}
type SecretsManager interface {
GetConfig(context.Context) map[string]interface{}
GetSecret(context.Context) (map[string]interface{}, error)
GetSecretByKey(context.Context, string) (interface{}, error)
}
// App implements security manager.
type App struct {
Name string `json:"-"`
Config *authcrunch.Config `json:"config,omitempty"`
OAuthRegistrationStore *OAuthRegistrationStoreConfig `json:"oauth_registration_store,omitempty"`
OAuthApplicationSources []*OAuthApplicationSource `json:"oauth_application_sources,omitempty"`
// OIDCProviderDirectives preserves complete provider bodies across Caddy JSON.
// Reattach them after all explicit/stored applications are available, before
// validating the runtime portal. Declarative JSON omits copied client snapshots.
OIDCProviderDirectives map[string][]string `json:"oidc_provider_directives,omitempty"`
// PortalCookieDirectives holds complete cookie snapshots awaiting runtime
// replacement or a deferred refresh override, keyed by portal name. It replaces
// that portal's CookieConfig after resolving token refresh configuration.
PortalCookieDirectives map[string][]string `json:"portal_cookie_directives,omitempty"`
// PortalTokenRefreshDirectives preserves complete token refresh bodies with
// runtime references. Resolve and attach before validating runtime portals.
PortalTokenRefreshDirectives map[string][]string `json:"portal_token_refresh_directives,omitempty"`
// OAuthProviderDirectives retains complete, validated provider statements
// with runtime references, keyed by provider name. Reparse after replacement
// so driver defaults never become part of an unresolved secret lookup.
OAuthProviderDirectives map[string][]string `json:"oauth_provider_directives,omitempty"`
SecretsManagerConfigs []json.RawMessage `json:"secrets_managers,omitempty" caddy:"namespace=security.secrets inline_key=driver"`
secretsManagers []SecretsManager
server *authcrunch.Server
logger *zap.Logger
mu sync.Mutex
provisioned bool
disposing bool
requests sync.WaitGroup
cleanupOnce sync.Once
cleanupErr error
identityFiles []string
}
// CaddyModule returns the Caddy module information.
func (*App) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "security",
New: func() caddy.Module { return new(App) },
}
}
// Provision constructs the security runtime for this Caddy configuration.
func (app *App) Provision(ctx caddy.Context) error {
app.mu.Lock()
defer app.mu.Unlock()
if app.provisioned || app.disposing {
return fmt.Errorf("security app instance cannot be reprovisioned")
}
app.provisioned = true
if app.Config == nil {
return fmt.Errorf("security app config is nil")
}
// Validation, runtime replacement, and NewServer all mutate configuration.
// Keep the declarative input separate, including nested maps and slices.
data, err := json.Marshal(app.Config)
if err != nil {
return fmt.Errorf("copy security app config: %w", err)
}
var config authcrunch.Config
if err := json.Unmarshal(data, &config); err != nil {
return fmt.Errorf("copy security app config: %w", err)
}
if err := app.resolveOAuthRegistrationConfig(ctx, &config); err != nil {
return fmt.Errorf("resolve OAuth registrations: %w", err)
}
app.Name = appName
app.logger = ctx.Logger(app)
app.logger.Info(
"provisioning app instance",
zap.String("app", app.Name),
)
secretsManagerConfigs, err := ctx.LoadModule(app, "SecretsManagerConfigs")
if err != nil {
app.logger.Error(
"app failed loading secrets manager plugins",
zap.String("app_name", app.Name),
zap.Error(err),
)
return err
}
for _, conf := range secretsManagerConfigs.([]any) {
secretsManagerPlugin := conf.(SecretsManager)
app.logger.Info(
"loaded secrets manager plugin",
zap.String("app_name", app.Name),
zap.Any("config", secretsManagerPlugin.GetConfig(ctx)),
)
app.secretsManagers = append(app.secretsManagers, secretsManagerPlugin)
}
repl := caddy.NewReplacer()
if err := resolveRuntimeAppConfig(ctx, repl, app.secretsManagers, &config, app.OAuthProviderDirectives, app.PortalTokenRefreshDirectives, app.logger); err != nil {
return err
}
// Apply resolved snapshots last so substituted paths are not expanded twice.
if err := resolvePortalCookieDirectives(ctx, repl, app.secretsManagers, &config, app.PortalCookieDirectives, app.logger); err != nil {
return err
}
if err := config.Validate(); err != nil {
app.logger.Error(
"app failed validating config",
zap.String("app_name", app.Name),
zap.Error(err),
)
return err
}
// Check issuer isolation on the completed config, before construction.
if err := validateOIDCProviderMounts(&config); err != nil {
return err
}
// Local stores cache independent database snapshots. Until AuthCrunch can
// coordinate those snapshots, overlapping owners must fail before NewServer
// can initialize or overwrite users in a file used by the live deployment.
app.identityFiles, err = reserveIdentityFiles(&config)
if err != nil {
return err
}
server, err := authcrunch.NewServer(&config, app.logger)
if err != nil {
app.logger.Error(
"failed provisioning app server instance",
zap.String("app", app.Name),
zap.Error(err),
)
return err
}
app.server = server
app.logger.Info(
"provisioned app instance",
zap.String("app", app.Name),
)
return nil
}
// Start starts the App.
func (app *App) Start() error {
app.logger.Debug(
"started app instance",
zap.String("app", app.Name),
)
return nil
}
// Stop leaves disposal to Cleanup. Caddy stops apps in unspecified order;
// HTTP handlers may still be using this app, even after the HTTP app's Stop.
func (app *App) Stop() error {
app.logger.Debug(
"stopped app instance",
zap.String("app", app.Name),
)
return nil
}
// Cleanup prevents new AuthCrunch calls, drains calls already admitted, and
// disposes the runtime once. Caddy invokes it for abandoned candidates as well
// as retired configurations. HTTP shutdown is asynchronous during reload, so
// cancellation of the Caddy module context is not a request-drain guarantee.
// Only this app owns the server; individual route modules never close it.
func (app *App) Cleanup() error {
app.cleanupOnce.Do(func() {
app.mu.Lock()
app.disposing = true
app.mu.Unlock()
app.requests.Wait()
app.cleanupErr = app.server.Close()
releaseIdentityFiles(app.identityFiles)
})
return app.cleanupErr
}
// acquireRequest pins the runtime for one portal/gatekeeper call. Admission and
// WaitGroup.Add share the disposal lock, so no Add can race the cleanup Wait.
func (app *App) acquireRequest() (release func(), ok bool) {
if app == nil {
return nil, false
}
app.mu.Lock()
defer app.mu.Unlock()
if app.disposing || app.server == nil {
return nil, false
}
app.requests.Add(1)
return app.requests.Done, true
}
func (app *App) getPortal(s string) (*authn.Portal, error) {
app.mu.Lock()
defer app.mu.Unlock()
if app.disposing || app.server == nil {
return nil, authcrunch.ErrServerClosed
}
return app.server.GetPortalByName(s)
}
func (app *App) getGatekeeper(s string) (*authz.Gatekeeper, error) {
app.mu.Lock()
defer app.mu.Unlock()
if app.disposing || app.server == nil {
return nil, authcrunch.ErrServerClosed
}
return app.server.GetGatekeeperByName(s)
}