Skip to content

KEYCLOAK-2045 Simple Validation API - #7887

Closed
thomasdarimont wants to merge 25 commits into
keycloak:masterfrom
thomasdarimont:issue/KEYCLOAK-2045-Simple-Validation-SPI
Closed

thomasdarimont wants to merge 25 commits into
keycloak:masterfrom
thomasdarimont:issue/KEYCLOAK-2045-Simple-Validation-SPI

Conversation

@thomasdarimont

@thomasdarimont thomasdarimont commented Mar 26, 2021 •

Copy link
Copy Markdown
Contributor

Another try for a simple Validator SPI for Keycloak.

This builds on the ideas from #7324 in a lean fashion. This introduces a new Validator SPI that supports built-in as well as user provided validations.

@hmlnarik

Copy link
Copy Markdown
Contributor

Thank you @thomasdarimont. Validation seems to be a rather broad problem in Keycloak spanning several layers, and as such would need a proper design in keycloak-community repo first.
A random thought - can we leverage JSR 303 (Java API for JavaBean Validation)?

@thomasdarimont

thomasdarimont commented Mar 26, 2021 •

Copy link
Copy Markdown
Contributor Author

@hmlnarik thanks for the feedback. I've been working on a new validation SPI for quite a while now as you can see here: #7324

Perhaps you should talk to @pedroigor and / or @velias who are also involved in the design of this to support the user-profile implementation.

@thomasdarimont

thomasdarimont commented Mar 26, 2021 •

Copy link
Copy Markdown
Contributor Author

@pedroigor as discussed I moved the implementation to the server-spi-private module.

The package name org.keycloak.validate is a placeholder for now. org.keycloak.validation should be the final place, but this requires some adaptation of the existing classes in org.keycloak.validation (and their consumers) to the new validation api.

@hmlnarik

Copy link
Copy Markdown
Contributor

@hmlnarik thanks for the feedback. I've been working on a new validation SPI for quite a while now as you can see here: #7324

I can see that as well as that this has been closed without merging.

Perhaps you should talk to @pedroigor and / or @velias who are also involved in the design of this to ease the work on the user-profile support.

The point is that currently the validation in Keycloak is broken design-wise: there are too many validations scattered across the codebase that are mutually incompatible. So far there has been no consensus as of how to do the validation consistently. This PR seems to add yet another way for the validations. Before this can be considered for merging, the full picture how the existing validations would be transformed into a single approach needs to be clear, and sadly I have not seen any proposal in this regard.

Perhaps you can point me to a design document about that?

Validation is known priority in Keycloak and needs to be done, and needs to be done right. I am not arguing that this PR is not the right approach. However I need to see how the proposed approach would fit existing codebase and cover existing usecases before assessing this PR.

Cc: @stianst @mposolda @vmuzikar @mhajas

@thomasdarimont

thomasdarimont commented Mar 27, 2021 •

Copy link
Copy Markdown
Contributor Author

@hmlnarik Thank you for the detailed feedback.

I am aware that validation is a very complex topic in Keycloak and the area is currently a bit messy.

The current validation SPI proposal is based on numerous discussions on user profiles and other topics on the mailing list and direct meetings.

I am sorry if there were any misunderstandings.

I have just added a PR with a corresponding design document in the Keycloak community repository.
This document also contains the rationale behind not using Bean Validation.

Hope that helps to clarify a few things.

@thomasdarimont
thomasdarimont force-pushed the issue/KEYCLOAK-2045-Simple-Validation-SPI branch 2 times, most recently from a5dda52 to e8130fa Compare March 28, 2021 21:24
@vmuzikar

Copy link
Copy Markdown
Contributor

@hmlnarik +1

I think we should discuss and merge the design doc first.

@pedroigor

Copy link
Copy Markdown
Contributor

@hmlnarik @vmuzikar Please, also consider looking at #7846.

That is what is driving the design for this validation and how it would be used in contexts other than user profile.

@stianst

stianst commented Mar 30, 2021

Copy link
Copy Markdown
Contributor

I'd like to make it fully clear here that we are reviewing this work in the context of user profile only. Before we use this validation framework for other purposes we have some areas like what @hmlnarik has mentioned that requires a bit more thought and consideration before we consider using this as the validation framework for other purposes as well. However, this is a good start and I would say that most likely this will evolve to becoming such a framework in the future, but follow-up both in terms of design and code would be needed first.

