require.config({ paths: { 'app': '../app' } }); require([ "underscore", "jquery", "splunkjs/mvc/utils", "splunkjs/mvc", "splunkjs/mvc/tokenutils", "splunkjs/mvc/simplexml", "splunkjs/mvc/layoutview", "splunkjs/mvc/simplexml/dashboardview", "splunkjs/mvc/simplexml/dashboard/panelref", "splunkjs/mvc/simplexml/element/chart", "splunkjs/mvc/simplexml/element/event", "splunkjs/mvc/simplexml/element/html", "splunkjs/mvc/simplexml/element/list", "splunkjs/mvc/simplexml/element/map", "splunkjs/mvc/simplexml/element/single", "splunkjs/mvc/simplexml/element/table", "splunkjs/mvc/simplexml/element/visualization", "splunkjs/mvc/simpleform/formutils", "splunkjs/mvc/simplexml/eventhandler", "splunkjs/mvc/simplexml/searcheventhandler", "splunkjs/mvc/simpleform/input/dropdown", "splunkjs/mvc/simpleform/input/radiogroup", "splunkjs/mvc/simpleform/input/linklist", "splunkjs/mvc/simpleform/input/multiselect", "splunkjs/mvc/simpleform/input/checkboxgroup", "splunkjs/mvc/simpleform/input/text", "splunkjs/mvc/simpleform/input/timerange", "splunkjs/mvc/simpleform/input/submit", "splunkjs/mvc/searchmanager", "splunkjs/mvc/savedsearchmanager", "splunkjs/mvc/postprocessmanager", "splunkjs/mvc/simplexml/urltokenmodel", "splunkjs/mvc/tableview", "splunkjs/mvc/simplexml/ready!" ], function( _, $, utils, mvc, TokenUtils, DashboardController, LayoutView, Dashboard, PanelRef, ChartElement, EventElement, HtmlElement, ListElement, MapElement, SingleElement, TableElement, VisualizationElement, FormUtils, EventHandler, SearchEventHandler, DropdownInput, RadioGroupInput, LinkListInput, MultiSelectInput, CheckboxGroupInput, TextInput, TimeRangeInput, SubmitButton, SearchManager, SavedSearchManager, PostProcessManager, UrlTokenModel, TableView ) { require(["splunkjs/mvc/simplexml/ready!"], function () { console.log("loading javascript ..."); var org_ids = null; var sfx_token_configured = false; var test_completed = false; function dataLoaded() { if ( (sfx_token_configured == true) && (test_completed == true) ) { console.log("test_completed..." + test_completed); $("#configuration_complete").show(); $("#configuration_complete_message").show(); } } // Create a service object using the Splunk SDK for JavaScript // to send REST requests let service = mvc.createService({ owner: "nobody" , app: "splunk_incident_intelligence_app"}); var isAdmin = false; var roleSearchManager = new SearchManager({ "id": "roleSearch", "status_buckets": 300, "search": '| rest splunk_server=local /services/authentication/current-context | table roles | mvexpand roles' }); var roleResults = roleSearchManager.data("results", {count:0}); roleResults.on("data", function() { console.log("Got Role Results"); var d = roleResults.data(); if (d && d.rows) { for (var i = 0; i < d.rows.length; i++) { if ( (d.rows[i][0] === 'admin') || (d.rows[i][0] === 'incident_intelligence_admin') || (d.rows[i][0] === 'sc_admin') ) { $("#configPanel").show(); isAdmin = true; break; } } } $("#mainPanel").show(); }); roleSearchManager.startSearch(); function getAppVersion() { console.log("getAppVersion() entry ..."); const query = '| rest /services/apps/local | where title="splunk_incident_intelligence_app" | table version'; const search = service.oneshotSearch( query, null, function(err, results) { if (err) { console.error('getAppVersion error - ', err); return; } if (results && results.rows && results.rows.length > 0) { version = "Release " + results.rows[0]; $("#appVersion").html(version); } }); } getAppVersion(); function checkOrgIdOrgLevelAccessKey() { console.log("checkOrgIdOrgLevelIdSetup() entry ..."); const query = '| inputlookup incident_intelligence_kvstore_lookup | table _key, realm, org_id, is_default, test_result'; const search = service.oneshotSearch( query, null, function(err, results) { //console.log("results..." + results); if (err) { console.error('lookupKey error - ', err); dataLoaded(); return; } if (results.rows.length == 0) { console.log("results.rows.length..." + results.rows.length); dataLoaded(); return; } // Loop through results and get default record and check its status for (var i = 0; i < results.rows.length; i++) { var record = results.rows[i]; //console.log("results.record..." + record); if (record[3] === "true") { if (record[4] != null) { if (record[4] === "SUCCESS") { test_completed = true; console.log("test_completed..." + test_completed); $("#setup_test_alert_fail").hide(); $("#setup_test_alert_success").show(); $("#setup_test_alert_success").parent().prev().addClass("selected"); } else { $("#setup_test_alert_fail").show(); $("#setup_test_alert_success").hide(); } } var orgId = record[2]; $("#org_id").html(orgId); } } org_ids = results.rows; sfx_token_configured = true; dataLoaded(); //console.log("org_ids..." + org_ids); //$("#setup_account").parent().prev().addClass("selected"); //$("#setup_account").show(); $("#setup_api").parent().prev().addClass("selected"); $("#setup_api").show(); }); } function testKey(keyVal, realm, orgId) { const query = '| rest /servicesNS/admin/splunk_incident_intelligence_app/configs/conf-alert_actions/incident_intelligence' const search = service.oneshotSearch( query, null, function(err,results) { var hostname = window.location.hostname; if (results) { var hostnameIndex = -1; // Is there a field for hostname? for (var i = 0; i < results.fields.length; i++) { if (results.fields[i] == 'hostname') { hostnameIndex = i; break; } } if (hostnameIndex != -1) { if (results.rows[0][hostnameIndex] != "") { hostname = results.rows[0][hostnameIndex]; } } } sendAlert(keyVal, realm, orgId, hostname) }) } function sendAlert(keyVal, realm, orgId, hostname) { // Use the sendalert command to generate a test alert. This will invoke incident_intelligence.py in the app' // bin directory. The command will lookup the api key to test from storage password using the value passed // the org_id parameter. console.log('realm: ' + realm); const query = '| sendalert incident_intelligence param.severity="" param.title="Test Alert" param.description="Test Alert from ' + hostname + '" param.service_id="Test Service" param.realm="' + encodeURIComponent(realm) + '" param.org_id="' + encodeURIComponent(orgId) + '"'; console.log('Sending test alert'); const search = service.oneshotSearch( query, null, function(err,results) { let isSuccess = true; //console.log("results..." + results) console.log("err..." + err) if (results && results.messages) { for (let i = 0; i < results.messages.length; i++) { //console.log("results.messages[i]: " + results.messages[i]) const obj = results.messages[i]; if (obj.type === 'FATAL' || obj.type === 'ERROR') { isSuccess = false; // NOTE: Errors should already be logged in the console. } } const status = isSuccess ? "Connection successful!" : "Connection refused!"; console.log("status="+status); if (isSuccess) { $("#setup_test_alert_success").show(); $("#setup_test_alert_fail").hide(); $("#setup_test_alert_success").parent().prev().addClass("selected"); } else { $("#setup_test_alert_success").hide(); $("#setup_test_alert_fail").show(); } require(["bootstrap.modal"],function() { $(".modal-body").html(status); $("#cancelDialogButton").html("Dismiss"); $("#cancelDialogButton").show(); $("#confirmDialogButton").hide(); $('#homeModal').css('z-index', '9999'); $('#homeModal').modal('show'); }); setTestResult(orgId, isSuccess); } }); } function setTestResult(orgId, isSuccess) { var status = (isSuccess == true)?'SUCCESS':'FAIL'; const query = '| setTestResult org_id=' + orgId + ' test_result=' + status; //console.log("query=" + query) const search = service.oneshotSearch( query, null, function(err,results) { // don't care if (results && results.rows && results.rows.length > 0) { //console.log("results.rows[0]..." + results.rows[0]) if (results.rows[0] == "SUCCESS") { console.log("Successfully updated test result"); test_completed = true; dataLoaded(); } else { console.log("Failure updating test result"); } } }); } $("#send_test_alert").on("click", function(e) { console.log("Sent Test Alert Click"); if (org_ids && org_ids.length ) { // get the default key for (var i = 0; i < org_ids.length; i++) { if (org_ids[i][3] == "true") { console.log("Found Default OrgId entry"); testKey(org_ids[i][0], org_ids[i][1], org_ids[i][2]); break; } } } }); console.log("javascript load done ..."); checkOrgIdOrgLevelAccessKey(); }); });