All Apps and Add-ons

How to add custom validation on Configuration Page- Add-on Settings tab.

kamlesh_vaghela
SplunkTrust
SplunkTrust

Hi Splunkers,

I have an issue regarding validation on Alert action Configuration Page.
I have created app from Splunk AOB with a single alert action. AOB generates all files we required. TA_testing_add_on_rh_settings.py is one of them.

Here, we can validate single input value but I want to validate all values together. below is my code.

import ta_testing_add_on_declare

from splunktaucclib.rest_handler.endpoint import (
    field,
    validator,
    RestModel,
    MultipleModel,
)
from splunktaucclib.rest_handler import admin_external, util
from splunk_aoblib.rest_migration import ConfigMigrationHandler

util.remove_http_proxy_env_vars()


fields_additional_parameters = [
        field.RestField(
        'url',
        required=False,
        encrypted=False,
        default='',
        validator=validator.String(
            max_len=8192, 
            min_len=0, 
        )
    ),
    field.RestField(
        'user_name',
        required=False,
        encrypted=False,
        default='',
        validator=validator.String(
            max_len=8192, 
            min_len=0, 
        )
    ), 
    field.RestField(
        'password',
        required=False,
        encrypted=True,
        default='',
        validator=validator.String(
            max_len=8192, 
            min_len=0, 
        )
    )
]
model_additional_parameters = RestModel(fields_additional_parameters, name='additional_parameters')


endpoint = MultipleModel(
    'ta_testing_add_on_settings',
    models=[
        model_additional_parameters
    ],
)


if __name__ == '__main__':
    admin_external.handle(
        endpoint,
        handler=ConfigMigrationHandler,
    )

This code is totally generated from AOB. There is no custom modification.

Here I found we can do field level validation by using :

validator=validator.String(
            max_len=8192, 
            min_len=0, 
)

and we can add custom validation using UserDefined class.

My questions are:
1) How can we use UserDefined validator?
2) How can we access field values, like(url/username/password), to do custom validations which verify the username/password by login with url?

Any hint??

Thanks

Tags (1)
0 Karma

chli_splunk
Splunk Employee
Splunk Employee

You can try following 2 options
1. Hack TA_testing_add_on_rh_settings.py. You can update it manually, but it's not recommended unless you dont want to reload it in AoB, otherwise there might be some errors due to inconsistent status.
2. Validate inputs in your Python code. AoB provides a UI to edit your Python codes.

# get some fields
url = helper.settings["server_uri"]

# add your validate code here
if not success:
    # write events
    helper.addevent("error msg", sourcetype="sample_sourcetype")
    helper.writeevents(index="summary", host="localhost", source="localhost")
    return

# if success
helper.log_info("Alert action sdfsdfsd started.")
0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Announcing Modern Navigation: A New Era of Splunk User Experience

We are excited to introduce the Modern Navigation feature in the Splunk Platform, available to both cloud and ...

Modernize your Splunk Apps – Introducing Python 3.13 in Splunk

We are excited to announce that the upcoming releases of Splunk Enterprise 10.2.x and Splunk Cloud Platform ...

Step into “Hunt the Insider: An Splunk ES Premier Mystery” to catch a cybercriminal ...

After a whole week of being on call, you fell asleep on your keyboard, and you hit a sequence of buttons that ...