Search in sources :

Example 11 with WebXmlAsset

use of org.wildfly.swarm.undertow.descriptors.WebXmlAsset in project wildfly-swarm by wildfly-swarm.

the class WebXmlAssetTest method testLoginAuthMethod.

@Test
public void testLoginAuthMethod() throws Exception {
    WebXmlAsset asset = new WebXmlAsset();
    assertThat(asset.getLoginRealm("keycloak")).isNull();
    asset.setLoginConfig("keycloak", "myRealm");
    String realm = asset.getLoginRealm("keycloak");
    assertThat(realm).isNotNull();
    assertThat(realm).isNotEmpty();
    assertThat(realm).isEqualTo("myRealm");
}
Also used : WebXmlAsset(org.wildfly.swarm.undertow.descriptors.WebXmlAsset) Test(org.junit.Test)

Example 12 with WebXmlAsset

use of org.wildfly.swarm.undertow.descriptors.WebXmlAsset in project wildfly-swarm by wildfly-swarm.

the class WebXmlAssetTest method testSecurityConstraintPermitAll.

@Test
public void testSecurityConstraintPermitAll() throws IOException {
    WebXmlAsset asset = new WebXmlAsset();
    asset.protect().permitAll();
    List<SecurityConstraint> constraints = asset.allConstraints();
    assertThat(constraints).hasSize(1);
    SecurityConstraint sc = constraints.get(0);
    assertThat(sc.isPermitAll()).isEqualTo(true);
    InputStreamReader reader = new InputStreamReader(asset.openStream());
    BufferedReader buffered = new BufferedReader(reader);
    StringBuilder tmp = new StringBuilder();
    String line = buffered.readLine();
    while (line != null) {
        tmp.append(line);
        line = buffered.readLine();
    }
    assertThat(tmp.toString()).contains("security-constraint");
    assertThat(tmp.toString()).doesNotContain("auth-constraint");
}
Also used : InputStreamReader(java.io.InputStreamReader) WebXmlAsset(org.wildfly.swarm.undertow.descriptors.WebXmlAsset) BufferedReader(java.io.BufferedReader) SecurityConstraint(org.wildfly.swarm.undertow.descriptors.SecurityConstraint) Test(org.junit.Test)

Example 13 with WebXmlAsset

use of org.wildfly.swarm.undertow.descriptors.WebXmlAsset in project wildfly-swarm by wildfly-swarm.

the class WebXmlAssetTest method testUnsetContextParamName.

@Test
public void testUnsetContextParamName() throws Exception {
    WebXmlAsset asset = new WebXmlAsset();
    assertThat(asset.getContextParam("my-param")).isNull();
}
Also used : WebXmlAsset(org.wildfly.swarm.undertow.descriptors.WebXmlAsset) Test(org.junit.Test)

Example 14 with WebXmlAsset

use of org.wildfly.swarm.undertow.descriptors.WebXmlAsset in project wildfly-swarm by wildfly-swarm.

the class HttpSecurityPreparer method process.

@SuppressWarnings("unchecked")
@Override
public void process() {
    if (deploymentConfigs == null || deploymentConfigs.isEmpty()) {
        return;
    }
    // find a matching archive declaration
    Optional<String> match = deploymentConfigs.keySet().stream().filter(c -> archive.getName().equals(c)).findFirst();
    if (!match.isPresent()) {
        // no matching archive
        return;
    }
    Map<String, Object> matchingConfig = (Map<String, Object>) deploymentConfigs.get(match.get());
    if (!matchingConfig.containsKey("web")) {
        // missing web configuration
        return;
    }
    Map<String, Object> deploymentConfig = (Map<String, Object>) matchingConfig.get("web");
    WARArchive war = archive.as(WARArchive.class);
    WebXmlAsset webXml = war.findWebXmlAsset();
    JBossWebAsset jbossWeb = war.findJbossWebAsset();
    // login-config
    Map<String, Object> loginConfig = (Map<String, Object>) deploymentConfig.get("login-config");
    if (loginConfig != null) {
        String authMethod = (String) loginConfig.getOrDefault("auth-method", "NONE");
        // Setup login-config
        webXml.setLoginConfig(authMethod, "ignored");
        // security domain
        if (loginConfig.containsKey("security-domain")) {
            jbossWeb.setSecurityDomain((String) loginConfig.get("security-domain"));
        }
        // form login
        if (loginConfig.containsKey("form-login-config")) {
            Map<String, Object> formLoginConfig = (Map<String, Object>) loginConfig.get("form-login-config");
            webXml.setFormLoginConfig("Security Realm", (String) formLoginConfig.get("form-login-page"), (String) formLoginConfig.get("form-error-page"));
        }
    }
    // security constraints
    List<Map<String, Object>> securityConstraints = (List<Map<String, Object>>) deploymentConfig.getOrDefault("security-constraints", Collections.EMPTY_LIST);
    for (Map<String, Object> sc : securityConstraints) {
        SecurityConstraint securityConstraint = webXml.protect((String) sc.getOrDefault("url-pattern", "/*"));
        ((List<String>) sc.getOrDefault("methods", Collections.emptyList())).forEach(securityConstraint::withMethod);
        ((List<String>) sc.getOrDefault("roles", Collections.emptyList())).forEach(securityConstraint::withRole);
    }
}
Also used : DeploymentProcessor(org.wildfly.swarm.spi.api.DeploymentProcessor) DeploymentScoped(org.wildfly.swarm.spi.runtime.annotations.DeploymentScoped) Logger(org.jboss.logging.Logger) JBossWebAsset(org.wildfly.swarm.undertow.descriptors.JBossWebAsset) Archive(org.jboss.shrinkwrap.api.Archive) WebXmlAsset(org.wildfly.swarm.undertow.descriptors.WebXmlAsset) SecurityConstraint(org.wildfly.swarm.undertow.descriptors.SecurityConstraint) Inject(javax.inject.Inject) List(java.util.List) Configurable(org.wildfly.swarm.spi.api.annotations.Configurable) Map(java.util.Map) WARArchive(org.wildfly.swarm.undertow.WARArchive) AttributeDocumentation(org.wildfly.swarm.config.runtime.AttributeDocumentation) Optional(java.util.Optional) Collections(java.util.Collections) JBossWebAsset(org.wildfly.swarm.undertow.descriptors.JBossWebAsset) WebXmlAsset(org.wildfly.swarm.undertow.descriptors.WebXmlAsset) WARArchive(org.wildfly.swarm.undertow.WARArchive) SecurityConstraint(org.wildfly.swarm.undertow.descriptors.SecurityConstraint) List(java.util.List) Map(java.util.Map)

Aggregations

WebXmlAsset (org.wildfly.swarm.undertow.descriptors.WebXmlAsset)14 Test (org.junit.Test)9 WARArchive (org.wildfly.swarm.undertow.WARArchive)5 JBossWebAsset (org.wildfly.swarm.undertow.descriptors.JBossWebAsset)2 SecurityConstraint (org.wildfly.swarm.undertow.descriptors.SecurityConstraint)2 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Inject (javax.inject.Inject)1 AnnotationInstance (org.jboss.jandex.AnnotationInstance)1 AnnotationValue (org.jboss.jandex.AnnotationValue)1 ClassInfo (org.jboss.jandex.ClassInfo)1 MethodInfo (org.jboss.jandex.MethodInfo)1 Logger (org.jboss.logging.Logger)1 Archive (org.jboss.shrinkwrap.api.Archive)1 StringAsset (org.jboss.shrinkwrap.api.asset.StringAsset)1 AttributeDocumentation (org.wildfly.swarm.config.runtime.AttributeDocumentation)1