@hmlnarik

Copy link
Copy Markdown
Contributor

Thank you @stianst.
If the proposed solution is expected to only cover user profile and is expected as a short-term solution, then it works for me.
For the reference, the more in-depth solution would likely target validation on the level of *Model interfaces (e.g. UserModel, ClientModel) rather than representations and REST endpoints. That way maintenance cost and space for omissions and inconsistencies between validations of the same type of object in several places would be kept minimal.

@thomasdarimont

thomasdarimont commented Mar 30, 2021 •

Copy link
Copy Markdown
Contributor Author

@hmlnarik Thanks again for the feedback.

I think the proposed Validation API will work fine for validating *Model objects. One just need to define a UserModelValidator, ClientModelValidator, RealmModelValidator etc.

@pedroigor

Copy link
Copy Markdown
Contributor

@thomasdarimont I agree. That is an implementation detail that would basically rely on looking annotations and mapping to their respective validators.

Another point I would highlight is that by having a very minimal and really simple API, we don't need any other dependency added and something more focused on our needs. Although Keycloak is basically a regular JAX-RS application, our requirements usually extrapolate what specs and 3rd party libraries offer in terms of API.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

  1. Was it considered to have ValidatorConfig as part of the ValidationContext?
  2. The ValidationContext including session should always be available to the validator, e.g for getting other validators and delegate.
    But method ValidationContext validate(Object input) allows to request validation without session available. Waht is a validotor supposed to do which needs the session?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

  1. Yes it was, I had this (via simple Map<String,Object> attributes) in my initial API design: https://github.com/keycloak/keycloak/pull/7324/files#diff-a30ff7afb40dfeee146a59e5e46fbd15598d5d36a2dbd616095d049e5181613dR36
    We want to support simple and complex validations. Simple validations (just one call to validate(...)) don't require an initial ValidationConfig but might want to fine tune the Validator.
    Complex validations might invoke validate(...) consecutively for different Validators with a shared ValidationContext. Since the ValidationContext is shared across multiple Validator invocations, it would require additional context management to maintain the proper ValidationConfig. Explicitly passing the ValidationConfig makes the API easier for simple and complex validations.

  2. A Validator which needs the KeycloakSession needs to be invoked with a properly configured ValidationContext with a session. #validate(object) allows simple validations like

Assert.assertTrue(Validators.email().validate("admin@example.org").isValid());

while still sharing the common Validator interface.

@jbman jbman Apr 1, 2021 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

  1. Thanks, shared context but not shared config is a good reason to separate the parameters
  2. Ok, got the different cases. The main drawback is the difficulty for the validator implementation to state that that a ValidationContext instance is required and must not be null. Would be nice to know that just by the interface.
public interface Validator {
    ValidationContext validate(Object input, ValidatorConfig config)) ...

public interface ValidatorWithContext {
    ValidationContext validate(Object input, ValidationContext context, ValidatorConfig config)) ...

Unfortunately this can't be done for 'ValidatorConfig' parameter at the same time. Get's a bit unhandy to have a ValidatorWithContextAndConfig and a ValidatorWithConfig interface on top.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Some validators may only work with an appropriate config. Defaults like 0, 255 make the validator do its work, but probably it should fail if not configured explicitly. It could fail with a validation result error. If a valid config is missing any input is invalid.
This approach might also make validateConfig superfluous. If a validator is configurable it should validate the config as first step before looking at the input. Then it an just be called with input==null and will return validation errors referring to the config.

@thomasdarimont thomasdarimont Mar 31, 2021 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That was indeed my initial design, but then the Keycloak team asked how validator configuration can be validated with this approach. That's why I introduced the ValidatorFactory.validateConfig(..) method.

The ValidatorFactory.validateConfig(..) method allows to validate Validator configurations during edits in the admin-console.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1 we need both options so we can validate config beforehand as well as (optionally) before validation.

@vmuzikar vmuzikar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@thomasdarimont Nice work, thanks!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we rather throw some exception? It's not exactly a validation error, the user providing the input is not responsible for this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'd rather not throw an exception by default in that case, but we could configure the ValidationContext to throw an exception when a ValidationError is reported.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ok. My original assumption was that the user cannot affect the input type and therefore throwing incompatible input type at the validator is a dev's fault and as such is not a validation error. This is, of course, wrong – a user can in fact define input type (e.g. JSON payload in REST endpoints). Let's return a validation error.

