Skip to content

Commit a261fcb

Browse files
committed
feat(coredhcp/bootloop): add support for config comments
Signed-off-by: Devon Bautista <17506592+synackd@users.noreply.github.com>
1 parent fd025bf commit a261fcb

3 files changed

Lines changed: 97 additions & 9 deletions

File tree

‎examples/coredhcp/coredhcp.yaml‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ server4:
249249
# overwritten if so. The format of the file is an Sqlite database.
250250
#
251251
# script_path (OPTIONAL, string, default=default)
252-
# The filesystem path to an PXE script to serve to DHCP requests. If
252+
# The filesystem path to an iPXE script to serve to DHCP requests. If
253253
# using the build-in coresmd FTP server, this should be 'default', which
254254
# will make bootloop serve a simple iPXE script that reboots.
255255
#
@@ -270,10 +270,15 @@ server4:
270270
# ipv4_end (REQUIRED, string)
271271
# The end IPv4 address for the pool of IPs to assign.
272272
- bootloop: |
273+
/* file to store DHCP leases in */
273274
lease_file=/tmp/coredhcp.db
275+
/* OPTIONAL: path to custom iPXE script to serve for DHCP requests (default is default script) */
274276
script_path=default
277+
/* OPTIONAL: lease duration (default 5m)
275278
lease_time=5m
279+
/* IPv4 address starting the range, inclusive */
276280
ipv4_start=172.16.0.156
281+
/* IPv4 address ending the range, inclusive */
277282
ipv4_end=172.16.0.200
278283
279284
###############################################################################

