Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

[FLOSS] Enable validation on read [Java example whitelist plugin] #4001

Closed
tucek opened this issue Aug 22, 2021 · 12 comments · Fixed by #4718
Closed

[FLOSS] Enable validation on read [Java example whitelist plugin] #4001

tucek opened this issue Aug 22, 2021 · 12 comments · Fixed by #4718
Assignees

Comments

@tucek
Copy link
Contributor

tucek commented Aug 22, 2021

The Java example plugin whitelist should also validate values on get instead on set only. Validation errors should result in warnings.

See also: #3889 (comment)

@tucek tucek self-assigned this Aug 22, 2021
@markus2330
Copy link
Contributor

I am okay with checking on kdbGet (and providing warnings), see also #1511, but I do not think adding an option for that in every plugin makes sense. Instead I would prefer to have one plugin "failonwarnings" (globally mounted) which allows to globally switch the behavior to fail also during kdbGet.

@tucek
Copy link
Contributor Author

tucek commented Aug 22, 2021

I am okay with checking on kdbGet (and providing warnings), see also #1511, but I do not think adding an option for that in every plugin makes sense. Instead I would prefer to have one plugin "failonwarnings" (globally mounted) which allows to globally switch the behavior to fail also during kdbGet.

ok, i've updated the description accordingly

@tucek
Copy link
Contributor Author

tucek commented Sep 23, 2021

Issue blocked by #4053

@markus2330
Copy link
Contributor

@tucek for the range plugin this already gets implemented, there should be no blocker to also implement it for the whitelist plugin.

@markus2330 markus2330 added 3p three points cm2022s for university course triage needed Issue needs clarifications. labels Oct 13, 2021
@marvin-the-parrot marvin-the-parrot self-assigned this Oct 20, 2021
@marvin-the-parrot
Copy link

marvin-the-parrot commented Oct 20, 2021

[FLOSS H1]

  • The Java example plugin whitelist should also validate values on get instead on set only.

The implementation of validate values in set should also be applied to get. The way this was achieved for set should help as a guideline to use a similar approach for set.

  • Validation errors should result in warnings.

This can be achieved in much the same way as it was achieved for the range plugin. The way it was achieved should be visible in this commit.

I was thinking of removing the lang/java tag and assigning the lang/c tag since this validation seems to be done in c code. If this is not the case I will leave the lang/java tag as it is.

@markus2330
Copy link
Contributor

I was thinking of removing the lang/java tag and assigning the lang/c tag since this validation seems to be done in c code. If this is not the case I will leave the lang/java tag as it is.

The whitelist plugin is Java ./src/bindings/jna/plugins/whitelist and only this code needs to be touched. Otherwise your analysis looks good to me!

@bhampl
Copy link
Contributor

bhampl commented Mar 27, 2022

We (@bhampl and @eiskasten ) would like to work on this issue, but we haven't fully understood how the code works. The main questions we have is where should the validation be done in the get method and what exactly is the relevant validation code in the set method, which we would move into its one method to do the validation only once.

@kodebach
Copy link
Member

kodebach commented Mar 27, 2022

You correctly identified the get and set methods this issue is about. In theory they should have the exact same behaviour, except that get should only produce warnings where set produces errors. However, get is (currently) also used to access the contract of a plugin. This is why there is an additional if at the beginning. After this if (currently just return) is where the actual implementation of get should be.

For a bit of background on this issue: Producing errors in set ensures that a kdb set terminal command can result in an invalid configuration. However, the underlying configuration file could be modified manually too. This is why we also need validation in get. But we cannot produce errors in get, because kdb set needs to successfully execute get before it can call set. Therefore an error in get would prevent fixing the error via kdb set.

I hope this clarifies the issue a bit. If not feel free to ask again.

@eiskasten
Copy link
Contributor

Ok, thank you. So, I think that the solution is supposed to outsource the error checks and use that in both, the get and the set method?

@kodebach
Copy link
Member

Correct, the code duplication should be minimized for the sake of maintainability. How you achieve this is up to you. Extracting the checks is one possibility, another possibility would be a check function that takes something like an OnErrorListener as an argument.

@kodebach kodebach changed the title Enable validation on read [Java example whitelist plugin] [CM T0] Enable validation on read [Java example whitelist plugin] Apr 2, 2022
@bhampl bhampl removed their assignment Jun 15, 2022
@eiskasten eiskasten removed their assignment Jun 22, 2022
@flo91 flo91 added floss2022w and removed cm2022s for university course labels Oct 5, 2022
@flo91 flo91 changed the title [CM T0] Enable validation on read [Java example whitelist plugin] Enable validation on read [Java example whitelist plugin] Oct 5, 2022
@Bujuhu Bujuhu self-assigned this Oct 19, 2022
@Bujuhu Bujuhu added the H1 label Oct 19, 2022
@Bujuhu
Copy link
Contributor

Bujuhu commented Oct 19, 2022

OK, just to sum it up once again. The issue is we want to validate values on get and validation errors should result in warnings, and in set and validation errors should result in failures in https://github.com/ElektraInitiative/libelektra/blob/master/src/bindings/jna/plugins/whitelist/src/main/java/org/libelektra/plugin/WhitelistPlugin.java

Currently, set is validating and returning warnings and errors and get is not validating at all.

I propose the following solution:

The quickest (albeit, not the most readable) solution would be to take advantage of the similar signature of setError and addWarning and just use the function itself as a parameter like this:

    public static int validate(Key key, Key parentKey, BiFunction<ErrorCode, String, Key> setErrorOrWarning, int problemStatus) {
        //should use the validation implementation of set without the part where the value is added; i.e. pretty much the whole set function without line 58
        //setting a problem that should always be a warning
        parentKey.addWarning(/*ErrorCode*/, /*Message*/);
        //setting a problem that should either be a warning or an error depndening on the context
        setErrorOrWarning.apply(/*ErrorCode*/, /*Message*/);
        //return problemStatus if an error has occured, otherwise return STATUS_SUCCESS
    }

We can then just call the validation function in set like this:

   validate(key, parentKey, parentKey::setError, STATUS_ERROR)
   //add key on success or return status_error on faiure

And for get like this:

   validate(key, parentKey, parentKey::addWarning, STATUS_SUCCESS)
  // return value

@kodebach
Copy link
Member

The quickest (albeit, not the most readable) solution

I think the solution is fine, but we might want to have a different name instead of setValidationProblem to make it a bit more readable. How about setErrorOrWarning?

@Bujuhu Bujuhu removed the triage needed Issue needs clarifications. label Oct 19, 2022
@flo91 flo91 changed the title Enable validation on read [Java example whitelist plugin] [FLOSS] Enable validation on read [Java example whitelist plugin] Oct 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants