Skip to content

Commit

Permalink
Merge pull request #105 from dengsh12/directives_fix
Browse files Browse the repository at this point in the history
Fix Nplus directives definition
  • Loading branch information
dengsh12 authored Jul 11, 2024
2 parents e00ed70 + 23ebd67 commit 7ccbf75
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 3 deletions.
6 changes: 3 additions & 3 deletions analyze_nplus_latest_directives.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,8 +695,8 @@ var ngxPlusLatestDirectives = map[string][]uint{
ngxHTTPMainConf | ngxHTTPSrvConf | ngxHTTPLocConf | ngxConfTake12,
},
"keyval": {
ngxHTTPMainConf | ngxConfTake3 | ngxConfTake2,
ngxStreamMainConf | ngxConfTake3 | ngxConfTake2,
ngxHTTPMainConf | ngxConfTake3 | ngxConfTake4,
ngxStreamMainConf | ngxConfTake3 | ngxConfTake4,
},
"keyval_zone": {
ngxHTTPMainConf | ngxConf1More,
Expand Down Expand Up @@ -754,7 +754,7 @@ var ngxPlusLatestDirectives = map[string][]uint{
ngxHTTPMainConf | ngxHTTPSrvConf | ngxHTTPLocConf | ngxConfTake1,
},
"limit_req_zone": {
ngxHTTPMainConf | ngxConfTake3 | ngxConfTake2,
ngxHTTPMainConf | ngxConfTake3 | ngxConfTake4,
},
"lingering_close": {
ngxHTTPMainConf | ngxHTTPSrvConf | ngxHTTPLocConf | ngxConfTake1,
Expand Down
37 changes: 37 additions & 0 deletions analyze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2601,3 +2601,40 @@ func TestAnalyze_directiveSources_defaultBehavior(t *testing.T) {
})
}
}

func TestAnalyze_limit_req_zone(t *testing.T) {
t.Parallel()
testcases := map[string]struct {
stmt *Directive
ctx blockCtx
wantErr bool
}{
"limit_req_zone ok http": {
&Directive{
Directive: "limit_req_zone",
Args: []string{"$binary_remote_addr", "zone=one:10m", "rate=1r/s", "sync"},
Line: 5,
},
blockCtx{"http"},
false,
},
}

for name, tc := range testcases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
err := analyze("nginx.conf", tc.stmt, ";", tc.ctx, &ParseOptions{
DirectiveSources: []MatchFunc{NgxPlusLatestDirectivesMatchFn, AppProtectWAFv4DirectivesMatchFn},
})

if !tc.wantErr && err != nil {
t.Fatal(err)
}

if tc.wantErr && err == nil {
t.Fatal("expected error, got nil")
}
})
}
}
94 changes: 94 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2012,6 +2012,100 @@ var parseFixtures = []parseFixture{
},
},
}},
{"limit-req-zone", "", ParseOptions{SingleFile: true}, Payload{
Status: "ok",
Errors: []PayloadError{},
Config: []Config{
{
File: getTestConfigPath("limit-req-zone", "nginx.conf"),
Status: "ok",
Errors: []ConfigError{},
Parsed: Directives{
{
Directive: "user",
Args: []string{"nginx"},
Line: 1,
Block: nil,
},
{
Directive: "worker_processes",
Args: []string{"auto"},
Line: 2,
Block: nil,
},
{
Directive: "error_log",
Args: []string{"/var/log/nginx/error.log", "notice"},
Line: 4,
Block: nil,
},
{
Directive: "pid",
Args: []string{"/var/run/nginx.pid"},
Line: 5,
Block: nil,
},
{
Directive: "events",
Args: []string{},
Line: 7,
Block: Directives{
{
Directive: "worker_connections",
Args: []string{"1024"},
Line: 8,
},
},
},
{
Directive: "http",
Args: []string{},
Line: 11,
Block: Directives{
{
Directive: "include",
Args: []string{"/etc/nginx/mime.types"},
Line: 12,
},
{
Directive: "default_type",
Args: []string{"application/octet-stream"},
Line: 13,
},
{
Directive: "limit_req_zone",
Args: []string{"$binary_remote_addr", "zone=one:10m", "rate=1r/s", "sync"},
Line: 15,
},
{
Directive: "log_format",
Args: []string{"main",
"$remote_addr - $remote_user [$time_local] \"$request\" ",
"$status $body_bytes_sent \"$http_referer\" ",
"\"$http_user_agent\" \"$http_x_forwarded_for\"",
},
Line: 17,
},
{
Directive: "access_log",
Args: []string{"/var/log/nginx/access.log", "main"},
Line: 21,
},
{
Directive: "sendfile",
Args: []string{"on"},
Line: 23,
}, {
Directive: "keepalive_timeout",
Args: []string{"65"},
Line: 25,
},
},
},
},
},
},
}},
}

func TestParse(t *testing.T) {
Expand Down
26 changes: 26 additions & 0 deletions testdata/configs/limit-req-zone/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
user nginx;
worker_processes auto;

error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s sync;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;

keepalive_timeout 65;
}

0 comments on commit 7ccbf75

Please sign in to comment.