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

The NGINX lens reports "types" as a syntax error incorrectly #834

Open
johnnybubonic opened this issue Jun 20, 2024 · 1 comment · May be fixed by #844
Open

The NGINX lens reports "types" as a syntax error incorrectly #834

johnnybubonic opened this issue Jun 20, 2024 · 1 comment · May be fixed by #844

Comments

@johnnybubonic
Copy link

Place the following as /etc/nginx/conf.d/test.conf:

server {
	listen 127.0.0.1:80;
	server_name poc.localhost;

	types {
	        text/plain h;
	}

	location / {
		root /srv/http;
	}
}

Ensure it is parsed by Nginx:

# grep -E '^\s*include\s+' /etc/nginx/nginx.conf
    include       mime.types;
    include /etc/nginx/conf.d/*.conf;

# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

# echo $?
0

As you can see, the types directive as a block is perfectly valid and a block is expected per the example in Nginx documentation.

Now try to load it.

augtool> load
augtool> print /augeas/files/etc/nginx/conf.d/test.conf/error
/augeas/files/etc/nginx/conf.d/test.conf/error = "parse_failed"
/augeas/files/etc/nginx/conf.d/test.conf/error/pos = "131"
/augeas/files/etc/nginx/conf.d/test.conf/error/line = "12"
/augeas/files/etc/nginx/conf.d/test.conf/error/char = "0"
/augeas/files/etc/nginx/conf.d/test.conf/error/lens = "/usr/share/augeas/lenses/dist/nginx.aug:121.10-.52:"
/augeas/files/etc/nginx/conf.d/test.conf/error/message = "Syntax error"

The obvious reason is that type is not noted as a block directive (neither in block_re nor block_re_all). However, unlike other block_re_all, it only has the form of <directive> <block>, there are no args.

I tried fiddling with the lens a bit but wasn't able to get it to properly pull in the block itself.

@tupyy
Copy link
Contributor

tupyy commented Aug 23, 2024

hello,
your problem is not there. It's the / in text/plain (actually is the plain).
See lens nginx

in let mask = [ label "mask" . Util.del_str "/" . store Rx.integer ]

It's removing the / to save the mask of the IP and it wants an integer. Replace plain with 24 and it will work.

A quick fix will be to replace the mask with:

in let mask = [ label "mask" . Util.del_str "/" . store Rx.word ]

and you'll obtain something like:

augtool> print etc/nginx/conf.d/
/files/etc/nginx/conf.d
/files/etc/nginx/conf.d/test.conf
/files/etc/nginx/conf.d/test.conf/server
/files/etc/nginx/conf.d/test.conf/server/listen = "127.0.0.1:80"
/files/etc/nginx/conf.d/test.conf/server/server_name = "poc.localhost"
/files/etc/nginx/conf.d/test.conf/server/types
/files/etc/nginx/conf.d/test.conf/server/types/text = "h"
/files/etc/nginx/conf.d/test.conf/server/types/text/mask = "plain"
/files/etc/nginx/conf.d/test.conf/server/location
/files/etc/nginx/conf.d/test.conf/server/location/#uri = "/"
/files/etc/nginx/conf.d/test.conf/server/location/root = "/srv/http"

But, IHO, is not clean and it will break something if users do math op with the mask

or

--- a/lenses/nginx.aug
+++ b/lenses/nginx.aug
@@ -33,10 +33,11 @@ autoload xfm

 (* Variable: word *)
 let word = /[A-Za-z0-9_.:-]+/
+let str = /[A-Za-z]*/

 (* Variable: block_re
      The keywords reserved for block entries *)
-let block_re = "http" | "events" | "server" | "mail" | "stream"
+let block_re = "http" | "events" | "server" | "mail" | "stream" | "types"

 (* All block keywords, including the ones we treat specially *)
 let block_re_all = block_re | "if" | "location" | "geo" | "map"
@@ -47,9 +48,10 @@ let block_re_all = block_re | "if" | "location" | "geo" | "map"
 let simple =
      let kw = word - block_re_all
   in let mask = [ label "mask" . Util.del_str "/" . store Rx.integer ]
+  in let sub_type = [ label "sub_type" . Util.del_str "/" . store str ]
   in let sto = store /[^ \t\n;#]([^";#]|"[^"]*\")*/
   in [ Util.indent .
-       key kw . mask? .
+       key kw . (sub_type | mask)?  .
        (Sep.space . sto)? . Sep.semicolon .
        (Util.eol|Util.comment_eol) ]

@georgehansper WDYT

tupyy added a commit to tupyy/augeas that referenced this issue Aug 27, 2024
This commit allow types block to be parsed.

Fixes: hercules-team#834.

Signed-off-by: Cosmin Tupangiu <[email protected]>
@tupyy tupyy linked a pull request Aug 27, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants