From 0f28539c5c238842d9c8900277cd4363ef37d00e Mon Sep 17 00:00:00 2001 From: Sylvia Wang <139922338+sylwang@users.noreply.github.com> Date: Wed, 16 Aug 2023 02:20:44 -0700 Subject: [PATCH] fix: update crossplane version to use the latest parsing logic for nginx config files (#438) --- go.mod | 2 +- go.sum | 4 +- go.work.sum | 2 + sdk/go.mod | 2 +- sdk/go.sum | 4 +- .../nginx-go-crossplane/.golangci.yml | 6 ++- .../nginxinc/nginx-go-crossplane/analyze.go | 40 ++++++++++++------- .../nginx-go-crossplane/analyze_map.go | 29 +++++++++----- .../nginxinc/nginx-go-crossplane/build.go | 9 ++--- .../nginxinc/nginx-go-crossplane/errors.go | 10 +++-- .../nginxinc/nginx-go-crossplane/lex.go | 9 ++--- .../nginxinc/nginx-go-crossplane/parse.go | 36 ++++++++++++----- .../nginxinc/nginx-go-crossplane/util.go | 7 ++-- sdk/vendor/modules.txt | 2 +- .../nginx-go-crossplane/.golangci.yml | 6 ++- .../nginxinc/nginx-go-crossplane/analyze.go | 40 ++++++++++++------- .../nginx-go-crossplane/analyze_map.go | 29 +++++++++----- .../nginxinc/nginx-go-crossplane/build.go | 9 ++--- .../nginxinc/nginx-go-crossplane/errors.go | 10 +++-- .../nginxinc/nginx-go-crossplane/lex.go | 9 ++--- .../nginxinc/nginx-go-crossplane/parse.go | 36 ++++++++++++----- .../nginxinc/nginx-go-crossplane/util.go | 7 ++-- vendor/modules.txt | 2 +- 23 files changed, 196 insertions(+), 114 deletions(-) diff --git a/go.mod b/go.mod index 9b6fbc4d20..24f3b5f14c 100644 --- a/go.mod +++ b/go.mod @@ -238,7 +238,7 @@ require ( github.com/muesli/termenv v0.15.1 // indirect github.com/nakabonne/nestif v0.3.1 // indirect github.com/nbutton23/zxcvbn-go v0.0.0-20210217022336-fa2cb2858354 // indirect - github.com/nginxinc/nginx-go-crossplane v0.4.17 // indirect + github.com/nginxinc/nginx-go-crossplane v0.4.24 // indirect github.com/nishanths/exhaustive v0.9.5 // indirect github.com/nishanths/predeclared v0.2.2 // indirect github.com/nunnatsa/ginkgolinter v0.9.0 // indirect diff --git a/go.sum b/go.sum index e4fac2f5bf..d6a61cafe8 100644 --- a/go.sum +++ b/go.sum @@ -675,8 +675,8 @@ github.com/nakabonne/nestif v0.3.1 h1:wm28nZjhQY5HyYPx+weN3Q65k6ilSBxDb8v5S81B81 github.com/nakabonne/nestif v0.3.1/go.mod h1:9EtoZochLn5iUprVDmDjqGKPofoUEBL8U4Ngq6aY7OE= github.com/nbutton23/zxcvbn-go v0.0.0-20210217022336-fa2cb2858354 h1:4kuARK6Y6FxaNu/BnU2OAaLF86eTVhP2hjTB6iMvItA= github.com/nbutton23/zxcvbn-go v0.0.0-20210217022336-fa2cb2858354/go.mod h1:KSVJerMDfblTH7p5MZaTt+8zaT2iEk3AkVb9PQdZuE8= -github.com/nginxinc/nginx-go-crossplane v0.4.17 h1:kBISwQT36Uv4h57ran0bT4MIWUqaoJrMwoTMpryBZcg= -github.com/nginxinc/nginx-go-crossplane v0.4.17/go.mod h1:UzbZnyFv0vPlt1Urbnp/mrFCzBL4tYCReFuNBpFQEfI= +github.com/nginxinc/nginx-go-crossplane v0.4.24 h1:6gwZ8Boh8FS/kiZlfwSImO6UFnCjPeZ2uleHGSx4b+4= +github.com/nginxinc/nginx-go-crossplane v0.4.24/go.mod h1:UzbZnyFv0vPlt1Urbnp/mrFCzBL4tYCReFuNBpFQEfI= github.com/nginxinc/nginx-plus-go-client v0.10.0 h1:3zsMMkPvRDo8D7ZSprXtbAEW/SDmezZWzxdyS+6oAlc= github.com/nginxinc/nginx-plus-go-client v0.10.0/go.mod h1:0v3RsQCvRn/IyrMtW+DK6CNkz+PxEsXDJPjQ3yUMBF0= github.com/nginxinc/nginx-prometheus-exporter v0.11.0 h1:21xjnqNgxtni2jDgAQ90bl15uDnrTreO9sIlu1YsX/U= diff --git a/go.work.sum b/go.work.sum index af23fa7cc6..69c893f9b1 100644 --- a/go.work.sum +++ b/go.work.sum @@ -1235,6 +1235,7 @@ github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOA github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-sqlite3 v1.6.0 h1:TDwTWbeII+88Qy55nWlof0DclgAtI4LqGujkYMzmQII= +github.com/maxbrunsfeld/counterfeiter/v6 v6.6.1/go.mod h1:qbKwBR+qQODzH2WD/s53mdgp/xVcXMlJb59GRFOp6Z4= github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517 h1:zpIH83+oKzcpryru8ceC6BxnoG8TBrhgAvRg8obzup0= github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517/go.mod h1:KQ7+USdGKfpPjXk4Ga+5XxQM4Lm4e3gAogrreFAYpOg= github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d h1:5PJl274Y63IEHC+7izoQE9x6ikvDFZS2mDVS3drnohI= @@ -1519,6 +1520,7 @@ golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 h1:VLliZ0d+/avPrXXH+OakdXhp golang.org/x/lint v0.0.0-20210508222113-6edffad5e616/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028 h1:4+4C/Iv2U4fMZBiMCc98MG1In4gJY5YRhtpDNeDeHWs= golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20181023162649-9b4f9f5ad519/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= diff --git a/sdk/go.mod b/sdk/go.mod index b9c689b2d8..019f0d1510 100644 --- a/sdk/go.mod +++ b/sdk/go.mod @@ -8,7 +8,7 @@ require ( github.com/google/uuid v1.3.0 github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 github.com/mitchellh/mapstructure v1.5.0 - github.com/nginxinc/nginx-go-crossplane v0.4.17 + github.com/nginxinc/nginx-go-crossplane v0.4.24 github.com/sirupsen/logrus v1.9.3 github.com/stretchr/testify v1.8.4 google.golang.org/grpc v1.56.2 diff --git a/sdk/go.sum b/sdk/go.sum index c1b2d03fbb..d1683cd102 100644 --- a/sdk/go.sum +++ b/sdk/go.sum @@ -49,8 +49,8 @@ github.com/maxbrunsfeld/counterfeiter/v6 v6.6.2 h1:CEy7VRV/Vbm7YLuZo3pGKa5GlPX4z github.com/maxbrunsfeld/counterfeiter/v6 v6.6.2/go.mod h1:otjOyjeqm3LALYcmX2AQIGH0VlojDoSd8aGOzsHAnBc= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= -github.com/nginxinc/nginx-go-crossplane v0.4.17 h1:kBISwQT36Uv4h57ran0bT4MIWUqaoJrMwoTMpryBZcg= -github.com/nginxinc/nginx-go-crossplane v0.4.17/go.mod h1:UzbZnyFv0vPlt1Urbnp/mrFCzBL4tYCReFuNBpFQEfI= +github.com/nginxinc/nginx-go-crossplane v0.4.24 h1:6gwZ8Boh8FS/kiZlfwSImO6UFnCjPeZ2uleHGSx4b+4= +github.com/nginxinc/nginx-go-crossplane v0.4.24/go.mod h1:UzbZnyFv0vPlt1Urbnp/mrFCzBL4tYCReFuNBpFQEfI= github.com/onsi/gomega v1.27.8 h1:gegWiwZjBsf2DgiSbf5hpokZ98JVDMcWkUiigk6/KXc= github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= diff --git a/sdk/vendor/github.com/nginxinc/nginx-go-crossplane/.golangci.yml b/sdk/vendor/github.com/nginxinc/nginx-go-crossplane/.golangci.yml index e69465e072..7888e9f1a6 100644 --- a/sdk/vendor/github.com/nginxinc/nginx-go-crossplane/.golangci.yml +++ b/sdk/vendor/github.com/nginxinc/nginx-go-crossplane/.golangci.yml @@ -4,6 +4,9 @@ linters: enable-all: true disable: + - cyclop + - depguard + - varnamelen - goimports # handled by `make format` - gofmt # handled by `make format` - wsl # hyper specific to a the creator and not configurable enough to be useful - https://github.com/bombsimon/wsl @@ -11,7 +14,6 @@ linters: - gomnd - godox - nlreturn - - exhaustivestruct - wrapcheck - thelper - testpackage @@ -25,11 +27,13 @@ linters: - golint - maligned - scopelint + - exhaustruct # deprecated - deadcode - varcheck - structcheck - nosnakecase + - exhaustivestruct # Run options diff --git a/sdk/vendor/github.com/nginxinc/nginx-go-crossplane/analyze.go b/sdk/vendor/github.com/nginxinc/nginx-go-crossplane/analyze.go index 8adf1e4acc..1ad51d795a 100644 --- a/sdk/vendor/github.com/nginxinc/nginx-go-crossplane/analyze.go +++ b/sdk/vendor/github.com/nginxinc/nginx-go-crossplane/analyze.go @@ -61,7 +61,8 @@ const ngxAnyConf = ngxMainConf | ngxEventConf | ngxMailMainConf | ngxMailSrvConf ngxHTTPMainConf | ngxHTTPSrvConf | ngxHTTPLocConf | ngxHTTPUpsConf // map for getting bitmasks from certain context tuples -// nolint:gochecknoglobals +// +//nolint:gochecknoglobals var contexts = map[string]uint{ blockCtx{}.key(): ngxMainConf, blockCtx{"events"}.key(): ngxEventConf, @@ -88,7 +89,7 @@ func enterBlockCtx(stmt *Directive, ctx blockCtx) blockCtx { return append(ctx, stmt.Directive) } -// nolint:gocyclo,funlen,gocognit +//nolint:gocyclo,funlen,gocognit func analyze(fname string, stmt *Directive, term string, ctx blockCtx, options *ParseOptions) error { masks, knownDirective := directives[stmt.Directive] currCtx, knownContext := contexts[ctx.key()] @@ -96,9 +97,11 @@ func analyze(fname string, stmt *Directive, term string, ctx blockCtx, options * // if strict and directive isn't recognized then throw error if options.ErrorOnUnknownDirectives && !knownDirective { return &ParseError{ - What: fmt.Sprintf(`unknown directive "%s"`, stmt.Directive), - File: &fname, - Line: &stmt.Line, + What: fmt.Sprintf(`unknown directive "%s"`, stmt.Directive), + File: &fname, + Line: &stmt.Line, + Statement: stmt.String(), + BlockCtx: ctx.getLastBlock(), } } @@ -120,9 +123,11 @@ func analyze(fname string, stmt *Directive, term string, ctx blockCtx, options * } if len(ctxMasks) == 0 && !options.SkipDirectiveContextCheck { return &ParseError{ - What: fmt.Sprintf(`"%s" directive is not allowed here`, stmt.Directive), - File: &fname, - Line: &stmt.Line, + What: fmt.Sprintf(`"%s" directive is not allowed here`, stmt.Directive), + File: &fname, + Line: &stmt.Line, + Statement: stmt.String(), + BlockCtx: ctx.getLastBlock(), } } } @@ -136,7 +141,6 @@ func analyze(fname string, stmt *Directive, term string, ctx blockCtx, options * var what string for i := 0; i < len(ctxMasks); i++ { mask := ctxMasks[i] - // if the directive is an expression type, there must be '(' 'expr' ')' args if (mask&ngxConfExpr) > 0 && !validExpr(stmt) { what = fmt.Sprintf(`directive "%s"'s is not enclosed in parentheses`, stmt.Directive) @@ -156,7 +160,7 @@ func analyze(fname string, stmt *Directive, term string, ctx blockCtx, options * } // use mask to check the directive's arguments - // nolint:gocritic + //nolint:gocritic if ((mask>>len(stmt.Args)&1) != 0 && len(stmt.Args) <= 7) || // NOARGS to TAKE7 ((mask&ngxConfFlag) != 0 && len(stmt.Args) == 1 && validFlag(stmt.Args[0])) || ((mask & ngxConfAny) != 0) || @@ -171,9 +175,11 @@ func analyze(fname string, stmt *Directive, term string, ctx blockCtx, options * } return &ParseError{ - What: what, - File: &fname, - Line: &stmt.Line, + What: what, + File: &fname, + Line: &stmt.Line, + Statement: stmt.String(), + BlockCtx: ctx.getLastBlock(), } } @@ -479,10 +485,10 @@ var directives = map[string][]uint{ "fastcgi_next_upstream": { ngxHTTPMainConf | ngxHTTPSrvConf | ngxHTTPLocConf | ngxConf1More, }, - "fastcgi_next_upStreamtimeout": { + "fastcgi_next_upstream_timeout": { ngxHTTPMainConf | ngxHTTPSrvConf | ngxHTTPLocConf | ngxConfTake1, }, - "fastcgi_next_upStreamtries": { + "fastcgi_next_upstream_tries": { ngxHTTPMainConf | ngxHTTPSrvConf | ngxHTTPLocConf | ngxConfTake1, }, "fastcgi_no_cache": { @@ -540,6 +546,10 @@ var directives = map[string][]uint{ ngxHTTPMainConf | ngxConfBlock | ngxConfTake12, ngxStreamMainConf | ngxConfBlock | ngxConfTake12, }, + "geoip2": { + ngxHTTPMainConf | ngxConfBlock | ngxConfTake1, + ngxStreamMainConf | ngxConfBlock | ngxConfTake1, + }, "geoip_city": { ngxHTTPMainConf | ngxConfTake12, ngxStreamMainConf | ngxConfTake12, diff --git a/sdk/vendor/github.com/nginxinc/nginx-go-crossplane/analyze_map.go b/sdk/vendor/github.com/nginxinc/nginx-go-crossplane/analyze_map.go index 953a64a62f..06c12fda4f 100644 --- a/sdk/vendor/github.com/nginxinc/nginx-go-crossplane/analyze_map.go +++ b/sdk/vendor/github.com/nginxinc/nginx-go-crossplane/analyze_map.go @@ -38,23 +38,26 @@ var mapBodies = map[string]mapParameterMasks{ "split_clients": { defaultMasks: ngxConfTake1, }, + "geoip2": { + defaultMasks: ngxConf1More, + }, } // analyzeMapBody validates the body of a map-like directive. Map-like directives are block directives // that don't contain nginx directives, and therefore cannot be analyzed in the same way as other blocks. func analyzeMapBody(fname string, parameter *Directive, term string, mapCtx string) error { masks, known := mapBodies[mapCtx] - // if we're not inside a known map-like directive, don't bother analyzing if !known { return nil } - if term != ";" { return &ParseError{ - What: fmt.Sprintf(`unexpected "%s"`, term), - File: &fname, - Line: ¶meter.Line, + What: fmt.Sprintf(`unexpected "%s"`, term), + File: &fname, + Line: ¶meter.Line, + Statement: parameter.String(), + BlockCtx: mapCtx, } } @@ -65,9 +68,11 @@ func analyzeMapBody(fname string, parameter *Directive, term string, mapCtx stri } return &ParseError{ - What: "invalid number of parameters", - File: &fname, - Line: ¶meter.Line, + What: "invalid number of parameters", + File: &fname, + Line: ¶meter.Line, + Statement: parameter.String(), + BlockCtx: mapCtx, } } @@ -79,9 +84,11 @@ func analyzeMapBody(fname string, parameter *Directive, term string, mapCtx stri } return &ParseError{ - What: "invalid number of parameters", - File: &fname, - Line: ¶meter.Line, + What: "invalid number of parameters", + File: &fname, + Line: ¶meter.Line, + Statement: parameter.String(), + BlockCtx: mapCtx, } } diff --git a/sdk/vendor/github.com/nginxinc/nginx-go-crossplane/build.go b/sdk/vendor/github.com/nginxinc/nginx-go-crossplane/build.go index 3ab45c84dd..72a9f64e08 100644 --- a/sdk/vendor/github.com/nginxinc/nginx-go-crossplane/build.go +++ b/sdk/vendor/github.com/nginxinc/nginx-go-crossplane/build.go @@ -25,7 +25,7 @@ type BuildOptions struct { const MaxIndent = 100 -// nolint:gochecknoglobals +//nolint:gochecknoglobals var ( marginSpaces = strings.Repeat(" ", MaxIndent) marginTabs = strings.Repeat("\t", MaxIndent) @@ -108,7 +108,6 @@ func Build(w io.Writer, config Config, options *BuildOptions) error { return err } -//nolint:gocognit func buildBlock(sb io.StringWriter, parent *Directive, block Directives, depth int, lastLine int, options *BuildOptions) { for i, stmt := range block { // if the this statement is a comment on the same line as the preview, do not emit EOL for this stmt @@ -185,7 +184,7 @@ func Enquote(arg string) string { return strings.ReplaceAll(repr(arg), `\\`, `\`) } -// nolint:gocyclo,gocognit +//nolint:gocyclo func needsQuote(s string) bool { if s == "" { return true @@ -201,7 +200,7 @@ func needsQuote(s string) bool { } // get first rune - char, off := utf8.DecodeRune([]byte(chars)) + char, off := utf8.DecodeRuneInString(chars) // arguments can't start with variable expansion syntax if unicode.IsSpace(char) || strings.ContainsRune("{};\"'", char) || strings.HasPrefix(chars, "${") { @@ -211,7 +210,7 @@ func needsQuote(s string) bool { chars = chars[off:] expanding := false - var prev rune = 0 + var prev rune for _, c := range chars { char = c diff --git a/sdk/vendor/github.com/nginxinc/nginx-go-crossplane/errors.go b/sdk/vendor/github.com/nginxinc/nginx-go-crossplane/errors.go index ea7892f604..3201d1eb57 100644 --- a/sdk/vendor/github.com/nginxinc/nginx-go-crossplane/errors.go +++ b/sdk/vendor/github.com/nginxinc/nginx-go-crossplane/errors.go @@ -13,9 +13,13 @@ import ( ) type ParseError struct { - What string - File *string - Line *int + What string + File *string + Line *int + // Raw directive statement causing the parse error. + Statement string + // Block in which parse error occurred. + BlockCtx string originalErr error } diff --git a/sdk/vendor/github.com/nginxinc/nginx-go-crossplane/lex.go b/sdk/vendor/github.com/nginxinc/nginx-go-crossplane/lex.go index cdca15f21c..e26d26383f 100644 --- a/sdk/vendor/github.com/nginxinc/nginx-go-crossplane/lex.go +++ b/sdk/vendor/github.com/nginxinc/nginx-go-crossplane/lex.go @@ -33,10 +33,10 @@ const ( const TokenChanCap = 2048 -// nolint:gochecknoglobals +//nolint:gochecknoglobals var lexerFile = "lexer" // pseudo file name for use by parse errors -// nolint:gochecknoglobals +//nolint:gochecknoglobals var tokChanCap = TokenChanCap // capacity of lexer token channel // note: this is only used during tests, should not be changed @@ -49,7 +49,7 @@ func Lex(reader io.Reader) chan NgxToken { return tc } -// nolint:gocyclo,funlen,gocognit +//nolint:gocyclo,funlen,gocognit func tokenize(reader io.Reader, tokenCh chan NgxToken) { token := strings.Builder{} tokenLine := 1 @@ -198,12 +198,11 @@ func tokenize(reader io.Reader, tokenCh chan NgxToken) { token.WriteString(la) case inVar: + token.WriteString(la) // this is using the same logic as the exiting lexer, but this is wrong since it does not terminate on token boundary if !strings.HasSuffix(token.String(), "}") && !isSpace(la) { - token.WriteString(la) continue } - token.WriteString(la) lexState = inWord case inQuote: diff --git a/sdk/vendor/github.com/nginxinc/nginx-go-crossplane/parse.go b/sdk/vendor/github.com/nginxinc/nginx-go-crossplane/parse.go index cfaa06babc..2945bc3569 100644 --- a/sdk/vendor/github.com/nginxinc/nginx-go-crossplane/parse.go +++ b/sdk/vendor/github.com/nginxinc/nginx-go-crossplane/parse.go @@ -18,7 +18,7 @@ import ( "strings" ) -// nolint:gochecknoglobals +//nolint:gochecknoglobals var ( hasMagic = regexp.MustCompile(`[*?[]`) osOpen = func(path string) (io.Reader, error) { return os.Open(path) } @@ -31,6 +31,13 @@ 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 @@ -182,7 +189,8 @@ func (p *parser) openFile(path string) (io.Reader, error) { } // parse Recursively parses directives from an nginx config context. -// nolint:gocyclo,funlen,gocognit +// +//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 @@ -191,6 +199,7 @@ func (p *parser) parse(parsing *Config, tokens <-chan NgxToken, ctx blockCtx, co var perr *ParseError if errors.As(t.Error, &perr) { perr.File = &parsing.File + perr.BlockCtx = ctx.getLastBlock() return nil, perr } return nil, &ParseError{ @@ -198,6 +207,7 @@ func (p *parser) parse(parsing *Config, tokens <-chan NgxToken, ctx blockCtx, co File: &parsing.File, Line: &t.Line, originalErr: t.Error, + BlockCtx: ctx.getLastBlock(), } } @@ -249,13 +259,14 @@ func (p *parser) parse(parsing *Config, tokens <-chan NgxToken, ctx blockCtx, co 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 { - commentsInArgs = append(commentsInArgs, t.Value[1:]) - } else { + 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 { @@ -264,6 +275,7 @@ func (p *parser) parse(parsing *Config, tokens <-chan NgxToken, ctx blockCtx, co File: &parsing.File, Line: &stmt.Line, originalErr: ErrPrematureLexEnd, + BlockCtx: ctx.getLastBlock(), } } } @@ -329,8 +341,10 @@ func (p *parser) parse(parsing *Config, tokens <-chan NgxToken, ctx blockCtx, co parsing.File, stmt.Line, ), - File: &parsing.File, - Line: &stmt.Line, + File: &parsing.File, + Line: &stmt.Line, + Statement: stmt.String(), + BlockCtx: ctx.getLastBlock(), } } @@ -352,9 +366,11 @@ func (p *parser) parse(parsing *Config, tokens <-chan NgxToken, ctx blockCtx, co // 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, + What: err.Error(), + File: &parsing.File, + Line: &stmt.Line, + Statement: stmt.String(), + BlockCtx: ctx.getLastBlock(), } if !p.options.StopParsingOnError { p.handleError(parsing, perr) diff --git a/sdk/vendor/github.com/nginxinc/nginx-go-crossplane/util.go b/sdk/vendor/github.com/nginxinc/nginx-go-crossplane/util.go index a1993fc341..d2e84adeb9 100644 --- a/sdk/vendor/github.com/nginxinc/nginx-go-crossplane/util.go +++ b/sdk/vendor/github.com/nginxinc/nginx-go-crossplane/util.go @@ -154,9 +154,10 @@ func performIncludes(old *Payload, fromfile string, block Directives) chan inclu if idx >= len(old.Config) { c <- included{ err: &ParseError{ - What: fmt.Sprintf("include config with index: %d", idx), - File: &fromfile, - Line: &dir.Line, + What: fmt.Sprintf("include config with index: %d", idx), + File: &fromfile, + Line: &dir.Line, + Statement: dir.String(), }, } return diff --git a/sdk/vendor/modules.txt b/sdk/vendor/modules.txt index a528bc0ed1..3c767ace5f 100644 --- a/sdk/vendor/modules.txt +++ b/sdk/vendor/modules.txt @@ -44,7 +44,7 @@ github.com/maxbrunsfeld/counterfeiter/v6/generator # github.com/mitchellh/mapstructure v1.5.0 ## explicit; go 1.14 github.com/mitchellh/mapstructure -# github.com/nginxinc/nginx-go-crossplane v0.4.17 +# github.com/nginxinc/nginx-go-crossplane v0.4.24 ## explicit; go 1.19 github.com/nginxinc/nginx-go-crossplane # github.com/pmezard/go-difflib v1.0.0 diff --git a/vendor/github.com/nginxinc/nginx-go-crossplane/.golangci.yml b/vendor/github.com/nginxinc/nginx-go-crossplane/.golangci.yml index e69465e072..7888e9f1a6 100644 --- a/vendor/github.com/nginxinc/nginx-go-crossplane/.golangci.yml +++ b/vendor/github.com/nginxinc/nginx-go-crossplane/.golangci.yml @@ -4,6 +4,9 @@ linters: enable-all: true disable: + - cyclop + - depguard + - varnamelen - goimports # handled by `make format` - gofmt # handled by `make format` - wsl # hyper specific to a the creator and not configurable enough to be useful - https://github.com/bombsimon/wsl @@ -11,7 +14,6 @@ linters: - gomnd - godox - nlreturn - - exhaustivestruct - wrapcheck - thelper - testpackage @@ -25,11 +27,13 @@ linters: - golint - maligned - scopelint + - exhaustruct # deprecated - deadcode - varcheck - structcheck - nosnakecase + - exhaustivestruct # Run options diff --git a/vendor/github.com/nginxinc/nginx-go-crossplane/analyze.go b/vendor/github.com/nginxinc/nginx-go-crossplane/analyze.go index 8adf1e4acc..1ad51d795a 100644 --- a/vendor/github.com/nginxinc/nginx-go-crossplane/analyze.go +++ b/vendor/github.com/nginxinc/nginx-go-crossplane/analyze.go @@ -61,7 +61,8 @@ const ngxAnyConf = ngxMainConf | ngxEventConf | ngxMailMainConf | ngxMailSrvConf ngxHTTPMainConf | ngxHTTPSrvConf | ngxHTTPLocConf | ngxHTTPUpsConf // map for getting bitmasks from certain context tuples -// nolint:gochecknoglobals +// +//nolint:gochecknoglobals var contexts = map[string]uint{ blockCtx{}.key(): ngxMainConf, blockCtx{"events"}.key(): ngxEventConf, @@ -88,7 +89,7 @@ func enterBlockCtx(stmt *Directive, ctx blockCtx) blockCtx { return append(ctx, stmt.Directive) } -// nolint:gocyclo,funlen,gocognit +//nolint:gocyclo,funlen,gocognit func analyze(fname string, stmt *Directive, term string, ctx blockCtx, options *ParseOptions) error { masks, knownDirective := directives[stmt.Directive] currCtx, knownContext := contexts[ctx.key()] @@ -96,9 +97,11 @@ func analyze(fname string, stmt *Directive, term string, ctx blockCtx, options * // if strict and directive isn't recognized then throw error if options.ErrorOnUnknownDirectives && !knownDirective { return &ParseError{ - What: fmt.Sprintf(`unknown directive "%s"`, stmt.Directive), - File: &fname, - Line: &stmt.Line, + What: fmt.Sprintf(`unknown directive "%s"`, stmt.Directive), + File: &fname, + Line: &stmt.Line, + Statement: stmt.String(), + BlockCtx: ctx.getLastBlock(), } } @@ -120,9 +123,11 @@ func analyze(fname string, stmt *Directive, term string, ctx blockCtx, options * } if len(ctxMasks) == 0 && !options.SkipDirectiveContextCheck { return &ParseError{ - What: fmt.Sprintf(`"%s" directive is not allowed here`, stmt.Directive), - File: &fname, - Line: &stmt.Line, + What: fmt.Sprintf(`"%s" directive is not allowed here`, stmt.Directive), + File: &fname, + Line: &stmt.Line, + Statement: stmt.String(), + BlockCtx: ctx.getLastBlock(), } } } @@ -136,7 +141,6 @@ func analyze(fname string, stmt *Directive, term string, ctx blockCtx, options * var what string for i := 0; i < len(ctxMasks); i++ { mask := ctxMasks[i] - // if the directive is an expression type, there must be '(' 'expr' ')' args if (mask&ngxConfExpr) > 0 && !validExpr(stmt) { what = fmt.Sprintf(`directive "%s"'s is not enclosed in parentheses`, stmt.Directive) @@ -156,7 +160,7 @@ func analyze(fname string, stmt *Directive, term string, ctx blockCtx, options * } // use mask to check the directive's arguments - // nolint:gocritic + //nolint:gocritic if ((mask>>len(stmt.Args)&1) != 0 && len(stmt.Args) <= 7) || // NOARGS to TAKE7 ((mask&ngxConfFlag) != 0 && len(stmt.Args) == 1 && validFlag(stmt.Args[0])) || ((mask & ngxConfAny) != 0) || @@ -171,9 +175,11 @@ func analyze(fname string, stmt *Directive, term string, ctx blockCtx, options * } return &ParseError{ - What: what, - File: &fname, - Line: &stmt.Line, + What: what, + File: &fname, + Line: &stmt.Line, + Statement: stmt.String(), + BlockCtx: ctx.getLastBlock(), } } @@ -479,10 +485,10 @@ var directives = map[string][]uint{ "fastcgi_next_upstream": { ngxHTTPMainConf | ngxHTTPSrvConf | ngxHTTPLocConf | ngxConf1More, }, - "fastcgi_next_upStreamtimeout": { + "fastcgi_next_upstream_timeout": { ngxHTTPMainConf | ngxHTTPSrvConf | ngxHTTPLocConf | ngxConfTake1, }, - "fastcgi_next_upStreamtries": { + "fastcgi_next_upstream_tries": { ngxHTTPMainConf | ngxHTTPSrvConf | ngxHTTPLocConf | ngxConfTake1, }, "fastcgi_no_cache": { @@ -540,6 +546,10 @@ var directives = map[string][]uint{ ngxHTTPMainConf | ngxConfBlock | ngxConfTake12, ngxStreamMainConf | ngxConfBlock | ngxConfTake12, }, + "geoip2": { + ngxHTTPMainConf | ngxConfBlock | ngxConfTake1, + ngxStreamMainConf | ngxConfBlock | ngxConfTake1, + }, "geoip_city": { ngxHTTPMainConf | ngxConfTake12, ngxStreamMainConf | ngxConfTake12, diff --git a/vendor/github.com/nginxinc/nginx-go-crossplane/analyze_map.go b/vendor/github.com/nginxinc/nginx-go-crossplane/analyze_map.go index 953a64a62f..06c12fda4f 100644 --- a/vendor/github.com/nginxinc/nginx-go-crossplane/analyze_map.go +++ b/vendor/github.com/nginxinc/nginx-go-crossplane/analyze_map.go @@ -38,23 +38,26 @@ var mapBodies = map[string]mapParameterMasks{ "split_clients": { defaultMasks: ngxConfTake1, }, + "geoip2": { + defaultMasks: ngxConf1More, + }, } // analyzeMapBody validates the body of a map-like directive. Map-like directives are block directives // that don't contain nginx directives, and therefore cannot be analyzed in the same way as other blocks. func analyzeMapBody(fname string, parameter *Directive, term string, mapCtx string) error { masks, known := mapBodies[mapCtx] - // if we're not inside a known map-like directive, don't bother analyzing if !known { return nil } - if term != ";" { return &ParseError{ - What: fmt.Sprintf(`unexpected "%s"`, term), - File: &fname, - Line: ¶meter.Line, + What: fmt.Sprintf(`unexpected "%s"`, term), + File: &fname, + Line: ¶meter.Line, + Statement: parameter.String(), + BlockCtx: mapCtx, } } @@ -65,9 +68,11 @@ func analyzeMapBody(fname string, parameter *Directive, term string, mapCtx stri } return &ParseError{ - What: "invalid number of parameters", - File: &fname, - Line: ¶meter.Line, + What: "invalid number of parameters", + File: &fname, + Line: ¶meter.Line, + Statement: parameter.String(), + BlockCtx: mapCtx, } } @@ -79,9 +84,11 @@ func analyzeMapBody(fname string, parameter *Directive, term string, mapCtx stri } return &ParseError{ - What: "invalid number of parameters", - File: &fname, - Line: ¶meter.Line, + What: "invalid number of parameters", + File: &fname, + Line: ¶meter.Line, + Statement: parameter.String(), + BlockCtx: mapCtx, } } diff --git a/vendor/github.com/nginxinc/nginx-go-crossplane/build.go b/vendor/github.com/nginxinc/nginx-go-crossplane/build.go index 3ab45c84dd..72a9f64e08 100644 --- a/vendor/github.com/nginxinc/nginx-go-crossplane/build.go +++ b/vendor/github.com/nginxinc/nginx-go-crossplane/build.go @@ -25,7 +25,7 @@ type BuildOptions struct { const MaxIndent = 100 -// nolint:gochecknoglobals +//nolint:gochecknoglobals var ( marginSpaces = strings.Repeat(" ", MaxIndent) marginTabs = strings.Repeat("\t", MaxIndent) @@ -108,7 +108,6 @@ func Build(w io.Writer, config Config, options *BuildOptions) error { return err } -//nolint:gocognit func buildBlock(sb io.StringWriter, parent *Directive, block Directives, depth int, lastLine int, options *BuildOptions) { for i, stmt := range block { // if the this statement is a comment on the same line as the preview, do not emit EOL for this stmt @@ -185,7 +184,7 @@ func Enquote(arg string) string { return strings.ReplaceAll(repr(arg), `\\`, `\`) } -// nolint:gocyclo,gocognit +//nolint:gocyclo func needsQuote(s string) bool { if s == "" { return true @@ -201,7 +200,7 @@ func needsQuote(s string) bool { } // get first rune - char, off := utf8.DecodeRune([]byte(chars)) + char, off := utf8.DecodeRuneInString(chars) // arguments can't start with variable expansion syntax if unicode.IsSpace(char) || strings.ContainsRune("{};\"'", char) || strings.HasPrefix(chars, "${") { @@ -211,7 +210,7 @@ func needsQuote(s string) bool { chars = chars[off:] expanding := false - var prev rune = 0 + var prev rune for _, c := range chars { char = c diff --git a/vendor/github.com/nginxinc/nginx-go-crossplane/errors.go b/vendor/github.com/nginxinc/nginx-go-crossplane/errors.go index ea7892f604..3201d1eb57 100644 --- a/vendor/github.com/nginxinc/nginx-go-crossplane/errors.go +++ b/vendor/github.com/nginxinc/nginx-go-crossplane/errors.go @@ -13,9 +13,13 @@ import ( ) type ParseError struct { - What string - File *string - Line *int + What string + File *string + Line *int + // Raw directive statement causing the parse error. + Statement string + // Block in which parse error occurred. + BlockCtx string originalErr error } diff --git a/vendor/github.com/nginxinc/nginx-go-crossplane/lex.go b/vendor/github.com/nginxinc/nginx-go-crossplane/lex.go index cdca15f21c..e26d26383f 100644 --- a/vendor/github.com/nginxinc/nginx-go-crossplane/lex.go +++ b/vendor/github.com/nginxinc/nginx-go-crossplane/lex.go @@ -33,10 +33,10 @@ const ( const TokenChanCap = 2048 -// nolint:gochecknoglobals +//nolint:gochecknoglobals var lexerFile = "lexer" // pseudo file name for use by parse errors -// nolint:gochecknoglobals +//nolint:gochecknoglobals var tokChanCap = TokenChanCap // capacity of lexer token channel // note: this is only used during tests, should not be changed @@ -49,7 +49,7 @@ func Lex(reader io.Reader) chan NgxToken { return tc } -// nolint:gocyclo,funlen,gocognit +//nolint:gocyclo,funlen,gocognit func tokenize(reader io.Reader, tokenCh chan NgxToken) { token := strings.Builder{} tokenLine := 1 @@ -198,12 +198,11 @@ func tokenize(reader io.Reader, tokenCh chan NgxToken) { token.WriteString(la) case inVar: + token.WriteString(la) // this is using the same logic as the exiting lexer, but this is wrong since it does not terminate on token boundary if !strings.HasSuffix(token.String(), "}") && !isSpace(la) { - token.WriteString(la) continue } - token.WriteString(la) lexState = inWord case inQuote: diff --git a/vendor/github.com/nginxinc/nginx-go-crossplane/parse.go b/vendor/github.com/nginxinc/nginx-go-crossplane/parse.go index cfaa06babc..2945bc3569 100644 --- a/vendor/github.com/nginxinc/nginx-go-crossplane/parse.go +++ b/vendor/github.com/nginxinc/nginx-go-crossplane/parse.go @@ -18,7 +18,7 @@ import ( "strings" ) -// nolint:gochecknoglobals +//nolint:gochecknoglobals var ( hasMagic = regexp.MustCompile(`[*?[]`) osOpen = func(path string) (io.Reader, error) { return os.Open(path) } @@ -31,6 +31,13 @@ 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 @@ -182,7 +189,8 @@ func (p *parser) openFile(path string) (io.Reader, error) { } // parse Recursively parses directives from an nginx config context. -// nolint:gocyclo,funlen,gocognit +// +//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 @@ -191,6 +199,7 @@ func (p *parser) parse(parsing *Config, tokens <-chan NgxToken, ctx blockCtx, co var perr *ParseError if errors.As(t.Error, &perr) { perr.File = &parsing.File + perr.BlockCtx = ctx.getLastBlock() return nil, perr } return nil, &ParseError{ @@ -198,6 +207,7 @@ func (p *parser) parse(parsing *Config, tokens <-chan NgxToken, ctx blockCtx, co File: &parsing.File, Line: &t.Line, originalErr: t.Error, + BlockCtx: ctx.getLastBlock(), } } @@ -249,13 +259,14 @@ func (p *parser) parse(parsing *Config, tokens <-chan NgxToken, ctx blockCtx, co 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 { - commentsInArgs = append(commentsInArgs, t.Value[1:]) - } else { + 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 { @@ -264,6 +275,7 @@ func (p *parser) parse(parsing *Config, tokens <-chan NgxToken, ctx blockCtx, co File: &parsing.File, Line: &stmt.Line, originalErr: ErrPrematureLexEnd, + BlockCtx: ctx.getLastBlock(), } } } @@ -329,8 +341,10 @@ func (p *parser) parse(parsing *Config, tokens <-chan NgxToken, ctx blockCtx, co parsing.File, stmt.Line, ), - File: &parsing.File, - Line: &stmt.Line, + File: &parsing.File, + Line: &stmt.Line, + Statement: stmt.String(), + BlockCtx: ctx.getLastBlock(), } } @@ -352,9 +366,11 @@ func (p *parser) parse(parsing *Config, tokens <-chan NgxToken, ctx blockCtx, co // 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, + What: err.Error(), + File: &parsing.File, + Line: &stmt.Line, + Statement: stmt.String(), + BlockCtx: ctx.getLastBlock(), } if !p.options.StopParsingOnError { p.handleError(parsing, perr) diff --git a/vendor/github.com/nginxinc/nginx-go-crossplane/util.go b/vendor/github.com/nginxinc/nginx-go-crossplane/util.go index a1993fc341..d2e84adeb9 100644 --- a/vendor/github.com/nginxinc/nginx-go-crossplane/util.go +++ b/vendor/github.com/nginxinc/nginx-go-crossplane/util.go @@ -154,9 +154,10 @@ func performIncludes(old *Payload, fromfile string, block Directives) chan inclu if idx >= len(old.Config) { c <- included{ err: &ParseError{ - What: fmt.Sprintf("include config with index: %d", idx), - File: &fromfile, - Line: &dir.Line, + What: fmt.Sprintf("include config with index: %d", idx), + File: &fromfile, + Line: &dir.Line, + Statement: dir.String(), }, } return diff --git a/vendor/modules.txt b/vendor/modules.txt index ce23072f26..437f86c936 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1132,7 +1132,7 @@ github.com/nginx/agent/sdk/v2/proto github.com/nginx/agent/sdk/v2/proto/common github.com/nginx/agent/sdk/v2/proto/events github.com/nginx/agent/sdk/v2/zip -# github.com/nginxinc/nginx-go-crossplane v0.4.17 +# github.com/nginxinc/nginx-go-crossplane v0.4.24 ## explicit; go 1.19 github.com/nginxinc/nginx-go-crossplane # github.com/nginxinc/nginx-plus-go-client v0.10.0