@marcel91200 / caddy Mirror · read-only / commits / abad9bc2
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 13, 16:45 UTC

cmd: Fix reload with stdin (#4900)

Francis Lavoie committed Jul 20, 2022 at 20:14 UTC abad9bc256eea7353b9959d066f5b7ea64990a22
2 files changed +27 -18
cmd/commandfuncs.go
+25 -16
@@ -280,7 +280,7 @@ func cmdStop(fl Flags) (int, error) {
280 configFlag := fl.String("config")
281 configAdapterFlag := fl.String("adapter")
282
283 - adminAddr, err := DetermineAdminAPIAddress(addrFlag, configFlag, configAdapterFlag)
283 + adminAddr, err := DetermineAdminAPIAddress(addrFlag, nil, configFlag, configAdapterFlag)
284 if err != nil {
285 return caddy.ExitCodeFailedStartup, fmt.Errorf("couldn't determine admin API address: %v", err)
286 }
@@ -310,7 +310,7 @@ func cmdReload(fl Flags) (int, error) {
310 return caddy.ExitCodeFailedStartup, fmt.Errorf("no config file to load")
311 }
312
313 - adminAddr, err := DetermineAdminAPIAddress(addrFlag, configFlag, configAdapterFlag)
313 + adminAddr, err := DetermineAdminAPIAddress(addrFlag, config, configFlag, configAdapterFlag)
314 if err != nil {
315 return caddy.ExitCodeFailedStartup, fmt.Errorf("couldn't determine admin API address: %v", err)
316 }
@@ -732,10 +732,11 @@ func AdminAPIRequest(adminAddr, method, uri string, headers http.Header, body io
732
733 // DetermineAdminAPIAddress determines which admin API endpoint address should
734 // be used based on the inputs. By priority: if `address` is specified, then
735 -// it is returned; if `configFile` (and `configAdapter`) are specified, then that
736 -// config will be loaded to find the admin address; otherwise, the default
737 -// admin listen address will be returned.
738 -func DetermineAdminAPIAddress(address, configFile, configAdapter string) (string, error) {
735 +// it is returned; if `config` is specified, then that config will be used for
736 +// finding the admin address; if `configFile` (and `configAdapter`) are specified,
737 +// then that config will be loaded to find the admin address; otherwise, the
738 +// default admin listen address will be returned.
739 +func DetermineAdminAPIAddress(address string, config []byte, configFile, configAdapter string) (string, error) {
740 // Prefer the address if specified and non-empty
741 if address != "" {
742 return address, nil
@@ -743,21 +744,29 @@ func DetermineAdminAPIAddress(address, configFile, configAdapter string) (string
744
745 // Try to load the config from file if specified, with the given adapter name
746 if configFile != "" {
746 - // get the config in caddy's native format
747 - config, loadedConfigFile, err := LoadConfig(configFile, configAdapter)
748 - if err != nil {
749 - return "", err
750 - }
751 - if loadedConfigFile == "" {
752 - return "", fmt.Errorf("no config file to load")
747 + var loadedConfigFile string
748 + var err error
749 +
750 + // use the provided loaded config if non-empty
751 + // otherwise, load it from the specified file/adapter
752 + loadedConfig := config
753 + if len(loadedConfig) == 0 {
754 + // get the config in caddy's native format
755 + loadedConfig, loadedConfigFile, err = LoadConfig(configFile, configAdapter)
756 + if err != nil {
757 + return "", err
758 + }
759 + if loadedConfigFile == "" {
760 + return "", fmt.Errorf("no config file to load")
761 + }
762 }
763
755 - // get the address of the admin listener if set
756 - if len(config) > 0 {
764 + // get the address of the admin listener from the config
765 + if len(loadedConfig) > 0 {
766 var tmpStruct struct {
767 Admin caddy.AdminConfig `json:"admin"`
768 }
760 - err = json.Unmarshal(config, &tmpStruct)
769 + err := json.Unmarshal(loadedConfig, &tmpStruct)
770 if err != nil {
771 return "", fmt.Errorf("unmarshaling admin listener address from config: %v", err)
772 }
modules/caddypki/command.go
+2 -2
@@ -113,7 +113,7 @@ func cmdTrust(fl caddycmd.Flags) (int, error) {
113 }
114
115 // Determine where we're sending the request to get the CA info
116 - adminAddr, err := caddycmd.DetermineAdminAPIAddress(addrFlag, configFlag, configAdapterFlag)
116 + adminAddr, err := caddycmd.DetermineAdminAPIAddress(addrFlag, nil, configFlag, configAdapterFlag)
117 if err != nil {
118 return caddy.ExitCodeFailedStartup, fmt.Errorf("couldn't determine admin API address: %v", err)
119 }
@@ -182,7 +182,7 @@ func cmdUntrust(fl caddycmd.Flags) (int, error) {
182 }
183
184 // Determine where we're sending the request to get the CA info
185 - adminAddr, err := caddycmd.DetermineAdminAPIAddress(addrFlag, configFlag, configAdapterFlag)
185 + adminAddr, err := caddycmd.DetermineAdminAPIAddress(addrFlag, nil, configFlag, configAdapterFlag)
186 if err != nil {
187 return caddy.ExitCodeFailedStartup, fmt.Errorf("couldn't determine admin API address: %v", err)
188 }