How to publish CodeNarc Reports in jenkins Pipeline? [duplicate] - jenkins

I have a strange problem with the Jenkins HTML Publisher plugin, wherein all the fancy CSS I have added to the report is stripped out when viewed in Jenkins. If I download the report to local, I am able to see the CSS formatting. Is there a setting in Jenkins which allows CSS to be viewed?
My HTML Publisher Settings in Jenkins:
My Report Page when displayed in Jenkins :
My Report Page when displayed in Local :

Figured out the issue. Sharing it here for other users.
CSS is stripped out because of the Content Security Policy in Jenkins. (https://wiki.jenkins-ci.org/display/JENKINS/Configuring+Content+Security+Policy)
The default rule is set to:
sandbox; default-src 'none'; img-src 'self'; style-src 'self';
This rule set results in the following:
No JavaScript allowed at all
No plugins (object/embed) allowed
No inline CSS, or CSS from other sites allowed
No images from other
sites allowed
No frames allowed
No web fonts allowed
No XHR/AJAX allowed, etc.
To relax this rule, go to
Manage Jenkins->
Manage Nodes->
Click settings(gear icon)->
click Script console on left and type in the following command:
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")
and Press Run. If you see the output as 'Result:' below "Result" header then the protection disabled. Re-Run your build and you can see that the new HTML files archived will have the CSS enabled.

In CentOs, to enable images in html report
sudo vi /etc/sysconfig/jenkins
set following in JENKINS_JAVA_OPTION
JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true -Dhudson.model.DirectoryBrowserSupport.CSP=\"default-src 'self' 'unsafe-inline' 'unsafe-eval'; img-src 'self' 'unsafe-inline' data:;\""
This will work even after restarting jenkins' server.
Directive
default-src: The default-src is the default policy for loading content such as JavaScript, Images, CSS, Fonts, AJAX requests, Frames, HTML5 Media
img-src: Defines valid sources of images.
Source Value
' self ' - Allows loading resources from the same origin (same scheme, host and port).
Usage : default-src 'self'
' unsafe-inline ' - Allows use of inline source elements such as style attribute, onclick, or script tag bodies (depends on the context of the source it is applied to) and javascript: URIs.
Usage : default-src 'unsafe-inline'
' unsafe-eval ' - Allows unsafe dynamic code evaluation such as JavaScript eval()
Usage : default-src 'unsafe-eval'
data: - Allows loading resources via the data scheme (eg Base64 encoded images).
Usage : img-src 'self' data:
Please refer more about content security policy here

Go to “Manage Jenkins” -> “Script console”
and run below command:
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")

You can fix this by using the groovy command as specified in Vall's answer.
The effect is in place until Jenkins restarts and afterwards you have to do it again.
A solution to solve this problem is to configure a job that will do this for you whenever jenkins starts up.
You can do this by using the Startup Trigger plugin.
After you install it create a new job and you will have a new checkbox under the Build Triggers section that you will have to check.
Then add an Execute system Groovy script build step with the command:
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP","")
Save and everything should work.

On CentOS, the below solution (which was suggested in comments of another answer) is the only one which has worked for me:
Go to: Manage Jenkins > Manage Nodes and Clouds
Click Gear icon on the right hand side for the node (by default there will be just one Node called Master)
Click 'Script Console' on the left
Enter the following into the console window: System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "default-src 'self' 'unsafe-inline' 'unsafe-eval'; img-src 'self' 'unsafe-inline' data:;")
Click Run
You should see some output in the Result section, similar to below screenshot:
My particular issue was missing images/css in Serenity BDD reports. After performing these steps my Serenity reports had all images/css rendering properly, including reports from builds which had executed prior to this change. This solution will also work for any published html-based report.

(The following solution is for Windows.)
A permanent fix is to change a line in [Jenkins directory]\jenkins.xml (for me it's at C:\Jenkins\jenkins.xml)
<executable>java.exe</executable>
<arguments>[arguments are here]</arguments>
Add the following argument to the whitespace-separated list of arguments:
-Dhudson.model.DirectoryBrowserSupport.CSP=
Then restart the Jenkins service to pick up the change.

For Ubuntu 14 version helpful was special plugins:
https://wiki.jenkins-ci.org/display/JENKINS/Startup+Trigger - To start job on jenkins startup
https://wiki.jenkins-ci.org/display/JENKINS/Groovy+plugin - To run System Groovy script
And I made a job, that starts on Jenkins restart and sets parametr.
And added system Groovy script to set parametr.
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "sandbox; img-src 'self';")

Go To
Manage Jenkins --> Script console
and type in the following command:
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")
then Press Run. if you get the output as 'Result', then rerun the build check the HTML report format

