Skip to content

Bugfix: Wrong filenames after includes in certain scenarios #95

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

Closed
wants to merge 3 commits into from
Closed
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: 35 additions & 32 deletions crossplane/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,40 @@ def _handle_error(parsing, e):
payload['status'] = 'failed'
payload['errors'].append(payload_error)

def _handle_include(parsing, ctx, stmt):
pattern = stmt['args'][0]
if not os.path.isabs(pattern):
pattern = os.path.join(config_dir, pattern)

stmt['includes'] = []

# get names of all included files
if glob.has_magic(pattern):
fnames = glob.glob(pattern)
fnames.sort()
else:
try:
# if the file pattern was explicit, nginx will check
# that the included file can be opened and read
open(str(pattern)).close()
fnames = [pattern]
except Exception as e:
fnames = []
e.lineno = stmt['line']
if catch_errors:
_handle_error(parsing, e)
else:
raise e

for fname in fnames:
# the included set keeps files from being parsed twice
# TODO: handle files included from multiple contexts
if fname not in included:
included[fname] = len(includes)
includes.append((fname, ctx))
index = included[fname]
stmt['includes'].append(index)

def _parse(parsing, tokens, ctx=(), consume=False):
"""Recursively parses nginx config contexts"""
fname = parsing['file']
Expand Down Expand Up @@ -162,38 +196,7 @@ def _parse(parsing, tokens, ctx=(), consume=False):

# add "includes" to the payload if this is an include statement
if not single and stmt['directive'] == 'include':
pattern = args[0]
if not os.path.isabs(args[0]):
pattern = os.path.join(config_dir, args[0])

stmt['includes'] = []

# get names of all included files
if glob.has_magic(pattern):
fnames = glob.glob(pattern)
fnames.sort()
else:
try:
# if the file pattern was explicit, nginx will check
# that the included file can be opened and read
open(str(pattern)).close()
fnames = [pattern]
except Exception as e:
fnames = []
e.lineno = stmt['line']
if catch_errors:
_handle_error(parsing, e)
else:
raise e

for fname in fnames:
# the included set keeps files from being parsed twice
# TODO: handle files included from multiple contexts
if fname not in included:
included[fname] = len(includes)
includes.append((fname, ctx))
index = included[fname]
stmt['includes'].append(index)
_handle_include(parsing, ctx, stmt)

# if this statement terminated with '{' then it is a block
if token == '{' and not quoted:
Expand Down
2 changes: 2 additions & 0 deletions tests/configs/includes-regular-combined/conf.d/bar.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# this file should never be included
asdfasdfasdfasdfasdfasdfasdfasdfasdf
2 changes: 2 additions & 0 deletions tests/configs/includes-regular-combined/conf.d/foo.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# this file should never be included
asdfasdfasdfasdfasdfasdfasdfasdfasdf
15 changes: 15 additions & 0 deletions tests/configs/includes-regular-combined/conf.d/server.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
server {
listen 127.0.0.1:8080;
server_name default_server;
include foo.conf;

location /A {
return 200 'A';
}

location /B {
return 200 'B';
}

include bar.conf;
}
3 changes: 3 additions & 0 deletions tests/configs/includes-regular-combined/foo.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
location /foo {
return 200 'foo';
}
4 changes: 4 additions & 0 deletions tests/configs/includes-regular-combined/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
events {}
http {
include conf.d/server.conf;
}
105 changes: 105 additions & 0 deletions tests/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,111 @@ def test_includes_regular():
]
}

def test_includes_regular_combined():
dirname = os.path.join(here, 'configs', 'includes-regular-combined')
config = os.path.join(dirname, 'nginx.conf')
payload = crossplane.parse(config, combine=True)
assert payload == {
'status': 'failed',
'errors': [
{
'file': os.path.join(dirname, 'conf.d', 'server.conf'),
'error': '[Errno 2] No such file or directory: %r' % os.path.join(dirname, 'bar.conf'),
'line': 14
}
],
'config': [
{
'file': os.path.join(dirname, 'nginx.conf'),
'status': 'failed',
'errors': [
{
'error': '[Errno 2] No such file or directory: %r' % os.path.join(dirname, 'bar.conf'),
'line': 14
}
],
'parsed': [
{
'directive': 'events',
'file': os.path.join(dirname, 'nginx.conf'),
'line': 1,
'args': [],
'block': []
},
{
'directive': 'http',
'file': os.path.join(dirname, 'nginx.conf'),
'line': 2,
'args': [],
'block': [
{
'file': os.path.join(dirname, 'conf.d', 'server.conf'),
'directive': 'server',
'line': 1,
'args': [],
'block': [
{
'file': os.path.join(dirname, 'conf.d', 'server.conf'),
'directive': 'listen',
'line': 2,
'args': ['127.0.0.1:8080']
},
{
'file': os.path.join(dirname, 'conf.d', 'server.conf'),
'directive': 'server_name',
'line': 3,
'args': ['default_server']
},
{
'file': os.path.join(dirname, 'foo.conf'),
'directive': 'location',
'line': 1,
'args': ['/foo'],
'block': [
{
'file': os.path.join(dirname, 'foo.conf'),
'directive': 'return',
'line': 2,
'args': ['200', 'foo']
}
]
},
{
'file': os.path.join(dirname, 'conf.d', 'server.conf'),
'directive': 'location',
'line': 6,
'args': ['/A'],
'block': [
{
'file': os.path.join(dirname, 'conf.d', 'server.conf'),
'directive': 'return',
'line': 7,
'args': ['200', 'A']
}
]
},
{
'file': os.path.join(dirname, 'conf.d', 'server.conf'),
'directive': 'location',
'line': 10,
'args': ['/B'],
'block': [
{
'file': os.path.join(dirname, 'conf.d', 'server.conf'),
'directive': 'return',
'line': 11,
'args': ['200', 'B']
}
]
}
]
}
]
}
]
}
]
}

def test_includes_globbed():
dirname = os.path.join(here, 'configs', 'includes-globbed')
Expand Down