Allow resoving theme resources from flat classpath (#10989)

Closes #10951
This commit is contained in:
Pedro Igor 2022-04-05 04:16:20 -03:00 committed by GitHub
parent e00c8d03ae
commit 2b5d68d645
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 81 additions and 6 deletions

View file

@ -127,11 +127,14 @@ import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.vertx.http.deployment.FilterBuildItem;
import org.keycloak.quarkus.runtime.storage.database.jpa.NamedJpaConnectionProviderFactory;
import org.keycloak.quarkus.runtime.themes.FlatClasspathThemeResourceProviderFactory;
import org.keycloak.representations.provider.ScriptProviderDescriptor;
import org.keycloak.representations.provider.ScriptProviderMetadata;
import org.keycloak.quarkus.runtime.integration.web.NotFoundHandler;
import org.keycloak.services.ServicesLogger;
import org.keycloak.theme.ClasspathThemeResourceProviderFactory;
import org.keycloak.theme.FolderThemeProviderFactory;
import org.keycloak.theme.ThemeResourceSpi;
import org.keycloak.transaction.JBossJtaTransactionManagerLookup;
import org.keycloak.quarkus.runtime.Environment;
import org.keycloak.url.DefaultHostnameProviderFactory;
@ -160,7 +163,8 @@ class KeycloakProcessor {
FixedHostnameProviderFactory.class,
RequestHostnameProviderFactory.class,
FilesPlainTextVaultProviderFactory.class,
BlacklistPasswordPolicyProviderFactory.class);
BlacklistPasswordPolicyProviderFactory.class,
ClasspathThemeResourceProviderFactory.class);
static {
DEPLOYEABLE_SCRIPT_PROVIDERS.put(AUTHENTICATORS, KeycloakProcessor::registerScriptAuthenticator);
@ -288,6 +292,10 @@ class KeycloakProcessor {
if (spi instanceof JpaConnectionSpi) {
configureUserDefinedPersistenceUnits(descriptors, factories, preConfiguredProviders, spi);
}
if (spi instanceof ThemeResourceSpi) {
configureThemeResourceProviders(factories, spi);
}
}
recorder.configSessionFactory(factories, defaultProviders, preConfiguredProviders, Environment.isRebuild());
@ -295,6 +303,20 @@ class KeycloakProcessor {
return new KeycloakSessionFactoryPreInitBuildItem();
}
private void configureThemeResourceProviders(Map<Spi, Map<Class<? extends Provider>, Map<String, Class<? extends ProviderFactory>>>> factories, Spi spi) {
try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Enumeration<URL> resources = classLoader.getResources(FlatClasspathThemeResourceProviderFactory.THEME_RESOURCES);
if (resources.hasMoreElements()) {
// make sure theme resources are loaded using a flat classpath. if no resources are available the provider is not registered
factories.computeIfAbsent(spi, key -> new HashMap<>()).computeIfAbsent(spi.getProviderClass(), aClass -> new HashMap<>()).put(FlatClasspathThemeResourceProviderFactory.ID, FlatClasspathThemeResourceProviderFactory.class);
}
} catch (Exception e) {
throw new RuntimeException("Failed to install default theme resource provider", e);
}
}
private void configureUserDefinedPersistenceUnits(List<PersistenceXmlDescriptorBuildItem> descriptors,
Map<Spi, Map<Class<? extends Provider>, Map<String, Class<? extends ProviderFactory>>>> factories,
Map<String, ProviderFactory> preConfiguredProviders, Spi spi) {

View file

@ -0,0 +1,49 @@
/*
* Copyright 2022 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.keycloak.quarkus.runtime.themes;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import org.keycloak.theme.ClasspathThemeResourceProviderFactory;
public class FlatClasspathThemeResourceProviderFactory extends ClasspathThemeResourceProviderFactory {
public static final String ID = "flat-classpath";
@Override
public InputStream getResourceAsStream(String path) throws IOException {
Enumeration<URL> resources = classLoader.getResources(THEME_RESOURCES_RESOURCES);
while (resources.hasMoreElements()) {
InputStream is = getResourceAsStream(path, resources.nextElement());
if (is != null) {
return is;
}
}
return null;
}
@Override
public String getId() {
return ID;
}
}

View file

@ -15,12 +15,13 @@ import org.keycloak.models.KeycloakSessionFactory;
public class ClasspathThemeResourceProviderFactory implements ThemeResourceProviderFactory, ThemeResourceProvider {
public static final String THEME_RESOURCES_TEMPLATES = "theme-resources/templates/";
public static final String THEME_RESOURCES_RESOURCES = "theme-resources/resources/";
public static final String THEME_RESOURCES_MESSAGES = "theme-resources/messages/";
public static final String THEME_RESOURCES = "theme-resources";
public static final String THEME_RESOURCES_TEMPLATES = THEME_RESOURCES + "/templates/";
public static final String THEME_RESOURCES_RESOURCES = THEME_RESOURCES + "/resources/";
public static final String THEME_RESOURCES_MESSAGES = THEME_RESOURCES + "/messages/";
private final String id;
private final ClassLoader classLoader;
protected final ClassLoader classLoader;
public ClasspathThemeResourceProviderFactory() {
this("classpath", Thread.currentThread().getContextClassLoader());
@ -43,7 +44,10 @@ public class ClasspathThemeResourceProviderFactory implements ThemeResourceProvi
@Override
public InputStream getResourceAsStream(String path) throws IOException {
final URL rootResourceURL = classLoader.getResource(THEME_RESOURCES_RESOURCES);
return getResourceAsStream(path, classLoader.getResource(THEME_RESOURCES_RESOURCES));
}
protected InputStream getResourceAsStream(String path, URL rootResourceURL) throws IOException {
if (rootResourceURL == null) {
return null;
}