Commit 21d92d68 authored by Toby Allen's avatar Toby Allen Committed by Matt Holt

Add a cli parameter to -validate a Caddyfile. Issue #1328 (#1344)

* Allow -validate flag to validate caddyfile and return

* Ensure logging without -log flag

* Changes to validate seperatly to Starup func

* Removed change to Start signature.  Created function to ValidateCaddyfile

* comment and tidyup

* ValidateandExecuteDirectives with justValidate option

* remove debugging code

* Tidy up comments

* additional parameter added to calls to mustLogFataf

* ValidateAndExecuteDirectives needs to only return err
parent 69679278
...@@ -432,31 +432,7 @@ func startWithListenerFds(cdyfile Input, inst *Instance, restartFds map[string]r ...@@ -432,31 +432,7 @@ func startWithListenerFds(cdyfile Input, inst *Instance, restartFds map[string]r
cdyfile = CaddyfileInput{} cdyfile = CaddyfileInput{}
} }
stypeName := cdyfile.ServerType() err := ValidateAndExecuteDirectives(cdyfile, inst, false)
stype, err := getServerType(stypeName)
if err != nil {
return err
}
inst.caddyfileInput = cdyfile
sblocks, err := loadServerBlocks(stypeName, cdyfile.Path(), bytes.NewReader(cdyfile.Body()))
if err != nil {
return err
}
inst.context = stype.NewContext()
if inst.context == nil {
return fmt.Errorf("server type %s produced a nil Context", stypeName)
}
sblocks, err = inst.context.InspectServerBlocks(cdyfile.Path(), sblocks)
if err != nil {
return err
}
err = executeDirectives(inst, cdyfile.Path(), stype.Directives(), sblocks)
if err != nil { if err != nil {
return err return err
} }
...@@ -516,9 +492,48 @@ func startWithListenerFds(cdyfile Input, inst *Instance, restartFds map[string]r ...@@ -516,9 +492,48 @@ func startWithListenerFds(cdyfile Input, inst *Instance, restartFds map[string]r
return nil return nil
} }
func executeDirectives(inst *Instance, filename string, func ValidateAndExecuteDirectives(cdyfile Input, inst *Instance, justValidate bool) error {
directives []string, sblocks []caddyfile.ServerBlock) error {
// If parsing only inst will be nil, create an instance for this function call only.
if justValidate {
inst = &Instance{serverType: cdyfile.ServerType(), wg: new(sync.WaitGroup)}
}
stypeName := cdyfile.ServerType()
stype, err := getServerType(stypeName)
if err != nil {
return err
}
inst.caddyfileInput = cdyfile
sblocks, err := loadServerBlocks(stypeName, cdyfile.Path(), bytes.NewReader(cdyfile.Body()))
if err != nil {
return err
}
inst.context = stype.NewContext()
if inst.context == nil {
return fmt.Errorf("server type %s produced a nil Context", stypeName)
}
sblocks, err = inst.context.InspectServerBlocks(cdyfile.Path(), sblocks)
if err != nil {
return err
}
err = executeDirectives(inst, cdyfile.Path(), stype.Directives(), sblocks, justValidate)
if err != nil {
return err
}
return nil
}
func executeDirectives(inst *Instance, filename string,
directives []string, sblocks []caddyfile.ServerBlock, justValidate bool) error {
// map of server block ID to map of directive name to whatever. // map of server block ID to map of directive name to whatever.
storages := make(map[int]map[string]interface{}) storages := make(map[int]map[string]interface{})
...@@ -568,6 +583,7 @@ func executeDirectives(inst *Instance, filename string, ...@@ -568,6 +583,7 @@ func executeDirectives(inst *Instance, filename string,
} }
} }
if !justValidate {
// See if there are any callbacks to execute after this directive // See if there are any callbacks to execute after this directive
if allCallbacks, ok := parsingCallbacks[inst.serverType]; ok { if allCallbacks, ok := parsingCallbacks[inst.serverType]; ok {
callbacks := allCallbacks[dir] callbacks := allCallbacks[dir]
...@@ -578,6 +594,7 @@ func executeDirectives(inst *Instance, filename string, ...@@ -578,6 +594,7 @@ func executeDirectives(inst *Instance, filename string,
} }
} }
} }
}
return nil return nil
} }
......
...@@ -40,6 +40,7 @@ func init() { ...@@ -40,6 +40,7 @@ func init() {
flag.StringVar(&revoke, "revoke", "", "Hostname for which to revoke the certificate") flag.StringVar(&revoke, "revoke", "", "Hostname for which to revoke the certificate")
flag.StringVar(&serverType, "type", "http", "Type of server to run") flag.StringVar(&serverType, "type", "http", "Type of server to run")
flag.BoolVar(&version, "version", false, "Show version") flag.BoolVar(&version, "version", false, "Show version")
flag.BoolVar(&validate, "validate", false, "Parse the Caddyfile but do not start the server")
caddy.RegisterCaddyfileLoader("flag", caddy.LoaderFunc(confLoader)) caddy.RegisterCaddyfileLoader("flag", caddy.LoaderFunc(confLoader))
caddy.SetDefaultCaddyfileLoader("default", caddy.LoaderFunc(defaultLoader)) caddy.SetDefaultCaddyfileLoader("default", caddy.LoaderFunc(defaultLoader))
...@@ -74,7 +75,7 @@ func Run() { ...@@ -74,7 +75,7 @@ func Run() {
if revoke != "" { if revoke != "" {
err := caddytls.Revoke(revoke) err := caddytls.Revoke(revoke)
if err != nil { if err != nil {
mustLogFatalf(err.Error()) mustLogFatalf("%v", err.Error())
} }
fmt.Printf("Revoked certificate for %s\n", revoke) fmt.Printf("Revoked certificate for %s\n", revoke)
os.Exit(0) os.Exit(0)
...@@ -94,19 +95,29 @@ func Run() { ...@@ -94,19 +95,29 @@ func Run() {
// Set CPU cap // Set CPU cap
err := setCPU(cpu) err := setCPU(cpu)
if err != nil { if err != nil {
mustLogFatalf(err.Error()) mustLogFatalf("%v", err.Error())
} }
// Get Caddyfile input // Get Caddyfile input
caddyfile, err := caddy.LoadCaddyfile(serverType) caddyfileinput, err := caddy.LoadCaddyfile(serverType)
if err != nil { if err != nil {
mustLogFatalf(err.Error()) mustLogFatalf("%v", err.Error())
}
if validate {
justValidate := true
err := caddy.ValidateAndExecuteDirectives(caddyfileinput, nil, justValidate)
if err != nil {
mustLogFatalf("%v", err.Error())
}
log.Println("[INFO] Caddyfile Valid")
os.Exit(0)
} }
// Start your engines // Start your engines
instance, err := caddy.Start(caddyfile) instance, err := caddy.Start(caddyfileinput)
if err != nil { if err != nil {
mustLogFatalf(err.Error()) mustLogFatalf("%v", err.Error())
} }
// Twiddle your thumbs // Twiddle your thumbs
...@@ -226,6 +237,7 @@ var ( ...@@ -226,6 +237,7 @@ var (
revoke string revoke string
version bool version bool
plugins bool plugins bool
validate bool
) )
// Build information obtained with the help of -ldflags // Build information obtained with the help of -ldflags
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment