-
Notifications
You must be signed in to change notification settings - Fork 15
/
parse.go
480 lines (421 loc) · 13 KB
/
parse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/**
* Copyright (c) F5, Inc.
*
* This source code is licensed under the Apache License, Version 2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
package crossplane
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
)
//nolint:gochecknoglobals
var (
hasMagic = regexp.MustCompile(`[*?[]`)
osOpen = func(path string) (io.ReadCloser, error) { return os.Open(path) }
ErrPrematureLexEnd = errors.New("premature end of file")
)
type blockCtx []string
func (c blockCtx) key() string {
return strings.Join(c, ">")
}
func (c blockCtx) getLastBlock() string {
if len(c) == 0 {
return "main"
}
return c[len(c)-1]
}
type fileCtx struct {
path string
ctx blockCtx
}
type parser struct {
configDir string
options *ParseOptions
handleError func(*Config, error)
includes []fileCtx
included map[string]int
includeEdges map[string][]string
includeInDegree map[string]int
}
// MatchFunc is the signature of the match function used to identify NGINX directives that
// can be encountered when parsing an NGINX configuration that references dynamic or
// non-core modules. The argument is the name of a directive found when parsing an NGINX
// configuration.
//
// The return value is a list of bitmasks that indicate the valid contexts in which the
// directive may appear as well as the number of arguments the directive accepts. The
// return value must contain at least one non-zero bitmask if matched is true.
type MatchFunc func(directive string) (masks []uint, matched bool)
// ParseOptions determine the behavior of an NGINX config parse.
type ParseOptions struct {
// An array of directives to skip over and not include in the payload.
IgnoreDirectives []string
// If an error is found while parsing, it will be passed to this callback
// function. The results of the callback function will be set in the
// PayloadError struct that's added to the Payload struct's Errors array.
ErrorCallback func(error) interface{}
// If specified, use this alternative to open config files
Open func(path string) (io.ReadCloser, error)
// Glob will return a matching list of files if specified
Glob func(path string) ([]string, error)
// If true, parsing will stop immediately if an error is found.
StopParsingOnError bool
// If true, include directives are used to combine all of the Payload's
// Config structs into one.
CombineConfigs bool
// If true, only the config file with the given filename will be parsed
// and Parse will not parse files included files.
SingleFile bool
// If true, comments will be parsed and added to the resulting Payload.
ParseComments bool
// If true, add an error to the payload when encountering a directive that
// is unrecognized. The unrecognized directive will not be included in the
// resulting Payload.
ErrorOnUnknownDirectives bool
// If true, checks that directives are in valid contexts.
SkipDirectiveContextCheck bool
// If true, checks that directives have a valid number of arguments.
SkipDirectiveArgsCheck bool
// DirectiveSources is used to indicate the set of directives to be expected
// by the parser. DirectiveSources can include different versions of NGINX
// and dynamic modules. If DirectiveSources is empty, the parser defaults
// to DefaultDirectivesMatchFunc.
DirectiveSources []MatchFunc
LexOptions LexOptions
}
// Parse parses an NGINX configuration file.
//
//nolint:funlen
func Parse(filename string, options *ParseOptions) (*Payload, error) {
payload := &Payload{
Status: "ok",
Errors: []PayloadError{},
Config: []Config{},
}
if options.Glob == nil {
options.Glob = filepath.Glob
}
handleError := func(config *Config, err error) {
var line *int
if e, ok := err.(*ParseError); ok {
line = e.Line
}
cerr := ConfigError{Line: line, Error: err}
perr := PayloadError{Line: line, Error: err, File: config.File}
if options.ErrorCallback != nil {
perr.Callback = options.ErrorCallback(err)
}
const failedSts = "failed"
config.Status = failedSts
config.Errors = append(config.Errors, cerr)
payload.Status = failedSts
payload.Errors = append(payload.Errors, perr)
}
// Start with the main nginx config file/context.
p := parser{
configDir: filepath.Dir(filename),
options: options,
handleError: handleError,
includes: []fileCtx{{path: filename, ctx: blockCtx{}}},
included: map[string]int{filename: 0},
// adjacency list where an edge exists between a file and the file it includes
includeEdges: map[string][]string{},
// number of times a file is included by another file
includeInDegree: map[string]int{filename: 0},
}
for len(p.includes) > 0 {
incl := p.includes[0]
p.includes = p.includes[1:]
file, err := p.openFile(incl.path)
if err != nil {
return nil, err
}
defer file.Close()
tokens := LexWithOptions(file, options.LexOptions)
config := Config{
File: incl.path,
Status: "ok",
Errors: []ConfigError{},
Parsed: Directives{},
}
parsed, err := p.parse(&config, tokens, incl.ctx, false)
if err != nil {
if options.StopParsingOnError {
return nil, err
}
handleError(&config, err)
} else {
config.Parsed = parsed
}
payload.Config = append(payload.Config, config)
}
if p.isAcyclic() {
return nil, errors.New("configs contain include cycle")
}
if options.CombineConfigs {
return payload.Combined()
}
return payload, nil
}
func (p *parser) openFile(path string) (io.ReadCloser, error) {
open := osOpen
if p.options.Open != nil {
open = p.options.Open
}
return open(path)
}
// parse Recursively parses directives from an nginx config context.
//
//nolint:gocyclo,funlen,gocognit,maintidx,nonamedreturns
func (p *parser) parse(parsing *Config, tokens <-chan NgxToken, ctx blockCtx, consume bool) (parsed Directives, err error) {
var tokenOk bool
// parse recursively by pulling from a flat stream of tokens
for t := range tokens {
if t.Error != nil {
var perr *ParseError
if errors.As(t.Error, &perr) {
perr.File = &parsing.File
perr.BlockCtx = ctx.getLastBlock()
return nil, perr
}
return nil, &ParseError{
What: t.Error.Error(),
File: &parsing.File,
Line: &t.Line,
originalErr: t.Error,
BlockCtx: ctx.getLastBlock(),
}
}
var commentsInArgs []string
// we are parsing a block, so break if it's closing
if t.Value == "}" && !t.IsQuoted {
break
}
// if we are consuming, then just continue until end of context
if consume {
// if we find a block inside this context, consume it too
if t.Value == "{" && !t.IsQuoted {
_, _ = p.parse(parsing, tokens, nil, true)
}
continue
}
var fileName string
if p.options.CombineConfigs {
fileName = parsing.File
}
// the first token should always be an nginx directive
stmt := &Directive{
Directive: t.Value,
Line: t.Line,
Args: []string{},
File: fileName,
}
// if token is comment
if strings.HasPrefix(t.Value, "#") && !t.IsQuoted {
if p.options.ParseComments {
comment := t.Value[1:]
stmt.Directive = "#"
stmt.Comment = &comment
parsed = append(parsed, stmt)
}
continue
}
// parse arguments by reading tokens
t, tokenOk = <-tokens
if !tokenOk {
return nil, &ParseError{
What: ErrPrematureLexEnd.Error(),
File: &parsing.File,
Line: &stmt.Line,
originalErr: ErrPrematureLexEnd,
BlockCtx: ctx.getLastBlock(),
}
}
for t.IsQuoted || (t.Value != "{" && t.Value != ";" && t.Value != "}") {
if !strings.HasPrefix(t.Value, "#") || t.IsQuoted {
stmt.Args = append(stmt.Args, t.Value)
} else if p.options.ParseComments {
commentsInArgs = append(commentsInArgs, t.Value[1:])
}
t, tokenOk = <-tokens
if !tokenOk {
return nil, &ParseError{
What: ErrPrematureLexEnd.Error(),
File: &parsing.File,
Line: &stmt.Line,
originalErr: ErrPrematureLexEnd,
BlockCtx: ctx.getLastBlock(),
}
}
}
// if inside "map-like" block - add contents to payload, but do not parse further
if len(ctx) > 0 {
if _, ok := mapBodies[ctx[len(ctx)-1]]; ok {
mapErr := analyzeMapBody(parsing.File, stmt, t.Value, ctx[len(ctx)-1])
if mapErr != nil && p.options.StopParsingOnError {
return nil, mapErr
} else if mapErr != nil {
p.handleError(parsing, mapErr)
// consume invalid block
if t.Value == "{" && !t.IsQuoted {
_, _ = p.parse(parsing, tokens, nil, true)
}
continue
}
parsed = append(parsed, stmt)
continue
}
}
// consume the directive if it is ignored and move on
if contains(p.options.IgnoreDirectives, stmt.Directive) {
// if this directive was a block consume it too
if t.Value == "{" && !t.IsQuoted {
_, _ = p.parse(parsing, tokens, nil, true)
}
continue
}
// raise errors if this statement is invalid
err = analyze(parsing.File, stmt, t.Value, ctx, p.options)
if perr, ok := err.(*ParseError); ok && !p.options.StopParsingOnError {
p.handleError(parsing, perr)
// if it was a block but shouldn"t have been then consume
if strings.HasSuffix(perr.What, ` is not terminated by ";"`) {
if t.Value != "}" && !t.IsQuoted {
_, _ = p.parse(parsing, tokens, nil, true)
} else {
break
}
}
// keep on parsin'
continue
} else if err != nil {
return nil, err
}
// prepare arguments - strip parentheses
if stmt.Directive == "if" {
stmt = prepareIfArgs(stmt)
}
// add "includes" to the payload if this is an include statement
if !p.options.SingleFile && stmt.Directive == "include" {
if len(stmt.Args) == 0 {
return nil, &ParseError{
What: fmt.Sprintf(`invalid number of arguments in "%s" directive in %s:%d`,
stmt.Directive,
parsing.File,
stmt.Line,
),
File: &parsing.File,
Line: &stmt.Line,
Statement: stmt.String(),
BlockCtx: ctx.getLastBlock(),
}
}
pattern := stmt.Args[0]
if !filepath.IsAbs(pattern) {
pattern = filepath.Join(p.configDir, pattern)
}
// get names of all included files
var fnames []string
if hasMagic.MatchString(pattern) {
fnames, err = p.options.Glob(pattern)
if err != nil {
return nil, err
}
sort.Strings(fnames)
} else {
// if the file pattern was explicit, nginx will check
// that the included file can be opened and read
if f, err := p.openFile(pattern); err != nil {
perr := &ParseError{
What: err.Error(),
File: &parsing.File,
Line: &stmt.Line,
Statement: stmt.String(),
BlockCtx: ctx.getLastBlock(),
}
if !p.options.StopParsingOnError {
p.handleError(parsing, perr)
} else {
return nil, perr
}
} else {
defer f.Close()
fnames = []string{pattern}
}
}
for _, fname := range fnames {
// the included set keeps files from being parsed twice
// TODO: handle files included from multiple contexts
if _, ok := p.included[fname]; !ok {
p.included[fname] = len(p.included)
p.includes = append(p.includes, fileCtx{fname, ctx})
}
stmt.Includes = append(stmt.Includes, p.included[fname])
// add edge between the current file and it's included file and
// increase the included file's in degree
p.includeEdges[parsing.File] = append(p.includeEdges[parsing.File], fname)
p.includeInDegree[fname]++
}
}
// if this statement terminated with "{" then it is a block
if t.Value == "{" && !t.IsQuoted {
stmt.Block = make(Directives, 0)
inner := enterBlockCtx(stmt, ctx) // get context for block
blocks, err := p.parse(parsing, tokens, inner, false)
if err != nil {
return nil, err
}
stmt.Block = append(stmt.Block, blocks...)
}
parsed = append(parsed, stmt)
// add all comments found inside args after stmt is added
for _, comment := range commentsInArgs {
comment := comment
parsed = append(parsed, &Directive{
Directive: "#",
Line: stmt.Line,
Args: []string{},
File: fileName,
Comment: &comment,
})
}
}
return parsed, nil
}
// isAcyclic performs a topological sort to check if there are cycles created by configs' includes.
// First, it adds any files who are not being referenced by another file to a queue (in degree of 0).
// For every file in the queue, it will remove the reference it has towards its neighbors.
// At the end, if the queue is empty but not all files were once in the queue,
// then files still exist with references, and therefore, a cycle is present.
func (p *parser) isAcyclic() bool {
// add to queue if file is not being referenced by any other file
var queue []string
for k, v := range p.includeInDegree {
if v == 0 {
queue = append(queue, k)
}
}
fileCount := 0
for len(queue) > 0 {
// dequeue
file := queue[0]
queue = queue[1:]
fileCount++
// decrease each neighbor's in degree
neighbors := p.includeEdges[file]
for _, f := range neighbors {
p.includeInDegree[f]--
if p.includeInDegree[f] == 0 {
queue = append(queue, f)
}
}
}
return fileCount != len(p.includeInDegree)
}