Repository navigation
KEYCLOAK-2045 Simple Validation API - #7887
thomasdarimont wants to merge 25 commits into
Conversation
|
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. |
|
@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. |
|
@pedroigor as discussed I moved the implementation to the server-spi-private module. The package name |
I can see that as well as that this has been closed without merging.
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. |
|
@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. Hope that helps to clarify a few things. |
a5dda52 to
e8130fa
Compare
|
@hmlnarik +1 I think we should discuss and merge the design doc first. |
|
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. |
|
Thank you @stianst. |
|
@hmlnarik Thanks again for the feedback. I think the proposed Validation API will work fine for validating |
|
@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. |
There was a problem hiding this comment.
- Was it considered to have ValidatorConfig as part of the ValidationContext?
- The ValidationContext including session should always be available to the validator, e.g for getting other validators and delegate.
But methodValidationContext validate(Object input)allows to request validation without session available. Waht is a validotor supposed to do which needs the session?
There was a problem hiding this comment.
-
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 initialValidationConfigbut might want to fine tune theValidator.
Complex validations might invokevalidate(...)consecutively for differentValidators with a sharedValidationContext. Since theValidationContextis shared across multipleValidatorinvocations, it would require additional context management to maintain the properValidationConfig. Explicitly passing theValidationConfigmakes the API easier for simple and complex validations. -
A
Validatorwhich needs theKeycloakSessionneeds to be invoked with a properly configuredValidationContextwith a session.#validate(object)allows simple validations like
Assert.assertTrue(Validators.email().validate("admin@example.org").isValid());
while still sharing the common Validator interface.
There was a problem hiding this comment.
- Thanks, shared context but not shared config is a good reason to separate the parameters
- Ok, got the different cases. The main drawback is the difficulty for the validator implementation to state that that a
ValidationContextinstance 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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
+1 we need both options so we can validate config beforehand as well as (optionally) before validation.
vmuzikar
left a comment
There was a problem hiding this comment.
@thomasdarimont Nice work, thanks!
There was a problem hiding this comment.
Should we rather throw some exception? It's not exactly a validation error, the user providing the input is not responsible for this.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
Should we check min <= max?
There was a problem hiding this comment.
Should we rather throw some exception?
There was a problem hiding this comment.
Should we rather throw some exception?
88a6d97 to
12dce02
Compare
pedroigor
left a comment
There was a problem hiding this comment.
@thomasdarimont Looks great. Just a few minor comments.
There was a problem hiding this comment.
+1 we need both options so we can validate config beforehand as well as (optionally) before validation.
99361c8 to
e85ad01
Compare
|
Just rebased this on current master. |
|
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:
|
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. |
…tion in validate methods
…ed ValidatorLookup class for now
…r implementations
- Merged BuiltinValidators and ValidatorLookup into Validators facade class
… don't support config.
…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
8d02c09 to
21b9d05
Compare
| if (!(containsMin || containsMax)) { | ||
| return ValidationResult.OK; | ||
| } | ||
|
|
There was a problem hiding this comment.
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()) { |
There was a problem hiding this comment.
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) { | |||
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
Can we revert changes to this class?
|
@thomasdarimont I think we should change the built-in validators to handle multiple values too. Would you be able to update them? |
|
@thomasdarimont As discussed, we are closing this one and sending a new one with the few missing bits. Thanks for your work. |
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.