forked from keycloak/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SAML javascript protocol mapper: disable uploading scripts through ad…
…min console by default (keycloak#14293) Closes keycloak#14292
- Loading branch information
Showing
9 changed files
with
269 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...es/src/main/java/org/keycloak/protocol/saml/mappers/DeployedScriptSAMLProtocolMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.keycloak.protocol.saml.mappers; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.keycloak.common.Profile; | ||
import org.keycloak.models.ProtocolMapperModel; | ||
import org.keycloak.provider.ProviderConfigProperty; | ||
import org.keycloak.representations.provider.ScriptProviderMetadata; | ||
|
||
/** | ||
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a> | ||
*/ | ||
public class DeployedScriptSAMLProtocolMapper extends ScriptBasedMapper { | ||
|
||
protected ScriptProviderMetadata metadata; | ||
|
||
public DeployedScriptSAMLProtocolMapper(ScriptProviderMetadata metadata) { | ||
this.metadata = metadata; | ||
} | ||
|
||
public DeployedScriptSAMLProtocolMapper() { | ||
// for reflection | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return metadata.getId(); | ||
} | ||
|
||
@Override | ||
public String getDisplayType() { | ||
return metadata.getName(); | ||
} | ||
|
||
@Override | ||
public String getHelpText() { | ||
return metadata.getDescription(); | ||
} | ||
|
||
@Override | ||
protected String getScriptCode(ProtocolMapperModel mapperModel) { | ||
return metadata.getCode(); | ||
} | ||
|
||
public List<ProviderConfigProperty> getConfigProperties() { | ||
return super.getConfigProperties().stream() | ||
.filter(providerConfigProperty -> !ProviderConfigProperty.SCRIPT_TYPE.equals(providerConfigProperty.getName())) // filter "script" property | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public void setMetadata(ScriptProviderMetadata metadata) { | ||
this.metadata = metadata; | ||
} | ||
|
||
public ScriptProviderMetadata getMetadata() { | ||
return metadata; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
.../tests/base/src/test/java/org/keycloak/testsuite/script/DeployedSAMLScriptMapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
package org.keycloak.testsuite.script; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.stream.Stream; | ||
|
||
import javax.ws.rs.core.Response; | ||
|
||
import org.jboss.arquillian.container.test.api.Deployer; | ||
import org.jboss.arquillian.container.test.api.Deployment; | ||
import org.jboss.arquillian.container.test.api.TargetsContainer; | ||
import org.jboss.arquillian.test.api.ArquillianResource; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.keycloak.common.Profile; | ||
import org.keycloak.dom.saml.v2.assertion.AssertionType; | ||
import org.keycloak.dom.saml.v2.assertion.AttributeType; | ||
import org.keycloak.protocol.saml.SamlProtocol; | ||
import org.keycloak.protocol.saml.mappers.AttributeStatementHelper; | ||
import org.keycloak.protocol.saml.mappers.ScriptBasedMapper; | ||
import org.keycloak.provider.ProviderConfigProperty; | ||
import org.keycloak.representations.idm.ProtocolMapperRepresentation; | ||
import org.keycloak.representations.provider.ScriptProviderDescriptor; | ||
import org.keycloak.saml.common.constants.JBossSAMLURIConstants; | ||
import org.keycloak.saml.processing.core.saml.v2.common.SAMLDocumentHolder; | ||
import org.keycloak.testsuite.arquillian.annotation.EnableFeature; | ||
import org.keycloak.testsuite.arquillian.annotation.EnableFeatures; | ||
import org.keycloak.testsuite.saml.AbstractSamlTest; | ||
import org.keycloak.testsuite.saml.RoleMapperTest; | ||
import org.keycloak.testsuite.updaters.ClientAttributeUpdater; | ||
import org.keycloak.testsuite.updaters.ProtocolMappersUpdater; | ||
import org.keycloak.testsuite.util.ContainerAssume; | ||
import org.keycloak.testsuite.util.Matchers; | ||
import org.keycloak.testsuite.util.SamlClient; | ||
import org.keycloak.testsuite.util.SamlClientBuilder; | ||
import org.keycloak.util.JsonSerialization; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertThat; | ||
import static org.keycloak.common.Profile.Feature.SCRIPTS; | ||
import static org.keycloak.testsuite.arquillian.DeploymentTargetModifier.AUTH_SERVER_CURRENT; | ||
import static org.keycloak.testsuite.saml.RoleMapperTest.createSamlProtocolMapper; | ||
import static org.keycloak.testsuite.util.SamlStreams.assertionsUnencrypted; | ||
import static org.keycloak.testsuite.util.SamlStreams.attributeStatements; | ||
import static org.keycloak.testsuite.util.SamlStreams.attributesUnecrypted; | ||
|
||
/** | ||
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a> | ||
*/ | ||
public class DeployedSAMLScriptMapperTest extends AbstractSamlTest { | ||
|
||
private static final String SCRIPT_DEPLOYMENT_NAME = "scripts.jar"; | ||
|
||
private ClientAttributeUpdater cau; | ||
private ProtocolMappersUpdater pmu; | ||
|
||
@Deployment(name = SCRIPT_DEPLOYMENT_NAME, managed = false, testable = false) | ||
@TargetsContainer(AUTH_SERVER_CURRENT) | ||
public static JavaArchive deploy() throws IOException { | ||
ScriptProviderDescriptor representation = new ScriptProviderDescriptor(); | ||
|
||
representation.addSAMLMapper("My Mapper", "mapper-a.js"); | ||
|
||
return ShrinkWrap.create(JavaArchive.class, SCRIPT_DEPLOYMENT_NAME) | ||
.addAsManifestResource(new StringAsset(JsonSerialization.writeValueAsPrettyString(representation)), | ||
"keycloak-scripts.json") | ||
.addAsResource("scripts/mapper-example.js", "mapper-a.js"); | ||
} | ||
|
||
@BeforeClass | ||
public static void verifyEnvironment() { | ||
ContainerAssume.assumeNotAuthServerUndertow(); | ||
} | ||
|
||
@ArquillianResource | ||
private Deployer deployer; | ||
|
||
@Before | ||
public void deployScripts() throws Exception { | ||
deployer.deploy(SCRIPT_DEPLOYMENT_NAME); | ||
reconnectAdminClient(); | ||
} | ||
|
||
@Before | ||
public void cleanMappersAndScopes() { | ||
this.cau = ClientAttributeUpdater.forClient(adminClient, REALM_NAME, SAML_CLIENT_ID_EMPLOYEE_2) | ||
.setDefaultClientScopes(Collections.EMPTY_LIST) | ||
.update(); | ||
this.pmu = cau.protocolMappers() | ||
.clear() | ||
.update(); | ||
|
||
getCleanup(REALM_NAME) | ||
.addCleanup(this.cau) | ||
.addCleanup(this.pmu); | ||
} | ||
|
||
@After | ||
public void onAfter() throws Exception { | ||
deployer.undeploy(SCRIPT_DEPLOYMENT_NAME); | ||
reconnectAdminClient(); | ||
} | ||
|
||
@Test | ||
public void testScriptMapperNotAvailableThroughAdminRest() { | ||
assertFalse(adminClient.serverInfo().getInfo().getProtocolMapperTypes().get(SamlProtocol.LOGIN_PROTOCOL).stream() | ||
.anyMatch( | ||
mapper -> ScriptBasedMapper.PROVIDER_ID.equals(mapper.getId()))); | ||
|
||
// Doublecheck not possible to create mapper through admin REST | ||
ProtocolMapperRepresentation mapperRep = createSamlProtocolMapper(ScriptBasedMapper.PROVIDER_ID, | ||
ProviderConfigProperty.SCRIPT_TYPE, "'hello_' + user.username", | ||
AttributeStatementHelper.SAML_ATTRIBUTE_NAMEFORMAT, AttributeStatementHelper.BASIC, | ||
AttributeStatementHelper.SAML_ATTRIBUTE_NAME, "SCRIPT_ATTRIBUTE" | ||
); | ||
|
||
Response response = pmu.getResource().createMapper(mapperRep); | ||
Assert.assertEquals(404, response.getStatus()); | ||
response.close(); | ||
} | ||
|
||
|
||
@Test | ||
@EnableFeature(value = SCRIPTS, skipRestart = true, executeAsLast = false) | ||
public void testScriptMappingThroughServerDeploy() { | ||
// ScriptBasedMapper still not available even if SCRIPTS feature is enabled | ||
testScriptMapperNotAvailableThroughAdminRest(); | ||
|
||
pmu.add( | ||
createSamlProtocolMapper("script-mapper-a.js", | ||
AttributeStatementHelper.SAML_ATTRIBUTE_NAMEFORMAT, AttributeStatementHelper.BASIC, | ||
AttributeStatementHelper.SAML_ATTRIBUTE_NAME, "SCRIPT_ATTRIBUTE" | ||
) | ||
).update(); | ||
|
||
assertLoginSuccessWithAttributeAvailable(); | ||
} | ||
|
||
|
||
private void assertLoginSuccessWithAttributeAvailable() { | ||
SAMLDocumentHolder samlResponse = new SamlClientBuilder() | ||
.authnRequest(getAuthServerSamlEndpoint(REALM_NAME), SAML_CLIENT_ID_EMPLOYEE_2, RoleMapperTest.SAML_ASSERTION_CONSUMER_URL_EMPLOYEE_2, SamlClient.Binding.POST) | ||
.build() | ||
.login().user(bburkeUser).build() | ||
.getSamlResponse(SamlClient.Binding.POST); | ||
|
||
assertThat(samlResponse.getSamlObject(), Matchers.isSamlResponse(JBossSAMLURIConstants.STATUS_SUCCESS)); | ||
|
||
Stream<AssertionType> assertions = assertionsUnencrypted(samlResponse.getSamlObject()); | ||
Stream<AttributeType> attributes = attributesUnecrypted(attributeStatements(assertions)); | ||
String scriptAttrValue = attributes | ||
.filter(attribute -> "SCRIPT_ATTRIBUTE".equals(attribute.getName())) | ||
.map(attribute -> attribute.getAttributeValue().get(0).toString()) | ||
.findFirst().orElseThrow(() -> new AssertionError("Attribute SCRIPT_ATTRIBUTE was not available in SAML assertion")); | ||
|
||
Assert.assertEquals("hello_bburke", scriptAttrValue); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.