Skip to content

KEYCLOAK-14665 initial spi interfaces with validator chain and UserAttributesReprese… - #7155

Merged
pedroigor merged 7 commits into
keycloak:masterfrom
bosch-io:legacy-profile-spi
Oct 5, 2020
Merged

pedroigor merged 7 commits into
keycloak:masterfrom
bosch-io:legacy-profile-spi

Conversation

@bs-matil

@bs-matil bs-matil commented Jun 9, 2020 •

Copy link
Copy Markdown
Contributor

This is the initial contribution for the Milestone M1 for the legacy profile as documented in https://github.com/keycloak/keycloak-community/blob/master/design/user-profile.md and relates to https://issues.jboss.org/browse/KEYCLOAK-14665

A specific Ticket will be created and added soon.

Contents:

  • SPI which supports only methods needed to support the legacy profile e.g. the available attributes feature is not implemented yet as nothing would evaluate it.
  • Combination of all validation logic into one SPI implementation
  • SPI with ValidationScenario handling for different places of validation (e.g Registration, IdpReview etc.)
  • A ValidaitorChainBuilder to hardcode the validation rules as long there is no configuration mechanism in place

@abstractj

Copy link
Copy Markdown
Contributor

@bs-matil is this PR covering the whole epic here https://issues.jboss.org/browse/KEYCLOAK-2966 ? Or part of this? For now I'm adding the label missing, Jira, but please let me know if my assumption is completely wrong.

@abstractj abstractj added area/server area/core Core server — startup, configuration, providers/SPIs, cross-cutting concerns kind/feature Categorizes a PR related to a new feature labels Jun 16, 2020
@stianst

stianst commented Jun 19, 2020

Copy link
Copy Markdown
Contributor

@abstractj It's just doing first part of it. We should have a JIRA to describe what it is and associate this PR/commit with it.

@stianst
stianst requested a review from mposolda June 19, 2020 13:08
@stianst

stianst commented Jun 19, 2020

Copy link
Copy Markdown
Contributor

@bs-matil I ran out of time before hols' so won't be able to take a look at least before July I'm afraid.

@bs-matil bs-matil changed the title initial spi interfaces with validator chain and UserAttributesReprese… KEYCLOAK-14665 initial spi interfaces with validator chain and UserAttributesReprese… Jul 3, 2020
@bs-matil
bs-matil force-pushed the legacy-profile-spi branch 2 times, most recently from c15b483 to 12e4582 Compare July 6, 2020 14:49
@bs-matil

bs-matil commented Jul 6, 2020

Copy link
Copy Markdown
Contributor Author

I just refactored all against the new attribute handling in the user model

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.

This is needed because of the "chained action" approach in the registration. May it should be considered to adapt the registration flow in order to remove this switch so we can go here with configuration only.

@bs-matil
bs-matil force-pushed the legacy-profile-spi branch 12 times, most recently from e45e518 to 6536ab1 Compare July 6, 2020 21:20
@stianst stianst removed their assignment Sep 18, 2020
@bs-matil

bs-matil commented Sep 28, 2020 •

Copy link
Copy Markdown
Contributor Author

@pedroigor any news on that?

Should I may refile the PR, the history is a bit cluttered.

@pedroigor

pedroigor commented Sep 28, 2020 •

Copy link
Copy Markdown
Contributor

@pedroigor any news on that?

Should I may refile the PR, the history is a bit cluttered.

@bs-matil I'll be looking User Profile this week, starting today. Need to sync what was discussed/done so far before reviewing this PR or any other that is related.

One thing though, from your comments it seems this PR is delivering what was defined as M1 in the design document. I agree that we can deal with validation later (including the PR from @thomasdarimont around validation) and focus on the core changes to support user profiles. Does it make sense for you?

Btw, could you please rebase?

@pedroigor

Copy link
Copy Markdown
Contributor

@bs-matil, There are a lot of changes to formatting/cleanup and empty lines being removed that do not seem related to the changes you are doing.

