Skip to content

Commit

Permalink
fix: inputs.conf modular input settings are invalid
Browse files Browse the repository at this point in the history
Modular input settings defined in inputs.conf may show as invalid.  Addresses #50
  • Loading branch information
JasonConger committed Aug 3, 2022
1 parent fb3e4ce commit c146f8f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
6 changes: 2 additions & 4 deletions out/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const splunkSpec = require("./spec.js");
const { transpileModule } = require("typescript");
//const { AsyncLocalStorage } = require("async_hooks");
const PLACEHOLDER_REGEX = /\<([^\>]+)\>/g
const DROPDOWN_PLACEHOLDER_REGEX = /(\[|{)\w+(\|\w+)+(]|})/g
let specConfigs = {};
let timeout = undefined;
let diagnosticCollection = undefined;
Expand Down Expand Up @@ -243,8 +242,7 @@ function handleSplunkFile(context) {
snippets[currentDocument] = snippetFilePath;
}


// If this file is globalConfig.json, return as there is not spec file for it.
// If this file is globalConfig.json, return as there is not a spec file for it.
if(currentDocument.toLowerCase() == "globalconfig.json") {
return;
}
Expand Down Expand Up @@ -406,7 +404,7 @@ function provideSettingCompletionItems(specConfig, trimWhitespace) {

// Convert [foo|bar|baz] or {foo|bar|baz} values to a dropdown placeholder
// ${1|foo,bar,baz|}
if(DROPDOWN_PLACEHOLDER_REGEX.test(settingSnippet)) {
if(splunkSpec.DROPDOWN_PLACEHOLDER_REGEX.test(settingSnippet)) {
settingSnippet = settingSnippet.replace(/\|/g, ',')
settingSnippet = settingSnippet.replace(/\[|{/, '${1|')
settingSnippet = settingSnippet.replace(/]|}/, '|}')
Expand Down
35 changes: 30 additions & 5 deletions out/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ const COMMENT_REGEX = /^#/
const BLANK_LINE_REGEX = /^\s*\n/gm
const DEFAULT_STANZA_REGEX = /^# Use the \[(default)\] stanza/
const STANZA_REGEX = /^\[(?<stanza>|[^\]].*?)\]/
exports.STANZA_REGEX = STANZA_REGEX
const STANZA_PREFIX_REGEX = /^\[(?<prefix>[^\]].*?(=|:|::|::...|_|\/))[\<|\w|\/]/ // matches things like [author=<name>], [tcp:<port>], [tcp:123], [source::...a...], [tcp://<remote server>:<port>], [tcp://123], [views/<view_name>]
const STANZA_FREEFORM_REGEX = /^\[\<(?<stanza>.*?)\>\]/ // matches things like [<spec>] or [<custom_alert_action>]
const STANZA_ABSOLUTE_REGEX = /^\[(?<stanza>|[^\<\>\:\/]+)\]/ // matches things like [tcp] or [SSL] (does not allow <, >, :, or /)
//const SETTING_REGEX = /^(?<setting>\w.*?)\s*=\s*(?<value>[^\r\n]+)/
const SETTING_REGEX = /^(?<setting>((\w)|\<name\>|\<tag\d\>|\<.+\>).*?)\s*=\s*(?<value>[^\r\n]+)/
exports.SETTING_REGEX = SETTING_REGEX
const SETTING_PREFIX_REGEX = /^(?<prefix>[^-\.].*?)\<.*?\>/
const SETTING_FREEFORM_REGEX = /^\<(?<setting>.*?)\>/
const modularSpecFiles = ["inputs.conf.spec", "alert_actions.conf.spec", "indexes.conf.spec"];
const DROPDOWN_PLACEHOLDER_REGEX = /(\[|{)\w+(\|\w+)+(]|})/g

const lineTypes = {
DEFAULT_STANZA: 'defaultStanza',
STANZA: 'stanza',
Expand Down Expand Up @@ -283,6 +283,19 @@ function getStanzaSettingsbyFreeform(specConfig) {
}
}

// Special case for inputs.conf
// Inputs.conf can have modular inputs with all kinds of settings.
// Add python.version setting here. See https://github.com/splunk/vscode-extension-splunk/issues/50
// TODO: add settings from README/inputs.conf.spec in a future release. See issue https://github.com/splunk/vscode-extension-splunk/issues/59
if(specConfig.specName == "inputs.conf.spec") {
let specialPythonSeting = {
"name": "python.version",
"value": "{default|python|python2|python3}",
"docString": "* For Python scripts only, selects which Python version to use.\n* Set to either \"default\" or \"python\" to use the system-wide default Python\n version.\n* Optional.\n* Default: Not set; uses the system-wide Python version."
}
freeFormStanzas.push(specialPythonSeting)
}

return freeFormStanzas
}

Expand Down Expand Up @@ -399,6 +412,7 @@ function isValueValid(specValue, settingValue) {

let isValid = true

// Look for known types
switch(specValue) {
case "<boolean>": {
let booleans = ["true", "false", "1", "0"]
Expand Down Expand Up @@ -441,7 +455,15 @@ function isValueValid(specValue, settingValue) {
}
}

return isValid;
// Look for multiple choice types
if(DROPDOWN_PLACEHOLDER_REGEX.test(specValue)) {
// Remove square brackets and curly braces
let possibleValues = specValue.replace(/[\[\{\}\]]/g, '')
let settings = possibleValues.split('|')
if (!settings.includes(settingValue.toLowerCase())) isValid = false;
}

return isValid;
}

function isSettingValid(specConfig, stanzaName, settingString) {
Expand All @@ -458,7 +480,7 @@ function isSettingValid(specConfig, stanzaName, settingString) {
let settingValue = setting.groups["value"]

// Get settings for this stanza
let stanzaSettings = getStanzaSettings(specConfig, stanzaName)
let stanzaSettings = getStanzaSettings(specConfig, stanzaName)

stanzaSettings.forEach(specSetting => {

Expand Down Expand Up @@ -493,7 +515,10 @@ function isSettingValid(specConfig, stanzaName, settingString) {
return isValid
}

exports.getSpecConfig = getSpecConfig;
exports.SETTING_REGEX = SETTING_REGEX
exports.DROPDOWN_PLACEHOLDER_REGEX = DROPDOWN_PLACEHOLDER_REGEX
exports.STANZA_REGEX = STANZA_REGEX
exports.getSpecConfig = getSpecConfig
exports.getStanzaSettings = getStanzaSettings
exports.isStanzaValid = isStanzaValid
exports.isSettingValid = isSettingValid

0 comments on commit c146f8f

Please sign in to comment.