Skip to content
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

feat: update client response breaking changes #40

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,24 @@ public UpdateControl<KeycloakClient> reconcile(KeycloakClient keycloakClient, Co
keycloakClient.getSpec().getKcConfig().setPassword(new String(password));
}

int responseCode = service.createClient(keycloakClient);
var response = service.createClient(keycloakClient);

updateStatusPojo(keycloakClient, responseCode);
updateStatusPojo(keycloakClient, response);
log.info("Resource '{}' reconciled - updating status", keycloakClient.getMetadata().getName());
return UpdateControl.updateStatus(keycloakClient);
}

private void updateStatusPojo(KeycloakClient keycloakClient, int responseCode) {
private void updateStatusPojo(KeycloakClient keycloakClient, KeycloakAdminService.CreateClientResponse response) {
KeycloakClientStatus result = new KeycloakClientStatus();
KeycloakClientSpec spec = keycloakClient.getSpec();
result.setClientId(spec.getKcConfig().getClientId());
result.setResponseCode(responseCode);
result.setResponseCode(response.getStatusCode());
result.setMessage(response.getMessage());
var status = KeycloakClientStatus.Status.UNDEFINED;
if (responseCode == 200) {
if (response.getStatusCode() == 200) {
status = KeycloakClientStatus.Status.UPDATED;
}
if (responseCode == 201) {
if (response.getStatusCode() == 201) {
status = KeycloakClientStatus.Status.CREATED;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.tkit.onecx.iam.kc.client.operator.models;

import org.keycloak.representations.idm.ClientRepresentation;
import org.mapstruct.Mapper;

@Mapper
public interface ClientRepresentationMapper {

ExtClientRepresentation create(ClientRepresentation data);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.tkit.onecx.iam.kc.client.operator.models;

import org.keycloak.representations.idm.ClientRepresentation;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties({ "type" })
public class ExtClientRepresentation extends ClientRepresentation {

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.control.ActivateRequestContext;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;

import org.keycloak.admin.client.Keycloak;
import org.keycloak.admin.client.resource.ClientResource;
Expand All @@ -17,6 +18,8 @@
import org.tkit.onecx.iam.kc.client.operator.KeycloakClient;
import org.tkit.onecx.iam.kc.client.operator.config.KCClientConfig;
import org.tkit.onecx.iam.kc.client.operator.config.KCDefaultConfig;
import org.tkit.onecx.iam.kc.client.operator.models.ClientRepresentationMapper;
import org.tkit.onecx.iam.kc.client.operator.models.ExtClientRepresentation;

@ApplicationScoped
public class KeycloakAdminService {
Expand All @@ -34,8 +37,11 @@ public class KeycloakAdminService {
@Inject
KCClientConfig kcClientConfig;

@Inject
ClientRepresentationMapper mapper;

@ActivateRequestContext
public int createClient(KeycloakClient keycloakClient) {
public CreateClientResponse createClient(KeycloakClient keycloakClient) {
var spec = keycloakClient.getSpec();
var clientId = spec.getKcConfig().getClientId();
var realm = spec.getRealm() != null ? spec.getRealm() : kcClientConfig.realm();
Expand Down Expand Up @@ -71,15 +77,17 @@ public int createClient(KeycloakClient keycloakClient) {

if (clients.isEmpty()) {
// do create
try (var resp = keycloak.realm(realm).clients().create(client)) {
return resp.getStatus();
ExtClientRepresentation extClient = mapper.create(client);
try (var resp = keycloak.realm(realm).clients().create(extClient)) {
return CreateClientResponse.of(resp.getStatus(), resp.readEntity(String.class));
}
} else {
// do update
var defaultClientScopeNames = client.getDefaultClientScopes();
var optionalClientScopeNames = client.getOptionalClientScopes();
var clientToUpdate = keycloak.realm(realm).clients().get(clients.get(0).getId());
clientToUpdate.update(client);
ExtClientRepresentation extClient = mapper.create(client);
clientToUpdate.update(extClient);
// update default client scopes
var toRemove = clientToUpdate.getDefaultClientScopes().stream()
.filter(rep -> !defaultClientScopeNames.contains(rep.getName())).map(ClientScopeRepresentation::getId)
Expand All @@ -101,7 +109,7 @@ public int createClient(KeycloakClient keycloakClient) {
toRemoveOpt.forEach(scope -> removeOptClientScope(clientToUpdate, scope));
toAddOpt.forEach(scope -> addOptClientScope(clientToUpdate, scope));

return 200;
return CreateClientResponse.of(Response.Status.OK.getStatusCode());
}
}

Expand Down Expand Up @@ -258,4 +266,30 @@ private String resolveValue(String value, String defaultValue) {
return defaultValue;
}

public static class CreateClientResponse {
private final int statusCode;
private final String message;

public static CreateClientResponse of(int statusCode) {
return of(statusCode, null);
}

public static CreateClientResponse of(int statusCode, String message) {
return new CreateClientResponse(statusCode, message);
}

private CreateClientResponse(int statusCode, String message) {
this.message = message;
this.statusCode = statusCode;
}

public int getStatusCode() {
return statusCode;
}

public String getMessage() {
return message;
}
}

}