Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run ATH with CSP (report only) #1749

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ if (needSplittingFromWorkspace) {
}

def axes = [
jenkinsVersions: ['lts', 'latest'],
jenkinsVersions: ['latest'],
platforms: ['linux'],
jdks: [17, 21],
browsers: ['firefox'],
Expand Down
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,10 @@ and
</systemPropertyVariables>
<!-- SUREFIRE-1588 workaround; too late for systemProperties: -->
<argLine>-Djdk.net.URLClassPath.disableClassPathURLCheck=true</argLine>
<environmentVariables>
<!-- Use local version of the plugin over the released one ATH would otherwise use -->
<LOCAL_JARS>target/maven-plugin.hpi</LOCAL_JARS>
</environmentVariables>
</configuration>
</plugin>
<plugin>
Expand Down Expand Up @@ -810,6 +814,13 @@ and
<version>${jenkins.version}</version>
<type>war</type>
</artifactItem>
<!-- TODO https://github.com/jenkinsci/maven-plugin/pull/345 -->
<artifactItem>
<groupId>org.jenkins-ci.main</groupId>
<artifactId>maven-plugin</artifactId>
<version>3.24-rc2459.92567fa_a_4b_65</version>
<type>hpi</type>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}</outputDirectory>
<stripVersion>true</stripVersion>
Expand Down
45 changes: 45 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,45 @@
package org.jenkinsci.test.acceptance.junit;

import com.google.inject.Inject;
import com.google.inject.Injector;
import java.util.List;
import java.util.logging.Logger;
import org.jenkinsci.test.acceptance.plugins.csp.ContentSecurityPolicyReport;
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 {

private static final Logger LOGGER = Logger.getLogger(CspRule.class.getName());

@Inject
Injector injector;

@Override
public Statement apply(final Statement base, final Description d) {
return new Statement() {
private Jenkins jenkins;

@Override
public void evaluate() throws Throwable {
jenkins = injector.getInstance(Jenkins.class);
final PluginSpec plugin = new PluginSpec("csp");
LOGGER.info("Installing plugin for test: " + plugin);
jenkins.getPluginManager().installPlugins(plugin);
try {
base.evaluate();
} finally {
ContentSecurityPolicyReport csp = new ContentSecurityPolicyReport(jenkins);
List<String> lines = csp.getReport();
if (lines.size() > 2) {
throw new AssertionError(String.join("\n", csp.getReport()));
}
}
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.jenkinsci.test.acceptance.plugins.csp;

import java.util.ArrayList;
import java.util.List;
import org.jenkinsci.test.acceptance.po.Jenkins;
import org.jenkinsci.test.acceptance.po.PageObject;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;

public class ContentSecurityPolicyReport extends PageObject {
public ContentSecurityPolicyReport(Jenkins context) {
super(context, context.url("content-security-policy-reports/"));
}

public List<String> getReport() {
List<String> lines = new ArrayList<>();
WebElement table;
try {
open();
table = find(By.className("bigtable"));
} catch (NoSuchElementException e) {
try {
getContext().getJenkins().logout();
} catch (Exception ignored) {
// Ignore
}
getContext().getJenkins().login().doLogin("admin");
open();
table = find(By.className("bigtable"));
}
List<WebElement> headers = table.findElements(By.tagName("th"));
StringBuilder sb = new StringBuilder();
for (WebElement header : headers) {
sb.append(header.getText()).append("\t");
}
lines.add(sb.toString());
sb = new StringBuilder();
List<WebElement> rows = table.findElements(By.tagName("tr"));
for (WebElement row : rows) {
List<WebElement> cells = row.findElements(By.tagName("td"));
for (WebElement cell : cells) {
sb.append(cell.getText()).append("\t");
}
lines.add(sb.toString());
sb = new StringBuilder();
}
return lines;
}
}
Loading