‎plugin/coredhcp/bootloop/main.go‎

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,40 @@ func setup4(args ...string) (handler.Handler4, error) {
154154
// errors occur, they are gathered into errs, a slice of errors, so that they
155155
// can be printed or handled.
156156
func parseConfig(argv ...string) (cfg Config, errs []error) {
157-
for idx, arg := range argv {
157+
var (
158+
idx int = 0 // Separate arg index so comments aren't counted
159+
insideComment bool = false // Track if inside a comment
160+
commentIdx int = -1 // Track where last comment began for printing error
161+
)
162+
for argIdx, arg := range argv {
163+
// Parse comments (skip further arg parsing until out of comment)
164+
if len(arg) >= 2 {
165+
if arg[0:2] == "/*" || arg[len(arg)-2:] == "*/" {
166+
if arg[0:2] == "/*" {
167+
insideComment = true
168+
commentIdx = argIdx + 1
169+
}
170+
if arg[len(arg)-2:] == "*/" {
171+
if !insideComment {
172+
errs = append(errs, fmt.Errorf("arg %d: comment terminator (\"*/\") found without start of comment (\"/*\")", argIdx+1))
173+
}
174+
insideComment = false
175+
}
176+
continue
177+
}
178+
}
179+
if insideComment {
180+
continue
181+
} else {
182+
// If not a comment, increase separate arg index
183+
idx++
184+
}
185+
158186
opt := strings.SplitN(arg, "=", 2)
159187

160188
// Ensure key=val format
161189
if len(opt) != 2 {
162-
errs = append(errs, fmt.Errorf("arg %d: invalid format '%s', should be 'key=val' (skipping)", idx, arg))
190+
errs = append(errs, fmt.Errorf("non-comment arg %d: invalid format '%s', should be 'key=val' (skipping)", idx, arg))
163191
continue
164192
}
165193

@@ -168,48 +196,51 @@ func parseConfig(argv ...string) (cfg Config, errs []error) {
168196
case "lease_file":
169197
leaseFile := strings.Trim(opt[1], `"'`)
170198
if leaseFile == "" {
171-
errs = append(errs, fmt.Errorf("arg %d: %s: empty (skipping)", idx, opt[0]))
199+
errs = append(errs, fmt.Errorf("non-comment arg %d: %s: empty (skipping)", idx, opt[0]))
172200
continue
173201
} else {
174202
cfg.leaseFile = leaseFile
175203
}
176204
case "script_path":
177205
scriptPath := strings.Trim(opt[1], `"'`)
178206
if scriptPath == "" {
179-
errs = append(errs, fmt.Errorf("arg %d: %s: empty (setting to default script)", idx, opt[0]))
207+
errs = append(errs, fmt.Errorf("non-comment arg %d: %s: empty (setting to default script)", idx, opt[0]))
180208
cfg.scriptPath = defaultScriptPath
181209
continue
182210
} else {
183211
cfg.scriptPath = scriptPath
184212
}
185213
case "lease_time":
186214
if leaseTime, err := time.ParseDuration(opt[1]); err != nil {
187-
errs = append(errs, fmt.Errorf("arg %d: %s: invalid duration '%s' (skipping)", idx, opt[0], opt[1]))
215+
errs = append(errs, fmt.Errorf("non-comment arg %d: %s: invalid duration '%s' (skipping)", idx, opt[0], opt[1]))
188216
continue
189217
} else {
190218
cfg.leaseTime = &leaseTime
191219
}
192220
case "ipv4_start":
193221
ipv4Start := net.ParseIP(opt[1])
194222
if ipv4Start.To4() == nil {
195-
errs = append(errs, fmt.Errorf("arg %d: %s: invalid ip address '%s' (skipping)", idx, opt[0], opt[1]))
223+
errs = append(errs, fmt.Errorf("non-comment arg %d: %s: invalid ip address '%s' (skipping)", idx, opt[0], opt[1]))
196224
continue
197225
} else {
198226
cfg.ipv4Start = &ipv4Start
199227
}
200228
case "ipv4_end":
201229
ipv4End := net.ParseIP(opt[1])
202230
if ipv4End.To4() == nil {
203-
errs = append(errs, fmt.Errorf("arg %d: %s: invalid ip address '%s' (skipping)", idx, opt[0], opt[1]))
231+
errs = append(errs, fmt.Errorf("non-comment arg %d: %s: invalid ip address '%s' (skipping)", idx, opt[0], opt[1]))
204232
continue
205233
} else {
206234
cfg.ipv4End = &ipv4End
207235
}
208236
default:
209-
errs = append(errs, fmt.Errorf("arg %d: unknown config key '%s' (skipping)", idx, opt[0]))
237+
errs = append(errs, fmt.Errorf("non-comment arg %d: unknown config key '%s' (skipping)", idx, opt[0]))
210238
continue
211239
}
212240
}
241+
if insideComment {
242+
errs = append(errs, fmt.Errorf("arg %d: unterminated comment (\"/*\" found without a \"*/\")", commentIdx))
243+
}
213244
return
214245
}
215246

‎plugin/coredhcp/bootloop/main_test.go‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,58 @@ func TestParseConfig(t *testing.T) {
9696
scriptPath: "/tmp/test.ipxe",
9797
},
9898
},
99+
{
100+
name: "valid comments arg format",
101+
argv: strings.Fields(`/* comment at beginning */
102+
lease_file=/some/file
103+
/* comment in middle */
104+
script_path=/some/script
105+
/* comment at end */`),
106+
want: Config{
107+
leaseFile: "/some/file",
108+
scriptPath: "/some/script",
109+
},
110+
},
111+
{
112+
name: "runaway comment",
113+
argv: strings.Fields(`/* comment at beginning */
114+
lease_file=/some/file
115+
/* comment in middle
116+
script_path=/some/script`),
117+
want: Config{
118+
leaseFile: "/some/file",
119+
},
120+
wantErrCount: 1,
121+
wantErrSub: []string{`unterminated comment`},
122+
},
123+
{
124+
name: "comment end without comment start",
125+
argv: strings.Fields(`comment at beginning */`),
126+
want: Config{},
127+
wantErrCount: 4,
128+
wantErrSub: []string{`found without start of comment`},
129+
},
130+
{
131+
name: "duplicate comment start",
132+
argv: strings.Fields(`lease_file=/some/file
133+
/* comment in /* middle */
134+
script_path=/some/script
135+
/* comment at end */`),
136+
want: Config{
137+
leaseFile: "/some/file",
138+
scriptPath: "/some/script",
139+
},
140+
},
141+
{
142+
name: "comment without spaces",
143+
argv: strings.Fields(`lease_file=/some/file
144+
/*comment*/
145+
script_path=/some/script`),
146+
want: Config{
147+
leaseFile: "/some/file",
148+
scriptPath: "/some/script",
149+
},
150+
},
99151
{
100152
name: "invalid format without equal sign",
101153
argv: []string{"not_a_key_val"},

0 commit comments

Comments
 (0)