Skip to content

Commit

Permalink
Add Keycloak 23
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasvoelcker committed Dec 5, 2023
1 parent cc36192 commit ef760e9
Show file tree
Hide file tree
Showing 9 changed files with 1,872 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Used in docker-compose
# shellcheck disable=SC2034
KEYCLOAK_VERSION=22.0.4
KEYCLOAK_VERSION=23.0.1
17 changes: 16 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ jobs:
env:
# we keep 18.0.2 for backwards compatibility with RH-SSO 7.6
- KEYCLOAK_VERSION: 18.0.2
- KEYCLOAK_VERSION: 19.0.3
- KEYCLOAK_VERSION: 20.0.5
- KEYCLOAK_VERSION: 21.1.1
- KEYCLOAK_VERSION: 22.0.4
- KEYCLOAK_VERSION: 23.0.1
steps:
- uses: actions/checkout@v3
with:
Expand All @@ -50,6 +50,11 @@ jobs:
key: ${{ runner.os }}-maven-${{ matrix.env.KEYCLOAK_VERSION }}-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-maven-${{ matrix.env.KEYCLOAK_VERSION }}

- name: Adapt sources for Keycloak versions < 23.0.0 (UPConfig -> String)
if: ${{ matrix.env.KEYCLOAK_VERSION < '23.0.0' }}
run: |
echo "JAVAX_PROFILE=-Ppre-keycloak23" >> $GITHUB_ENV
- name: Adapt sources for Keycloak versions < 22.0.0 (jakarta -> javax)
if: ${{ matrix.env.KEYCLOAK_VERSION < '22.0.0' }}
run: |
Expand Down Expand Up @@ -159,6 +164,11 @@ jobs:
key: ${{ runner.os }}-${{ matrix.java }}-maven-build-pom-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-${{ matrix.java }}-maven-build-pom

- name: Adapt sources for Keycloak versions < 23.0.0 (UPConfig -> String)
if: ${{ matrix.env.KEYCLOAK_VERSION < '23.0.0' }}
run: |
echo "JAVAX_PROFILE=-Ppre-keycloak23" >> $GITHUB_ENV
- name: Adapt sources for Keycloak versions < 22.0.0 (jakarta -> javax)
if: ${{ matrix.env.KEYCLOAK_VERSION < '22.0.0' }}
run: |
Expand Down Expand Up @@ -192,6 +202,11 @@ jobs:
restore-keys: |
${{ runner.os }}-maven-keycloak-legacy
- name: Adapt sources for Keycloak versions < 23.0.0 (UPConfig -> String)
if: ${{ matrix.env.KEYCLOAK_VERSION < '23.0.0' }}
run: |
echo "JAVAX_PROFILE=-Ppre-keycloak23" >> $GITHUB_ENV
- name: Adapt sources for Keycloak versions < 22.0.0 (jakarta -> javax)
if: ${{ matrix.env.KEYCLOAK_VERSION < '22.0.0' }}
run: |
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
- Updated CI to use Keycloak 23.0.1

## [5.9.0] - 2023-10-13
- Updated CI to use Keycloak 22.0.4
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ FROM ${BUILDER_IMAGE} AS BUILDER

WORKDIR /app/

ARG KEYCLOAK_VERSION=22.0.4
ARG KEYCLOAK_VERSION=23.0.1
ARG MAVEN_CLI_OPTS="-ntp -B"

COPY .mvn .mvn
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ Additionally, the tag `maven` contains the source code and compile keycloak-conf
Keycloak versions, that not official supported., e.g.:

```bash
docker run --rm -ti -v $PWD:/config/ -eKEYCLOAK_VERSION=22.0.4 -eMAVEN_CLI_OPTS="-B -ntp -q" adorsys/keycloak-config-cli:edge-build
docker run --rm -ti -v $PWD:/config/ -eKEYCLOAK_VERSION=23.0.1 -eMAVEN_CLI_OPTS="-B -ntp -q" adorsys/keycloak-config-cli:edge-build
```

### Docker run
Expand Down
57 changes: 56 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<keycloak.version>22.0.4</keycloak.version>
<keycloak.version>23.0.1</keycloak.version>

