httpcaddyfile: remove certificate tags from global state (#3111)

* remove the certificate tag tracking from global state

* refactored helper state, added log counter

* moved state initialisation close to where it is used.

* added helper state comment
This commit is contained in:
Mark Sargent 2020-03-05 05:58:49 +13:00 committed by GitHub
parent e6c6210772
commit 26fb8b3efd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 11 deletions

View file

@ -152,6 +152,18 @@ func parseTLS(h Helper) ([]ConfigValue, error) {
// policy that is looking for any tag but the last one to be // policy that is looking for any tag but the last one to be
// loaded won't find it, and TLS handshakes will fail (see end) // loaded won't find it, and TLS handshakes will fail (see end)
// of issue #3004) // of issue #3004)
// tlsCertTags maps certificate filenames to their tag.
// This is used to remember which tag is used for each
// certificate files, since we need to avoid loading
// the same certificate files more than once, overwriting
// previous tags
tlsCertTags, ok := h.State["tlsCertTags"].(map[string]string)
if !ok {
tlsCertTags = make(map[string]string)
h.State["tlsCertTags"] = tlsCertTags
}
tag, ok := tlsCertTags[certFilename] tag, ok := tlsCertTags[certFilename]
if !ok { if !ok {
// haven't seen this cert file yet, let's give it a tag // haven't seen this cert file yet, let's give it a tag
@ -521,10 +533,17 @@ func parseLog(h Helper) ([]ConfigValue, error) {
var val namedCustomLog var val namedCustomLog
if !reflect.DeepEqual(cl, new(caddy.CustomLog)) { if !reflect.DeepEqual(cl, new(caddy.CustomLog)) {
logCounter, ok := h.State["logCounter"].(int)
if !ok {
logCounter = 0
}
cl.Include = []string{"http.log.access"} cl.Include = []string{"http.log.access"}
val.name = fmt.Sprintf("log%d", logCounter) val.name = fmt.Sprintf("log%d", logCounter)
val.log = cl val.log = cl
logCounter++ logCounter++
h.State["logCounter"] = logCounter
} }
configValues = append(configValues, ConfigValue{ configValues = append(configValues, ConfigValue{
Class: "custom_log", Class: "custom_log",
@ -533,12 +552,3 @@ func parseLog(h Helper) ([]ConfigValue, error) {
} }
return configValues, nil return configValues, nil
} }
// tlsCertTags maps certificate filenames to their tag.
// This is used to remember which tag is used for each
// certificate files, since we need to avoid loading
// the same certificate files more than once, overwriting
// previous tags
var tlsCertTags = make(map[string]string)
var logCounter int

View file

@ -114,6 +114,8 @@ func RegisterHandlerDirective(dir string, setupFunc UnmarshalHandlerFunc) {
// Caddyfile tokens. // Caddyfile tokens.
type Helper struct { type Helper struct {
*caddyfile.Dispenser *caddyfile.Dispenser
// State stores intermediate variables during caddyfile adaptation.
State map[string]interface{}
options map[string]interface{} options map[string]interface{}
warnings *[]caddyconfig.Warning warnings *[]caddyconfig.Warning
matcherDefs map[string]caddy.ModuleMap matcherDefs map[string]caddy.ModuleMap

View file

@ -42,6 +42,7 @@ func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock,
options map[string]interface{}) (*caddy.Config, []caddyconfig.Warning, error) { options map[string]interface{}) (*caddy.Config, []caddyconfig.Warning, error) {
var warnings []caddyconfig.Warning var warnings []caddyconfig.Warning
gc := counter{new(int)} gc := counter{new(int)}
state := make(map[string]interface{})
// load all the server blocks and associate them with a "pile" // load all the server blocks and associate them with a "pile"
// of config values; also prohibit duplicate keys because they // of config values; also prohibit duplicate keys because they
@ -133,14 +134,17 @@ func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock,
return nil, warnings, fmt.Errorf("%s:%d: unrecognized directive: %s", tkn.File, tkn.Line, dir) return nil, warnings, fmt.Errorf("%s:%d: unrecognized directive: %s", tkn.File, tkn.Line, dir)
} }
results, err := dirFunc(Helper{ h := Helper{
Dispenser: caddyfile.NewDispenser(segment), Dispenser: caddyfile.NewDispenser(segment),
options: options, options: options,
warnings: &warnings, warnings: &warnings,
matcherDefs: matcherDefs, matcherDefs: matcherDefs,
parentBlock: sb.block, parentBlock: sb.block,
groupCounter: gc, groupCounter: gc,
}) State: state,
}
results, err := dirFunc(h)
if err != nil { if err != nil {
return nil, warnings, fmt.Errorf("parsing caddyfile tokens for '%s': %v", dir, err) return nil, warnings, fmt.Errorf("parsing caddyfile tokens for '%s': %v", dir, err)
} }