[KEYCLOAK-18302] Fix redirect-rewrite-rule JSON name

This commit is contained in:
Stefan Guilhen 2021-07-07 00:11:23 -03:00 committed by Hynek Mlnařík
parent 3b3a61dfba
commit 8934bc82ba
2 changed files with 51 additions and 15 deletions

View file

@ -41,7 +41,7 @@ public final class KeycloakAdapterConfigService {
private static final String CREDENTIALS_JSON_NAME = "credentials";
private static final String REDIRECT_REWRITE_RULE_JSON_NAME = "redirect-rewrite-rule";
private static final String REDIRECT_REWRITE_RULE_JSON_NAME = "redirect-rewrite-rules";
private static final KeycloakAdapterConfigService INSTANCE = new KeycloakAdapterConfigService();
@ -147,21 +147,8 @@ public final class KeycloakAdapterConfigService {
if (!redirectRewritesRules.isDefined()) {
redirectRewritesRules = new ModelNode();
}
String redirectRewriteRuleName = redirectRewriteRule(operation);
if (!redirectRewriteRuleName.contains(".")) {
redirectRewritesRules.get(redirectRewriteRuleName).set(model.get("value").asString());
} else {
String[] parts = redirectRewriteRuleName.split("\\.");
String provider = parts[0];
String property = parts[1];
ModelNode redirectRewriteRule = redirectRewritesRules.get(provider);
if (!redirectRewriteRule.isDefined()) {
redirectRewriteRule = new ModelNode();
}
redirectRewriteRule.get(property).set(model.get("value").asString());
redirectRewritesRules.set(provider, redirectRewriteRule);
}
redirectRewritesRules.get(redirectRewriteRuleName).set(model.get("value").asString());
ModelNode deployment = this.secureDeployments.get(deploymentNameFromOp(operation));
deployment.get(REDIRECT_REWRITE_RULE_JSON_NAME).set(redirectRewritesRules);

View file

@ -21,9 +21,14 @@ import org.jboss.as.controller.PathElement;
import org.jboss.as.controller.descriptions.ModelDescriptionConstants;
import org.jboss.as.subsystem.test.AbstractSubsystemBaseTest;
import org.jboss.dmr.ModelNode;
import org.junit.Assert;
import org.junit.Test;
import org.keycloak.adapters.KeycloakDeploymentBuilder;
import org.keycloak.representations.adapters.config.AdapterConfig;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Map;
/**
@ -106,4 +111,48 @@ public class SubsystemParsingTestCase extends AbstractSubsystemBaseTest {
"/subsystem-templates/keycloak-adapter.xml"
};
}
/**
* Tests a subsystem configuration that contains a {@code redirect-rewrite-rule}, checking that the resulting JSON
* can be properly used to create an {@link AdapterConfig}.
*
* Added as part of the fix for {@code KEYCLOAK-18302}.
*/
@Test
public void testJsonFromRedirectRewriteRuleConfiguration() {
KeycloakAdapterConfigService service = KeycloakAdapterConfigService.getInstance();
// add a secure deployment with a redirect-rewrite-rule
PathAddress addr = PathAddress.pathAddress(PathElement.pathElement("subsystem", "keycloak"), PathElement.pathElement("secure-deployment", "foo"));
ModelNode deploymentOp = new ModelNode();
deploymentOp.get(ModelDescriptionConstants.OP_ADDR).set(addr.toModelNode());
ModelNode deployment = new ModelNode();
deployment.get("realm").set("demo");
deployment.get("resource").set("customer-portal");
service.addSecureDeployment(deploymentOp, deployment, false);
this.addRedirectRewriteRule(addr, service, "^/wsmaster/api/(.*)$", "api/$1");
// get the subsystem config as JSON
String jsonConfig = service.getJSON("foo");
// attempt to create an adapter config instance from the subsystem JSON config
AdapterConfig config = KeycloakDeploymentBuilder.loadAdapterConfig(new ByteArrayInputStream(jsonConfig.getBytes()));
Assert.assertNotNull(config);
// assert that the config has the configured rule
Map<String, String> redirectRewriteRules = config.getRedirectRewriteRules();
Assert.assertNotNull(redirectRewriteRules);
Map.Entry<String, String> entry = redirectRewriteRules.entrySet().iterator().next();
Assert.assertEquals("^/wsmaster/api/(.*)$", entry.getKey());
Assert.assertEquals("api/$1", entry.getValue());
}
private void addRedirectRewriteRule(PathAddress parent, KeycloakAdapterConfigService service, String key, String value) {
PathAddress redirectRewriteAddr = PathAddress.pathAddress(parent, PathElement.pathElement("redirect-rewrite-rule", key));
ModelNode redirectRewriteOp = new ModelNode();
redirectRewriteOp.get(ModelDescriptionConstants.OP_ADDR).set(redirectRewriteAddr.toModelNode());
ModelNode rule = new ModelNode();
rule.get("value").set(value);
service.addRedirectRewriteRule(redirectRewriteOp, rule);
}
}