Repository navigation
KEYCLOAK-15533 Client Policy : Extends Policy Interface to Migrate Client Registration Policies - #7723
Conversation
|
@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? |
|
This PR is slightly large size due to the following reason.
|
b2ac4f0 to
1b491e1
Compare
1b491e1 to
4c72043
Compare
4c72043 to
b5e94ce
Compare
|
|
||
| public AbstractDynamicClientCRUDContext(KeycloakSession session, JsonWebToken token, RealmModel realm) { | ||
| this.token = token; | ||
| if (token != null) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
@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; |
There was a problem hiding this comment.
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()))
There was a problem hiding this comment.
@valb3r Thank you. I'll fix it as you proposed.
|
|
||
| @Override | ||
| public List<ProviderConfigProperty> getConfigProperties() { | ||
| return new ArrayList<>(Arrays.asList(TARGET_CP_EVENTS_PROPERTY)); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@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()) { |
There was a problem hiding this comment.
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));
There was a problem hiding this comment.
@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()))); |
There was a problem hiding this comment.
Do we need to convert to mutable list here?
new ArrayList<>(Arrays.asList(ClientPolicyEvent.REGISTERED.toString()))
could be simply
Arrays.asList(ClientPolicyEvent.REGISTERED.toString())
There was a problem hiding this comment.
@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()))); |
There was a problem hiding this comment.
Do we need to convert to mutable list here?
new ArrayList<>(Arrays.asList(ClientPolicyEvent.UPDATED.toString()))
could be simply
Arrays.asList(ClientPolicyEvent.UPDATED.toString())
There was a problem hiding this comment.
@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()))); |
There was a problem hiding this comment.
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()))); |
There was a problem hiding this comment.
Do we need to convert to mutable list here?
new ArrayList<>(Arrays.asList(ClientPolicyEvent.UNREGISTER.toString()))
could be simply
Arrays.asList(ClientPolicyEvent.UNREGISTER.toString())
There was a problem hiding this comment.
@valb3r Thank you. I'll fix it as you proposed.
| } | ||
| } finally { | ||
| updateExecutor(executorName, (ComponentRepresentation provider) -> { | ||
| provider.getConfig().put(TestRaiseExeptionExecutorFactory.TARGET_CP_EVENTS, new ArrayList<>()); |
There was a problem hiding this comment.
new ArrayList<>());
could be replaced with immutable Collections.singletonList();
There was a problem hiding this comment.
@valb3r Thank you. Collections.singletonList((String)null) might be ok.
| assertEquals(ClientPolicyEvent.REGISTERED.toString(), cpe.getError()); | ||
| } | ||
|
|
||
| updateExecutor(executorName, (ComponentRepresentation provider) -> { |
There was a problem hiding this comment.
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));
There was a problem hiding this comment.
@valb3r I see, I'll fix it as you suggested.
|
@valb3r Hello, Thank you very much for your review. I would like to check your comments and refine codes. |
There was a problem hiding this comment.
@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() { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
@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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
@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; |
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
@mposolda Yes, I agree with you. How about creating the follow-up JIRA ticket for refactoring ClientPoliciesTest after the following PR being merged?
There was a problem hiding this comment.
@tnorimat +1 for creating follow-up JIRA. Thanks!
…ient Registration Policies Co-authored-by: Hryhorii Hevorkian <hhe@adorsys.com.ua> Co-authored-by: Andrii Murashkin <amu@adorsys.com.ua>
|
@mposolda I've revised codes as you commented. Could you check it? |
|
@tnorimat Thanks! |
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.