Chapter 7. Creating custom rules
You can refer to example rules that describe how to create a YAML rule. This assumes that you already have MTA installed. See the MTA CLI Guide for installation instructions.
7.1. Creating a YAML rule template リンクのコピーリンクがクリップボードにコピーされました!
MTA YAML-based rules have the following basic structure:
when(condition) message(message) tag(tags)
when(condition)
message(message)
tag(tags)
Procedure
Create a file containing the basic syntax for YAML rules as follows:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
7.2. Creating a YAML rule リンクのコピーリンクがクリップボードにコピーされました!
You can create and test your first MTA YAML-based rule by completing the following procedure. This assumes that you have already installed MTA. See Installing and running the CLI in the CLI Guide for installation instructions.
In this example, you will create a rule to discover instances where an application defines a jboss-web.xml file containing a <class-loading> element and to provide a link to the documentation that describes how to migrate the code.
-
Create a YAML file for the rule. In the following example, the rule is created in your
$HOMEdirectory.
mkdir /home/<USER>/rule.yaml
$ mkdir /home/<USER>/rule.yaml
Create
jboss-web.xmlandpom.xmlfiles in a directory:mkdir /home/<USER>/data/ touch /home/<USER>/data/jboss-web.xml touch /home/<USER>/data/pom.xml
mkdir /home/<USER>/data/ touch /home/<USER>/data/jboss-web.xml touch /home/<USER>/data/pom.xmlCopy to Clipboard Copied! Toggle word wrap Toggle overflow In the
jboss-web.xmlfile you created, paste the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow In the
pom.xmlfile you created, paste the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow In the
rule.yamlfile you created, paste the following contents:Copy to Clipboard Copied! Toggle word wrap Toggle overflow where:
ruleID-
Unique ID for your rule. For example,
jboss5-web-class-loading. description- Text description of the rule.
whenComplete the
whenblock specifying one or more conditions:Use the
builtinprovider’s XML capability because this rule checks for a match in an XML file.when: builtin.xml: xpath: /jboss-web/class-loadingwhen: builtin.xml: xpath: /jboss-web/class-loadingCopy to Clipboard Copied! Toggle word wrap Toggle overflow
message- Helpful message explaining the migration issue. The message is generated in the report when the rule matches. For example:
message: The class-loading element is no longer valid in the jboss-web.xml file.
message: The class-loading element is no longer valid in the jboss-web.xml file.
labels- List of string labels for the rule.
effort- Number of expected story points to resolve this issue.
links- One or more hyperlinks pointing to documentation around the migration issues that you find.
links:
- url: https://access.redhat.com/documentation/en-US/JBoss_Enterprise_Application_Platform/6.4/html-single/Migration_Guide/index.html#Create_or_Modify_Files_That_Control_Class_Loading_in_JBoss_Enterprise_Application_Platform_6
title: Create or Modify Files That Control Class Loading in JBoss EAP 6
links:
- url: https://access.redhat.com/documentation/en-US/JBoss_Enterprise_Application_Platform/6.4/html-single/Migration_Guide/index.html#Create_or_Modify_Files_That_Control_Class_Loading_in_JBoss_Enterprise_Application_Platform_6
title: Create or Modify Files That Control Class Loading in JBoss EAP 6
The rule is now complete and looks similar to the following:
Point the CLI to the rule file you created :
–rules /home/<USER>/rules.yaml
–rules /home/<USER>/rules.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow To test the rule, point the input to the test data you created and pass the rule using the rules option in MTA CLI:
mta-cli analyze --input /home/<USER>/data/ --output /home/<USER>/output/ --rules /home/<USER>/rules.yaml
mta-cli analyze --input /home/<USER>/data/ --output /home/<USER>/output/ --rules /home/<USER>/rules.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow Review the report to be sure that it provides the expected results.
Once the analysis is complete, the command outputs the path to the HTML report:
INFO[0066] Static report created. Access it at this URL: URL="file:/home/<USER>/output/static-report/index.html"
INFO[0066] Static report created. Access it at this URL: URL="file:/home/<USER>/output/static-report/index.html"Copy to Clipboard Copied! Toggle word wrap Toggle overflow Open
/home/<USER_NAME>/output/static-report/index.htmlin a web browser.- Navigate to the Issues tab in the left menu.
Verify that the rule is executed:
-
In the Issues table, type
JBoss XMLin the search bar. -
Verify that the issue with the title
Find class loading element in JBoss XML fileis present in the table.
-
In the Issues table, type
- Click the jboss-web.xml link to open the affected file.
7.3. Creating a custom Go rule リンクのコピーリンクがクリップボードにコピーされました!
You can create custom rules for Golang (Go) applications based on the following example.
You can use the following custom rule to check if MTA triggers an incident when it detects the v1beta1.CustomResourceDefinition pattern in the go file.
Procedure
-
Create a
go-rule-001.ymlfile in a directory. Copy the following rule in the
yamlfile:Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Create a test
gofile named example.go in yourHomedirectory. Paste the following code in the example.go file:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Run the following command in the MTA CLI:
./mta-cli analyze -i _<path-to-Go-project>_ \ -o _<path-to-report-directory>_ --run-local=false --rules \ _<path-to-go-rule-001.yml>_
$ ./mta-cli analyze -i _<path-to-Go-project>_ \ -o _<path-to-report-directory>_ --run-local=false --rules \ _<path-to-go-rule-001.yml>_Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteAdd the
--overwriteoption if you want to use the same directory for the report when you run subsequent tests. MTA overwrites the current report with the result of the latest analysis that you run.-
Open the static report at
/home/<USER>/output/static-report/in your browser. -
Navigate to the issues to verify the
golang apiextensions/v1/customresourcedefinitions foundissue.
7.4. Creating a custom Node.js rule リンクのコピーリンクがクリップボードにコピーされました!
You must create custom rules to analyze Node.js applications by using MTA. A Node.js rule contains the nodejs.referenced capability that supports the pattern field.
The following example uses a custom rule to check if a .tsx file in the Node.js project imports the React framework.
Procedure
Create the
test-nodejsdirectory.mkdir -p ~/test-nodejs
$ mkdir -p ~/test-nodejsCopy to Clipboard Copied! Toggle word wrap Toggle overflow Save the following rule as
nodejs-rule-001.ymlin thetest-nodejsdirectory:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create the following test application in the
Component.tsxfile:import React from 'react'; export const MyComponent: React.FC = () => <div>Hello</div>;
import React from 'react'; export const MyComponent: React.FC = () => <div>Hello</div>;Copy to Clipboard Copied! Toggle word wrap Toggle overflow Run the following command in the MTA CLI:
./mta-cli analyze -i ~/test-nodejs/ -o \ ~/test-nodejs/report --run-local=false \ --rules ~/test-nodejs/nodejs-rule-001.yml
$ ./mta-cli analyze -i ~/test-nodejs/ -o \ ~/test-nodejs/report --run-local=false \ --rules ~/test-nodejs/nodejs-rule-001.ymlCopy to Clipboard Copied! Toggle word wrap Toggle overflow NoteAdd the
--overwriteoption if you want to use the same directory for the report when you run subsequent tests. MTA overwrites the current report with the result of the latest analysis that you run.-
Open the static report at
~/test-nodejs/report/static-report/index.htmlin your browser. - Click the <application_name> to open the Dashboard.
- Review the incidents in the Issues tab.
7.5. Creating custom Python rules リンクのコピーリンクがクリップボードにコピーされました!
You must create custom rules to analyze Python applications by using MTA. A Python rule contains the python.referenced capability with the supported fields.
The following example uses two custom rules:
-
The first rule checks if
bad_methodis specified -
The second rule checks if
hello_worldis specified infile_a.pyin your project.
Procedure
Create the directory
test-python.mkdir -p ~/test-python
$ mkdir -p ~/test-pythonCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create a
python-rule-001.ymlfile in the directory and add the following rule:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a
python-rule-002.ymlfile in the directory and add the following rule:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Save the following
Pythoncode asfile_b.py.Copy to Clipboard Copied! Toggle word wrap Toggle overflow Save the following code as a second file,
file_a.py.import file_b print(file_b.hello_world()) print(file_b.bad_method())
import file_b print(file_b.hello_world()) print(file_b.bad_method())Copy to Clipboard Copied! Toggle word wrap Toggle overflow Run the following command in the MTA CLI:
./mta-cli analyze -i ~/test-python/ -o \ ~/test-python/report --run-local=false \ --rules ~/test-python/python-rule-001. \ --rules ~/test-python/python-rule-002.yml
$ ./mta-cli analyze -i ~/test-python/ -o \ ~/test-python/report --run-local=false \ --rules ~/test-python/python-rule-001. \ --rules ~/test-python/python-rule-002.ymlCopy to Clipboard Copied! Toggle word wrap Toggle overflow NoteAdd the
--overwriteoption if you want to use the same directory for the report when you run subsequent tests. MTA overwrites the current report with the result of the latest analysis that you run.-
Open the static report at
~/test-python/report/static-report/index.htmlin your browser. - Click the <application_name> to open the Dashboard.
- Review the incidents in the Issues tab.