---
schema_version: "1"
id: 0dad7a6c71dc4815015aabc64688bfadb9c2710b40d55c528b891ae862165251
title: Application Validator Service
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/runtime-manager/rtm-agent-app-validator.md
canonical_url: https://docs.mulesoft.com/runtime-manager/rtm-agent-app-validator
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 69bce3e29b2055378a15ce19ad318fcc8d2ab1c563eb22e08c8799cb46ed6fff
description: MuleSoft Documentation Site
latest: true
---
---
product: Runtime Manager
---

# Application Validator Service

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

[![logo hybrid active](/mulesoft/current/en-us/_/img/icons/hybrid-cloud.svg "Hybrid")Hybrid](deployment-strategies#hybrid-deployments)

> [!NOTE] Where possible, we changed noninclusive terms to align with our company value of Equality.
> We maintained certain terms to avoid any effect on customer implementations.

The Application Validator Service enables you to create your own custom validators that prevent your
application from being deployed if certain conditions are not met. Depending on the Mule Runtime
Agent configuration, the service invokes your custom validators to enforce business rules, such as
signing validation, naming conventions, or moratorium dates.

![Application Validation Flowchart](/mulesoft/current/en-us/runtime-manager/_images/app-validator-chart.png)

## Create an Application Validator

Follow these steps to create a custom validator that checks the value of an application property and
stops the deployment if the property does not meet the specified criteria:

- [Create a Maven project.](#create-a-maven-project)

- [Create the Project Object Model (POM) file.](#create-the-project-object-model-pom-file)

- [Create the validator class.](#create-the-validator-class)

- [Implement business logic.](#implement-business-logic)

- [Implement custom exceptions.](#implement-custom-exceptions)

- [Add the Guice configuration.](#add-the-guice-configuration)

- [Build the application validator.](#build-the-application-validator)

- [Install the application validator.](#install-the-application-validator)

- [Update the `mule-agent.yml` file.](#update-the-mule-agent-yml-file)

- [Start Mule Runtime.](#start-mule-runtime)

### Create a Maven Project

Open a terminal, navigate to the folder where you want to create the Java project, and type the
following command:

```console
mvn archetype:generate \
	-DgroupId=org.example.agent \
	-DartifactId=mule-agent-hello-validator \
	-DarchetypeArtifactId=maven-archetype-quickstart \
	-DinteractiveMode=false
```

#### Maven Directory Layout

The previous command creates the following project directory structure:

```java
./mule-agent-hello-validator
./mule-agent-hello-validator/pom.xml
./mule-agent-hello-validator/src
./mule-agent-hello-validator/src/test
./mule-agent-hello-validator/src/test/java
./mule-agent-hello-validator/src/test/java/org
./mule-agent-hello-validator/src/test/java/org/example
./mule-agent-hello-validator/src/test/java/org/example/agent
./mule-agent-hello-validator/src/test/java/org/example/agent/AppTest.java
./mule-agent-hello-validator/src/main
./mule-agent-hello-validator/src/main/java
./mule-agent-hello-validator/src/main/java/org
./mule-agent-hello-validator/src/main/java/org/example
./mule-agent-hello-validator/src/main/java/org/example/agent
./mule-agent-hello-validator/src/main/java/org/example/agent/App.java
```

### Create the Project Object Model (POM) File

Create a POM file with the following configuration details:

#### Properties

1.  Access the generated folder and locate the generated `pom.xml` file.

2.  Add the following properties:

```xml
<properties>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
    <mule.agent.version>2.4.28</mule.agent.version>
    <javax.servlet-api.version>3.1.0</javax.servlet-api.version>
    <javax.ws.version>2.0</javax.ws.version>
    <log4j.version>2.17.1</log4j.version>
    <guice.version>5.0.1</guice.version>
    <javax.inject.version>1</javax.inject.version>
</properties>
```

The previous properties represent the versions used for the following required dependencies:

<table><colgroup><col> <col></colgroup><tbody><tr><td><div><div><p><code>mule-agent-api</code></p></div></div></td><td><div><div><p>Java API for the Mule Agent Plugin</p></div></div></td></tr><tr><td><div><div><p><code>javax.ws.rs-api</code></p></div></div></td><td><div><div><p>Java API for RESTful Web Services</p></div></div></td></tr><tr><td><div><div><p><code>javax.servlet-api</code></p></div></div></td><td><div><div><p>Java Servlet API</p></div></div></td></tr><tr><td><div><div><p><code>javax.inject</code></p></div></div></td><td><div><div><p>The javax.inject API</p></div></div></td></tr><tr><td><div><div><p><code>guice</code></p></div></div></td><td><div><div><p>Google Guice Core Library</p></div></div></td></tr><tr><td><div><div><p><code>log4j-api</code></p></div></div></td><td><div><div><p>The Apache Log4j API</p></div></div></td></tr></tbody></table>

#### Dependencies

Add the `com.mulesoft.agent:mule-agent-api` dependency:

```xml
        <dependency>
            <groupId>com.mulesoft.agent</groupId>
            <artifactId>mule-agent-api</artifactId>
            <version>${mule.agent.version}</version>
            <scope>provided</scope>
        </dependency>
```

Add the `javax.ws.rs:javax.ws.rs-api` dependency:

```xml
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>${javax.ws.version}</version>
        </dependency>
```

Add the `javax.servlet:javax.servlet-api` dependency:

```xml
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>${javax.servlet-api.version}</version>
        </dependency>
```

Add the `javax.inject:javax.inject` dependency:

```xml
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>${javax.inject.version}</version>
        </dependency>
```

Add the `com.google.inject:guice` dependency:

```xml
        <dependency>
            <groupId>com.google.inject</groupId>
            <artifactId>guice</artifactId>
            <version>${guice.version}</version>
        </dependency>
```

Add the `log4j` dependencies:

```xml
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${log4j.version}</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${log4j.version}</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>${log4j.version}</version>
            <scope>compile</scope>
        </dependency>
```

#### Repositories

Add the MuleSoft releases and snapshots repositories:

```xml
    <repositories>
        <repository>
            <id>mulesoft-ci-releases</id>
            <name>Mulesoft Release Repository</name>
            <url>https://repository-master.mulesoft.org/nexus/content/repositories/releases-ee/</url>
        </repository>
        <repository>
            <id>mulesoft-ci-snapshots</id>
            <name>MuleSoft Snapshot Repository</name>
            <url>https://repository-master.mulesoft.org/nexus/content/repositories/ci-snapshots/</url>
        </repository>
    </repositories>
```

### Create the Validator Class

1.  Rename the default class to `MuleAgentHelloValidator`:

    **Before**: `mule-agent-hello-validator/src/main/java/org/example/App.java`

    **After**: `mule-agent-hello-validator/src/main/java/org/example/MuleAgentHelloValidator.java`

2.  Add the following annotations:

    ```java
    @Named("MuleAgentHelloValidator")
    @Singleton
    ```

3.  Add the following property to obtain a named logger based on the validator class:

    ```java
    private static final Logger LOGGER = LogManager.getLogger(MuleAgentHelloWorld.class);
    ```

4.  Implement the artifact validator interface:

    ```java
    public class MuleAgentHelloWorld implements ApplicationValidator {

        private static final Logger LOGGER = LogManager.getLogger(MuleAgentHelloWorld.class);


        // Type represents a group of validator implementations
        public String getType() {
            return "helloValidator";
        }


        // Custom name for the implementation.
        // Represent a specific use case related to the validator type
        // Must be unique in the type.
        public String getName() {
            return "defaultHelloValidator";
        }

        public void validate(Map<String, Object> args) throws ArtifactValidationException {

        }
    }
    ```

5.  Add the `args` parameter, which contains the validator configuration provided in the
    `mule-agent.yml` file:

    ```java
      services:
    	mule.agent.artifact.validator.service:
    		enabled: true
    		validators:
    		- type: helloValidator
    			name: defaultHelloValidator
    			enabled: true
    			args:
    				business: finance
    				welcomeMessage: 'Hi, welcome to Finance Cluster Runtimes'
    				errorMessage: 'I am sorry, you are not allowed to deploy in this Cluster'
    ```

    It also contains the following values that are injected by the service:

<table><colgroup><col> <col></colgroup><thead><tr><th>Key</th><th>Description</th></tr></thead><tbody><tr><td><div><div><p><code>_APPLICATION_NAME</code></p></div></div></td><td><div><div><p>A string value that represents the name of the application.</p></div></div></td></tr><tr><td><div><div><p><code>_APPLICATION_FILE_PATH</code></p></div></div></td><td><div><div><p>A string value that represents the path of the temporary file used to deploy the application.</p></div></div></td></tr><tr><td><div><div><p><code>_APPLICATION_PROPERTIES</code></p></div></div></td><td><div><div><p>A Map&lt;String, String&gt; that represents the application properties.</p></div></div></td></tr></tbody></table>

### Implement Business Logic

In the following example, every application deployed in the Mule runtime must have an application
property called `business`. Its value represents the department owner of the application.

```java
    string applicationBusiness =
       (applicationProperties == null)
          ? null
          : applicationProperties.get("business");

    if (applicationBusiness != null && applicationBusiness.equals(allowedBusiness)) {
        LOGGER.info(welcomeMessage);
        return;
    }

    throw new ArtifactValidationException(errorMessage);
```

### Implement Custom Exceptions

Create a custom exception that represents the previous error.

In the package `org.example`, add a new package called `exceptions` and create the following class:

```java
public class BusinessNotAllowedException extends ApplicationValidationException {

    public BusinessNotAllowedException(String message) {
        super(message);
    }
}
```

The project directory layout shows the following:

```java
./mule-agent-hello-validator/src/main/java/org/example/agent/exceptions
./mule-agent-hello-validator/src/main/java/org/example/agent/exceptions/BusinessNotAllowedException.java
```

Replace the generic `ApplicationValidationException` to `BusinessNotAllowedException`:

```java
    throw new BusinessNotAllowedException(errorMessage);
```

### Add the Guice Configuration

1.  Create the `BaseModuleProvider` class.

    In the package `org.example`, add a new package called `configuration.guice` and create the
    following class:

    ```java
    public class MuleAgentHelloValidatorProvider extends BaseModuleProvider {
        @Override
        protected void configureModule(Binder binder) {
            bindNamedSingleton(binder, ApplicationValidator.class, MuleAgentHelloValidator.class);
        }
    }
    ```

2.  Create the `BaseModuleProvider` class.

    In the directory `src/main`, add a new folder called `META-INF/services` and create the
    following file:

    Name: `com.mulesoft.agent.configuration.guice.ModuleProvider`

    Content: `com.example.configuration.guice.MuleAgentHelloValidatorProvider`

The project directory layout shows the following:

```java
./mule-agent-hello-validator/src/main/resources
./mule-agent-hello-validator/src/main/resources/META-INF
./mule-agent-hello-validator/src/main/resources/META-INF/services
./mule-agent-hello-validator/src/main/resources/META-INF/services/com.mulesoft.agent.configuration.guice.ModuleProvider
./mule-agent-hello-validator/src/main/java/org/example/agent/configuration
./mule-agent-hello-validator/src/main/java/org/example/agent/configuration/guice
./mule-agent-hello-validator/src/main/java/org/example/agent/configuration/guice/MuleAgentHelloValidatorProvider.java
```

### Build the Application Validator

Run the following command:

```console
mvn clean install
```

A JAR file is generated in the target directory:

```java
./mule-agent-hello-validator/target
./mule-agent-hello-validator/target/mule-agent-hello-validator-1.0-SNAPSHOT.jar
```

### Install the Application Validator

1.  Place the JAR file in `$MULE_HOME/server-plugins/mule-agent-plugin/lib`.

2.  Access the `args` parameter keys and values and print them to the console:

    ```java
    public void validate(Map<String, Object> args) throws ApplicationValidationException {

        // Values injected by the MuleAgentApplicationValidator service
        String applicationName = (String) args.get("_APPLICATION_NAME");
        String applicationFilePath = (String) args.get("_APPLICATION_FILE_PATH");
        Map<String, String> applicationProperties = (Map) args.get("_APPLICATION_PROPERTIES");

        // Validator configuration values
        String business = (String) args.get("business");
        String welcomeMessage = (String) args.get("welcomeMessage");
        String errorMessage = (String) args.get("errorMessage");

        LOGGER.info("Values injected by the service:");
        LOGGER.info("\tApplication name: {}", applicationName);
        LOGGER.info("\tApplication file path: {}", applicationFilePath);
        LOGGER.info("\tApplication properties: {}", applicationProperties);

        LOGGER.info("Validator configurations:");
        LOGGER.info("\tBusiness: {}", business);
        LOGGER.info("\tWelcome message: {}", welcomeMessage);
        LOGGER.info("\tError message: {}", errorMessage);
    }
    ```

### Update the mule-agent.yml File

Add the validation configuration in the service:

```java
services:
	mule.agent.artifact.validator.service:
		enabled: true
		validators:
		- type: helloValidator
			name: defaultHelloValidator
			enabled: true
			args:
				business: finance
				welcomeMessage: 'Hi, welcome to Finance Cluster Runtimes'
				errorMessage: 'I am sorry, you are not allowed to deploy in this Cluster'
```

### Start Mule Runtime

Start Mule Runtime and verify that the validator configuration was successful. The console shows the
following logs:

```console
AgentArtifactValidatorService: Total validators loaded: 1
AgentArtifactValidatorService: Validators listed by type:
AgentArtifactValidatorService: 	Type: helloValidator Names: [defaultHelloValidator]
```

## Deploy an Application

Once the validator is built and installed, the Agent Validator Service invokes the custom validator
when you deploy an application:

Curl command:

```console
curl -X PUT 'http://localhost:9999/mule/applications/app-01' \
-H 'Content-Type: application/json' \
-d '{
  "url": "file:/tmp/app-01.jar",
  "configuration": {
    "mule.agent.application.properties.service": {
      "applicationName": "app-01",
      "properties": {
          "business": "finance"
      }
    }
  }
}'
```

Response:

```console
{
  "application": {
    "name": "app-01"
  },
  "status": "Deployment attempt started"
}
```

Console logs:

```console
MuleAgentHelloValidator: Values injected by the service:
MuleAgentHelloValidator:   Application name: app-01
MuleAgentHelloValidator:   Application file path: /tmp/mule-received-artifact-1793122713159733968/app-01.jar
MuleAgentHelloValidator:   Application properties: {business=finance}
MuleAgentHelloValidator: Validator configurations:
MuleAgentHelloValidator:   Allowed Business: finance
MuleAgentHelloValidator:   Welcome message: Hi, welcome to Finance Cluster Runtimes
MuleAgentHelloValidator:   Error message: I am sorry you not allowed to be deployed in this Cluster
MuleAgentHelloValidator: Hi, welcome to Finance Cluster Runtimes
```

## Encrypt Values in the Validator Configuration

If the validator requires a secret provided in the `mule-agent.yml`, you can use the `amc_setup.sh`
script to encrypt secrets, passwords, or any other value. For more information, visit
[Encrypt Passwords](https://docs.mulesoft.com/runtime-manager/installing-and-configuring-runtime-manager-agent#encrypt-passwords).

1.  Encrypt a single value

    Set the `AGENT_VAR_master_password` environment variable to the main password:

    `export AGENT_VAR_master_password=myPassword`

2.  Run the encryption utility:

    `$MULE_HOME/bin/amc_setup --encrypt my-secret-value`

    Output:

    ```console
    Mule Agent Installer
    -----------------------------


    	INFO: The encrypted value to paste on the mule-agent.yml file is: '![PBEWITHSHA1ANDDESEDE,wFE1D5V4DMb0uG77mzU+gibrlmnj3Kzb]'
    ```

3.  Add the encrypted value in the validator configuration services:

```yaml
mule.agent.artifact.validator.service:
  enabled: true
  validators:
    - type: helloValidator
      name: defaultHelloValidator
      enabled: true
      args:
        business: finance
        welcomeMessage: "Hi, welcome to Finance Cluster Runtimes"
        errorMessage: "I am sorry you not allowed to be deployed in this Cluster"
        secret: "![PBEWITHSHA1ANDDESEDE,wFE1D5V4DMb0uG77mzU+gibrlmnj3Kzb]"
```

## Decrypt Values in the Validator

You can use the encryption service to decrypt values.

1.  Inject the encryption service into your validator class:

        @Inject
        EncryptionService encryptionService;

2.  Decrypt the value:

    ```java
    String secret = (String) args.get("secret");

    String secretPlainText = null;

    try {
       secretPlainText = encryptionService.decrypt(secret);
    } catch (AgentEncryptionException e) {
       LOGGER.error(e);
    }

    LOGGER.info("\tSecret: {}", secret);
    LOGGER.info("\tSecret (plain text): {}", secretPlainText);
    ```

## Use case: Pass Encrypted Values to the Validator

Curl command:

```console
curl -X PUT 'http://localhost:9999/mule/applications/app-01' \
-H 'Content-Type: application/json' \
-d '{
  "url": "file:/tmp/app-01.jar",
  "configuration": {
    "mule.agent.application.properties.service": {
      "applicationName": "app-01",
      "properties": {
          "business": "finance"
      }
    }
  }
}'
```

Response:

```console
{
  "application": {
    "name": "app-01"
  },
  "status": "Deployment attempt started"
}
```

Console logs:

```console
AgentApplicationService: Deploying the app-01 application from URL file:/tmp/app-01.jar
MuleAgentHelloValidator: Values injected by the service:
MuleAgentHelloValidator: Application name: app-01
MuleAgentHelloValidator: Application file path: /tmp/mule-received-artifact-6683649864468336756/app-01.jar
MuleAgentHelloValidator: Application properties: {business=finance}
MuleAgentHelloValidator: Validator configurations:
MuleAgentHelloValidator: Allowed Business: finance
MuleAgentHelloValidator: Welcome message: Hi, welcome to Finance Cluster Runtimes
MuleAgentHelloValidator: Secret: ![PBEWITHSHA1ANDDESEDE,wFE1D5V4DMb0uG77mzU+gibrlmnj3Kzb]
MuleAgentHelloValidator: Secret (plain text): my-secret-value
MuleAgentHelloValidator: Hi, welcome to Finance Cluster Runtimes
```

## See Also

- [Runtime Manager Agent](https://docs.mulesoft.com/runtime-manager/runtime-manager-agent)

- [Advanced Usage](https://docs.mulesoft.com/runtime-manager/advanced-usage)

- [JAR Signature Validator](https://docs.mulesoft.com/runtime-manager/rtm-agent-jar-signature-validator)