<checkstyle-plugin.version>3.2.0</checkstyle-plugin.version>
<checkstyle.version>10.0</checkstyle.version>
Expand Down Expand Up @@ -727,6 +727,61 @@
<token>import jakarta</token>
<value>import javax</value>
</replacement>
<replacement>
<token>;&#10;import org.keycloak.representations.userprofile.config.UPConfig;</token>
<value>;</value>
</replacement>
<replacement>
<token>return JsonUtil.toJson\(userProfileResource.getConfiguration\(\)\);</token>
<value>return userProfileResource.getConfiguration();</value>
</replacement>
<replacement>
<token>userProfileResource.update\(JsonUtil.readValue\(newUserProfileConfiguration, UPConfig.class\)\);</token>
<value>userProfileResource.update(newUserProfileConfiguration);</value>
</replacement>
</replacements>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>pre-keycloak23</id>
<build>
<plugins>
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>${maven-replacer.version}</version>
<executions>
<execution>
<id>replace-upconfig-with-string</id>
<phase>generate-sources</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<basedir>
${project.basedir}/src
</basedir>
<includes>
<include>main/java/de/adorsys/keycloak/config/repository/UserProfileRepository.java</include>
</includes>
<replacements>
<replacement>
<token>;&#10;import org.keycloak.representations.userprofile.config.UPConfig;</token>
<value>;</value>
</replacement>
<replacement>
<token>return JsonUtil.toJson\(userProfileResource.getConfiguration\(\)\);</token>
<value>return userProfileResource.getConfiguration();</value>
</replacement>
<replacement>
<token>userProfileResource.update\(JsonUtil.readValue\(newUserProfileConfiguration, UPConfig.class\)\);</token>
<value>userProfileResource.update(newUserProfileConfiguration);</value>
</replacement>
</replacements>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@
import de.adorsys.keycloak.config.exception.KeycloakRepositoryException;
import de.adorsys.keycloak.config.util.JsonUtil;
import org.keycloak.admin.client.resource.UserProfileResource;
import org.keycloak.representations.userprofile.config.UPConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Optional;

import jakarta.ws.rs.core.Response;

@Component
public class UserProfileRepository {

Expand All @@ -57,14 +54,13 @@ public void updateUserProfile(String realm, boolean newUserProfileEnabled, Strin

if (!newUserProfileEnabled) {
logger.trace("UserProfile is explicitly disabled, removing configuration.");
try (var response = userProfileResource.update(null)) {
logger.trace("UserProfile configuration removed.");
}
userProfileResource.update(null);
logger.trace("UserProfile configuration removed.");
return;
}

var realmAttributes = realmRepository.get(realm).getAttributesOrEmpty();
var currentUserProfileConfiguration = Optional.ofNullable(userProfileResource.getConfiguration()).orElse("");
var currentUserProfileConfiguration = getUserProfileConfiguration(userProfileResource);
if (!StringUtils.hasText(currentUserProfileConfiguration)) {
logger.warn("UserProfile is enabled, but no configuration string provided.");
return;
Expand All @@ -82,10 +78,10 @@ public void updateUserProfile(String realm, boolean newUserProfileEnabled, Strin
return;
}

try (var updateUserProfileResponse = userProfileResource.update(newUserProfileConfiguration)) {
if (!updateUserProfileResponse.getStatusInfo().equals(Response.Status.OK)) {
throw new KeycloakRepositoryException("Could not update UserProfile Definition");
}
try {
resolveUserProfileUpdate(userProfileResource, newUserProfileConfiguration);
} catch (Exception ex) {
throw new KeycloakRepositoryException("Could not update UserProfile Definition", ex);
}

logger.trace("UserProfile updated.");
Expand All @@ -97,6 +93,14 @@ private boolean hasUserProfileConfigurationChanged(String newUserProfileConfigur
return !currentValue.equals(newValue);
}

private String getUserProfileConfiguration(UserProfileResource userProfileResource) {
return JsonUtil.toJson(userProfileResource.getConfiguration());
}

private void resolveUserProfileUpdate(UserProfileResource userProfileResource, String newUserProfileConfiguration) {
userProfileResource.update(JsonUtil.readValue(newUserProfileConfiguration, UPConfig.class));
}

private UserProfileResource getResource(String realmName) {
return this.realmRepository.getResource(realmName).users().userProfile();
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/de/adorsys/keycloak/config/util/JsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,12 @@ private static JsonNode fromJsonAsNode(String value) {
throw new ImportProcessingException(e);
}
}

public static <T> T readValue(String value, Class<T> type) {
try {
return value == null ? null : objectMapper.readValue(value, type);
} catch (JsonProcessingException e) {
throw new ImportProcessingException(e);
}
}
}
Loading

0 comments on commit ef760e9

Please sign in to comment.