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

Fixes #7006: Parameter validation in Rudder should accept rudder variables ( ${ } ) #2942

Conversation

fanf
Copy link
Member

@fanf fanf commented Apr 29, 2020

https://issues.rudder.io/issues/7006

"oups".

It was really too complicated to add non-rudder variable to our compiler with scala lib parser combintor. Since we now have fastparse in the path, I tried to see if it was easier... And it was thanks to its amazing log possibilities (the parsing by itself is a bit tricky).
The most interesting is how little code changes from scala regex combinator to fastparse. And the perf are just in other league (it does not change much in our case, since node prop interpolation is in second range, but now we are in the 10s of millisec - it may still be interesting with group properties and many more parsing to do).
So I also switch the last parser we had (the one for our "search anything" bar queries), what allows to get ride of one dependancy and use the (very cool!) same combinator parser everywhere.

@fanf fanf requested a review from VinceMacBuche April 29, 2020 19:15
@fanf
Copy link
Member Author

fanf commented Apr 29, 2020

PR rebased

@fanf fanf force-pushed the ust_7006/regex_validation_in_rudder_should_accept_rudder_variables branch from 3a6740f to 5637598 Compare April 29, 2020 19:17
// here we defined a function to build them, with the possibility to
val plainString : Parser[CharSeq] = ("""(?iums)((?!(\Q${\E\s*rudder\s*\.|\Q${\E\s*node\s*\.\s*properties)).)+""").r ^^ { CharSeq(_) }
// here we defined a function to build them
def plainStringNoDollar[_: P] : P[CharSeq] = P( (!"${" ~ AnyChar).rep(1).! ).map { CharSeq(_) }
Copy link
Member

Choose a reason for hiding this comment

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

i'm worried that everything is a def rather than a val: the number of instanciated objects will explode.

Copy link
Member

Choose a reason for hiding this comment

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

see #2467

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm asking on fastparse how to handle that case

Copy link
Member Author

Choose a reason for hiding this comment

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

https://gitter.im/lihaoyi/fastparse?at=5ea9e817f0377f163158e8b4

Since fastparse 2.x, parsers no longer have any initialization cost. They are simply methods that you call just like any other methods

VTypeConstraint.checkIsProperty(value, forField) { v =>
try {
v.toInt
Right(())
Copy link
Member

Choose a reason for hiding this comment

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

why is it returning an empty Right ?

Copy link
Member Author

Choose a reason for hiding this comment

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

to force the user (me) to map(_ => escaped) for the return value. We don't use these value, the parsing/etc is just done to check if the provided input is of the correct type, but we need to use the escaped string char that is already computed before that method is called

@@ -52,15 +54,26 @@ case class RegexConstraint(pattern: String, errorMsg: String) {

val compiled = Pattern.compile(pattern)

val variablePattern = Pattern.compile(".*?\\$\\{.*?\\}.*?")
Copy link
Member

Choose a reason for hiding this comment

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

i like that it is removed :)

@ncharles
Copy link
Member

this looks good to me, but I'm not sure i'll be able to understand the parsing part

@fanf
Copy link
Member Author

fanf commented Apr 29, 2020

Fortunately, there is a bunch of test cases. It seems that whoever wrote the first parser had had to deal with a great deal of edge cases. Or he was just a bit paranoid. They helped a lot and caught a lot of problem when adding the ${whatever} case

@VinceMacBuche
Copy link
Member

Also I think you should rename s to space, put long names instead of shortname (ie: sqStr, should be singleQuoteString)

@VinceMacBuche
Copy link
Member

I started a PR of renaming, a removal, also adding some edge cases tests

@fanf
Copy link
Member Author

fanf commented Apr 30, 2020

PR updated with a new commit

2 similar comments
@fanf
Copy link
Member Author

fanf commented Apr 30, 2020

PR updated with a new commit

@fanf
Copy link
Member Author

fanf commented Apr 30, 2020

PR updated with a new commit

Copy link
Member Author

@fanf fanf left a comment

Choose a reason for hiding this comment

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

added comment for the quoted string

@fanf fanf force-pushed the ust_7006/regex_validation_in_rudder_should_accept_rudder_variables branch from f7f3326 to 024d539 Compare April 30, 2020 11:46
@fanf fanf requested a review from VinceMacBuche April 30, 2020 11:47
@fanf
Copy link
Member Author

fanf commented Apr 30, 2020

PR updated with a new commit

Copy link
Member

@VinceMacBuche VinceMacBuche left a comment

Choose a reason for hiding this comment

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

I accept this PR, but I will make one more treating some edge cases, adding tests, and some refactoring

@Normation-Quality-Assistant
Copy link
Contributor

This PR is not mergeable to upper versions.
Since it is "Ready for merge" you must merge it by yourself using the following command:
rudder-dev merge https://github.com/Normation/rudder/pull/2942
-- Your faithful QA
Kant merge: "Two things awe me most, the starry sky above me and the moral law within me."
(https://ci.normation.com/jenkins/job/merge-accepted-pr/24040/console)

@VinceMacBuche
Copy link
Member

OK, squash merging this PR

@VinceMacBuche VinceMacBuche force-pushed the ust_7006/regex_validation_in_rudder_should_accept_rudder_variables branch from 8b0de66 to 54504e8 Compare April 30, 2020 22:25
@VinceMacBuche VinceMacBuche merged commit 54504e8 into Normation:branches/rudder/6.1 Apr 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants