Skip to content

Commit

Permalink
Run ATH with CSP when csp.rule is defined
Browse files Browse the repository at this point in the history
  • Loading branch information
basil committed Oct 25, 2024
1 parent c0d8f26 commit c45889f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ and
<configuration>
<reuseForks>false</reuseForks>
<systemPropertyVariables>
<csp.rule>true</csp.rule>
<!-- allow large xpaths https://www.oracle.com/java/technologies/javase/11-0-15-relnotes.html#JDK-8270504 -->
<jdk.xml.xpathExprOpLimit>200</jdk.xml.xpathExprOpLimit>
<mavenRepoPath>${settings.localRepository}</mavenRepoPath>
Expand Down Expand Up @@ -825,6 +826,19 @@ and
<properties>
<jenkins.version>2.462.3</jenkins.version>
</properties>
<!-- TOOD remove when LTS line is at 2.481 or later -->
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<csp.rule>false</csp.rule>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
50 changes: 50 additions & 0 deletions src/main/java/org/jenkinsci/test/acceptance/junit/CspRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.jenkinsci.test.acceptance.junit;

import com.google.inject.Inject;
import com.google.inject.Injector;
import org.jenkinsci.test.acceptance.po.GlobalSecurityConfig;
import org.jenkinsci.test.acceptance.po.Jenkins;
import org.jenkinsci.test.acceptance.update_center.PluginSpec;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

@GlobalRule
public final class CspRule implements TestRule {

@Inject
private Injector injector;

@Override
public Statement apply(final Statement base, final Description d) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
if (isEnabled()
&& d.getAnnotation(WithInstallWizard.class) == null
&& d.getTestClass().getAnnotation(WithInstallWizard.class) == null) {
Jenkins jenkins = injector.getInstance(Jenkins.class);

PluginSpec plugin = new PluginSpec("csp");
jenkins.getPluginManager().installPlugins(plugin);

GlobalSecurityConfig security = new GlobalSecurityConfig(jenkins);
security.open();
security.disableCspReportOnly();
security.save();
}
base.evaluate();
}

private static boolean isEnabled() {
if (System.getProperty("csp.rule") == null) {
return false;
}
if (System.getProperty("csp.rule").isEmpty()) {
return true;
}
return Boolean.getBoolean("csp.rule");
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.net.URL;
import org.jenkinsci.test.acceptance.plugins.authorize_project.BuildAccessControl;
import org.jenkinsci.test.acceptance.plugins.git_client.ssh_host_key_verification.SshHostKeyVerificationStrategy;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;

Expand Down Expand Up @@ -83,6 +84,10 @@ private void maybeCheckUseSecurity() {
}
}

public void disableCspReportOnly() {
control(By.name("_.reportOnly")).uncheck();
}

public <T extends BuildAccessControl> T addBuildAccessControl(final Class<T> type) {
final String path =
createPageArea("/jenkins-security-QueueItemAuthenticatorConfiguration/authenticators", () -> control(
Expand Down

0 comments on commit c45889f

Please # to comment.