Skip to content

Commit

Permalink
Cleaned up code
Browse files Browse the repository at this point in the history
  • Loading branch information
hylkevds committed Oct 10, 2024
1 parent 116c6ea commit 99ce15a
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
*/
package de.fraunhofer.iosb.ilt.frostserver.auth.keycloak;

import static de.fraunhofer.iosb.ilt.frostserver.auth.keycloak.KeycloakAuthProvider.TAG_USER_CACHE_CLEANUP_INTERVAL;
import static de.fraunhofer.iosb.ilt.frostserver.auth.keycloak.KeycloakAuthProvider.TAG_USER_CACHE_LIFETIME;
import static de.fraunhofer.iosb.ilt.frostserver.auth.keycloak.KeycloakAuthProvider.TAG_USER_ROLE_DECODER_CLASS;
import static de.fraunhofer.iosb.ilt.frostserver.auth.keycloak.KeycloakSettings.TAG_USER_CACHE_CLEANUP_INTERVAL;
import static de.fraunhofer.iosb.ilt.frostserver.auth.keycloak.KeycloakSettings.TAG_USER_CACHE_LIFETIME;
import static de.fraunhofer.iosb.ilt.frostserver.auth.keycloak.KeycloakSettings.TAG_USER_ROLE_DECODER_CLASS;
import static de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.utils.ConnectionUtils.TAG_DB_URL;

