Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 12, 23:15 UTC
cmd: Require config for caddy validate (fix #5612) (#5614)
* Require config for caddy validate - fixes #5612 Signed-off-by: Pistasj <hi@pistasjis.net> * Try making adjacent Caddyfile check its own function Signed-off-by: Pistasj <hi@pistasjis.net> * add Francis' suggestion Co-authored-by: Francis Lavoie <lavofr@gmail.com> * Refactor * Fix borked commit, sigh --------- Signed-off-by: Pistasj <hi@pistasjis.net> Co-authored-by: Francis Lavoie <lavofr@gmail.com> Co-authored-by: Matthew Holt <mholt@users.noreply.github.com>
pistasjis committed
Aug 9, 2023 at 19:40 UTC
d8135505d38ca872d507418d2017f4dbf352f06a
2 files changed
+43
-19
cmd/commandfuncs.go
+41
-16
@@ -22,6 +22,7 @@ import (
22
"errors"
23
"fmt"
24
"io"
25
+ "io/fs"
26
"log"
27
"net"
28
"net/http"
@@ -423,24 +424,12 @@ func cmdAdaptConfig(fl Flags) (int, error) {
424
adaptCmdPrettyFlag := fl.Bool("pretty")
425
adaptCmdValidateFlag := fl.Bool("validate")
426
426
- // if no input file was specified, try a default
427
- // Caddyfile if the Caddyfile adapter is plugged in
428
- if adaptCmdInputFlag == "" && caddyconfig.GetAdapter("caddyfile") != nil {
429
- _, err := os.Stat("Caddyfile")
430
- if err == nil {
431
- // default Caddyfile exists
432
- adaptCmdInputFlag = "Caddyfile"
433
- caddy.Log().Info("using adjacent Caddyfile")
434
- } else if !os.IsNotExist(err) {
435
- // default Caddyfile exists, but error accessing it
436
- return caddy.ExitCodeFailedStartup, fmt.Errorf("accessing default Caddyfile: %v", err)
437
- }
427
+ var err error
428
+ adaptCmdInputFlag, err = configFileWithRespectToDefault(caddy.Log(), adaptCmdInputFlag)
429
+ if err != nil {
430
+ return caddy.ExitCodeFailedStartup, err
431
}
432
440
- if adaptCmdInputFlag == "" {
441
- return caddy.ExitCodeFailedStartup,
442
- fmt.Errorf("input file required when there is no Caddyfile in current directory (use --config flag)")
443
- }
433
if adaptCmdAdapterFlag == "" {
434
return caddy.ExitCodeFailedStartup,
435
fmt.Errorf("adapter name is required (use --adapt flag or leave unspecified for default)")
@@ -517,6 +506,17 @@ func cmdValidateConfig(fl Flags) (int, error) {
506
}
507
}
508
509
+ // use default config and ensure a config file is specified
510
+ var err error
511
+ validateCmdConfigFlag, err = configFileWithRespectToDefault(caddy.Log(), validateCmdConfigFlag)
512
+ if err != nil {
513
+ return caddy.ExitCodeFailedStartup, err
514
+ }
515
+ if validateCmdConfigFlag == "" {
516
+ return caddy.ExitCodeFailedStartup,
517
+ fmt.Errorf("input file required when there is no Caddyfile in current directory (use --config flag)")
518
+ }
519
+
520
input, _, err := LoadConfig(validateCmdConfigFlag, validateCmdAdapterFlag)
521
if err != nil {
522
return caddy.ExitCodeFailedStartup, err
@@ -736,6 +736,31 @@ func DetermineAdminAPIAddress(address string, config []byte, configFile, configA
736
return caddy.DefaultAdminListen, nil
737
}
738
739
+// configFileWithRespectToDefault returns the filename to use for loading the config, based
740
+// on whether a config file is already specified and a supported default config file exists.
741
+func configFileWithRespectToDefault(logger *zap.Logger, configFile string) (string, error) {
742
+ const defaultCaddyfile = "Caddyfile"
743
+
744
+ // if no input file was specified, try a default Caddyfile if the Caddyfile adapter is plugged in
745
+ if configFile == "" && caddyconfig.GetAdapter("caddyfile") != nil {
746
+ _, err := os.Stat(defaultCaddyfile)
747
+ if err == nil {
748
+ // default Caddyfile exists
749
+ if logger != nil {
750
+ logger.Info("using adjacent Caddyfile")
751
+ }
752
+ return defaultCaddyfile, nil
753
+ }
754
+ if !errors.Is(err, fs.ErrNotExist) {
755
+ // problem checking
756
+ return configFile, fmt.Errorf("checking if default Caddyfile exists: %v", err)
757
+ }
758
+ }
759
+
760
+ // default config file does not exist or is irrelevant
761
+ return configFile, nil
762
+}
763
+
764
type moduleInfo struct {
765
caddyModuleID string
766
goModule *debug.Module
cmd/main.go
+2
-3
@@ -117,9 +117,8 @@ func loadConfigWithLogger(logger *zap.Logger, configFile, adapterName string) ([
117
zap.String("config_adapter", adapterName))
118
}
119
} else if adapterName == "" {
120
- // as a special case when no config file or adapter
121
- // is specified, see if the Caddyfile adapter is
122
- // plugged in, and if so, try using a default Caddyfile
120
+ // if the Caddyfile adapter is plugged in, we can try using an
121
+ // adjacent Caddyfile by default
122
cfgAdapter = caddyconfig.GetAdapter("caddyfile")
123
if cfgAdapter != nil {
124
config, err = os.ReadFile("Caddyfile")