Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lua scenarios where configuration parsed incorrectly #131

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions lex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,73 @@ var lexFixtures = []lexFixture{
{"}", 39},
{"}", 40},
}},
{"lua-block-cert-slim", []tokenLine{
{"http", 1},
{"{", 1},
{"server", 2},
{"{", 2},
{"ssl_certificate_by_lua_block", 3},
{"{", 3},
{"print(\"Test lua ssl certificate!\")", 4},
{"}", 5},
Comment on lines +324 to +326
Copy link
Collaborator

@xynicole xynicole Jan 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{"\n            print(\"Test lua ssl certificate!\")\n        ",5},
		{";", 5},

We grab everything in the Lua block as a single token and drop the {}. Example:
content_by_lua_block {some code} will be content_by_lua_block "some code"; "some code" is an argument of directive.

For reference: #86

{"}", 6},
{"}", 7},
}},
{"lua-block-cert-double-server", []tokenLine{
{"http", 1},
{"{", 1},
{"server", 2},
{"{", 2},
{"listen", 3},
{"443", 3},
{"ssl", 3},
{";", 3},
{"server_name", 4},
{"lua.example.com", 4},
{";", 4},
{"location", 6},
{"/", 6},
{"{", 6},
{"root", 7},
{"/usr/share/nginx/html", 7},
{";", 7},
{"index", 8},
{"index.html", 8},
{"index.htm", 8},
{";", 8},
{"}", 9},
{"error_page", 11},
{"500", 11},
{"502", 11},
{"503", 11},
{"504", 11},
{"/50x.html", 11},
{";", 11},
{"location", 12},
{"=", 12},
{"/50x.html", 12},
{"{", 12},
{"root", 13},
{"/usr/share/nginx/html", 13},
{";", 13},
{"}", 14},
{"ssl_certificate", 16},
{"/etc/nginx/ssl/cert1.crt", 16},
{";", 16},
{"ssl_certificate_key", 17},
{"/etc/nginx/ssl/key1.key", 17},
{";", 17},
{"ssl_certificate_by_lua_block", 19},
{"{", 19},
{"\n print(\"Test lua ssl certificate!\")\n ", 19},
{")", 20},
{"}", 21},
Comment on lines +376 to +378
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{"\n print(\"Test lua ssl certificate!\")\n ", 19},
{")", 20},
{"}", 21},
{"\n print(\"Test lua ssl certificate!\")\n ",21},

{"}", 22},
{"server", 24},
{"{", 24},
{"}", 25},
{"}", 26},
}},
{"lua-block-larger", []tokenLine{
{"http", 1},
{"{", 1},
Expand Down
138 changes: 138 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1834,6 +1834,144 @@ var parseFixtures = []parseFixture{
},
},
}},
{"lua-block-cert-slim", "", ParseOptions{
SingleFile: true,
ErrorOnUnknownDirectives: true,
DirectiveSources: []MatchFunc{MatchNginxPlusLatest, MatchLuaLatest},
LexOptions: LexOptions{
Lexers: []RegisterLexer{lua.RegisterLexer()},
},
}, Payload{
Status: "ok",
Errors: []PayloadError{},
Config: []Config{
{
File: getTestConfigPath("lua-block-cert-slim", "nginx.conf"),
Status: "ok",
Parsed: Directives{
{
Directive: "http",
Line: 1,
Args: []string{},
Block: Directives{
{
Directive: "server",
Line: 2,
Args: []string{},
Block: Directives{
{
Directive: "ssl_certificate_by_lua_block",
Line: 3,
Args: []string{"\n print(\"Test lua ssl certificate!\")\n "},
},
},
},
},
},
},
},
},
}},
{"lua-block-cert-double-server", "", ParseOptions{
SingleFile: true,
ErrorOnUnknownDirectives: true,
DirectiveSources: []MatchFunc{MatchNginxPlusLatest, MatchLuaLatest},
LexOptions: LexOptions{
Lexers: []RegisterLexer{lua.RegisterLexer()},
},
}, Payload{
Status: "ok",
Errors: []PayloadError{},
Config: []Config{
{
File: "testdata/configs/lua-block-cert-double-server/nginx.conf",
Status: "ok",
Errors: []ConfigError{},
Parsed: Directives{
{
Directive: "http",
Line: 1,
Args: []string{},
Block: Directives{
{
Directive: "server",
Line: 2,
Args: []string{},
Block: Directives{
{
Directive: "listen",
Line: 3,
Args: []string{"443", "ssl"},
},
{
Directive: "server_name",
Line: 4,
Args: []string{"lua.example.com"},
},
{
Directive: "location",
Line: 6,
Args: []string{"/"},
Block: Directives{
{
Directive: "root",
Line: 7,
Args: []string{"/usr/share/nginx/html"},
},
{
Directive: "index",
Line: 8,
Args: []string{"index.html", "index.htm"},
},
},
},
{
Directive: "error_page",
Line: 11,
Args: []string{"500", "502", "503", "504", "/50x.html"},
},
{
Directive: "location",
Line: 12,
Args: []string{"=", "/50x.html"},
Block: Directives{
{
Directive: "root",
Line: 13,
Args: []string{"/usr/share/nginx/html"},
},
},
},
{
Directive: "ssl_certificate",
Line: 16,
Args: []string{"/etc/nginx/ssl/cert1.crt"},
},
{
Directive: "ssl_certificate_key",
Line: 17,
Args: []string{"/etc/nginx/ssl/key1.key"},
},
{
Directive: "ssl_certificate_by_lua_block",
Line: 19,
Args: []string{
"\n print(\"Test lua ssl certificate!\")\n ",
},
},
},
},
{
Directive: "server",
Line: 24,
Args: []string{},
},
},
},
},
},
},
}},
{"lua-block-larger", "", ParseOptions{
SingleFile: true,
ErrorOnUnknownDirectives: true,
Expand Down
26 changes: 26 additions & 0 deletions testdata/configs/lua-block-cert-double-server/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
http {
server {
listen 443 ssl;
server_name lua.example.com;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

ssl_certificate /etc/nginx/ssl/cert1.crt;
ssl_certificate_key /etc/nginx/ssl/key1.key;

ssl_certificate_by_lua_block {
print("Test lua ssl certificate!")
}
}

server {
}
}
93 changes: 93 additions & 0 deletions testdata/configs/lua-block-cert-double-server/nginx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
{
"status": "ok",
"errors": [],
"config": [
{
"file": "testdata/configs/lua-block-cert-double-server/nginx.conf",
"status": "ok",
"errors": [],
"parsed": [
{
"directive": "http",
"line": 1,
"args": [],
"block": [
{
"directive": "server",
"line": 2,
"args": [],
"block": [
{
"directive": "listen",
"line": 3,
"args": ["443", "ssl"]
},
{
"directive": "server_name",
"line": 4,
"args": ["lua.example.com"]
},
{
"directive": "location",
"line": 6,
"args": ["/"],
"block": [
{
"directive": "root",
"line": 7,
"args": ["/usr/share/nginx/html"]
},
{
"directive": "index",
"line": 8,
"args": ["index.html", "index.htm"]
}
]
},
{
"directive": "error_page",
"line": 11,
"args": ["500", "502", "503", "504", "/50x.html"]
},
{
"directive": "location",
"line": 12,
"args": ["=", "/50x.html"],
"block": [
{
"directive": "root",
"line": 13,
"args": ["/usr/share/nginx/html"]
}
]
},
{
"directive": "ssl_certificate",
"line": 16,
"args": ["/etc/nginx/ssl/cert1.crt"]
},
{
"directive": "ssl_certificate_key",
"line": 17,
"args": ["/etc/nginx/ssl/key1.key"]
},
{
"directive": "ssl_certificate_by_lua_block",
"line": 19,
"args": [
"\n print(\"Test lua ssl certificate!\")\n "
]
}
]
},
{
"directive": "server",
"line": 24,
"args": []
}
]
}
]
}
]
}
7 changes: 7 additions & 0 deletions testdata/configs/lua-block-cert-slim/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
http {
server {
ssl_certificate_by_lua_block {
print("Test lua ssl certificate!")
}
}
}
34 changes: 34 additions & 0 deletions testdata/configs/lua-block-cert-slim/nginx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"status": "ok",
"errors": [],
"config": [
{
"file": "testdata/configs/lua-block-cert-slim/nginx.conf",
"status": "ok",
"errors": [],
"parsed": [
{
"directive": "http",
"line": 1,
"args": [],
"block": [
{
"directive": "server",
"line": 2,
"args": [],
"block": [
{
"directive": "ssl_certificate_by_lua_block",
"line": 3,
"args": [
"\n print(\"Test lua ssl certificate!\")\n "
]
}
]
}
]
}
]
}
]
}
Loading