Debian Codegraph @

caddy.go

raw symbols SCIP
1 // Copyright 2015 Matthew Holt and The Caddy Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package caddy
16
17 import (
18 "bytes"
19 "context"
20 "encoding/hex"
21 "encoding/json"
22 "errors"
23 "fmt"
24 "io"
25 "io/fs"
26 "log"
27 "net/http"
28 "os"
29 "path"
30 "path/filepath"
31 "runtime/debug"
32 "strconv"
33 "strings"
34 "sync"
35 "sync/atomic"
36 "time"
37
38 "github.com/caddyserver/certmagic"
39 "github.com/google/uuid"
40 "go.uber.org/zap"
41
42 "github.com/caddyserver/caddy/v2/internal/filesystems"
43 "github.com/caddyserver/caddy/v2/notify"
44 )
45
46 // Config is the top (or beginning) of the Caddy configuration structure.
47 // Caddy config is expressed natively as a JSON document. If you prefer
48 // not to work with JSON directly, there are [many config adapters](/docs/config-adapters)
49 // available that can convert various inputs into Caddy JSON.
50 //
51 // Many parts of this config are extensible through the use of Caddy modules.
52 // Fields which have a json.RawMessage type and which appear as dots (•••) in
53 // the online docs can be fulfilled by modules in a certain module
54 // namespace. The docs show which modules can be used in a given place.
55 //
56 // Whenever a module is used, its name must be given either inline as part of
57 // the module, or as the key to the module's value. The docs will make it clear
58 // which to use.
59 //
60 // Generally, all config settings are optional, as it is Caddy convention to
61 // have good, documented default values. If a parameter is required, the docs
62 // should say so.
63 //
64 // Go programs which are directly building a Config struct value should take
65 // care to populate the JSON-encodable fields of the struct (i.e. the fields
66 // with `json` struct tags) if employing the module lifecycle (e.g. Provision
67 // method calls).
68 type Config struct {
69 Admin *AdminConfig `json:"admin,omitempty"`
70 Logging *Logging `json:"logging,omitempty"`
71
72 // StorageRaw is a storage module that defines how/where Caddy
73 // stores assets (such as TLS certificates). The default storage
74 // module is `caddy.storage.file_system` (the local file system),
75 // and the default path
76 // [depends on the OS and environment](/docs/conventions#data-directory).
77 StorageRaw json.RawMessage `json:"storage,omitempty" caddy:"namespace=caddy.storage inline_key=module"`
78
79 // AppsRaw are the apps that Caddy will load and run. The
80 // app module name is the key, and the app's config is the
81 // associated value.
82 AppsRaw ModuleMap `json:"apps,omitempty" caddy:"namespace="`
83
84 apps map[string]App
85
86 // failedApps is a map of apps that failed to provision with their underlying error.
87 failedApps map[string]error
88 storage certmagic.Storage
89 eventEmitter eventEmitter
90
91 cancelFunc context.CancelCauseFunc
92
93 // fileSystems is a dict of fileSystems that will later be loaded from and added to.
94 fileSystems FileSystems
95 }
96
97 // App is a thing that Caddy runs.
98 type App interface {
99 Start() error
100 Stop() error
101 }
102
103 // Run runs the given config, replacing any existing config.
104 func Run(cfg *Config) error {
105 cfgJSON, err := json.Marshal(cfg)
106 if err != nil {
107 return err
108 }
109 return Load(cfgJSON, true)
110 }
111
112 // Load loads the given config JSON and runs it only
113 // if it is different from the current config or
114 // forceReload is true.
115 func Load(cfgJSON []byte, forceReload bool) error {
116 if err := notify.Reloading(); err != nil {
117 Log().Error("unable to notify service manager of reloading state", zap.Error(err))
118 }
119
120 // after reload, notify system of success or, if
121 // failure, update with status (error message)
122 var err error
123 defer func() {
124 if err != nil {
125 if notifyErr := notify.Error(err, 0); notifyErr != nil {
126 Log().Error("unable to notify to service manager of reload error",
127 zap.Error(notifyErr),
128 zap.String("reload_err", err.Error()))
129 }
130 }
131 if notifyErr := notify.Ready(); notifyErr != nil {
132 Log().Error("unable to notify to service manager of ready state", zap.Error(notifyErr))
133 }
134 }()
135
136 err = changeConfig(http.MethodPost, "/"+rawConfigKey, cfgJSON, "", forceReload)
137 if errors.Is(err, errSameConfig) {
138 err = nil // not really an error
139 }
140
141 return err
142 }
143
144 // changeConfig changes the current config (rawCfg) according to the
145 // method, traversed via the given path, and uses the given input as
146 // the new value (if applicable; i.e. "DELETE" doesn't have an input).
147 // If the resulting config is the same as the previous, no reload will
148 // occur unless forceReload is true. If the config is unchanged and not
149 // forcefully reloaded, then errConfigUnchanged is returned. This function
150 // is safe for concurrent use.
151 // The ifMatchHeader can optionally be given a string of the format:
152 //
153 // "<path> <hash>"
154 //
155 // where <path> is the absolute path in the config and <hash> is the expected hash of
156 // the config at that path. If the hash in the ifMatchHeader doesn't match
157 // the hash of the config, then an APIError with status 412 will be returned.
158 func changeConfig(method, path string, input []byte, ifMatchHeader string, forceReload bool) error {
159 switch method {
160 case http.MethodGet,
161 http.MethodHead,
162 http.MethodOptions,
163 http.MethodConnect,
164 http.MethodTrace:
165 return fmt.Errorf("method not allowed")
166 }
167
168 rawCfgMu.Lock()
169 defer rawCfgMu.Unlock()
170
171 if ifMatchHeader != "" {
172 // expect the first and last character to be quotes
173 if len(ifMatchHeader) < 2 || ifMatchHeader[0] != '"' || ifMatchHeader[len(ifMatchHeader)-1] != '"' {
174 return APIError{
175 HTTPStatus: http.StatusBadRequest,
176 Err: fmt.Errorf("malformed If-Match header; expect quoted string"),
177 }
178 }
179
180 // read out the parts
181 parts := strings.Fields(ifMatchHeader[1 : len(ifMatchHeader)-1])
182 if len(parts) != 2 {
183 return APIError{
184 HTTPStatus: http.StatusBadRequest,
185 Err: fmt.Errorf("malformed If-Match header; expect format \"<path> <hash>\""),
186 }
187 }
188
189 // get the current hash of the config
190 // at the given path
191 hash := etagHasher()
192 err := unsyncedConfigAccess(http.MethodGet, parts[0], nil, hash)
193 if err != nil {
194 return err
195 }
196
197 if hex.EncodeToString(hash.Sum(nil)) != parts[1] {
198 return APIError{
199 HTTPStatus: http.StatusPreconditionFailed,
200 Err: fmt.Errorf("If-Match header did not match current config hash"),
201 }
202 }
203 }
204
205 err := unsyncedConfigAccess(method, path, input, nil)
206 if err != nil {
207 return err
208 }
209
210 // the mutation is complete, so encode the entire config as JSON
211 newCfg, err := json.Marshal(rawCfg[rawConfigKey])
212 if err != nil {
213 return APIError{
214 HTTPStatus: http.StatusBadRequest,
215 Err: fmt.Errorf("encoding new config: %v", err),
216 }
217 }
218
219 // if nothing changed, no need to do a whole reload unless the client forces it
220 if !forceReload && bytes.Equal(rawCfgJSON, newCfg) {
221 Log().Info("config is unchanged")
222 return errSameConfig
223 }
224
225 // find any IDs in this config and index them
226 idx := make(map[string]string)
227 err = indexConfigObjects(rawCfg[rawConfigKey], "/"+rawConfigKey, idx)
228 if err != nil {
229 if len(rawCfgJSON) > 0 {
230 var oldCfg any
231 err2 := json.Unmarshal(rawCfgJSON, &oldCfg)
232 if err2 != nil {
233 err = fmt.Errorf("%v; additionally, restoring old config: %v", err, err2)
234 }
235 rawCfg[rawConfigKey] = oldCfg
236 } else {
237 rawCfg[rawConfigKey] = nil
238 }
239 return APIError{
240 HTTPStatus: http.StatusBadRequest,
241 Err: fmt.Errorf("indexing config: %v", err),
242 }
243 }
244
245 // load this new config; if it fails, we need to revert to
246 // our old representation of caddy's actual config
247 err = unsyncedDecodeAndRun(newCfg, true)
248 if err != nil {
249 if len(rawCfgJSON) > 0 {
250 // restore old config state to keep it consistent
251 // with what caddy is still running; we need to
252 // unmarshal it again because it's likely that
253 // pointers deep in our rawCfg map were modified
254 var oldCfg any
255 err2 := json.Unmarshal(rawCfgJSON, &oldCfg)
256 if err2 != nil {
257 err = fmt.Errorf("%v; additionally, restoring old config: %v", err, err2)
258 }
259 rawCfg[rawConfigKey] = oldCfg
260 } else {
261 rawCfg[rawConfigKey] = nil
262 }
263
264 return fmt.Errorf("loading new config: %v", err)
265 }
266
267 // success, so update our stored copy of the encoded
268 // config to keep it consistent with what caddy is now
269 // running (storing an encoded copy is not strictly
270 // necessary, but avoids an extra json.Marshal for
271 // each config change)
272 rawCfgJSON = newCfg
273 rawCfgIndex = idx
274
275 return nil
276 }
277
278 // readConfig traverses the current config to path
279 // and writes its JSON encoding to out.
280 func readConfig(path string, out io.Writer) error {
281 rawCfgMu.RLock()
282 defer rawCfgMu.RUnlock()
283 return unsyncedConfigAccess(http.MethodGet, path, nil, out)
284 }
285
286 // indexConfigObjects recursively searches ptr for object fields named
287 // "@id" and maps that ID value to the full configPath in the index.
288 // This function is NOT safe for concurrent access; obtain a write lock
289 // on currentCtxMu.
290 func indexConfigObjects(ptr any, configPath string, index map[string]string) error {
291 switch val := ptr.(type) {
292 case map[string]any:
293 for k, v := range val {
294 if k == idKey {
295 var idStr string
296 switch idVal := v.(type) {
297 case string:
298 idStr = idVal
299 case float64: // all JSON numbers decode as float64
300 idStr = fmt.Sprintf("%v", idVal)
301 default:
302 return fmt.Errorf("%s: %s field must be a string or number", configPath, idKey)
303 }
304 if existingPath, ok := index[idStr]; ok {
305 return fmt.Errorf("duplicate ID '%s' found at %s and %s", idStr, existingPath, configPath)
306 }
307 index[idStr] = configPath
308 continue
309 }
310 // traverse this object property recursively
311 err := indexConfigObjects(val[k], path.Join(configPath, k), index)
312 if err != nil {
313 return err
314 }
315 }
316 case []any:
317 // traverse each element of the array recursively
318 for i := range val {
319 err := indexConfigObjects(val[i], path.Join(configPath, strconv.Itoa(i)), index)
320 if err != nil {
321 return err
322 }
323 }
324 }
325
326 return nil
327 }
328
329 // unsyncedDecodeAndRun removes any meta fields (like @id tags)
330 // from cfgJSON, decodes the result into a *Config, and runs
331 // it as the new config, replacing any other current config.
332 // It does NOT update the raw config state, as this is a
333 // lower-level function; most callers will want to use Load
334 // instead. A write lock on rawCfgMu is required! If
335 // allowPersist is false, it will not be persisted to disk,
336 // even if it is configured to.
337 func unsyncedDecodeAndRun(cfgJSON []byte, allowPersist bool) error {
338 // remove any @id fields from the JSON, which would cause
339 // loading to break since the field wouldn't be recognized
340 strippedCfgJSON := RemoveMetaFields(cfgJSON)
341
342 var newCfg *Config
343 err := StrictUnmarshalJSON(strippedCfgJSON, &newCfg)
344 if err != nil {
345 return err
346 }
347
348 // prevent recursive config loads; that is a user error, and
349 // although frequent config loads should be safe, we cannot
350 // guarantee that in the presence of third party plugins, nor
351 // do we want this error to go unnoticed (we assume it was a
352 // pulled config if we're not allowed to persist it)
353 if !allowPersist &&
354 newCfg != nil &&
355 newCfg.Admin != nil &&
356 newCfg.Admin.Config != nil &&
357 newCfg.Admin.Config.LoadRaw != nil &&
358 newCfg.Admin.Config.LoadDelay <= 0 {
359 return fmt.Errorf("recursive config loading detected: pulled configs cannot pull other configs without positive load_delay")
360 }
361
362 // run the new config and start all its apps
363 ctx, err := run(newCfg, true)
364 if err != nil {
365 return err
366 }
367
368 // swap old context (including its config) with the new one
369 currentCtxMu.Lock()
370 oldCtx := currentCtx
371 currentCtx = ctx
372 currentCtxMu.Unlock()
373
374 // Stop, Cleanup each old app
375 unsyncedStop(oldCtx)
376
377 // autosave a non-nil config, if not disabled
378 if allowPersist &&
379 newCfg != nil &&
380 (newCfg.Admin == nil ||
381 newCfg.Admin.Config == nil ||
382 newCfg.Admin.Config.Persist == nil ||
383 *newCfg.Admin.Config.Persist) {
384 dir := filepath.Dir(ConfigAutosavePath)
385 err := os.MkdirAll(dir, 0o700)
386 if err != nil {
387 Log().Error("unable to create folder for config autosave",
388 zap.String("dir", dir),
389 zap.Error(err))
390 } else {
391 err := os.WriteFile(ConfigAutosavePath, cfgJSON, 0o600)
392 if err == nil {
393 Log().Info("autosaved config (load with --resume flag)", zap.String("file", ConfigAutosavePath))
394 } else {
395 Log().Error("unable to autosave config",
396 zap.String("file", ConfigAutosavePath),
397 zap.Error(err))
398 }
399 }
400 }
401
402 return nil
403 }
404
405 // run runs newCfg and starts all its apps if
406 // start is true. If any errors happen, cleanup
407 // is performed if any modules were provisioned;
408 // apps that were started already will be stopped,
409 // so this function should not leak resources if
410 // an error is returned. However, if no error is
411 // returned and start == false, you should cancel
412 // the config if you are not going to start it,
413 // so that each provisioned module will be
414 // cleaned up.
415 //
416 // This is a low-level function; most callers
417 // will want to use Run instead, which also
418 // updates the config's raw state.
419 func run(newCfg *Config, start bool) (Context, error) {
420 ctx, err := provisionContext(newCfg, start)
421 if err != nil {
422 globalMetrics.configSuccess.Set(0)
423 return ctx, err
424 }
425
426 if !start {
427 return ctx, nil
428 }
429
430 defer func() {
431 // if newCfg fails to start completely, clean up the already provisioned modules
432 // partially copied from provisionContext
433 if err != nil {
434 globalMetrics.configSuccess.Set(0)
435 ctx.cfg.cancelFunc(fmt.Errorf("configuration start error: %w", err))
436
437 if currentCtx.cfg != nil {
438 certmagic.Default.Storage = currentCtx.cfg.storage
439 }
440 }
441 }()
442
443 // Start
444 err = func() error {
445 started := make([]string, 0, len(ctx.cfg.apps))
446 for name, a := range ctx.cfg.apps {
447 err := a.Start()
448 if err != nil {
449 // an app failed to start, so we need to stop
450 // all other apps that were already started
451 for _, otherAppName := range started {
452 err2 := ctx.cfg.apps[otherAppName].Stop()
453 if err2 != nil {
454 err = fmt.Errorf("%v; additionally, aborting app %s: %v",
455 err, otherAppName, err2)
456 }
457 }
458 return fmt.Errorf("%s app module: start: %v", name, err)
459 }
460 started = append(started, name)
461 }
462 return nil
463 }()
464 if err != nil {
465 return ctx, err
466 }
467 globalMetrics.configSuccess.Set(1)
468 globalMetrics.configSuccessTime.SetToCurrentTime()
469
470 // TODO: This event is experimental and subject to change.
471 ctx.emitEvent("started", nil)
472
473 // now that the user's config is running, finish setting up anything else,
474 // such as remote admin endpoint, config loader, etc.
475 err = finishSettingUp(ctx, ctx.cfg)
476 return ctx, err
477 }
478
479 // provisionContext creates a new context from the given configuration and provisions
480 // storage and apps.
481 // If `newCfg` is nil a new empty configuration will be created.
482 // If `replaceAdminServer` is true any currently active admin server will be replaced
483 // with a new admin server based on the provided configuration.
484 func provisionContext(newCfg *Config, replaceAdminServer bool) (Context, error) {
485 // because we will need to roll back any state
486 // modifications if this function errors, we
487 // keep a single error value and scope all
488 // sub-operations to their own functions to
489 // ensure this error value does not get
490 // overridden or missed when it should have
491 // been set by a short assignment
492 var err error
493
494 if newCfg == nil {
495 newCfg = new(Config)
496 }
497
498 // create a context within which to load
499 // modules - essentially our new config's
500 // execution environment; be sure that
501 // cleanup occurs when we return if there
502 // was an error; if no error, it will get
503 // cleaned up on next config cycle
504 ctx, cancelCause := NewContextWithCause(Context{Context: context.Background(), cfg: newCfg})
505 defer func() {
506 if err != nil {
507 globalMetrics.configSuccess.Set(0)
508 // if there were any errors during startup,
509 // we should cancel the new context we created
510 // since the associated config won't be used;
511 // this will cause all modules that were newly
512 // provisioned to clean themselves up
513 cancelCause(fmt.Errorf("configuration error: %w", err))
514
515 // also undo any other state changes we made
516 if currentCtx.cfg != nil {
517 certmagic.Default.Storage = currentCtx.cfg.storage
518 }
519 }
520 }()
521 newCfg.cancelFunc = cancelCause // clean up later
522
523 // set up logging before anything bad happens
524 if newCfg.Logging == nil {
525 newCfg.Logging = new(Logging)
526 }
527 err = newCfg.Logging.openLogs(ctx)
528 if err != nil {
529 return ctx, err
530 }
531
532 // create the new filesystem map
533 newCfg.fileSystems = &filesystems.FileSystemMap{}
534
535 // prepare the new config for use
536 newCfg.apps = make(map[string]App)
537 newCfg.failedApps = make(map[string]error)
538
539 // set up global storage and make it CertMagic's default storage, too
540 err = func() error {
541 if newCfg.StorageRaw != nil {
542 val, err := ctx.LoadModule(newCfg, "StorageRaw")
543 if err != nil {
544 return fmt.Errorf("loading storage module: %v", err)
545 }
546 stor, err := val.(StorageConverter).CertMagicStorage()
547 if err != nil {
548 return fmt.Errorf("creating storage value: %v", err)
549 }
550 newCfg.storage = stor
551 }
552
553 if newCfg.storage == nil {
554 newCfg.storage = DefaultStorage
555 }
556 certmagic.Default.Storage = newCfg.storage
557
558 return nil
559 }()
560 if err != nil {
561 return ctx, err
562 }
563
564 // start the admin endpoint (and stop any prior one)
565 if replaceAdminServer {
566 err = replaceLocalAdminServer(newCfg, ctx)
567 if err != nil {
568 return ctx, fmt.Errorf("starting caddy administration endpoint: %v", err)
569 }
570 }
571
572 // Load and Provision each app and their submodules
573 err = func() error {
574 for appName := range newCfg.AppsRaw {
575 if _, err := ctx.App(appName); err != nil {
576 return err
577 }
578 }
579 return nil
580 }()
581 return ctx, err
582 }
583
584 // ProvisionContext creates a new context from the configuration and provisions storage
585 // and app modules.
586 // The function is intended for testing and advanced use cases only, typically `Run` should be
587 // use to ensure a fully functional caddy instance.
588 // EXPERIMENTAL: While this is public the interface and implementation details of this function may change.
589 func ProvisionContext(newCfg *Config) (Context, error) {
590 return provisionContext(newCfg, false)
591 }
592
593 // finishSettingUp should be run after all apps have successfully started.
594 func finishSettingUp(ctx Context, cfg *Config) error {
595 // establish this server's identity (only after apps are loaded
596 // so that cert management of this endpoint doesn't prevent user's
597 // servers from starting which likely also use HTTP/HTTPS ports;
598 // but before remote management which may depend on these creds)
599 err := manageIdentity(ctx, cfg)
600 if err != nil {
601 return fmt.Errorf("provisioning remote admin endpoint: %v", err)
602 }
603
604 // replace any remote admin endpoint
605 err = replaceRemoteAdminServer(ctx, cfg)
606 if err != nil {
607 return fmt.Errorf("provisioning remote admin endpoint: %v", err)
608 }
609
610 // if dynamic config is requested, set that up and run it
611 if cfg != nil && cfg.Admin != nil && cfg.Admin.Config != nil && cfg.Admin.Config.LoadRaw != nil {
612 val, err := ctx.LoadModule(cfg.Admin.Config, "LoadRaw")
613 if err != nil {
614 return fmt.Errorf("loading config loader module: %s", err)
615 }
616
617 logger := Log().Named("config_loader").With(
618 zap.String("module", val.(Module).CaddyModule().ID.Name()),
619 zap.Int("load_delay", int(cfg.Admin.Config.LoadDelay)))
620
621 runLoadedConfig := func(config []byte) error {
622 logger.Info("applying dynamically-loaded config")
623 err := changeConfig(http.MethodPost, "/"+rawConfigKey, config, "", false)
624 if errors.Is(err, errSameConfig) {
625 return err
626 }
627 if err != nil {
628 logger.Error("failed to run dynamically-loaded config", zap.Error(err))
629 return err
630 }
631 logger.Info("successfully applied dynamically-loaded config")
632 return nil
633 }
634
635 if cfg.Admin.Config.LoadDelay > 0 {
636 go func() {
637 // the loop is here to iterate ONLY if there is an error, a no-op config load,
638 // or an unchanged config; in which case we simply wait the delay and try again
639 for {
640 timer := time.NewTimer(time.Duration(cfg.Admin.Config.LoadDelay))
641 select {
642 case <-timer.C:
643 loadedConfig, err := val.(ConfigLoader).LoadConfig(ctx)
644 if err != nil {
645 logger.Error("failed loading dynamic config; will retry", zap.Error(err))
646 continue
647 }
648 if loadedConfig == nil {
649 logger.Info("dynamically-loaded config was nil; will retry")
650 continue
651 }
652 err = runLoadedConfig(loadedConfig)
653 if errors.Is(err, errSameConfig) {
654 logger.Info("dynamically-loaded config was unchanged; will retry")
655 continue
656 }
657 case <-ctx.Done():
658 if !timer.Stop() {
659 <-timer.C
660 }
661 logger.Info("stopping dynamic config loading")
662 }
663 break
664 }
665 }()
666 } else {
667 // if no LoadDelay is provided, will load config synchronously
668 loadedConfig, err := val.(ConfigLoader).LoadConfig(ctx)
669 if err != nil {
670 return fmt.Errorf("loading dynamic config from %T: %v", val, err)
671 }
672 // do this in a goroutine so current config can finish being loaded; otherwise deadlock
673 go func() { _ = runLoadedConfig(loadedConfig) }()
674 }
675 }
676
677 return nil
678 }
679
680 // ConfigLoader is a type that can load a Caddy config. If
681 // the return value is non-nil, it must be valid Caddy JSON;
682 // if nil or with non-nil error, it is considered to be a
683 // no-op load and may be retried later.
684 type ConfigLoader interface {
685 LoadConfig(Context) ([]byte, error)
686 }
687
688 // Stop stops running the current configuration.
689 // It is the antithesis of Run(). This function
690 // will log any errors that occur during the
691 // stopping of individual apps and continue to
692 // stop the others. Stop should only be called
693 // if not replacing with a new config.
694 func Stop() error {
695 currentCtxMu.RLock()
696 ctx := currentCtx
697 currentCtxMu.RUnlock()
698
699 rawCfgMu.Lock()
700 unsyncedStop(ctx)
701
702 currentCtxMu.Lock()
703 currentCtx = Context{}
704 currentCtxMu.Unlock()
705
706 rawCfgJSON = nil
707 rawCfgIndex = nil
708 rawCfg[rawConfigKey] = nil
709 rawCfgMu.Unlock()
710
711 return nil
712 }
713
714 // unsyncedStop stops ctx from running, but has
715 // no locking around ctx. It is a no-op if ctx has a
716 // nil cfg. If any app returns an error when stopping,
717 // it is logged and the function continues stopping
718 // the next app. This function assumes all apps in
719 // ctx were successfully started first.
720 //
721 // A lock on rawCfgMu is required, even though this
722 // function does not access rawCfg, that lock
723 // synchronizes the stop/start of apps.
724 func unsyncedStop(ctx Context) {
725 if ctx.cfg == nil {
726 return
727 }
728
729 // TODO: This event is experimental and subject to change.
730 ctx.emitEvent("stopping", nil)
731
732 // stop each app
733 for name, a := range ctx.cfg.apps {
734 err := a.Stop()
735 if err != nil {
736 log.Printf("[ERROR] stop %s: %v", name, err)
737 }
738 }
739
740 // clean up all modules
741 ctx.cfg.cancelFunc(fmt.Errorf("stopping apps"))
742 }
743
744 // Validate loads, provisions, and validates
745 // cfg, but does not start running it.
746 func Validate(cfg *Config) error {
747 _, err := run(cfg, false)
748 if err == nil {
749 cfg.cancelFunc(fmt.Errorf("validation complete")) // call Cleanup on all modules
750 }
751 return err
752 }
753
754 // exitProcess exits the process as gracefully as possible,
755 // but it always exits, even if there are errors doing so.
756 // It stops all apps, cleans up external locks, removes any
757 // PID file, and shuts down admin endpoint(s) in a goroutine.
758 // Errors are logged along the way, and an appropriate exit
759 // code is emitted.
760 func exitProcess(ctx context.Context, logger *zap.Logger) {
761 // let the rest of the program know we're quitting; only do it once
762 if !exiting.CompareAndSwap(false, true) {
763 return
764 }
765
766 // give the OS or service/process manager our 2 weeks' notice: we quit
767 if err := notify.Stopping(); err != nil {
768 Log().Error("unable to notify service manager of stopping state", zap.Error(err))
769 }
770
771 if logger == nil {
772 logger = Log()
773 }
774 logger.Warn("exiting; byeee!! 👋")
775
776 exitCode := ExitCodeSuccess
777 lastContext := ActiveContext()
778
779 // stop all apps
780 if err := Stop(); err != nil {
781 logger.Error("failed to stop apps", zap.Error(err))
782 exitCode = ExitCodeFailedQuit
783 }
784
785 // clean up certmagic locks
786 certmagic.CleanUpOwnLocks(ctx, logger)
787
788 // remove pidfile
789 if pidfile != "" {
790 err := os.Remove(pidfile)
791 if err != nil {
792 logger.Error("cleaning up PID file:",
793 zap.String("pidfile", pidfile),
794 zap.Error(err))
795 exitCode = ExitCodeFailedQuit
796 }
797 }
798
799 // execute any process-exit callbacks
800 for _, exitFunc := range lastContext.exitFuncs {
801 exitFunc(ctx)
802 }
803 exitFuncsMu.Lock()
804 for _, exitFunc := range exitFuncs {
805 exitFunc(ctx)
806 }
807 exitFuncsMu.Unlock()
808
809 // shut down admin endpoint(s) in goroutines so that
810 // if this function was called from an admin handler,
811 // it has a chance to return gracefully
812 // use goroutine so that we can finish responding to API request
813 go func() {
814 defer func() {
815 logger = logger.With(zap.Int("exit_code", exitCode))
816 if exitCode == ExitCodeSuccess {
817 logger.Info("shutdown complete")
818 } else {
819 logger.Error("unclean shutdown")
820 }
821 os.Exit(exitCode)
822 }()
823
824 if remoteAdminServer != nil {
825 err := stopAdminServer(remoteAdminServer)
826 if err != nil {
827 exitCode = ExitCodeFailedQuit
828 logger.Error("failed to stop remote admin server gracefully", zap.Error(err))
829 }
830 }
831 if localAdminServer != nil {
832 err := stopAdminServer(localAdminServer)
833 if err != nil {
834 exitCode = ExitCodeFailedQuit
835 logger.Error("failed to stop local admin server gracefully", zap.Error(err))
836 }
837 }
838 }()
839 }
840
841 var exiting atomic.Bool
842
843 // Exiting returns true if the process is exiting.
844 // EXPERIMENTAL API: subject to change or removal.
845 func Exiting() bool { return exiting.Load() }
846
847 // OnExit registers a callback to invoke during process exit.
848 // This registration is PROCESS-GLOBAL, meaning that each
849 // function should only be registered once forever, NOT once
850 // per config load (etc).
851 //
852 // EXPERIMENTAL API: subject to change or removal.
853 func OnExit(f func(context.Context)) {
854 exitFuncsMu.Lock()
855 exitFuncs = append(exitFuncs, f)
856 exitFuncsMu.Unlock()
857 }
858
859 var (
860 exitFuncs []func(context.Context)
861 exitFuncsMu sync.Mutex
862 )
863
864 // Duration can be an integer or a string. An integer is
865 // interpreted as nanoseconds. If a string, it is a Go
866 // time.Duration value such as `300ms`, `1.5h`, or `2h45m`;
867 // valid units are `ns`, `us`/`µs`, `ms`, `s`, `m`, `h`, and `d`.
868 type Duration time.Duration
869
870 // UnmarshalJSON satisfies json.Unmarshaler.
871 func (d *Duration) UnmarshalJSON(b []byte) error {
872 if len(b) == 0 {
873 return io.EOF
874 }
875 var dur time.Duration
876 var err error
877 if b[0] == byte('"') && b[len(b)-1] == byte('"') {
878 dur, err = ParseDuration(strings.Trim(string(b), `"`))
879 } else {
880 err = json.Unmarshal(b, &dur)
881 }
882 *d = Duration(dur)
883 return err
884 }
885
886 // ParseDuration parses a duration string, adding
887 // support for the "d" unit meaning number of days,
888 // where a day is assumed to be 24h. The maximum
889 // input string length is 1024.
890 func ParseDuration(s string) (time.Duration, error) {
891 if len(s) > 1024 {
892 return 0, fmt.Errorf("parsing duration: input string too long")
893 }
894 var inNumber bool
895 var numStart int
896 for i := 0; i < len(s); i++ {
897 ch := s[i]
898 if ch == 'd' {
899 daysStr := s[numStart:i]
900 days, err := strconv.ParseFloat(daysStr, 64)
901 if err != nil {
902 return 0, err
903 }
904 hours := days * 24.0
905 hoursStr := strconv.FormatFloat(hours, 'f', -1, 64)
906 s = s[:numStart] + hoursStr + "h" + s[i+1:]
907 i--
908 continue
909 }
910 if !inNumber {
911 numStart = i
912 }
913 inNumber = (ch >= '0' && ch <= '9') || ch == '.' || ch == '-' || ch == '+'
914 }
915 return time.ParseDuration(s)
916 }
917
918 // InstanceID returns the UUID for this instance, and generates one if it
919 // does not already exist. The UUID is stored in the local data directory,
920 // regardless of storage configuration, since each instance is intended to
921 // have its own unique ID.
922 func InstanceID() (uuid.UUID, error) {
923 appDataDir := AppDataDir()
924 uuidFilePath := filepath.Join(appDataDir, "instance.uuid")
925 uuidFileBytes, err := os.ReadFile(uuidFilePath)
926 if errors.Is(err, fs.ErrNotExist) {
927 uuid, err := uuid.NewRandom()
928 if err != nil {
929 return uuid, err
930 }
931 err = os.MkdirAll(appDataDir, 0o700)
932 if err != nil {
933 return uuid, err
934 }
935 err = os.WriteFile(uuidFilePath, []byte(uuid.String()), 0o600)
936 return uuid, err
937 } else if err != nil {
938 return [16]byte{}, err
939 }
940 return uuid.ParseBytes(uuidFileBytes)
941 }
942
943 // CustomVersion is an optional string that overrides Caddy's
944 // reported version. It can be helpful when downstream packagers
945 // need to manually set Caddy's version. If no other version
946 // information is available, the short form version (see
947 // Version()) will be set to CustomVersion, and the full version
948 // will include CustomVersion at the beginning.
949 //
950 // Set this variable during `go build` with `-ldflags`:
951 //
952 // -ldflags '-X github.com/caddyserver/caddy/v2.CustomVersion=v2.6.2'
953 //
954 // for example.
955 var CustomVersion string
956
957 // CustomBinaryName is an optional string that overrides the root
958 // command name from the default of "caddy". This is useful for
959 // downstream projects that embed Caddy but use a different binary
960 // name. Shell completions and help text will use this name instead
961 // of "caddy".
962 //
963 // Set this variable during `go build` with `-ldflags`:
964 //
965 // -ldflags '-X github.com/caddyserver/caddy/v2.CustomBinaryName=my_custom_caddy'
966 //
967 // for example.
968 var CustomBinaryName string
969
970 // CustomLongDescription is an optional string that overrides the
971 // long description of the root Cobra command. This is useful for
972 // downstream projects that embed Caddy but want different help
973 // output.
974 //
975 // Set this variable in an init() function of a package that is
976 // imported by your main:
977 //
978 // func init() {
979 // caddy.CustomLongDescription = "My custom server based on Caddy..."
980 // }
981 //
982 // for example.
983 var CustomLongDescription string
984
985 // Version returns the Caddy version in a simple/short form, and
986 // a full version string. The short form will not have spaces and
987 // is intended for User-Agent strings and similar, but may be
988 // omitting valuable information. Note that Caddy must be compiled
989 // in a special way to properly embed complete version information.
990 // First this function tries to get the version from the embedded
991 // build info provided by go.mod dependencies; then it tries to
992 // get info from embedded VCS information, which requires having
993 // built Caddy from a git repository. If no version is available,
994 // this function returns "(devel)" because Go uses that, but for
995 // the simple form we change it to "unknown". If still no version
996 // is available (e.g. no VCS repo), then it will use CustomVersion;
997 // CustomVersion is always prepended to the full version string.
998 //
999 // See relevant Go issues: https://github.com/golang/go/issues/29228
1000 // and https://github.com/golang/go/issues/50603.
1001 //
1002 // This function is experimental and subject to change or removal.
1003 func Version() (simple, full string) {
1004 // the currently-recommended way to build Caddy involves
1005 // building it as a dependency so we can extract version
1006 // information from go.mod tooling; once the upstream
1007 // Go issues are fixed, we should just be able to use
1008 // bi.Main... hopefully.
1009 var module *debug.Module
1010 bi, ok := debug.ReadBuildInfo()
1011 if !ok {
1012 if CustomVersion != "" {
1013 full = CustomVersion
1014 simple = CustomVersion
1015 return simple, full
1016 }
1017 full = "unknown"
1018 simple = "unknown"
1019 return simple, full
1020 }
1021 // find the Caddy module in the dependency list
1022 for _, dep := range bi.Deps {
1023 if dep.Path == ImportPath {
1024 module = dep
1025 break
1026 }
1027 }
1028 if module != nil {
1029 simple, full = module.Version, module.Version
1030 if module.Sum != "" {
1031 full += " " + module.Sum
1032 }
1033 if module.Replace != nil {
1034 full += " => " + module.Replace.Path
1035 if module.Replace.Version != "" {
1036 simple = module.Replace.Version + "_custom"
1037 full += "@" + module.Replace.Version
1038 }
1039 if module.Replace.Sum != "" {
1040 full += " " + module.Replace.Sum
1041 }
1042 }
1043 }
1044
1045 if full == "" {
1046 var vcsRevision string
1047 var vcsTime time.Time
1048 var vcsModified bool
1049 for _, setting := range bi.Settings {
1050 switch setting.Key {
1051 case "vcs.revision":
1052 vcsRevision = setting.Value
1053 case "vcs.time":
1054 vcsTime, _ = time.Parse(time.RFC3339, setting.Value)
1055 case "vcs.modified":
1056 vcsModified, _ = strconv.ParseBool(setting.Value)
1057 }
1058 }
1059
1060 if vcsRevision != "" {
1061 var modified string
1062 if vcsModified {
1063 modified = "+modified"
1064 }
1065 full = fmt.Sprintf("%s%s (%s)", vcsRevision, modified, vcsTime.Format(time.RFC822))
1066 simple = vcsRevision
1067
1068 // use short checksum for simple, if hex-only
1069 if _, err := hex.DecodeString(simple); err == nil {
1070 simple = simple[:8]
1071 }
1072
1073 // append date to simple since it can be convenient
1074 // to know the commit date as part of the version
1075 if !vcsTime.IsZero() {
1076 simple += "-" + vcsTime.Format("20060102")
1077 }
1078 }
1079 }
1080
1081 if full == "" {
1082 if CustomVersion != "" {
1083 full = CustomVersion
1084 } else {
1085 full = "unknown"
1086 }
1087 } else if CustomVersion != "" {
1088 full = CustomVersion + " " + full
1089 }
1090
1091 if simple == "" || simple == "(devel)" {
1092 if CustomVersion != "" {
1093 simple = CustomVersion
1094 } else {
1095 simple = "unknown"
1096 }
1097 }
1098
1099 return simple, full
1100 }
1101
1102 // Event represents something that has happened or is happening.
1103 // An Event value is not synchronized, so it should be copied if
1104 // being used in goroutines.
1105 //
1106 // EXPERIMENTAL: Events are subject to change.
1107 type Event struct {
1108 // If non-nil, the event has been aborted, meaning
1109 // propagation has stopped to other handlers and
1110 // the code should stop what it was doing. Emitters
1111 // may choose to use this as a signal to adjust their
1112 // code path appropriately.
1113 Aborted error
1114
1115 // The data associated with the event. Usually the
1116 // original emitter will be the only one to set or
1117 // change these values, but the field is exported
1118 // so handlers can have full access if needed.
1119 // However, this map is not synchronized, so
1120 // handlers must not use this map directly in new
1121 // goroutines; instead, copy the map to use it in a
1122 // goroutine. Data may be nil.
1123 Data map[string]any
1124
1125 id uuid.UUID
1126 ts time.Time
1127 name string
1128 origin Module
1129 }
1130
1131 // NewEvent creates a new event, but does not emit the event. To emit an
1132 // event, call Emit() on the current instance of the caddyevents app instead.
1133 //
1134 // EXPERIMENTAL: Subject to change.
1135 func NewEvent(ctx Context, name string, data map[string]any) (Event, error) {
1136 id, err := uuid.NewRandom()
1137 if err != nil {
1138 return Event{}, fmt.Errorf("generating new event ID: %v", err)
1139 }
1140 name = strings.ToLower(name)
1141 return Event{
1142 Data: data,
1143 id: id,
1144 ts: time.Now(),
1145 name: name,
1146 origin: ctx.Module(),
1147 }, nil
1148 }
1149
1150 func (e Event) ID() uuid.UUID { return e.id }
1151 func (e Event) Timestamp() time.Time { return e.ts }
1152 func (e Event) Name() string { return e.name }
1153 func (e Event) Origin() Module { return e.origin } // Returns the module that originated the event. May be nil, usually if caddy core emits the event.
1154
1155 // CloudEvent exports event e as a structure that, when
1156 // serialized as JSON, is compatible with the
1157 // CloudEvents spec.
1158 func (e Event) CloudEvent() CloudEvent {
1159 dataJSON, _ := json.Marshal(e.Data)
1160 var source string
1161 if e.Origin() == nil {
1162 source = "caddy"
1163 } else {
1164 source = string(e.Origin().CaddyModule().ID)
1165 }
1166 return CloudEvent{
1167 ID: e.id.String(),
1168 Source: source,
1169 SpecVersion: "1.0",
1170 Type: e.name,
1171 Time: e.ts,
1172 DataContentType: "application/json",
1173 Data: dataJSON,
1174 }
1175 }
1176
1177 // CloudEvent is a JSON-serializable structure that
1178 // is compatible with the CloudEvents specification.
1179 // See https://cloudevents.io.
1180 // EXPERIMENTAL: Subject to change.
1181 type CloudEvent struct {
1182 ID string `json:"id"`
1183 Source string `json:"source"`
1184 SpecVersion string `json:"specversion"`
1185 Type string `json:"type"`
1186 Time time.Time `json:"time"`
1187 DataContentType string `json:"datacontenttype,omitempty"`
1188 Data json.RawMessage `json:"data,omitempty"`
1189 }
1190
1191 // ErrEventAborted cancels an event.
1192 var ErrEventAborted = errors.New("event aborted")
1193
1194 // ActiveContext returns the currently-active context.
1195 // This function is experimental and might be changed
1196 // or removed in the future.
1197 func ActiveContext() Context {
1198 currentCtxMu.RLock()
1199 defer currentCtxMu.RUnlock()
1200 return currentCtx
1201 }
1202
1203 // CtxKey is a value type for use with context.WithValue.
1204 type CtxKey string
1205
1206 // This group of variables pertains to the current configuration.
1207 var (
1208 // currentCtx is the root context for the currently-running
1209 // configuration, which can be accessed through this value.
1210 // If the Config contained in this value is not nil, then
1211 // a config is currently active/running.
1212 currentCtx Context
1213 currentCtxMu sync.RWMutex
1214
1215 // rawCfg is the current, generic-decoded configuration;
1216 // we initialize it as a map with one field ("config")
1217 // to maintain parity with the API endpoint and to avoid
1218 // the special case of having to access/mutate the variable
1219 // directly without traversing into it.
1220 rawCfg = map[string]any{
1221 rawConfigKey: nil,
1222 }
1223
1224 // rawCfgJSON is the JSON-encoded form of rawCfg. Keeping
1225 // this around avoids an extra Marshal call during changes.
1226 rawCfgJSON []byte
1227
1228 // rawCfgIndex is the map of user-assigned ID to expanded
1229 // path, for converting /id/ paths to /config/ paths.
1230 rawCfgIndex map[string]string
1231
1232 // rawCfgMu protects all the rawCfg fields and also
1233 // essentially synchronizes config changes/reloads.
1234 rawCfgMu sync.RWMutex
1235 )
1236
1237 // lastConfigFile and lastConfigAdapter remember the source config
1238 // file and adapter used when Caddy was started via the CLI "run" command.
1239 // These are consulted by the SIGUSR1 handler to attempt reloading from
1240 // the same source. They are intentionally not set for other entrypoints
1241 // such as "caddy start" or subcommands like file-server.
1242 var (
1243 lastConfigMu sync.RWMutex
1244 lastConfigFile string
1245 lastConfigAdapter string
1246 )
1247
1248 // reloadFromSourceFunc is the type of stored callback
1249 // which is called when we receive a SIGUSR1 signal.
1250 type reloadFromSourceFunc func(file, adapter string) error
1251
1252 // reloadFromSourceCallback is the stored callback
1253 // which is called when we receive a SIGUSR1 signal.
1254 var reloadFromSourceCallback reloadFromSourceFunc
1255
1256 // errReloadFromSourceUnavailable is returned when no reload-from-source callback is set.
1257 var errReloadFromSourceUnavailable = errors.New("reload from source unavailable in this process") //nolint:unused
1258
1259 // SetLastConfig records the given source file and adapter as the
1260 // last-known external configuration source. Intended to be called
1261 // only when starting via "caddy run --config <file> --adapter <adapter>".
1262 func SetLastConfig(file, adapter string, fn reloadFromSourceFunc) {
1263 lastConfigMu.Lock()
1264 lastConfigFile = file
1265 lastConfigAdapter = adapter
1266 reloadFromSourceCallback = fn
1267 lastConfigMu.Unlock()
1268 }
1269
1270 // ClearLastConfigIfDifferent clears the recorded last-config if the provided
1271 // source file/adapter do not match the recorded last-config. If both srcFile
1272 // and srcAdapter are empty, the last-config is cleared.
1273 func ClearLastConfigIfDifferent(srcFile, srcAdapter string) {
1274 if (srcFile != "" || srcAdapter != "") && lastConfigMatches(srcFile, srcAdapter) {
1275 return
1276 }
1277 SetLastConfig("", "", nil)
1278 }
1279
1280 // getLastConfig returns the last-known config file and adapter.
1281 func getLastConfig() (file, adapter string, fn reloadFromSourceFunc) {
1282 lastConfigMu.RLock()
1283 f, a, cb := lastConfigFile, lastConfigAdapter, reloadFromSourceCallback
1284 lastConfigMu.RUnlock()
1285 return f, a, cb
1286 }
1287
1288 // lastConfigMatches returns true if the provided source file and/or adapter
1289 // matches the recorded last-config. Matching rules (in priority order):
1290 // 1. If srcAdapter is provided and differs from the recorded adapter, no match.
1291 // 2. If srcFile exactly equals the recorded file, match.
1292 // 3. If both sides can be made absolute and equal, match.
1293 // 4. If basenames are equal, match.
1294 func lastConfigMatches(srcFile, srcAdapter string) bool {
1295 lf, la, _ := getLastConfig()
1296
1297 // If adapter is provided, it must match.
1298 if srcAdapter != "" && srcAdapter != la {
1299 return false
1300 }
1301
1302 // Quick equality check.
1303 if srcFile == lf {
1304 return true
1305 }
1306
1307 // Try absolute path comparison.
1308 sAbs, sErr := filepath.Abs(srcFile)
1309 lAbs, lErr := filepath.Abs(lf)
1310 if sErr == nil && lErr == nil && sAbs == lAbs {
1311 return true
1312 }
1313
1314 // Final fallback: basename equality.
1315 if filepath.Base(srcFile) == filepath.Base(lf) {
1316 return true
1317 }
1318
1319 return false
1320 }
1321
1322 // errSameConfig is returned if the new config is the same
1323 // as the old one. This isn't usually an actual, actionable
1324 // error; it's mostly a sentinel value.
1325 var errSameConfig = errors.New("config is unchanged")
1326
1327 // ImportPath is the package import path for Caddy core.
1328 // This identifier may be removed in the future.
1329 const ImportPath = "github.com/caddyserver/caddy/v2"