crossplane icon indicating copy to clipboard operation
crossplane copied to clipboard

*_by_lua should unescape the newline "\n"

Open Triple-Z opened this issue 4 years ago • 5 comments

Describe the bug

When I try to use the crossplane, just using parser to read a standard NGINX config file and then using the build to convert it back. However, I found that the *_by_lua directives' newline was replaced by literal \n. So I think this is a bug for that.

To Reproduce Steps to reproduce the behavior:

  1. Create an nginx config file with the following content:
user nobody;
worker_processes 1;

events {
  worker_connections 10240;
}

http {
    
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;

    server {
        listen      80;
        server_name test.triplez.space 1.2.3.4;
        rewrite_by_lua '
            print("rewrite phase")
        ';
        
        location / {
            access_by_lua 'print("access phase")';
            default_type 'text/plain';
            return 200 "this is a response\n";
        }
    }

}
  1. parse and build
  2. The content after rewrite_by_lua has changed. original:
    rewrite_by_lua '
        print("rewrite phase")
    ';
    
    after:
    rewrite_by_lua '\n    print("hello")\n    ';
    

Expected behavior *_by_lua directive content can set up a new line for Lua code.

Your environment

  • Operating System: Ubuntu 16.04
  • Version of crossplane: crossplane 0.5.4

Triple-Z avatar Jun 23 '20 03:06 Triple-Z

I have solved it by writing an extra plugin and enabled it in __init__.py just for *_by_lua directives.

Plugin code in here: https://gist.github.com/Triple-Z/64a0b6ad8fb673d77712e5aabfebfe82

Maybe I can create a PR for this?

Triple-Z avatar Jun 23 '20 08:06 Triple-Z

Looks good to me! I would submit a PR, seems like a great use of the extension system to me, although it might be worth considering adding a flag to enable/disable it by the user.

gshulegaard avatar Jun 23 '20 15:06 gshulegaard

@gshulegaard I have a WIP PR in here: https://github.com/nginxinc/crossplane/pull/86

although it might be worth considering adding a flag to enable/disable it by the user.

I will check this and try to do it later~

Triple-Z avatar Jun 23 '20 15:06 Triple-Z

@Triple-Z It's important to make sure that unescaping newlines won't break any embedded lua code, so when you add tests, please include a test that shows that newlines in the lua code that are supposed to be escaped stay escaped. For example if a lua block contains error("foo\nbar"), this should not get turned into two lines because lua's " quotes don't support multi-line strings so there would be an error introduced in the lua code.

To create a fixture for that test, you can use crossplane parse on a config that contains embedded lua that is supposed to have escaped newlines like error("foo\nbar").

aluttik avatar Jul 13 '20 09:07 aluttik

@aluttik Thx & I will check on this :)

Triple-Z avatar Jul 13 '20 14:07 Triple-Z