Skip to content

Commit 9ef3696

Browse files
committed
MEDIUM: Add support for the crt-store section
With all its keywords: crt-base, key-base and load.
1 parent 86ef212 commit 9ef3696

13 files changed

+549
-1
lines changed

init.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type ConfiguredParsers struct {
4444
Ring *Parsers
4545
LogForward *Parsers
4646
FCGIApp *Parsers
47+
CrtStore *Parsers
4748
// spoe parsers
4849
SPOEAgent *Parsers
4950
SPOEGroup *Parsers
@@ -93,4 +94,5 @@ func (p *configParser) initParserMaps() {
9394
p.Parsers[Ring] = map[string]*Parsers{}
9495
p.Parsers[LogForward] = map[string]*Parsers{}
9596
p.Parsers[FCGIApp] = map[string]*Parsers{}
97+
p.Parsers[CrtStore] = map[string]*Parsers{}
9698
}

parser.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const (
4646
Ring Section = "ring"
4747
LogForward Section = "log-forward"
4848
FCGIApp Section = "fcgi-app"
49+
CrtStore Section = "crt-store"
4950
// spoe sections
5051
SPOEAgent Section = "spoe-agent"
5152
SPOEGroup Section = "spoe-group"

parsers/crt-store-load.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
Copyright 2024 HAProxy Technologies
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package parsers
18+
19+
import (
20+
"strings"
21+
22+
"github.com/haproxytech/config-parser/v5/common"
23+
"github.com/haproxytech/config-parser/v5/errors"
24+
"github.com/haproxytech/config-parser/v5/types"
25+
)
26+
27+
type LoadCert struct {
28+
data []types.LoadCert
29+
preComments []string // comments that appear before the actual line
30+
}
31+
32+
func (p *LoadCert) parseError(line string) *errors.ParseError {
33+
return &errors.ParseError{Parser: "LoadCert", Line: line}
34+
}
35+
36+
func (p *LoadCert) parse(line string, parts []string, comment string) (*types.LoadCert, error) {
37+
if len(parts) < 3 {
38+
return nil, p.parseError(line)
39+
}
40+
if parts[0] != "load" {
41+
return nil, p.parseError(line)
42+
}
43+
44+
load := new(types.LoadCert)
45+
46+
for i := 1; i < len(parts); i++ {
47+
element := parts[i]
48+
switch element {
49+
case "crt":
50+
CheckParsePair(parts, &i, &load.Certificate)
51+
case "alias":
52+
CheckParsePair(parts, &i, &load.Alias)
53+
case "key":
54+
CheckParsePair(parts, &i, &load.Key)
55+
case "ocsp":
56+
CheckParsePair(parts, &i, &load.Ocsp)
57+
case "issuer":
58+
CheckParsePair(parts, &i, &load.Issuer)
59+
case "sctl":
60+
CheckParsePair(parts, &i, &load.Sctl)
61+
case "ocsp-update":
62+
i++
63+
load.OcspUpdate = new(bool)
64+
if parts[i] == "on" {
65+
*load.OcspUpdate = true
66+
} else if parts[i] != "off" {
67+
return nil, p.parseError(line)
68+
}
69+
}
70+
}
71+
load.Comment = comment
72+
73+
// crt is mandatory
74+
if load.Certificate == "" {
75+
return nil, p.parseError(line)
76+
}
77+
78+
return load, nil
79+
}
80+
81+
func (p *LoadCert) Result() ([]common.ReturnResultLine, error) {
82+
if len(p.data) == 0 {
83+
return nil, errors.ErrFetch
84+
}
85+
86+
result := make([]common.ReturnResultLine, len(p.data))
87+
sb := new(strings.Builder)
88+
89+
for i, load := range p.data {
90+
sb.Reset()
91+
sb.WriteString("load")
92+
CheckWritePair(sb, "crt", load.Certificate)
93+
CheckWritePair(sb, "alias", load.Alias)
94+
CheckWritePair(sb, "key", load.Key)
95+
CheckWritePair(sb, "ocsp", load.Ocsp)
96+
CheckWritePair(sb, "issuer", load.Issuer)
97+
CheckWritePair(sb, "sctl", load.Sctl)
98+
CheckWritePair(sb, "ocsp-update", fmtOnOff(load.OcspUpdate))
99+
100+
result[i] = common.ReturnResultLine{
101+
Data: sb.String(),
102+
Comment: load.Comment,
103+
}
104+
}
105+
106+
return result, nil
107+
}
108+
109+
func fmtOnOff(b *bool) string {
110+
if b == nil {
111+
return ""
112+
}
113+
if *b {
114+
return "on"
115+
}
116+
return "off"
117+
}

parsers/load_generated.go

Lines changed: 157 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

reader.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,16 @@ func (p *configParser) ProcessLine(line string, parts []string, comment string,
342342
if p.Options.Log {
343343
p.Options.Logger.Tracef("%log-forward section %s active", p.Options.LogPrefix, data.Name)
344344
}
345+
case "crt-store":
346+
parserSectionName := parser.(*extra.Section) //nolint:forcetypeassert
347+
rawData, _ := parserSectionName.Get(false)
348+
data := rawData.(*types.Section) //nolint:forcetypeassert
349+
config.CrtStore = p.getCrtStoreParser()
350+
p.Parsers[CrtStore][data.Name] = config.CrtStore
351+
config.Active = config.CrtStore
352+
if p.Options.Log {
353+
p.Options.Logger.Tracef("%scrt-store section %s active", p.Options.LogPrefix, data.Name)
354+
}
345355
case "snippet_beg":
346356
config.Previous = config.Active
347357
config.Active = &Parsers{

section-parsers.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func (p *configParser) createParsers(parser map[string]ParserInterface, sequence
4848
addParser(parser, &sequence, &extra.Section{Name: "ring"})
4949
addParser(parser, &sequence, &extra.Section{Name: "log-forward"})
5050
addParser(parser, &sequence, &extra.Section{Name: "fcgi-app"})
51+
addParser(parser, &sequence, &extra.Section{Name: "crt-store"})
5152
if !p.Options.DisableUnProcessed {
5253
addParser(parser, &sequence, &extra.UnProcessed{})
5354
}
@@ -938,3 +939,12 @@ func (p *configParser) getLogForwardParser() *Parsers {
938939
addParser(parser, &sequence, &simple.Timeout{Name: "client"})
939940
return p.createParsers(parser, sequence)
940941
}
942+
943+
func (p *configParser) getCrtStoreParser() *Parsers {
944+
parser := map[string]ParserInterface{}
945+
sequence := []Section{}
946+
addParser(parser, &sequence, &simple.Word{Name: "crt-base"})
947+
addParser(parser, &sequence, &simple.Word{Name: "key-base"})
948+
addParser(parser, &sequence, &parsers.LoadCert{})
949+
return p.createParsers(parser, sequence)
950+
}

tests/configs/haproxy.cfg.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ cache foobar
166166
total-max-size 4
167167
max-age 240
168168
169+
crt-store tpm2
170+
crt-base /c
171+
key-base /k
172+
load crt example.com.pem alias example
173+
load crt lol.pem
174+
169175
frontend healthz from A
170176
mode http
171177
monitor-uri /healthz

tests/configs/haproxy_generated.cfg.go

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)