8000 KEYCLOAK-15533 Client Policy : Extends Policy Interface to Migrate Client Registration Policies by tnorimat · Pull Request #7723 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

KEYCLOAK-15533 Client Policy : Extends Policy Interface to Migrate Client Registration Policies - #7723

Merged
mposolda merged 1 commit into
keycloak:masterfrom
Hitachi:KEYCLOAK-15533
Mar 2, 2021
Merged

mposolda merged 1 commit into
keycloak:masterfrom
Hitachi:KEYCLOAK-15533

Conversation

@tnorimat
@tnorimat tnorimat commented Jan 18, 2021 •
Copy link
Copy Markdown
Contributor

This PR is for KEYCLOAK-15533 Client Policy : Extends Policy Interface to Migrate Client Registration Policies in KEYCLOAK-13933 Client Policies, also is the part of the project Client Policy Official Support of FAPI-SIG activity.

Generally speaking, the aim of this PR is to support policy conditions defined in Client Policy design document

It behavior has been specified in the JIRA ticket.

This PR has been written with the contribution by @HryhoriiHevorkian and @andriimurashkin as FAPI-SIG activities.

@tnorimat
Copy link
Copy Markdown
Contributor Author

@mposolda This PR is about Client Policy Official Support I mentioned in FAPI-SIG meeting for migrating existing client registration policies by newly introduced client policies. Could you or your team review this PR?

@tnorimat
Copy link
Copy Markdown
Contributor Author

This PR is slightly large size due to the following reason.

  • Rename the context interface (to ClientCRUDContext) so that its name stands for its role.
  • Create the dedicated package (org.keycloak.services.clientpolicy.context) for accommodating context classes.
  • Create abstract context classes for CRUD operation by each Admin REST API (AbstractAdminClientCRUDContext) and by Dynamic Client Registration(AbstractDynamicClientCRUDContext).

@hmlnarik hmlnarik added area/oidc OpenID Connect / OAuth 2.0 — tokens, endpoints, scopes, DCR, PAR, DPoP kind/feature Categorizes a PR related to a new feature labels Jan 18, 2021
@tnorimat
Copy link
Copy Markdown
Contributor Author

@mposolda I've rebased and resolved the conflicts due to #7735 .