But maybe we could have some predefined error message common for all validators for this case (incompatible input type)?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we check min <= max?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes

Comment on lines 43 to 46

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we rather throw some exception?

Comment on lines 51 to 55

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we rather throw some exception?

@thomasdarimont
thomasdarimont force-pushed the issue/KEYCLOAK-2045-Simple-Validation-SPI branch from 88a6d97 to 12dce02 Compare April 1, 2021 14:50

@pedroigor pedroigor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@thomasdarimont Looks great. Just a few minor comments.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1 we need both options so we can validate config beforehand as well as (optionally) before validation.

@thomasdarimont
thomasdarimont force-pushed the issue/KEYCLOAK-2045-Simple-Validation-SPI branch from 99361c8 to e85ad01 Compare April 13, 2021 16:22
@thomasdarimont

Copy link
Copy Markdown
Contributor Author

Just rebased this on current master.

pedroigor
pedroigor previously approved these changes Apr 13, 2021

@pedroigor pedroigor left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@thomasdarimont

Copy link
Copy Markdown
Contributor Author

Thanks for having another look at this :)

I was wondering what the next steps will be.

In my point of view the next steps should address the following points:

  • merge the validate package into the validation package to avoid confusing package names
  • refactor the existing validation package to use the new validation api.
  • adapt the profile api to use the new validation api (create validation configs from json based profile definitions)
  • add / revise built-in validations as needed

@pedroigor

Copy link
Copy Markdown
Contributor

Thanks for having another look at this :)

I was wondering what the next steps will be.

In my point of view the next steps should address the following points:

  • merge the validate package into the validation package to avoid confusing package names
  • refactor the existing validation package to use the new validation api.
  • adapt the profile api to use the new validation api (create validation configs from json based profile definitions)
  • add / revise built-in validations as needed

Yeah, pretty much what we discussed. Your proposal should be able to replace the different validation approaches we have but I'm not sure if we want to do that now but focus on the user profile bits first.

We are already changing a lot in the user profile and the product release is quite close. So perhaps we should consider changing other parts when we have more time to bake their changes. In fact, I have the same concern surrounding the user profile, but this one is something we need ASAP. I'm also more confident that we have much better coverage for making deep changes in user profile after the last issues we had.

That said, I would focus on user profile for now.

thomasdarimont and others added 19 commits April 22, 2021 18:04
- Merged BuiltinValidators and ValidatorLookup into Validators facade class
…ods.

This allows ValidatorConfig validations to access services as well as
the current KeycloakContext.
…e review

- Derive valid state from errors in ValidationContext
- Simplify ValidationResult by not inheriting from Consumer<Consmer<ValidationResult>>
- Add addtional test cases
…e review

- Revise NumberValidator to only check for double conversion
- Removed support for string based regex pattern
@thomasdarimont
thomasdarimont force-pushed the issue/KEYCLOAK-2045-Simple-Validation-SPI branch from 8d02c09 to 21b9d05 Compare April 22, 2021 16:07
if (!(containsMin || containsMax)) {
return ValidationResult.OK;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd say that this validator needs at least one of min or max to be set in the configuration to work properly. So this config validation should fail if config is empty or doesn't contain any of these settings.

return context;
}

if (!EMAIL_PATTERN.matcher((String) input).matches()) {

@velias velias May 13, 2021 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is format validation, so value should be validated only if it is not null and not empty to be able to operate on optional user profile fields/attributes.

@@ -389,8 +388,8 @@ public <T extends Provider> Set<String> listProviderIds(Class<T> clazz) {
@Override
public <T extends Provider> Set<T> getAllProviders(Class<T> clazz) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we revert changes to this class?

import org.keycloak.component.ComponentModel;
import org.keycloak.models.cache.UserCache;
import org.keycloak.provider.InvalidationHandler;
import org.keycloak.provider.InvalidationHandler.InvalidableObjectType;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we revert changes to this class?

@pedroigor

Copy link
Copy Markdown
Contributor

@thomasdarimont I think we should change the built-in validators to handle multiple values too. Would you be able to update them?

@pedroigor

Copy link
Copy Markdown
Contributor

@thomasdarimont As discussed, we are closing this one and sending a new one with the few missing bits. Thanks for your work.

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 this pull request may close these issues.

7 participants