Skip to content

Commit d17a510

Browse files
authored
Merge pull request #49 from OpenCHAMI/synackd/feat/coredhcp/support-comments
feat(coredhcp): support config comments
2 parents 5c45a14 + a261fcb commit d17a510

6 files changed

Lines changed: 241 additions & 20 deletions

File tree

‎examples/coredhcp/README.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,23 @@ plugins:
4747
lease_time=1h single_port=false
4848
```
4949

50+
Comments are supported too:
51+
52+
```yaml
53+
plugins:
54+
- coresmd: |
55+
/* SMD base URI */
56+
svc_base_uri=https://foobar.openchami.cluster
57+
/* BSS boot script base URI */
58+
ipxe_base_uri=http://172.16.0.253:8081
59+
/* CA trust bundle */
60+
ca_cert=/root_ca/root_ca.crt
61+
/* SMD cache validity duration */
62+
cache_valid=30s
63+
/* lease duration */
64+
lease_time=1h single_port=false
65+
```
66+
5067
See [coredhcp.yaml](coredhcp.yaml) for a full example with documentation comments.
5168

5269
## DHCPv4 and DHCPv6 Support

‎examples/coredhcp/coredhcp.yaml‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,19 +177,32 @@ server4:
177177
# bmc_pattern, respectively. Values set for node_pattern and bmc_pattern will
178178
# override values set for Node and NodeBMC if set for hostname_default.
179179
- coresmd: |
180+
/* SMD base URI */
180181
svc_base_uri=https://foobar.openchami.cluster
182+
/* BSS base URI for obtaining boot scripts */
181183
ipxe_base_uri=http://172.16.0.253:8081
184+
/* OPTIONAL: path to CA trust certificate bundle */
182185
ca_cert=/root_ca/root_ca.crt
186+
/* OPTIONAL: duration that component/interface cache is valid (default 30s) */
183187
cache_valid=30s
188+
/* OPTIONAL: duration that served leases are valid (default 1h) */
184189
lease_time=1h
190+
/* OPTIONAL: toggle TFTP single-port mode (default false) */
185191
single_port=false
192+
/* OPTIONAL: TFTP server root directory (default /tftpboot) */
186193
tftp_dir=/tftpboot
194+
/* OPTIONAL: TFTP server listen port (default 69) */
187195
tftp_port=69
196+
/* OPTIONAL: pattern for determining node hostnames */
188197
node_pattern=nid{04d}
198+
/* OPTIONAL: pattern for determining BMC hostnames */
189199
bmc_pattern=bmc{04d}
200+
/* OPTIONAL: domain to append to hostnames */
190201
domain=openchami.cluster
202+
/* OPTIONAL: policies to use hostname pattern by SMD component type */
191203
hostname_by_type=Node:nid{04d}
192204
hostname_by_type=HSNSwitch:{id}
205+
/* OPTIONAL: fallback hostname pattern */
193206
hostname_default=
194207
195208
# Any requests reaching this point are unknown to SMD and it is up to the
@@ -236,7 +249,7 @@ server4:
236249
# overwritten if so. The format of the file is an Sqlite database.
237250
#
238251
# script_path (OPTIONAL, string, default=default)
239-
# 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
240253
# using the build-in coresmd FTP server, this should be 'default', which
241254
# will make bootloop serve a simple iPXE script that reboots.
242255
#
@@ -257,10 +270,15 @@ server4:
257270
# ipv4_end (REQUIRED, string)
258271
# The end IPv4 address for the pool of IPs to assign.
259272
- bootloop: |
273+
/* file to store DHCP leases in */
260274
lease_file=/tmp/coredhcp.db
275+
/* OPTIONAL: path to custom iPXE script to serve for DHCP requests (default is default script) */
261276
script_path=default
277+
/* OPTIONAL: lease duration (default 5m)
262278
lease_time=5m
279+
/* IPv4 address starting the range, inclusive */
263280
ipv4_start=172.16.0.156
281+
/* IPv4 address ending the range, inclusive */
264282
ipv4_end=172.16.0.200
265283
266284
###############################################################################

‎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"},

‎plugin/coredhcp/coresmd/main.go‎

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -209,27 +209,55 @@ func setup4(args ...string) (handler.Handler4, error) {
209209
// errors occur, they are gathered into errs, a slice of errors, so that they
210210
// can be printed or handled.
211211
func parseConfig(argv ...string) (cfg Config, errs []error) {
212-
for idx, arg := range argv {
212+
var (
213+
idx int = 0 // Separate arg index so comments aren't counted
214+
insideComment bool = false // Track if inside a comment
215+
commentIdx int = -1 // Track where last comment began for printing error
216+
)
217+
for argIdx, arg := range argv {
218+
// Parse comments (skip further arg parsing until out of comment)
219+
if len(arg) >= 2 {
220+
if arg[0:2] == "/*" || arg[len(arg)-2:] == "*/" {
221+
if arg[0:2] == "/*" {
222+
insideComment = true
223+
commentIdx = argIdx + 1
224+
}
225+
if arg[len(arg)-2:] == "*/" {
226+
if !insideComment {
227+
errs = append(errs, fmt.Errorf("arg %d: comment terminator (\"*/\") found without start of comment (\"/*\")", argIdx+1))
228+
}
229+
insideComment = false
230+
}
231+
continue
232+
}
233+
}
234+
if insideComment {
235+
continue
236+
} else {
237+
// If not a comment, increase separate arg index
238+
idx++
239+
}
240+
213241
opt := strings.SplitN(arg, "=", 2)
214242

215243
// Ensure key=val format
216244
if len(opt) != 2 {
217-
errs = append(errs, fmt.Errorf("arg %d: invalid format '%s', should be 'key=val' (skipping)", idx, arg))
245+
errs = append(errs, fmt.Errorf("non-comment arg %d: invalid format '%s', should be 'key=val' (skipping)", idx, arg))
218246
continue
219247
}
220248

221249
// Check that key is known and, if so, process value
222250
switch opt[0] {
223251
case "svc_base_uri":
224252
if svcURI, err := url.Parse(opt[1]); err != nil {
225-
errs = append(errs, fmt.Errorf("arg %d: %s: invalid URI '%s' (skipping): %w", idx, opt[0], opt[1], err))
253+
errs = append(errs, fmt.Errorf("non-comment arg %d: %s: invalid URI '%s' (skipping): %w", idx, opt[0], opt[1], err))
226254
continue
227255
} else {
228256
cfg.svcBaseURI = svcURI
229257
}
230258
case "ipxe_base_uri":
231259
if ipxeURI, err := url.Parse(opt[1]); err != nil {
232-
errs = append(errs, fmt.Errorf("arg %d: %s: invalid URI '%s' (skipping): %w", idx, opt[0], opt[1], err))
260+
errs = append(errs, fmt.Errorf("non-comment arg %d: %s: invalid URI '%s' (skipping): %w", idx, opt[0], opt[1], err))
233261
continue
234262
} else {
235263
cfg.ipxeBaseURI = ipxeURI
@@ -242,21 +270,21 @@ func parseConfig(argv ...string) (cfg Config, errs []error) {
242270
}
243271
case "cache_valid":
244272
if cacheValid, err := time.ParseDuration(opt[1]); err != nil {
245-
errs = append(errs, fmt.Errorf("arg %d: %s: invalid duration '%s' (skipping): %w", idx, opt[0], opt[1], err))
273+
errs = append(errs, fmt.Errorf("non-comment arg %d: %s: invalid duration '%s' (skipping): %w", idx, opt[0], opt[1], err))
246274
continue
247275
} else {
248276
cfg.cacheValid = &cacheValid
249277
}
250278
case "lease_time":
251279
if leaseTime, err := time.ParseDuration(opt[1]); err != nil {
252-
errs = append(errs, fmt.Errorf("arg %d: %s: invalid duration '%s' (skipping): %w", idx, opt[0], opt[1], err))
280+
errs = append(errs, fmt.Errorf("non-comment arg %d: %s: invalid duration '%s' (skipping): %w", idx, opt[0], opt[1], err))
253281
continue
254282
} else {
255283
cfg.leaseTime = &leaseTime
256284
}
257285
case "single_port":
258286
if singlePort, err := strconv.ParseBool(opt[1]); err != nil {
259-
errs = append(errs, fmt.Errorf("arg %d: %s: invalid value '%s' (defaulting to false): %w", idx, opt[0], opt[1], err))
287+
errs = append(errs, fmt.Errorf("non-comment arg %d: %s: invalid value '%s' (defaulting to false): %w", idx, opt[0], opt[1], err))
260288
continue
261289
} else {
262290
cfg.singlePort = singlePort
@@ -268,13 +296,13 @@ func parseConfig(argv ...string) (cfg Config, errs []error) {
268296
}
269297
case "tftp_port":
270298
if tftpPort, err := strconv.ParseInt(opt[1], 10, 64); err != nil {
271-
errs = append(errs, fmt.Errorf("arg %d: %s: invalid port '%s' (defaulting to %d): %w", idx, opt[0], opt[1], defaultTFTPPort, err))
299+
errs = append(errs, fmt.Errorf("non-comment arg %d: %s: invalid port '%s' (defaulting to %d): %w", idx, opt[0], opt[1], defaultTFTPPort, err))
272300
cfg.tftpPort = defaultTFTPPort
273301
} else {
274302
if tftpPort >= 0 && tftpPort <= 65535 {
275303
cfg.tftpPort = int(tftpPort)
276304
} else {
277-
errs = append(errs, fmt.Errorf("arg %d: %s: port '%d' out of range, must be between 0-65535 (defaulting to %d)", idx, opt[0], tftpPort, defaultTFTPPort))
305+
errs = append(errs, fmt.Errorf("non-comment arg %d: %s: port '%d' out of range, must be between 0-65535 (defaulting to %d)", idx, opt[0], tftpPort, defaultTFTPPort))
278306
cfg.tftpPort = defaultTFTPPort
279307
}
280308
}
@@ -306,7 +334,7 @@ func parseConfig(argv ...string) (cfg Config, errs []error) {
306334
// componentType = vals[0], pattern = vals[1]
307335
vals := strings.SplitN(opt[1], ":", 2)
308336
if len(vals) != 2 {
309-
errs = append(errs, fmt.Errorf("arg %d: invalid format for key '%s': expected hostname_by_type=<type>:<pattern>, got %s", idx, opt[0], opt[1]))
337+
errs = append(errs, fmt.Errorf("non-comment arg %d: invalid format for key '%s': expected hostname_by_type=<type>:<pattern>, got %s", idx, opt[0], opt[1]))
310338
continue
311339
}
312340
if cfg.policy.ByType == nil {
@@ -315,10 +343,13 @@ func parseConfig(argv ...string) (cfg Config, errs []error) {
315343
cfg.policy.ByType[vals[0]] = vals[1]
316344
}
317345
default:
318-
errs = append(errs, fmt.Errorf("arg %d: unknown config key '%s' (skipping)", idx, opt[0]))
346+
errs = append(errs, fmt.Errorf("non-comment arg %d: unknown config key '%s' (skipping)", idx, opt[0]))
319347
continue
320348
}
321349
}
350+
if insideComment {
351+
errs = append(errs, fmt.Errorf("arg %d: unterminated comment (\"/*\" found without a \"*/\")", commentIdx))
352+
}
322353
return
323354
}
324355

0 commit comments

Comments
 (0)