Dashboards & Visualizations

Is there any way to include javascript in the HTML element on a Simple XML Dashboard?

bshamsian
Path Finder

Is there any way to add Javascript to a Simple XML dashboard that is not externalized to a js file. It looks like Splunk strips out any script tag and their content from HTML elements. Below is an example dashbaord - You will NOT see "Hello World" popup when dashboard is displayed.

<dashboard>
    <label>HTML</label>
    <row>
        <html>
            <h1>
                HTML Panel Example
            </h1>
            <p>
                Here is 
                <script type="text/javascript">
                    alert("Hello World") ;
                </script>
                The Javascript
            </p>
        </html>
   </row>
</dashboard>

LukeMurphey
Champion

No, it isn't allowed. I talked to the Developers of Splunk Enterprise and was told they explicitly don't allow this.

Here are two workarounds:

Workaround 1: move the Javascript into a separate file
Put your code in a Javascript file and include it on the page. Make sure to put the Javascript under appserver/static (e.g. /etc/apps/my_app/appserver/static/my_code.js).

Then, include it in your view with the "javascript" attribute:

<dashboard script="my_code.js">
...

But what about SHC?
You would have to push this code from your conf-deployer because Javascript files will not be distributed between your search heads.

Workaround 2: use an app that runs your Javascript from your views

Install the "Javascript-on-Dashboards" app (download here) and put your code within the views (see the README on how to format the code).

Important note: this app allows people to write Javascript in views. Do not allow users to make views if you do not trust them to use Javascript safely.

But what about SHC?
Push this app to your search heads by pushing it from your conf-deployer. My understanding is that Splunk does replicate views being search heads (at least on later versions of Splunk). Thus, once deployed, anyone could make views with Javascript by making views on any search head.

bshamsian
Path Finder

Thanks LukeMurphey - This is a possible interim solution. I understand how you did it. The user still needs to create and app and drop the JS file but once that is done the dashboards created within that app can have javascript in them.

I am hoping Splunk comes up with a better solution long term so we can do this for a Dashboard created in Search and Reporting. The only other way I found to do this for now is to transfer the Dashboard from Simple XML to an HTML dashboard and add whatever Javascript I want. Since Splunk allows users to do this, the argument that we don't allow Javascript in Simple XML for security reasons is not valid because Splunk is allowing Javascript in an HTML dashboard. That has the exact same potential security issues as one created using Simple XML.

LukeMurphey
Champion

I'll spend some time thinking about this and see what I think the best way to approach this is (keeping SHC in mind).

0 Karma

LukeMurphey
Champion

I made an app that might be a solution to this problem. I updated the answer accordingly.

bshamsian
Path Finder

Issues with external js file are:

1) Not every user has access to the server and is able to get administrator to add a js file to the server.
2) What if the Dashboard is not under a custom app and it was just created in the Splunk search & reporting app

Assuming I use an external js file - looking at the /etc/apps//appserver/static/my_code.js path I am assuming the "//" is suppose to have the name of the app the dashboard is under. This means the only way you can add JavaScript to a dashboard is by creating an app that includes that dashboard even with the method you outlined above - You simply CANNOT add javascript in anyway to a dashboard that is part of the Splunk built in Search & Reporting app unless there is a way to drop JS files under the Search & Reporting App.

Finally this solution is harder when you are in a Splunk environment setup with Search Head Clustering.

LukeMurphey
Champion

On point 1: I agree. That said, Splunk currently requires this. My alternative approach is to use a single piece of JS that can be used anywhere that will allow you run code that is placed within the dashboard itself. It will require admin privileges to install the script, but once installed, everyone who makes dashboards in that app will be able to make JS without touching the file-system (they will just have a add a reference to the script).

On point 2: In this, neither of these approaches would work well in the Search app. You can add a custom JS file to it, but it won't likely survive an upgrade. If my understanding is correct, views are replicated across SHC (at least on newer versions of Splunk). In this case, you could make an app that includes the Javascript executer and then tell people to make their views in this app. The original file would have to be deployed on the conf-deployer, but the views could be made anywhere (if they are indeed replicated).

You are correct that "//" was supposed to include the app; it isn't being rendered because Answers thinks it is HTML and is stripping it (somewhat ironic given the content of this question).

0 Karma

LukeMurphey
Champion

I just had a thought. I think I could make a Javascript file that you could include that in your dashboard that would allow you to run Javascript the you entered inside the dashboard.

Basically, this would allow you to enter some Javascript on a page, and then my code would go ahead and execute it for you. In that case, you could still enter and modify the Javascript via the UI (since the code would be embedded in the dashboard).

I'll write up something tonight.

jeffland
SplunkTrust
SplunkTrust

I'm really interested in that as well!

0 Karma

bshamsian
Path Finder

looking forward to your solution.

0 Karma
Get Updates on the Splunk Community!

Earn a $35 Gift Card for Answering our Splunk Admins & App Developer Survey

Survey for Splunk Admins and App Developers is open now! | Earn a $35 gift card!      Hello there,  Splunk ...

Continuing Innovation & New Integrations Unlock Full Stack Observability For Your ...

You’ve probably heard the latest about AppDynamics joining the Splunk Observability portfolio, deepening our ...

Monitoring Amazon Elastic Kubernetes Service (EKS)

As we’ve seen, integrating Kubernetes environments with Splunk Observability Cloud is a quick and easy way to ...