public AbstractDynamicClientCRUDContext(KeycloakSession session, JsonWebToken token, RealmModel realm) {
this.token = token;
if (token != null) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Can be refactored to

if (token == null) {
   return;
}
if (token.getIssuedFor() != null) {
    this.authenticatedClient = realm.getClientByClientId(token.getIssuedFor());
}
if (token.getSubject() != null) {
    this.authenticatedUser = session.users().getUserById(token.getSubject(), realm);
}

to reduce cyclomatic complexity

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.

@valb3r Yes. It makes sense. I'll fix it as you suggested.

private boolean isThrowExceptionNeeded(ClientPolicyEvent event) {
ClientPolicyLogger.log(logger, "Client Policy Trigger Event = " + event);
List<String> l = componentModel.getConfig().get(TestRaiseExeptionExecutorFactory.TARGET_CP_EVENTS);
if (l != null && l.stream().anyMatch(i->i.equals(event.toString()))) return true;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This

 if (l != null && l.stream().anyMatch(i->i.equals(event.toString()))) return true;
  return false;

Can be simplified to
return l != null && l.stream().anyMatch(i->i.equals(event.toString()))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@valb3r Thank you. I'll fix it as you proposed.


@Override
public List<ProviderConfigProperty> getConfigProperties() {
return new ArrayList<>(Arrays.asList(TARGET_CP_EVENTS_PROPERTY));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do we really need to return the mutable list here? Can't we do just
return Arrays.asList(TARGET_CP_EVENTS_PROPERTY);

The returned list is not related to TARGET_CP_EVENTS_PROPERTY, so changes to return values will not propagate here. Also, typically configurationPropertes are semantically immutable.

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.

@valb3r Yes, that's right. I'll fix it as you said.


private void processClientPolicyExceptionByAdmin(BadRequestException bre) throws ClientPolicyException {
Response resp = bre.getResponse();
if (resp.getStatus() == Response.Status.BAD_REQUEST.getStatusCode()) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Can be simplified to

Response resp = bre.getResponse();
if (resp.getStatus() != Response.Status.BAD_REQUEST.getStatusCode()) {
  resp.close();
  return;
}

String respBody = resp.readEntity(String.class);
Map<String, String> responseJson = null;
try {
    responseJson = JsonSerialization.readValue(respBody, Map.class);
} catch (IOException e) {
    fail();
}
throw new ClientPolicyException(responseJson.get(OAuth2Constants.ERROR), responseJson.get(OAuth2Constants.ERROR_DESCRIPTION));

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.

@valb3r I see, I'll fix as you suggested.

logger.info("... Registered Condition : AnyClientConditionFactory");

createExecutor(executorName, TestRaiseExeptionExecutorFactory.PROVIDER_ID, null, (ComponentR 8000 epresentation provider) -> {
provider.getConfig().put(TestRaiseExeptionExecutorFactory.TARGET_CP_EVENTS, new ArrayList<>(Arrays.asList(ClientPolicyEvent.REGISTERED.toString())));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do we need to convert to mutable list here?
new ArrayList<>(Arrays.asList(ClientPolicyEvent.REGISTERED.toString()))
could be simply
Arrays.asList(ClientPolicyEvent.REGISTERED.toString())

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.

@valb3r Yes, I'll fix as you pointed out.

}

updateExecutor(executorName, (ComponentRepresentation provider) -> {
provider.getConfig().put(TestRaiseExeptionExecutorFactory.TARGET_CP_EVENTS, new ArrayList<>(Arrays.asList(ClientPolicyEvent.UPDATED.toString())));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do we need to convert to mutable list here?
new ArrayList<>(Arrays.asList(ClientPolicyEvent.UPDATED.toString()))
could be simply
Arrays.asList(ClientPolicyEvent.UPDATED.toString())

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.

@valb3r Yes, that's right. I'll fix it as you said.

assertEquals(false, getClientByAdmin(clientId).isEnabled());

updateExecutor(executorName, (ComponentRepresentation provider) -> {
provider.getConfig().put(TestRaiseExeptionExecutorFactory.TARGET_CP_EVENTS, new ArrayList<>(Arrays.asList(ClientPolicyEvent.VIEW.toString())));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do we need to convert to mutable list here?
new ArrayList<>(Arrays.asList(ClientPolicyEvent.VIEW.toString()))
could be simply
Arrays.asList(ClientPolicyEvent.VIEW.toString())

}

updateExecutor(executorName, (ComponentRepresentation provider) -> {
provider.getConfig().put(TestRaiseExeptionExecutorFactory.TARGET_CP_EVENTS, new ArrayList<>(Arrays.asList(ClientPolicyEvent.UNREGISTER.toString())));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do we need to convert to mutable list here?
new ArrayList<>(Arrays.asList(ClientPolicyEvent.UNREGISTER.toString()))
could be simply
Arrays.asList(ClientPolicyEvent.UNREGISTER.toString())

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.

@valb3r Thank you. I'll fix it as you proposed.

}
} finally {
updateExecutor(executorName, (ComponentRepresentation provider) -> {
provider.getConfig().put(TestRaiseExeptionExecutorFactory.TARGET_CP_EVENTS, new ArrayList<>());
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

new ArrayList<>());
could be replaced with immutable Collections.singletonList();

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.

@valb3r Thank you. Collections.singletonList((String)null) might be ok.

assertEquals(ClientPolicyEvent.REGISTERED.toString(), cpe.getError());
}

updateExecutor(executorName, (ComponentRepresentation provider) -> {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pattern

updateExecutor(executorName, (ComponentRepresentation provider) -> {
                provider.getConfig().put(TestRaiseExeptionExecutorFactory.TARGET_CP_EVENTS, new ArrayList<>());
            });

is used in multiple places

Can we create private helper method:

void updateTargetCPEvents(String executorName, List<ClientPolicyEvent> events) {
  updateExecutor(executorName, (ComponentRepresentation provider) 
10BC8
-> {
                provider.getConfig().put(TestRaiseExeptionExecutorFactory.TARGET_CP_EVENTS, events.stream().map(ClientPolicyEvent::toString()).collect(Collectors.toList()));
  });
}

So that

updateExecutor(executorName, (ComponentRepresentation provider) -> {
                provider.getConfig().put(TestRaiseExeptionExecutorFactory.TARGET_CP_EVENTS, new ArrayList<>());
            });

becomes updateTargetCPEvents(executorName, Arrays.asList(ClientPolicyEvent.UPDATED));

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.

@valb3r I see, I'll fix it as you suggested.

@tnorimat
Copy link
Copy Markdown
Contributor Author

@valb3r Hello,

Thank you very much for your review. I would like to check your comments and refine codes.

@tnorimat
Copy link
Copy Markdown
Contributor Author

@valb3r I've fixed codes and committed. e476036

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

@tnorimat Thanks for the PR and sorry for late review.

The PR will require another rebase due the conflict with merged PR for KEYCLOAK-16880 . I've added 2 minor comments inline and one comment with some consideration for the future (Not directly related to this PR).

/**
* @return the newly registered client {@link ClientModel}
*/
default ClientModel getRegisteredClient() {
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.

Not sure if methods "getRegisteredClient()" and "getUpdatedClient()" are necessary? Maybe it is sufficient to have a single method "getTargetClient()" ? It seems the only issue is, that this method will be null for the REGISTER event, which happens before client registration. But this can be possibly covered in the javadoc of the REGISTER event?

@tnorimat tnorimat Mar 2, 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.

@mposolda As you pointed out, we need not provide these 2 method. It is sufficient to provide getTargetClient() to satisfy their purposes of each implementing concrete context classes. Also, on REGISTER event, getTargetClient() returns null. Therefore, I will add comments about it onto the header of getTargetClient().

  • AbstractAdminClientCRUDContext
    getTargetClient() = null
    getRegisteredClient() = null
    getUpdatedClient() = null
  • AdminClientRegisterContext
    getTargetClient() = null
    getRegisteredClient() = null
    getUpdatedClient() = null
  • AdminClientRegisteredContext
    getTargetClient() = null
    getRegisteredClient() = registeredClient
    getUpdatedClient() = null
  • AdminClientUnregisterContext
    getTargetClient() = targetClient
    getRegisteredClient() = null
    getUpdatedClient() = null
  • AdminClientUpdateContext
    getTargetClient() = targetClient
    getRegisteredClient() = null
    getUpdatedClient() = null
  • AdminClientUpdatedContext
    getTargetClient() = null
    getRegisteredClient() = null
    getUpdatedClient() = targetClient
  • AdminClientViewContext
    getTargetClient() = targetClient
    getRegisteredClient() = null
    getUpdatedClient() = null
  • AbstractDynamicClientCRUDContext
    getTargetClient() = null
    getRegisteredClient() = null
    getUpdatedClient() = null
  • DynamicClientRegisterContext
    getTargetClient() = null
    getRegisteredClient() = null
    getUpdatedClient() = null
  • DynamicClientRegisteredContext
    getTargetClient() = null
    getRegisteredClient() = registeredClient
    getUpdatedClient() = null
  • DynamicClientUnregisterContext
    getTargetClient() = targetClient
    getRegisteredClient() = null
    getUpdatedClient() = null
  • DynamicClientUpdateContext
    getTargetClient() = targetClient
    getRegisteredClient() = null
    getUpdatedClient() = null
  • DynamicClientUpdatedContext
    getTargetClient() = null
    getRegisteredClient() = null
    getUpdatedClient() = targetClient
  • DynamicClientViewContext
    getTargetClient() = targetClient
    getRegisteredClient() = null
    getUpdatedClient() = null

import org.keycloak.representations.RefreshToken;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.services.clientpolicy.*;
import org.keycloak.services.clientpolicy.context.LogoutRequestContext;
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 suggest to remove star import. In general, we try to avoid star imports in Keycloak. Star import was not introduced in this PR, but maybe can be done as part of this PR when you are on 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.

@mposolda I see. I've recognized that keycloak does not use star import in its coding style but I've forgot it. I'll replace this star import with explicit import directives.

import java.util.List;

import javax.ws.rs.BadRequestException;
import java.util.stream.Collectors;
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 wonder if there is a good time to possibly "decompose" ClientPoliciesTest into multiple test classes? For example one class, which will focus mostly on CRUD of clients and another with focus on stuff like signatures, token introspection etc? I have some impression that current class is quite big and already has 29 test methods.

Just an idea for the future. I suggest to not do it in this PR, but rather have separate PR for this...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comm 8 ent

10BC8

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

@mposolda Yes, I agree with you. How about creating the follow-up JIRA ticket for refactoring ClientPoliciesTest after the following PR being merged?

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.

@tnorimat +1 for creating follow-up JIRA. Thanks!

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.

@mposolda I've created the JIRA ticket.

…ient Registration Policies

Co-authored-by: Hryhorii Hevorkian <hhe@adorsys.com.ua>
Co-authored-by: Andrii Murashkin <amu@adorsys.com.ua>
@tnorimat
tnorimat commented Mar 2, 2021
Copy link
Copy Markdown
Contributor Author

@mposolda I've revised codes as you commented. Could you check it?

@mposolda
mposolda commented Mar 2, 2021
Copy link
Copy Markdown
Contributor

@tnorimat Thanks!

@mposolda
mposolda merged commit 882f5ff into keycloak:master Mar 2, 2021
@tnorimat
tnorimat deleted the KEYCLOAK-15533 branch March 2, 2021 08:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/oidc OpenID Connect / OAuth 2.0 — tokens, endpoints, scopes, DCR, PAR, DPoP kind/feature Categorizes a PR related to a new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

0