import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.utils.ConnectionUtils;
Expand Down Expand Up @@ -87,8 +87,8 @@ public static DatabaseHandler getInstance(CoreSettings coreSettings) {
private DatabaseHandler(CoreSettings coreSettings) {
authSettings = coreSettings.getAuthSettings();
connectionUrl = authSettings.get(TAG_DB_URL, ConnectionUtils.class);
String userRoleDecoderClass = authSettings.get(TAG_USER_ROLE_DECODER_CLASS, KeycloakAuthProvider.class);
String lifeTimeString = authSettings.get(TAG_USER_CACHE_LIFETIME, KeycloakAuthProvider.class);
String userRoleDecoderClass = authSettings.get(TAG_USER_ROLE_DECODER_CLASS, KeycloakSettings.class);
String lifeTimeString = authSettings.get(TAG_USER_CACHE_LIFETIME, KeycloakSettings.class);
lifetime = Duration.parse(lifeTimeString);
try {
Class<?> urdClass = Class.forName(userRoleDecoderClass);
Expand All @@ -97,7 +97,7 @@ private DatabaseHandler(CoreSettings coreSettings) {
} catch (ReflectiveOperationException ex) {
LOGGER.error("Could not create UserRoleDecoder: Class '{}' could not be instantiated", userRoleDecoderClass, ex);
}
String cleanupIntervalString = authSettings.get(TAG_USER_CACHE_CLEANUP_INTERVAL, KeycloakAuthProvider.class);
String cleanupIntervalString = authSettings.get(TAG_USER_CACHE_CLEANUP_INTERVAL, KeycloakSettings.class);
cleanupIntervalMs = Duration.parse(cleanupIntervalString).toMillis();
}

Expand Down Expand Up @@ -169,6 +169,7 @@ private void cleanupSleep() {
Thread.sleep(cleanupIntervalMs);
} catch (InterruptedException ex) {
LOGGER.trace("Rude Wakeup.", ex);
Thread.currentThread().interrupt();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@
*/
package de.fraunhofer.iosb.ilt.frostserver.auth.keycloak;

import static de.fraunhofer.iosb.ilt.frostserver.auth.keycloak.KeycloakSettings.TAG_MAX_CLIENTS_PER_USER;
import static de.fraunhofer.iosb.ilt.frostserver.auth.keycloak.KeycloakSettings.TAG_MAX_PASSWORD_LENGTH;
import static de.fraunhofer.iosb.ilt.frostserver.auth.keycloak.KeycloakSettings.TAG_MAX_USERNAME_LENGTH;
import static de.fraunhofer.iosb.ilt.frostserver.auth.keycloak.KeycloakSettings.TAG_REGISTER_USER_LOCALLY;
import static de.fraunhofer.iosb.ilt.frostserver.settings.CoreSettings.TAG_AUTHENTICATE_ONLY;
import static de.fraunhofer.iosb.ilt.frostserver.settings.CoreSettings.TAG_AUTH_ROLE_ADMIN;
import static de.fraunhofer.iosb.ilt.frostserver.util.user.UserData.MAX_PASSWORD_LENGTH;
import static de.fraunhofer.iosb.ilt.frostserver.util.user.UserData.MAX_USERNAME_LENGTH;

import de.fraunhofer.iosb.ilt.frostclient.settings.annotation.SensitiveValue;
import de.fraunhofer.iosb.ilt.frostserver.service.InitResult;
import de.fraunhofer.iosb.ilt.frostserver.settings.ConfigDefaults;
import de.fraunhofer.iosb.ilt.frostserver.settings.CoreSettings;
import de.fraunhofer.iosb.ilt.frostserver.settings.Settings;
import de.fraunhofer.iosb.ilt.frostserver.settings.annotation.DefaultValue;
import de.fraunhofer.iosb.ilt.frostserver.settings.annotation.DefaultValueBoolean;
import de.fraunhofer.iosb.ilt.frostserver.settings.annotation.DefaultValueInt;
import de.fraunhofer.iosb.ilt.frostserver.util.AuthProvider;
import de.fraunhofer.iosb.ilt.frostserver.util.LiquibaseUser;
import de.fraunhofer.iosb.ilt.frostserver.util.exception.UpgradeFailedException;
Expand Down Expand Up @@ -57,62 +56,8 @@
*
* @author scf
*/
public class KeycloakAuthProvider implements AuthProvider, LiquibaseUser, ConfigDefaults {
public class KeycloakAuthProvider implements AuthProvider, LiquibaseUser {

@DefaultValue("")
@SensitiveValue
public static final String TAG_KEYCLOAK_CONFIG = "keycloakConfig";

@DefaultValue("")
public static final String TAG_KEYCLOAK_CONFIG_FILE = "keycloakConfigFile";

/**
* The URL on the Keycloak server that can be used to download the Keycloak
* config file. Usually this URL is in the form of:
* https://keycloak.example.com/auth/realms/[realm]/clients-registrations/install/[clientId]
*/
@DefaultValue("")
public static final String TAG_KEYCLOAK_CONFIG_URL = "keycloakConfigUrl";

/**
* If the client has "access-type" set to "confidential" then a secret is
* required to download the configuration. This secret can be found in the
* configuration itself, in Keycloak.
*/
@DefaultValue("")
@SensitiveValue
public static final String TAG_KEYCLOAK_CONFIG_SECRET = "keycloakConfigSecret";

@DefaultValueInt(10)
public static final String TAG_MAX_CLIENTS_PER_USER = "maxClientsPerUser";

@DefaultValueBoolean(false)
public static final String TAG_REGISTER_USER_LOCALLY = "registerUserLocally";

@DefaultValue("USERS")
public static final String TAG_USER_TABLE = "userTable";

@DefaultValue("USER_NAME")
public static final String TAG_USERNAME_COLUMN = "usernameColumn";

@DefaultValueInt(MAX_PASSWORD_LENGTH)
public static final String TAG_MAX_PASSWORD_LENGTH = "maxPasswordLength";

@DefaultValueInt(MAX_USERNAME_LENGTH)
public static final String TAG_MAX_USERNAME_LENGTH = "maxUsernameLength";

@DefaultValue("de.fraunhofer.iosb.ilt.frostserver.auth.keycloak.UserRoleDecoderDflt")
public static final String TAG_USER_ROLE_DECODER_CLASS = "userRoleDecoderClass";

@DefaultValue("PT5M")
public static final String TAG_USER_CACHE_LIFETIME = "userCacheLifetime";

@DefaultValue("PT5S")
public static final String TAG_USER_CACHE_CLEANUP_INTERVAL = "userCacheCleanupInterval";

/**
* The logger for this class.
*/
private static final Logger LOGGER = LoggerFactory.getLogger(KeycloakAuthProvider.class);

/**
Expand Down Expand Up @@ -148,10 +93,10 @@ public InitResult init(CoreSettings coreSettings) {
OPTIONS.put("keycloak-config-file", FROST_SERVER_KEYCLOAKJSON);
final Settings authSettings = coreSettings.getAuthSettings();
roleAdmin = authSettings.get(TAG_AUTH_ROLE_ADMIN, CoreSettings.class);
maxClientsPerUser = authSettings.getInt(TAG_MAX_CLIENTS_PER_USER, getClass());
maxPassLength = authSettings.getInt(TAG_MAX_PASSWORD_LENGTH, getClass());
maxNameLength = authSettings.getInt(TAG_MAX_USERNAME_LENGTH, getClass());
registerUserLocally = authSettings.getBoolean(TAG_REGISTER_USER_LOCALLY, KeycloakAuthProvider.class);
maxClientsPerUser = authSettings.getInt(TAG_MAX_CLIENTS_PER_USER, KeycloakSettings.class);
maxPassLength = authSettings.getInt(TAG_MAX_PASSWORD_LENGTH, KeycloakSettings.class);
maxNameLength = authSettings.getInt(TAG_MAX_USERNAME_LENGTH, KeycloakSettings.class);
registerUserLocally = authSettings.getBoolean(TAG_REGISTER_USER_LOCALLY, KeycloakSettings.class);
authenticateOnly = authSettings.getBoolean(TAG_AUTHENTICATE_ONLY, CoreSettings.class);
if (registerUserLocally) {
DatabaseHandler.init(coreSettings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
package de.fraunhofer.iosb.ilt.frostserver.auth.keycloak;

import static de.fraunhofer.iosb.ilt.frostserver.auth.keycloak.KeycloakAuthProvider.TAG_REGISTER_USER_LOCALLY;
import static de.fraunhofer.iosb.ilt.frostserver.auth.keycloak.KeycloakSettings.TAG_REGISTER_USER_LOCALLY;
import static de.fraunhofer.iosb.ilt.frostserver.settings.CoreSettings.TAG_AUTHENTICATE_ONLY;
import static de.fraunhofer.iosb.ilt.frostserver.settings.CoreSettings.TAG_AUTH_ALLOW_ANON_READ;
import static de.fraunhofer.iosb.ilt.frostserver.settings.CoreSettings.TAG_AUTH_ROLE_ADMIN;
Expand Down Expand Up @@ -99,7 +99,7 @@ public void init(FilterConfig filterConfig) throws ServletException {
roleMappings = AuthUtils.loadRoleMapping(authSettings);
roleAdmin = authSettings.get(TAG_AUTH_ROLE_ADMIN, CoreSettings.class);
authenticateOnly = authSettings.getBoolean(TAG_AUTHENTICATE_ONLY, CoreSettings.class);
registerUserLocally = authSettings.getBoolean(TAG_REGISTER_USER_LOCALLY, KeycloakAuthProvider.class);
registerUserLocally = authSettings.getBoolean(TAG_REGISTER_USER_LOCALLY, KeycloakSettings.class);
if (registerUserLocally) {
databaseHandler = DatabaseHandler.getInstance(coreSettings);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (C) 2024 Fraunhofer Institut IOSB, Fraunhoferstr. 1, D 76131
* Karlsruhe, Germany.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.fraunhofer.iosb.ilt.frostserver.auth.keycloak;

import static de.fraunhofer.iosb.ilt.frostserver.util.user.UserData.MAX_PASSWORD_LENGTH;
import static de.fraunhofer.iosb.ilt.frostserver.util.user.UserData.MAX_USERNAME_LENGTH;

import de.fraunhofer.iosb.ilt.frostclient.settings.annotation.SensitiveValue;
import de.fraunhofer.iosb.ilt.frostserver.settings.ConfigDefaults;
import de.fraunhofer.iosb.ilt.frostserver.settings.annotation.DefaultValue;
import de.fraunhofer.iosb.ilt.frostserver.settings.annotation.DefaultValueBoolean;
import de.fraunhofer.iosb.ilt.frostserver.settings.annotation.DefaultValueInt;

/**
* Holds the settigs for the Keycloak Auth Provider.
*/
public class KeycloakSettings implements ConfigDefaults {

@DefaultValue("")
@SensitiveValue
public static final String TAG_KEYCLOAK_CONFIG = "keycloakConfig";

@DefaultValue("")
public static final String TAG_KEYCLOAK_CONFIG_FILE = "keycloakConfigFile";

/**
* The URL on the Keycloak server that can be used to download the Keycloak
* config file. Usually this URL is in the form of:
* https://keycloak.example.com/auth/realms/[realm]/clients-registrations/install/[clientId]
*/
@DefaultValue("")
public static final String TAG_KEYCLOAK_CONFIG_URL = "keycloakConfigUrl";

/**
* If the client has "access-type" set to "confidential" then a secret is
* required to download the configuration. This secret can be found in the
* configuration itself, in Keycloak.
*/
@DefaultValue("")
@SensitiveValue
public static final String TAG_KEYCLOAK_CONFIG_SECRET = "keycloakConfigSecret";

@DefaultValueInt(10)
public static final String TAG_MAX_CLIENTS_PER_USER = "maxClientsPerUser";

@DefaultValueBoolean(false)
public static final String TAG_REGISTER_USER_LOCALLY = "registerUserLocally";

@DefaultValue("USERS")
public static final String TAG_USER_TABLE = "userTable";

@DefaultValue("USER_NAME")
public static final String TAG_USERNAME_COLUMN = "usernameColumn";

@DefaultValueInt(MAX_PASSWORD_LENGTH)
public static final String TAG_MAX_PASSWORD_LENGTH = "maxPasswordLength";

@DefaultValueInt(MAX_USERNAME_LENGTH)
public static final String TAG_MAX_USERNAME_LENGTH = "maxUsernameLength";

@DefaultValue("de.fraunhofer.iosb.ilt.frostserver.auth.keycloak.UserRoleDecoderDflt")
public static final String TAG_USER_ROLE_DECODER_CLASS = "userRoleDecoderClass";

@DefaultValue("PT5M")
public static final String TAG_USER_CACHE_LIFETIME = "userCacheLifetime";

@DefaultValue("PT5S")
public static final String TAG_USER_CACHE_CLEANUP_INTERVAL = "userCacheCleanupInterval";

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
*/
package de.fraunhofer.iosb.ilt.frostserver.auth.keycloak;

import static de.fraunhofer.iosb.ilt.frostserver.auth.keycloak.KeycloakAuthProvider.TAG_USERNAME_COLUMN;
import static de.fraunhofer.iosb.ilt.frostserver.auth.keycloak.KeycloakAuthProvider.TAG_USER_TABLE;
import static de.fraunhofer.iosb.ilt.frostserver.auth.keycloak.KeycloakSettings.TAG_USERNAME_COLUMN;
import static de.fraunhofer.iosb.ilt.frostserver.auth.keycloak.KeycloakSettings.TAG_USER_TABLE;

import de.fraunhofer.iosb.ilt.frostserver.settings.CoreSettings;
import de.fraunhofer.iosb.ilt.frostserver.settings.Settings;
Expand All @@ -40,8 +40,8 @@ public class UserRoleDecoderDflt implements UserRoleDecoder {
@Override
public void init(CoreSettings coreSettings) {
Settings authSettings = coreSettings.getAuthSettings();
userTable = authSettings.get(TAG_USER_TABLE, KeycloakAuthProvider.class);
usernameColumn = authSettings.get(TAG_USERNAME_COLUMN, KeycloakAuthProvider.class);
userTable = authSettings.get(TAG_USER_TABLE, KeycloakSettings.class);
usernameColumn = authSettings.get(TAG_USERNAME_COLUMN, KeycloakSettings.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
*/
package de.fraunhofer.iosb.ilt.frostserver.auth.keycloak;

import static de.fraunhofer.iosb.ilt.frostserver.auth.keycloak.KeycloakAuthProvider.TAG_KEYCLOAK_CONFIG;
import static de.fraunhofer.iosb.ilt.frostserver.auth.keycloak.KeycloakAuthProvider.TAG_KEYCLOAK_CONFIG_FILE;
import static de.fraunhofer.iosb.ilt.frostserver.auth.keycloak.KeycloakAuthProvider.TAG_KEYCLOAK_CONFIG_SECRET;
import static de.fraunhofer.iosb.ilt.frostserver.auth.keycloak.KeycloakAuthProvider.TAG_KEYCLOAK_CONFIG_URL;
import static de.fraunhofer.iosb.ilt.frostserver.auth.keycloak.KeycloakSettings.TAG_KEYCLOAK_CONFIG;
import static de.fraunhofer.iosb.ilt.frostserver.auth.keycloak.KeycloakSettings.TAG_KEYCLOAK_CONFIG_FILE;
import static de.fraunhofer.iosb.ilt.frostserver.auth.keycloak.KeycloakSettings.TAG_KEYCLOAK_CONFIG_SECRET;
import static de.fraunhofer.iosb.ilt.frostserver.auth.keycloak.KeycloakSettings.TAG_KEYCLOAK_CONFIG_URL;

import de.fraunhofer.iosb.ilt.frostserver.settings.CoreSettings;
import de.fraunhofer.iosb.ilt.frostserver.settings.Settings;
Expand Down Expand Up @@ -78,7 +78,7 @@ public static interface MethodRoleMapper {
*/
public static String getKeycloakConfig(CoreSettings coreSettings) {
Settings authSettings = coreSettings.getAuthSettings();
String keycloakConfig = authSettings.get(TAG_KEYCLOAK_CONFIG, "");
String keycloakConfig = authSettings.get(TAG_KEYCLOAK_CONFIG, KeycloakSettings.class);
if (!StringHelper.isNullOrEmpty(keycloakConfig)) {
return keycloakConfig;
}
Expand All @@ -99,7 +99,7 @@ public static String getKeycloakConfig(CoreSettings coreSettings) {
* @return the contents of the config file.
*/
private static String getKeycloakConfigFromFile(Settings authSettings) {
String keycloakConfigFile = authSettings.get(TAG_KEYCLOAK_CONFIG_FILE, "");
String keycloakConfigFile = authSettings.get(TAG_KEYCLOAK_CONFIG_FILE, KeycloakSettings.class);
if (StringHelper.isNullOrEmpty(keycloakConfigFile)) {
return "";
}
Expand All @@ -120,11 +120,11 @@ private static String getKeycloakConfigFromFile(Settings authSettings) {
* @return the contents of the config file.
*/
private static String getKeycloakConfigFromServer(Settings authSettings) {
String keycloakConfigUrl = authSettings.get(TAG_KEYCLOAK_CONFIG_URL, "");
String keycloakConfigUrl = authSettings.get(TAG_KEYCLOAK_CONFIG_URL, KeycloakSettings.class);
if (StringHelper.isNullOrEmpty(keycloakConfigUrl)) {
return "";
}
String keycloakConfigSecret = authSettings.get(TAG_KEYCLOAK_CONFIG_SECRET, "");
String keycloakConfigSecret = authSettings.get(TAG_KEYCLOAK_CONFIG_SECRET, KeycloakSettings.class);

LOGGER.info("Fetching Keycloak config from server: {}", keycloakConfigUrl);
try (CloseableHttpClient client = HttpClients.createSystem()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static Object getFrom(final Map<String, Object> map, final List<String> p
return getFrom((Object) map, path);
}

public static Object getFrom(final ComplexValue cv, final List<String> path) {
public static Object getFrom(final ComplexValue<?> cv, final List<String> path) {
return getFrom((Object) cv, path);
}

Expand All @@ -83,7 +83,7 @@ private static Object getFrom(final Object mapOrList, final List<String> path) {
String key = path.get(idx);
if (currentEntry instanceof Map map) {
currentEntry = map.get(key);
} else if (currentEntry instanceof ComplexValue cv) {
} else if (currentEntry instanceof ComplexValue<?> cv) {
currentEntry = cv.getProperty(key);
} else if (currentEntry instanceof List list) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,31 +289,31 @@ public Property getProperty(String name) {
}

public EntityPropertyMain getEntityProperty(String name) {
Property property = propertiesByName.get(name);
Property<?> property = propertiesByName.get(name);
if (property instanceof EntityPropertyMain entityPropertyMain) {
return entityPropertyMain;
}
return null;
}

public NavigationPropertyMain getNavigationProperty(String name) {
Property property = propertiesByName.get(name);
Property<?> property = propertiesByName.get(name);
if (property instanceof NavigationPropertyMain navigationPropertyMain) {
return navigationPropertyMain;
}
return null;
}

public NavigationPropertyEntity getNavigationPropertyEntity(String name) {
Property property = propertiesByName.get(name);
Property<?> property = propertiesByName.get(name);
if (property instanceof NavigationPropertyEntity npe) {
return npe;
}
return null;
}

public NavigationPropertyEntitySet getNavigationPropertyEntitySet(String name) {
Property property = propertiesByName.get(name);
Property<?> property = propertiesByName.get(name);
if (property instanceof NavigationPropertyEntitySet npes) {
return npes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -937,8 +936,8 @@ private LinkData parseForRefWithId(ServiceRequest request, ResourcePath path, Pa
if (!targetUrl.startsWith(serviceRootUrl)) {
try {
// id is a relative url, resolve against the request url.
URL requestUrl = new URL(serviceRootUrl + '/' + versionUrl + request.getUrlPath());
targetUrl = new URL(requestUrl, targetUrl).toString();
URI requestUri = URI.create(serviceRootUrl + '/' + versionUrl + request.getUrlPath());
targetUrl = requestUri.resolve(targetUrl).toURL().toString();
} catch (MalformedURLException ex) {
return LinkData.error("Failed to parse URL in $id: " + ex.getMessage());
}
Expand Down
Loading

0 comments on commit 99ce15a

Please sign in to comment.