For setting permanently create a Groovy script file $JENKINS_HOME/init.groovy, or any .groovy file in the directory $JENKINS_HOME/init.groovy.d/ with the following content:
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "sandbox allow-scripts; default-src 'self'; img-src *; style-src 'self' 'unsafe-inline'; script-src * 'unsafe-inline';")
systemctl restart jenkins
https://wiki.jenkins.io/display/JENKINS/Post-initialization+script

Open jenkins.xml file and copy arguments as below. it will fix permanently. Once its done restart your machine.
<arguments>-Xrs -Xmx256m -Dhudson.lifecycle=hudson.lifecycle.WindowsServiceLifecycle -Dhudson.model.DirectoryBrowserSupport.CSP="sandbox allow-scripts; default-src 'self'; style-src 'self' 'unsafe-inline';" -jar "%BASE%\jenkins.war" --httpPort=8080 --webroot="%BASE%\war"</arguments

It's too late to respond but thought to share.
I was struggling with Jenkins deployed on Tomcat, tried execution of script, it helps but goes away if tomcat is rebooted.
Made the permanent fix by setting the property in catalina.properties in tomcat.
Properties file: tomcat_installation_dir/conf/catalina.properties
Just copy paste the below line in catalina.properties at the last (you can set it anywhere just to not mess with existing properties)
-Dhudson.model.DirectoryBrowserSupport.CSP=""

To set the system property permanently if using Jenkins-X, then create the file myvalues.yaml in the current directory, with the following content:
jenkins:
Master:
JavaOpts: >
-Dhudson.model.DirectoryBrowserSupport.CSP=
Then restart the jenkins-x platform, which can be done by upgrading it:
$ jx upgrade platform --always-upgrade
# Presumably jx.exe is used if on Windows (not tested)
To avoid actually upgrading the platform, just forcibly upgrade it to the same version:
$ version=$(jx version --no-version-check\
| grep 'jenkins x platform' | sed -e 's/^jenkins.\+ //')
$ jx upgrade platform --version ${version} --always-upgrade

For those who are using asciidoctor-maven-plugin plugin to produce an HTML document from the asciidoc file to further publish it to Jenkins, update the plugin configuration, add linkcss attribute:
<configuration>
<attributes>
<linkcss>true</linkcss>
</attributes>
<backend>html5</backend>
<outputDirectory>${project.build.directory}/generated-resources</outputDirectory>
</configuration>

We have a much simpler solution to the problem. Unless you really insist on having the HTML reports, you can just use the Warnings NG plugin (which is a good idea anyway):
https://github.com/jenkinsci/warnings-ng-plugin/blob/master/SUPPORTED-FORMATS.md
We use this for CodeNarc (Groovy), but also for Checkstyle, PMD, SpotBugs and Java warnings (Java).

In MacOS, Jenkins runs a service, that needs to create a groovy script inside $JENKINS_HOME/init.groovy.d
Call startup-properties.groovy and code :
import jenkins.model.Jenkins
import java.util.logging.LogManager
/* Jenkins home directory */
def jenkinsHome = Jenkins.instance.getRootDir().absolutePath
def logger = LogManager.getLogManager().getLogger("")
/* Replace the Key and value with the values you want to set.*/
/* System.setProperty(key, value) */
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")
logger.info("Jenkins Startup Script: Successfully updated the system properties value for hudson.model.DirectoryBrowserSupport.CSP . Script location : ${jenkinsHome}/init.groovy.d")
Restart the Jenkins Service: brew services restart jenkins-lts
Re-build the job and verify the HTML report into the build
https://i.stack.imgur.com/A60BN.png

I had the same issues after adding HTTPS to my jenkins. In case you are having the same issue, the solution is easy - set your Jenkins url to use HTTPS protocol instead of HTTP. It can be configured via jenkins configuration -> jenkins url

I know this is old, but this worked great for me, and it is what seems to be recommended in the Jenkins docs. I just set the resource root to a different url served from the same location.
"It is strongly recommended to set up the Resource Root URL instead of customizing Content-Security-Policy. Most of the documentation below was written when Content-Security-Policy was first introduced and is retained for use by administrators unable to set up Jenkins to serve user content from a different domain."

Related

Content Security Policy violation on code review HTML

