Skip to content

Commit 7ccbf75

Browse files
authored
Merge pull request #105 from dengsh12/directives_fix
Fix Nplus directives definition
2 parents e00ed70 + 23ebd67 commit 7ccbf75

File tree

4 files changed

+160
-3
lines changed

4 files changed

+160
-3
lines changed

analyze_nplus_latest_directives.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -695,8 +695,8 @@ var ngxPlusLatestDirectives = map[string][]uint{
695695
ngxHTTPMainConf | ngxHTTPSrvConf | ngxHTTPLocConf | ngxConfTake12,
696696
},
697697
"keyval": {
698-
ngxHTTPMainConf | ngxConfTake3 | ngxConfTake2,
699-
ngxStreamMainConf | ngxConfTake3 | ngxConfTake2,
698+
ngxHTTPMainConf | ngxConfTake3 | ngxConfTake4,
699+
ngxStreamMainConf | ngxConfTake3 | ngxConfTake4,
700700
},
701701
"keyval_zone": {
702702
ngxHTTPMainConf | ngxConf1More,
@@ -754,7 +754,7 @@ var ngxPlusLatestDirectives = map[string][]uint{
754754
ngxHTTPMainConf | ngxHTTPSrvConf | ngxHTTPLocConf | ngxConfTake1,
755755
},
756756
"limit_req_zone": {
757-
ngxHTTPMainConf | ngxConfTake3 | ngxConfTake2,
757+
ngxHTTPMainConf | ngxConfTake3 | ngxConfTake4,
758758
},
759759
"lingering_close": {
760760
ngxHTTPMainConf | ngxHTTPSrvConf | ngxHTTPLocConf | ngxConfTake1,

analyze_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2601,3 +2601,40 @@ func TestAnalyze_directiveSources_defaultBehavior(t *testing.T) {
26012601
})
26022602
}
26032603
}
2604+
2605+
func TestAnalyze_limit_req_zone(t *testing.T) {
2606+
t.Parallel()
2607+
testcases := map[string]struct {
2608+
stmt *Directive
2609+
ctx blockCtx
2610+
wantErr bool
2611+
}{
2612+
"limit_req_zone ok http": {
2613+
&Directive{
2614+
Directive: "limit_req_zone",
2615+
Args: []string{"$binary_remote_addr", "zone=one:10m", "rate=1r/s", "sync"},
2616+
Line: 5,
2617+
},
2618+
blockCtx{"http"},
2619+
false,
2620+
},
2621+
}
2622+
2623+
for name, tc := range testcases {
2624+
tc := tc
2625+
t.Run(name, func(t *testing.T) {
2626+
t.Parallel()
2627+
err := analyze("nginx.conf", tc.stmt, ";", tc.ctx, &ParseOptions{
2628+
DirectiveSources: []MatchFunc{NgxPlusLatestDirectivesMatchFn, AppProtectWAFv4DirectivesMatchFn},
2629+
})
2630+
2631+
if !tc.wantErr && err != nil {
2632+
t.Fatal(err)
2633+
}
2634+
2635+
if tc.wantErr && err == nil {
2636+
t.Fatal("expected error, got nil")
2637+
}
2638+
})
2639+
}
2640+
}

parse_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,6 +2012,100 @@ var parseFixtures = []parseFixture{
20122012
},
20132013
},
20142014
}},
2015+
{"limit-req-zone", "", ParseOptions{SingleFile: true}, Payload{
2016+
Status: "ok",
2017+
Errors: []PayloadError{},
2018+
Config: []Config{
2019+
{
2020+
File: getTestConfigPath("limit-req-zone", "nginx.conf"),
2021+
Status: "ok",
2022+
Errors: []ConfigError{},
2023+
Parsed: Directives{
2024+
{
2025+
Directive: "user",
2026+
Args: []string{"nginx"},
2027+
Line: 1,
2028+
Block: nil,
2029+
},
2030+
{
2031+
Directive: "worker_processes",
2032+
Args: []string{"auto"},
2033+
Line: 2,
2034+
Block: nil,
2035+
},
2036+
{
2037+
Directive: "error_log",
2038+
Args: []string{"/var/log/nginx/error.log", "notice"},
2039+
Line: 4,
2040+
Block: nil,
2041+
},
2042+
{
2043+
Directive: "pid",
2044+
Args: []string{"/var/run/nginx.pid"},
2045+
Line: 5,
2046+
Block: nil,
2047+
},
2048+
{
2049+
Directive: "events",
2050+
Args: []string{},
2051+
Line: 7,
2052+
Block: Directives{
2053+
{
2054+
Directive: "worker_connections",
2055+
Args: []string{"1024"},
2056+
Line: 8,
2057+
},
2058+
},
2059+
},
2060+
{
2061+
Directive: "http",
2062+
Args: []string{},
2063+
Line: 11,
2064+
Block: Directives{
2065+
{
2066+
Directive: "include",
2067+
Args: []string{"/etc/nginx/mime.types"},
2068+
Line: 12,
2069+
},
2070+
{
2071+
Directive: "default_type",
2072+
Args: []string{"application/octet-stream"},
2073+
Line: 13,
2074+
},
2075+
{
2076+
Directive: "limit_req_zone",
2077+
Args: []string{"$binary_remote_addr", "zone=one:10m", "rate=1r/s", "sync"},
2078+
Line: 15,
2079+
},
2080+
{
2081+
Directive: "log_format",
2082+
Args: []string{"main",
2083+
"$remote_addr - $remote_user [$time_local] \"$request\" ",
2084+
"$status $body_bytes_sent \"$http_referer\" ",
2085+
"\"$http_user_agent\" \"$http_x_forwarded_for\"",
2086+
},
2087+
Line: 17,
2088+
},
2089+
{
2090+
Directive: "access_log",
2091+
Args: []string{"/var/log/nginx/access.log", "main"},
2092+
Line: 21,
2093+
},
2094+
{
2095+
Directive: "sendfile",
2096+
Args: []string{"on"},
2097+
Line: 23,
2098+
}, {
2099+
Directive: "keepalive_timeout",
2100+
Args: []string{"65"},
2101+
Line: 25,
2102+
},
2103+
},
2104+
},
2105+
},
2106+
},
2107+
},
2108+
}},
20152109
}
20162110

20172111
func TestParse(t *testing.T) {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
user nginx;
2+
worker_processes auto;
3+
4+
error_log /var/log/nginx/error.log notice;
5+
pid /var/run/nginx.pid;
6+
7+
events {
8+
worker_connections 1024;
9+
}
10+
11+
http {
12+
include /etc/nginx/mime.types;
13+
default_type application/octet-stream;
14+
15+
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s sync;
16+
17+
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
18+
'$status $body_bytes_sent "$http_referer" '
19+
'"$http_user_agent" "$http_x_forwarded_for"';
20+
21+
access_log /var/log/nginx/access.log main;
22+
23+
sendfile on;
24+
25+
keepalive_timeout 65;
26+
}

0 commit comments

Comments
 (0)