Repository navigation
shows what patching logic should look like #40450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
core/src/main/java/org/keycloak/representations/admin/v2/BaseRepresentation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package org.keycloak.representations.admin.v2; | ||
|
|
||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonAnyGetter; | ||
| import com.fasterxml.jackson.annotation.JsonAnySetter; | ||
| import com.fasterxml.jackson.annotation.JsonIgnore; | ||
| import com.fasterxml.jackson.annotation.JsonInclude; | ||
|
|
||
| @JsonInclude(JsonInclude.Include.NON_ABSENT) | ||
| public class BaseRepresentation { | ||
|
|
||
| @JsonIgnore | ||
| protected Map<String, Object> additionalFields = new LinkedHashMap<String, Object>(); | ||
|
|
||
| @JsonAnyGetter | ||
| public Map<String, Object> getAdditionalFields() { | ||
| return additionalFields; | ||
| } | ||
|
|
||
| @JsonAnySetter | ||
| public void setAdditionalField(String name, Object value) { | ||
| this.additionalFields.put(name, value); | ||
| } | ||
|
|
||
| public void setAdditionalFields(Map<String, Object> additionalFields) { | ||
| this.additionalFields = additionalFields; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
rest/admin-api/src/main/java/org/keycloak/admin/api/FieldValidation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package org.keycloak.admin.api; | ||
|
|
||
| public enum FieldValidation { | ||
| Ignore, | ||
| Strict, | ||
| Warn | ||
| } |
20 changes: 19 additions & 1 deletion
20
rest/admin-api/src/main/java/org/keycloak/admin/api/client/ClientApi.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,32 @@ | ||
| package org.keycloak.admin.api.client; | ||
|
|
||
| import jakarta.ws.rs.Consumes; | ||
| import jakarta.ws.rs.GET; | ||
| import jakarta.ws.rs.PATCH; | ||
| import jakarta.ws.rs.PUT; | ||
| import jakarta.ws.rs.PathParam; | ||
| import jakarta.ws.rs.Produces; | ||
| import jakarta.ws.rs.QueryParam; | ||
| import jakarta.ws.rs.core.MediaType; | ||
|
|
||
| import org.keycloak.admin.api.FieldValidation; | ||
| import org.keycloak.representations.admin.v2.ClientRepresentation; | ||
|
|
||
| import com.fasterxml.jackson.databind.JsonNode; | ||
|
|
||
| public interface ClientApi { | ||
|
|
||
| @GET | ||
| @Produces(MediaType.APPLICATION_JSON) | ||
| ClientRepresentation getClient(); | ||
|
|
||
| @PUT | ||
| @Consumes(MediaType.APPLICATION_JSON) | ||
| @Produces(MediaType.APPLICATION_JSON) | ||
| ClientRepresentation createOrUpdateClient(ClientRepresentation client, @PathParam("fieldValidation") FieldValidation fieldValidation); | ||
|
|
||
| @PATCH | ||
| @Consumes("application/merge-patch+json") | ||
| @Produces(MediaType.APPLICATION_JSON) | ||
| ClientRepresentation patchClient(JsonNode patch, @PathParam("fieldValidation") FieldValidation fieldValidation); | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 57 additions & 7 deletions
64
rest/admin-api/src/main/java/org/keycloak/admin/api/client/DefaultClientApi.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,83 @@ | ||
| package org.keycloak.admin.api.client; | ||
|
|
||
| import jakarta.ws.rs.GET; | ||
| import jakarta.ws.rs.NotFoundException; | ||
| import jakarta.ws.rs.Produces; | ||
| import jakarta.ws.rs.QueryParam; | ||
| import jakarta.ws.rs.core.MediaType; | ||
| import java.io.IOException; | ||
|
|
||
| import org.keycloak.admin.api.FieldValidation; | ||
| import org.keycloak.http.HttpResponse; | ||
| import org.keycloak.models.KeycloakSession; | ||
| import org.keycloak.models.RealmModel; | ||
| import org.keycloak.representations.admin.v2.ClientRepresentation; | ||
| import org.keycloak.services.ErrorResponse; | ||
| import org.keycloak.services.ServiceException; | ||
| import org.keycloak.services.client.ClientService; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.databind.ObjectReader; | ||
|
|
||
| import jakarta.ws.rs.NotFoundException; | ||
| import jakarta.ws.rs.WebApplicationException; | ||
| import jakarta.ws.rs.core.Response; | ||
|
|
||
| public class DefaultClientApi implements ClientApi { | ||
| private final KeycloakSession session; | ||
| private final RealmModel realm; | ||
| private final String clientId; | ||
| private final ClientService clientService; | ||
| private HttpResponse response; | ||
|
|
||
| public DefaultClientApi(KeycloakSession session, String clientId) { | ||
| this.session = session; | ||
| this.clientId = clientId; | ||
| this.realm = session.getContext().getRealm(); | ||
| this.clientService = session.services().clients(); | ||
| this.response = session.getContext().getHttpResponse(); | ||
| } | ||
|
|
||
| @GET | ||
| @Produces(MediaType.APPLICATION_JSON) | ||
| @Override | ||
| public ClientRepresentation getClient() { | ||
| return clientService.getClient(realm, clientId, null) | ||
| .orElseThrow(() -> new NotFoundException("Cannot find the specified client")); | ||
| } | ||
|
|
||
| @Override | ||
| public ClientRepresentation createOrUpdateClient(ClientRepresentation client, FieldValidation fieldValidation) { | ||
| try { | ||
| var result = clientService.createOrUpdate(realm, client, true); | ||
| if (result.created()) { | ||
| response.setStatus(Response.Status.CREATED.getStatusCode()); | ||
| } | ||
| return result.representation(); | ||
| } catch (ServiceException e) { | ||
| throw new WebApplicationException(e.getMessage(), e.getSuggestedResponseStatus().orElse(Response.Status.BAD_REQUEST)); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public ClientRepresentation patchClient(JsonNode patch, FieldValidation fieldValidation) { | ||
| // patches don't yet allow for creating | ||
| ClientRepresentation client = getClient(); | ||
| try { | ||
| // TODO: there should be a more centralized objectmapper | ||
| final ObjectReader objectReader = new ObjectMapper().readerForUpdating(client); | ||
| ClientRepresentation updated = objectReader.readValue(patch); | ||
|
|
||
| // TODO: reuse in the other methods | ||
| if (!updated.getAdditionalFields().isEmpty()) { | ||
| if (fieldValidation == null || fieldValidation == FieldValidation.Strict) { | ||
| // validation failed | ||
| throw new WebApplicationException("Payload contains unknown fields: " + updated.getAdditionalFields().keySet(), Response.Status.BAD_REQUEST); | ||
| } else if (fieldValidation == FieldValidation.Warn) { | ||
| response.addHeader("WARNING", "Payload contains unknown fields: " + updated.getAdditionalFields().keySet()); | ||
| } | ||
| } | ||
| return clientService.createOrUpdate(realm, updated, true).representation(); | ||
| } catch (JsonProcessingException e) { | ||
| // TODO: kubernetes uses 422 instead | ||
| throw new WebApplicationException(e.getMessage(), Response.Status.BAD_REQUEST); | ||
| } catch (IOException e) { | ||
| throw ErrorResponse.error("Unknown Error Occurred", Response.Status.INTERNAL_SERVER_ERROR); | ||
| } | ||
| } | ||
| } |
10BC8
43 changes: 10 additions & 33 deletions
43
rest/admin-api/src/main/java/org/keycloak/admin/api/client/DefaultClientsApi.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
server-spi-private/src/main/java/org/keycloak/models/mapper/ClientModelMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,12 @@ | ||
| package org.keycloak.models.mapper; | ||
|
|
||
| import org.keycloak.models.ClientModel; | ||
| import org.keycloak.models.RealmModel; | ||
| import org.keycloak.representations.admin.v2.ClientRepresentation; | ||
|
|
||
| public interface ClientModelMapper { | ||
|
|
||
| ClientRepresentation fromModel(ClientModel model); | ||
|
|
||
| // ClientModel toModel(ClientModel baseModel, ClientRepresentation representation); | ||
| void toModel(ClientModel model, ClientRepresentation rep, RealmModel realm); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Field validation might need a better name not to be confused with regular validations (from semantic perspective etc.). Possibly something like
unknownFieldsPolicybut not sure.Also should we really give the power to set this policy to the user? Shouldn't it rather be a global server option? In other words, do we really want to set this per request?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is possible to tie this to "optional" semantic validation - for example in kubernetes the handling of the StatefulSet serviceName - it is not required unless fieldValidation=Strict.
8000Yes the naming can be whatever we want.
You would probably want control per request yes. You can certainly make a case for session or server level control as well.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking about that more, maybe
fieldNameValidationwould be better? WDYT?Ok. We can add the server level one as a follow-up.