I am generating code coverage for my rust project and am trying to publish the results to Jenkins using publishHTML. All good except that when I try to view the HTML the CSS does not load because of this error:
Refused to load the stylesheet 'https://cdn.jsdelivr.net/npm/bulma#0.9.1/css/bulma.min.css' because it violates the following Content Security Policy directive: "default-src 'self'". Note that 'style-src-elem' was not explicitly set, so 'default-src' is used as a fallback.
How do I fix this? I doubt I'm the first person to run into this.
I did have the thought to rewrite the generated HTML to use a local stylesheet, but I ran into weird Access permissions in the Jenkins workspace and I could not get that to work.
This is the command I am using the generate the reports:
grcov . --binary-path workspaces -s . -t html --branch --ignore-not-existing -o ./code_coverage/
I'd appreciate any suggestions on how to fix this or how to better deploy my results.
Default CSP header in Jenkins is:
sandbox; default-src 'none'; img-src 'self'; style-src 'self';
so you have to add https://cdn.jsdelivr.net/npm/ (or https://cdn.jsdelivr.net) into style-src directive.
You can use post initialization script (init hook) to run some additional things right after Jenkins starts up. Create a file, such as $JENKINS_HOME/init.groovy.d/adjust-content-security-policy.groovy (or any .groovy file in $JENKINS_HOME/init.groovy.d/ directory) with the single line of:
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "sandbox; default-src 'self'; img-src 'self'; style-src 'self' https://cdn.jsdelivr.net/npm/")
and it will be executed after Jenkins has started.
Pls note a Note that 'style-src-elem' was not explicitly set, so 'default-src' is used as a fallback in the violation message. This means that default Jenkins CSP was changed and style-src directive is absent. So check files in the $JENKINS_HOME/init.groovy.d/ directory maybe you already have one with CSP settings.
Safiest way is to see in the browser console what CSP header you do have, to add style-src 'self' https://cdn.jsdelivr.net/npm/" into it and then set resulting CSP into System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "..."). Perhaps the Jenkins documentation just is behind the times.
There is a way of use jenkins.xml file if you run Jenkins on Windows. Add the property:
-Dhudson.model.DirectoryBrowserSupport.CSP="sandbox; default-src 'self'; img-src 'self'; style-src 'self' https://cdn.jsdelivr.net/npm/"
in <service><arguments> before the -jar, than restart the service. See details here and here.

The default Content-Security-Policy is currently overridden using the hudson.model.DirectoryBrowserSupport.CSP issue

After upgrading Jenkins to v2.222.1 we got the below warning message
The default Content-Security-Policy is currently overridden using the hudson.model.DirectoryBrowserSupport.CSP system property, which is a potential security issue when browsing untrusted files. As an alternative, you can set up a Resource Root URL that Jenkins will use to serve some static files without adding Content-Security-Policy headers.
we don't want to have separate source to serve static content meanwhile this warning has to be addressed, pleased provide your suggestions..
By default the CSS content will not be displayed when you publish any report using HTML Publisher plugin. Jenkins blocks the CSS based on CSP(Content Security Policy).
Ref: https://www.jenkins.io/doc/book/system-administration/security/configuring-content-security-policy/
To enable CSS content: Manage Jenkins -> Script Console and execute System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")
When the CSS is enabled you get the following warning The default Content-Security-Policy is currently overridden using the hudson.model.DirectoryBrowserSupport.CSP system property, which is a potential security issue when browsing untrusted files.
To disable it just restart the Jenkins Server. The CSP will be again set to default.
When requesting .css or .js, the following message may occur (blocked:csp).
Refused to apply inline style because it violates the following Content Security Policy directive: "style-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-Epaif2cHkSx/K62AHKClT5geuHQeilAdJVvUuNPdcuw='), or a nonce ('nonce-...') is required to enable inline execution.
In that case, you should deal with it like this. Temporarily relaxing Content Security Policy. Go to Manage Jenkins -> Script Console and type into console the following commands:
System.clearProperty("hudson.model.DirectoryBrowserSupport.CSP");
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "sandbox allow-same-origin allow-scripts; default-src *; style-src * 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval' ; font-src * data: ");

Jenkins doesn't load CSS at HTML published pages

I have a Java program that uses Selenium to test web sites.
However when I run the program in Jenkins it generates a HTML report. When I open the report I get it with out CSS loaded.
To fix this I usually go to Jenkins->Script console and type in the following command:
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")
(As described here: https://wiki.jenkins.io/display/JENKINS/Configuring+Content+Security+Policy)
However after some time this rule is gone, so I need to put it into some config file.
My server is a Ubuntu 18.04 server.
I have installed Jenkins under /var/lib/jenkins
Here I have a file named config.xml, but I cant see any option on this hudson.model.DirectoryBrowserSupport.CSP.
Create a Groovy script file $JENKINS_HOME/init.groovy, or any .groovy file in the directory $JENKINS_HOME/init.groovy.d/ with the following content:
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "sandbox allow-scripts; default-src 'self'; img-src *; style-src 'self' 'unsafe-inline'; script-src * 'unsafe-inline';")
systemctl restart jenkins
https://wiki.jenkins.io/display/JENKINS/Post-initialization+script

Unable to Open Robot Frame Work Log file from Jenkins log. Throws 'Allow-Script' permission error