In order to make the reviewing process a bit easier and objective, could you please revert any change like that?

@bs-matil

Copy link
Copy Markdown
Contributor Author

@bs-matil, There are a lot of changes to formatting/cleanup and empty lines being removed that do not seem related to the changes you are doing.

In order to make the reviewing process a bit easier and objective, could you please revert any change like that?

I'll work on this.

@bs-matil

Copy link
Copy Markdown
Contributor Author

@pedroigor any news on that?
Should I may refile the PR, the history is a bit cluttered.

@bs-matil I'll be looking User Profile this week, starting today. Need to sync what was discussed/done so far before reviewing this PR or any other that is related.

One thing though, from your comments it seems this PR is delivering what was defined as M1 in the design document. I agree that we can deal with validation later (including the PR from @thomasdarimont around validation) and focus on the core changes to support user profiles. Does it make sense for you?

Btw, could you please rebase?

rebase has been completed. tests pending.


public void singleAttribute(String name, String value) {
if (this.attributes == null) attributes = new HashMap<>();
attributes.put(name, (value == null ? new ArrayList<String>() : Arrays.asList(value)));

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 think we should not even set the attribute if the value is null or empty.

We can probably throw an IllegalArgumentException too in this case.

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.

the check does not check for attribute values but for the attribute list. If the list is not initialized (which is likely due to the nature of the Representation objects) this should IMHO nothing the user of this function has to deal with.


public UserRepresentation singleAttribute(String name, String value) {
public void singleAttribute(String name, String value) {
if (this.attributes == null) attributes = new HashMap<>();

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.

The same here:

  • Do not accept null values by either not updating the map or throwing an IllegalArgumentException.

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.

same here its not a value check

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.

Maybe we can make life easier for implementors if we change some methods like that:

    default String getFirstAttributeValue(String attributeKey) {
        List<String> values = getAttributes().getOrDefault(name, Collections.emptyList());
        
        if (values.isEmpty()) {
            return null;
        }
        
        return values.get(0);
    }

    default List<String> getAttribute(String attributeKey) {
        return getAttributes().get(key);
    }

So that you only need to override getId and getAttributes. The latter being the real source of data.

I'm also wondering if we should not have attributes in a separate structure similar to Attributes. One of the main points being that we can add more controls such as read/write flags without impacting the main UserProfile API.

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.

As I tried to explain in the other comment regards the interface of the userprofile. The current UserProfile interface is actually just an attributes interfaces (may a bad one) but I'm right now not sure how to go towards your solution. I will push what I understood soon..

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 think UserProfileUpdateHelper can be simpler if we just pass the attribute map instead of the UserModel. It would avoid having to create this anonymous class to work around the contract to only fetch attributes.

I'm also wondering why the logic on UserProfileUpdateHelper is not on the UserProfileProvider itself?

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.

Regards the position of user profile itself see other comment.

Regards the AttributeList.
This won't work because of the nature of the different UserModel implementations (e,g, for idpReview). Some just are not useable as simple attribute list. (Their interface for the attributes does not work consistently)

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.

The differences are reflected in the different user profile implementations

@pedroigor pedroigor Sep 28, 2020 •

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 think we could have some methods here to create the UserProfileProvider instance without pushing too much to clients of the API. For instance:

Map<String, List<String>> newAttributes = // obtain attributes to update
UserProfileProviderFactory factory = // obtain factory from session
UserProfileProvider provider = factory.create(UserUpdateEvent.IdpReview, user.getAttributes(), newAttributes);
UserProfileValidationResult result = profileProvider.validate();

This can hide some implementation details such as the UserProfileContext implementation that should be used. As well as the different UserProfile implementations (if we really need all those impls). In fact, another approach to the API could be:

Map<String, List<String>> newAttributes = // obtain attributes to update
UserProfileProvider provider = // obtain provider from factory
UserProfileValidationResult result = profileProvider.validate(UserProfile.forIdpReview(userCtx), newAttributes);

So that the event would become irrelevant and we could check for the context based on the type of the UserProfile instance.

In addition to that, the user ID also works like any other attribute into the attribute map that is the real source of data and data structure we care about to perform the validations.

The Attributes API I gave as an example could also help to make this SPI more attribute oriented and simplify even more how clients pass over the attributes from the different places in Keycloak as well as using different sources.

I've also another idea for this SPI based on events. But I think it would demand changes that we probably don't want now to include a more generic and robust event system in Keycloak.

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.

@pedroigor I tried to go into this direction. It's not possible to do this on the SPI level as the interfaces would have to know the concrete types in the service impl. So I moved the factory like methods into the DefaultUserProfileContext

@pedroigor

Copy link
Copy Markdown
Contributor

@bs-matil These are my initial thoughts, I hope they make sense for you. I'll hold a bit the review and wait for your feedback considering that some of my comments are related to the design of the SPI.

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 understand the reason behind it I think. So that a UserProfile is marked as writable.

After reading through your code, looks like to me that the best would be if we had a single UserProfile implementation, with attributes encapsulated via a Attributes API so that the read/write capability could be controlled accordingly depending on the type of the event.

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'm a bit confused to be honest. the current UserProfile interface is actually very close to an Attributes interface. So to move towards your solution I would rename UserProfile to UserAttributes and merge the StoredUserProfile methods into this interface. Then on all implementations I would add the write methods and throw Exceptions when they are called.

Is this even close to your idea? I will do so. But may I got you fully wrong here, I at least suspect that.

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.

Why create an attribute with a list of null values ?

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.

because for example this test expects that the handling of email is "" -> null while this is not the case anywhere else...

public void resetPasswordNoEmail() throws IOException, MessagingException, InterruptedException {

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 invested time in keeping the api of the validation as consistent as possible. But there are inconsistencies in the code

Thats why I initially put the user update method into the user profile. That would have enabled to override the behaviour for the old very inconsistent code path while having a clean new store method in the future.

But Stian argued against this as the profile should not care about storage but validation only. So I moved the code to this helper class and out of the SPI interface.

@bs-matil
bs-matil force-pushed the legacy-profile-spi branch from c9b53b2 to 93bb60b Compare October 1, 2020 10:24
@bs-matil
bs-matil force-pushed the legacy-profile-spi branch from 93bb60b to ebe9157 Compare October 1, 2020 21:50
@bs-matil
bs-matil force-pushed the legacy-profile-spi branch from ebe9157 to cc0f547 Compare October 1, 2020 21:55
@bs-matil
bs-matil force-pushed the legacy-profile-spi branch from 0365399 to 4b0bbae Compare October 2, 2020 13:18
@bs-matil

bs-matil commented Oct 2, 2020

Copy link
Copy Markdown
Contributor Author

@bs-matil These are my initial thoughts, I hope they make sense for you. I'll hold a bit the review and wait for your feedback considering that some of my comments are related to the design of the SPI.

I would wait now for additional feedback @pedroigor

@bs-matil
bs-matil force-pushed the legacy-profile-spi branch from 0b3446b to 6c85a0d Compare October 3, 2020 06:53

@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.

LGTM. Thanks, @bs-matil.

I think it is a very good start and unblock us to start working on the next round(s) of changes.

@pedroigor
pedroigor merged commit 43206d3 into keycloak:master Oct 5, 2020
*/
@Path("/applications/{clientId}")
@GET
@Produces(MediaType.APPLICATION_JSON)

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.

@bs-matil I forgot to mention this change in my last review. Why did you add this endpoint for?

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.

looks like accidentally. Shall I open a PR for removal?

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.

Yes, please. Sorry about that. I added the comment but it got lost during the review ...

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.

Sorry too, I tought I will at least have one more time to squash and cleanup. Wasn't expecting a so fast merge after the history of this PR

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.

#7467
PR for cleanup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Core server — startup, configuration, providers/SPIs, cross-cutting concerns kind/feature Categorizes a PR related to a new feature priority/important Must be worked on very soon

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants