Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,17 @@ def _get_arguments(self, config: dict[str, Any]) -> dict[str, Any]:
return {"regex": "r" + quote_regex(regex)}


class IndexNameValidator(BaseValidator):
def _get_class_name(self) -> str:
return "IndexName"

def _get_arguments(self, config: dict[str, Any]) -> dict[str, Any]:
return {}

def build(self, config: dict[str, Any]) -> str:
return "validator.IndexName()"
Comment thread
wtobis-splunk marked this conversation as resolved.


class ValidatorBuilder:
_validation_config_map = {
"string": StringValidator,
Expand All @@ -150,6 +161,7 @@ class ValidatorBuilder:
"ipv4": Ipv4Validator,
"date": DateValidator,
"url": UrlValidator,
"index_name": IndexNameValidator,
# file validator does not need any generated code, everything is
# validated in the UI
}
Expand Down
13 changes: 1 addition & 12 deletions splunk_add_on_ucc_framework/entity/index_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,7 @@ def long_form(self) -> dict[str, Any]:
"createSearchChoice": True,
},
"validators": [
{
"type": "regex",
"errorMsg": "Index names must begin with a letter or a number "
"and must contain only letters, numbers, underscores or hyphens.",
"pattern": r"^[a-zA-Z0-9][a-zA-Z0-9\\_\\-]*$",
},
{
"type": "string",
"errorMsg": "Length of index name should be between 1 and 80.",
"minLength": 1,
"maxLength": 80,
},
{"type": "index_name"},
],
}

Expand Down
2 changes: 1 addition & 1 deletion splunk_add_on_ucc_framework/install_python_libraries.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
logger = logging.getLogger("ucc_gen")


LIBS_REQUIRED_FOR_UI = {"splunktaucclib": "6.6.0"}
LIBS_REQUIRED_FOR_UI = {"splunktaucclib": "8.2.0"}
LIBS_REQUIRED_FOR_OAUTH = {"solnlib": "5.5.0"}


Expand Down
24 changes: 24 additions & 0 deletions splunk_add_on_ucc_framework/schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,9 @@
},
{
"$ref": "#/definitions/DateValidator"
},
{
"$ref": "#/definitions/IndexNameValidator"
}
]
}
Expand Down Expand Up @@ -3331,6 +3334,24 @@
],
"additionalProperties": false
},
"IndexNameValidator": {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds index_name as a schema-level validator type, but the docs still describe the old contract. docs/entity/components.md says the index field has two internal validators (regex + length), and docs/entity/validators.md does not document index_name even though it is now accepted by AnyValidator. Please update the docs so users know whether index_name is public and so the index component documentation matches the generated config.

"type": "object",
"description": "Validates that the field value is a valid Splunk index name.",
"properties": {
"errorMsg": {
"$ref": "#/definitions/errorMsg"
},
"type": {
"const": "index_name",
"type": "string",
"description": "Exactly: index_name"
}
},
"required": [
"type"
],
"additionalProperties": false
},
"AnyValidator": {
"type": "array",
"description": "It is used to validate the values of fields using various validators.",
Expand All @@ -3356,6 +3377,9 @@
},
{
"$ref": "#/definitions/DateValidator"
},
{
"$ref": "#/definitions/IndexNameValidator"
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,7 @@
required=True,
encrypted=False,
default='default',
validator=validator.String(
max_len=80,
min_len=1,
)
validator=validator.IndexName()
),
field.RestField(
'account',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,7 @@
required=True,
encrypted=False,
default='default',
validator=validator.AllOf(
validator.Pattern(
regex=r"""^[a-zA-Z0-9][a-zA-Z0-9\\_\\-]*$""",
),
validator.String(
max_len=80,
min_len=1,
)
)
validator=validator.IndexName()
),
field.RestField(
'account',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1216,10 +1216,7 @@
"label": "Index",
"validators": [
{
"type": "string",
"errorMsg": "Length of index name should be between 1 and 80.",
"minLength": 1,
"maxLength": 80
"type": "index_name"
}
],
"defaultValue": "default",
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/commands/rest_builder/test_validator_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@
[{"type": "url"}],
get_testdata_file("validator_builder_result_url"),
),
(
[{"type": "index_name"}],
Comment thread
wtobis-splunk marked this conversation as resolved.
get_testdata_file("validator_builder_result_index_name"),
),
(
[
{
Expand Down
34 changes: 4 additions & 30 deletions tests/unit/entity/test_index_entity.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from splunk_add_on_ucc_framework.entity import IndexEntity


def test_interval_min_definition():
def test_index_min_definition():
definition = {"type": "index", "field": "index", "label": "Index"}
index = IndexEntity.from_definition(definition)

Expand All @@ -17,24 +17,11 @@ def test_interval_min_definition():
"denyList": "^_.*$",
"createSearchChoice": True,
},
"validators": [
{
"type": "regex",
"errorMsg": "Index names must begin with a letter or a number and must contain only letters, "
"numbers, underscores or hyphens.",
"pattern": "^[a-zA-Z0-9][a-zA-Z0-9\\\\_\\\\-]*$",
},
{
"type": "string",
"errorMsg": "Length of index name should be between 1 and 80.",
"minLength": 1,
"maxLength": 80,
},
],
"validators": [{"type": "index_name"}],
Comment thread
wtobis-splunk marked this conversation as resolved.
}


def test_interval_max_definition():
def test_index_max_definition():
definition = {
"type": "index",
"field": "index",
Expand All @@ -59,18 +46,5 @@ def test_interval_max_definition():
"denyList": "^_.*$",
"createSearchChoice": True,
},
"validators": [
{
"type": "regex",
"errorMsg": "Index names must begin with a letter or a number and must contain only letters, numbers, "
"underscores or hyphens.",
"pattern": "^[a-zA-Z0-9][a-zA-Z0-9\\\\_\\\\-]*$",
},
{
"type": "string",
"errorMsg": "Length of index name should be between 1 and 80.",
"minLength": 1,
"maxLength": 80,
},
],
"validators": [{"type": "index_name"}],
}
4 changes: 2 additions & 2 deletions tests/unit/test_install_python_libraries.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def test_install_libraries_when_no_splunktaucclib_is_present_but_has_ui(

expected_msg = (
f"This add-on has an UI, so the splunktaucclib is required but not found in {tmp_lib_reqs_file}. "
f"Please add it there and make sure it is at least version 6.6.0."
f"Please add it there and make sure it is at least version 8.2.0."
)

expected_caplog = "Command result: WARNING: Package(s) not found: splunktaucclib"
Expand All @@ -280,7 +280,7 @@ def test_install_libraries_when_wrong_splunktaucclib_is_present_but_has_ui(
tmp_lib_reqs_file = tmp_lib_path / "requirements.txt"
tmp_lib_reqs_file.write_text("splunktaucclib==6.3\n")

expected_msg = "splunktaucclib found but has the wrong version. Please make sure it is at least version 6.6.0."
expected_msg = "splunktaucclib found but has the wrong version. Please make sure it is at least version 8.2.0."
expected_caplog = "Command result: Name: splunktaucclib\nVersion: 6.3.0"

with pytest.raises(WrongSplunktaucclibVersion) as exc:
Expand Down
1 change: 1 addition & 0 deletions tests/unit/testdata/validator_builder_result_index_name
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
validator.IndexName()
15 changes: 1 addition & 14 deletions ui/src/CustomEntity/helper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,6 @@ export const migrateIndexTypeEntity = (
denyList: '^_.*$',
createSearchChoice: true,
},
validators: [
{
type: 'regex',
pattern: '^[a-zA-Z0-9][a-zA-Z0-9\\_\\-]*$',
errorMsg:
'Index names must begin with a letter or a number and must contain only letters, numbers, underscores or hyphens.',
},
{
type: 'string',
minLength: 1,
maxLength: 80,
errorMsg: 'Length of index name should be between 1 and 80.',
},
],
validators: [{ type: 'index_name' }],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking: the UI runtime validator does not handle index_name. ui/src/util/Validator.ts switches over validator types but has no case 'index_name', so after this migration the old regex/string checks are removed and invalid index names are silently accepted in the form. Please add runtime validation and tests for valid/invalid index values before replacing the old validators.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No UI left intentionally, this means no validation will be done during field edit. However when user tries to save the form backend validation is triggered not letting to close from with save operation.
Keeping regexes in UI makes central validation endpoint useless UI validation will always come before backend

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, backend-only validation on save is acceptable for this flow. No need to add a frontend case 'index_name' or duplicate the index-name checks in the UI if this is the intended contract.

};
};
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions ui/src/types/globalConfig/baseSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { z } from 'zod';
import {
DateValidator,
EmailValidator,
IndexNameValidator,
Ipv4Validator,
NumberValidator,
RegexValidator,
Expand Down Expand Up @@ -146,5 +147,6 @@ export const AllValidators = z.array(
Ipv4Validator.strict(),
UrlValidator.strict(),
DateValidator.strict(),
IndexNameValidator.strict(),
Comment thread
wtobis-splunk marked this conversation as resolved.
])
);
6 changes: 6 additions & 0 deletions ui/src/types/globalConfig/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ export const DateValidator = z.object({
type: z.literal('date'),
});

export const IndexNameValidator = z.object({
errorMsg: z.string().optional(),
type: z.literal('index_name'),
});

export const AnyOfValidators = z.discriminatedUnion('type', [
NumberValidator,
StringValidator,
Expand All @@ -42,4 +47,5 @@ export const AnyOfValidators = z.discriminatedUnion('type', [
Ipv4Validator,
UrlValidator,
DateValidator,
IndexNameValidator,
]);
Loading