We have jenkins setup configured with 1 Master and 3 slaves using SSH. Accessing jenkins via VPN, since placed in remote location.
We are able to log in, create jobs, Execute jobs successfully and got reports in email as well. But, logs are unable to open and throwing an error Opening Robot Framework report failed with asking to check the browser and java script log.
I have inspected the issue found below error Blocked script execution in 'http://xx.xx.3.9:8080/job/API_External_Staging/66/robot/report/report.html' because the document's frame is sandboxed and the 'allow-scripts' permission is not set.
Screen shot attached for reference. I believe some where we need to enable this permission. If any body as solution please let me know.
Thanks
To fix this error add below entry to Jenkins configuration file (jenkins.xml):
-Dhudson.model.DirectoryBrowserSupport.CSP=
and restart Jenkins (with http://(jenkins_url)/safeRestart).
You have to edit /etc/default/jenkins if your Jenkins instance is on linux.
Add following lines to your config file:
JAVA_ARGS="-Dhudson.model.DirectoryBrowserSupport.CSP=\"sandbox allow-scripts allow-same-origin; default-src 'none'; img-src 'self' data: ; style-src 'self' 'unsafe-inline' data: ; script-src 'self' 'unsafe-inline' 'unsafe-eval' ;\"
Restart Jenkins:
sudo service jenkins start
This is only a workaround please read linked Jira ticket for further information.
https://issues.jenkins-ci.org/browse/JENKINS-32118

System properties management

Is there any "adequate" way to change system properties in Jenkins? What is the easiest/fastest way change them? For instance, I need to turn off the useless (in my case) pinging thread.
If you really want a quick and simple way to change a system property, you can use the script console
System.setProperty("hudson.remoting.Launcher.pingIntervalSec", 0)
But that won't survive a restart. To make it permanent, add the setting to the java args. For me (CentOS, Jenkins 2.7.1) that's a line about halfway down /etc/sysconfig/jenkins (for other distributions I believe it's /etc/default/jenkins) where you should add your option to the existing list like this:
JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true -Dhudson.remoting.Launcher.pingIntervalSec=0"
You'll have to restart Jenkins after you make that change (thanks Mark Tickner)
If you run Jenkins on windows as a service without tomcat, you can edit jenkins.xml. Add the property in <service><arguments> before the -jar.
Than restart the service.
<service>
<!-- ... -->
<arguments>-Xrs -Xmx256m -Dhudson.lifecycle=hudson.lifecycle.WindowsServiceLifecycle -Dhudson.tasks.MailSender.SEND_TO_UNKNOWN_USERS=true -Dhudson.tasks.MailSender.SEND_TO_USERS_WITHOUT_READ=true -jar "%BASE%\jenkins.war" --httpPort=8080 --webroot="%BASE%\war"</arguments>
The system properties available and how to set them are listed on the wiki:
https://wiki.jenkins-ci.org/display/JENKINS/Features+controlled+by+system+properties
To disable slave pinging, you can set hudson.remoting.Launcher.pingIntervalSec to 0.
System properties can be set in the same way as with any other Java program, e.g.:
java -Dhudson.remoting.Launcher.pingIntervalSec=0 -jar jenkins.war
If you use Tomcat on Windows you can edit the File C:\apache-tomcat-7.0.67\conf\catalina.properties and simply add the Line
hudson.DNSMultiCast.disabled=true
at the End of the File. Then safe the File and restart Tomcat.
I have the similar problem: I need to disable DNSMultiCast (set hudson.DNSMultiCast.disabled = false) and I can't understand how to do it
for example, https://wiki.jenkins-ci.org/display/JENKINS/Features+controlled+by+system+properties - there is such advice "...pass all of these arguments before the -jar argument..." but I run jenkins under tomcat so I am not sure I can change startup parameters.
I tried to change /etc/tomcat6/Catalina/localhost/jenkins.xml to
<?xml version="1.0" encoding="UTF-8"?>
<Context >
<Environment name="JENKINS_HOME" value="/var/jenkins"
type="java.lang.String" override="false"/>
<Environment name="hudson.DNSMultiCast.disabled" value="true"
type="java.lang.Boolean" override="false"/>
</Context>
but I didn't help.
Can someone explain how to change jenkins system properties when tomcat is used.
Maybe it's a bad hack but I set it in the pipeline job that needs the setting.
Like this:
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "") // allow formatted HTML pages to be published
It seems to work - as far as I can tell...
I followed each steps mentioned above but it fails.
So I did change the system time zone using timedatectl set-timezone Europe/London command and then I have restarted jenkins service service jenkins restart it worked.
I was using Rehdat 7.5
Jenkins version 2.168.
Jenkins Installed via yum install jenkins
I hope this will help some one.

Resources