cmd: Fix validate command when JSON contains "@id" fields

Also, don't run admin server when validating...
This commit is contained in:
Matthew Holt 2019-12-12 14:30:22 -07:00
parent 2e0615270d
commit f935458e3e
No known key found for this signature in database
GPG key ID: 2A349DD577D586A5
3 changed files with 22 additions and 13 deletions

View file

@ -734,6 +734,20 @@ traverseLoop:
return nil return nil
} }
// RemoveMetaFields removes meta fields like "@id" from a JSON message.
func RemoveMetaFields(rawJSON []byte) []byte {
return idRegexp.ReplaceAllFunc(rawJSON, func(in []byte) []byte {
// matches with a comma on both sides (when "@id" property is
// not the first or last in the object) need to keep exactly
// one comma for correct JSON syntax
comma := []byte{','}
if bytes.HasPrefix(in, comma) && bytes.HasSuffix(in, comma) {
return comma
}
return []byte{}
})
}
// AdminHandler is like http.Handler except ServeHTTP may return an error. // AdminHandler is like http.Handler except ServeHTTP may return an error.
// //
// If any handler encounters an error, it should be returned for proper // If any handler encounters an error, it should be returned for proper

View file

@ -153,16 +153,7 @@ func changeConfig(method, path string, input []byte, forceReload bool) error {
// (an alternate way to do this would be to delete them from // (an alternate way to do this would be to delete them from
// rawCfg as they are indexed, then iterate the index we made // rawCfg as they are indexed, then iterate the index we made
// and add them back after encoding as JSON) // and add them back after encoding as JSON)
newCfg = idRegexp.ReplaceAllFunc(newCfg, func(in []byte) []byte { newCfg = RemoveMetaFields(newCfg)
// matches with a comma on both sides (when "@id" property is
// not the first or last in the object) need to keep exactly
// one comma for correct JSON syntax
comma := []byte{','}
if bytes.HasPrefix(in, comma) && bytes.HasSuffix(in, comma) {
return comma
}
return []byte{}
})
// load this new config; if it fails, we need to revert to // load this new config; if it fails, we need to revert to
// our old representation of caddy's actual config // our old representation of caddy's actual config
@ -295,10 +286,12 @@ func run(newCfg *Config, start bool) error {
var err error var err error
// start the admin endpoint (and stop any prior one) // start the admin endpoint (and stop any prior one)
if start {
err = replaceAdmin(newCfg) err = replaceAdmin(newCfg)
if err != nil { if err != nil {
return fmt.Errorf("starting caddy administration endpoint: %v", err) return fmt.Errorf("starting caddy administration endpoint: %v", err)
} }
}
if newCfg == nil { if newCfg == nil {
return nil return nil

View file

@ -465,6 +465,8 @@ func cmdValidateConfig(fl Flags) (int, error) {
input = adaptedConfig input = adaptedConfig
} }
input = caddy.RemoveMetaFields(input)
var cfg *caddy.Config var cfg *caddy.Config
err = json.Unmarshal(input, &cfg) err = json.Unmarshal(input, &cfg)
if err != nil { if err != nil {