-
Notifications
You must be signed in to change notification settings - Fork 8
MTA-5174 Add Custom Rules in More Languages #272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+235
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| // Module included in the following assemblies: | ||
| // | ||
| // * docs/rules-development-guide/master.adoc | ||
|
|
||
| :_mod-docs-content-type: PROCEDURE | ||
| [id="create-go-custom-rule_{context}"] | ||
| = Creating a custom Go rule | ||
|
|
||
| [role="_abstract"] | ||
| You can create custom rules for Golang (Go) applications based on the following example. | ||
|
|
||
| You can use the following custom rule to check if {ProductShortName} triggers an incident when it detects a `go` file in your project. | ||
|
|
||
| .Procedure | ||
| . Create a `go-rule-001.yml` file in a directory. | ||
|
|
||
| . Copy the following rule in the `yaml` file: | ||
| + | ||
|
|
||
| [source, yaml] | ||
| ---- | ||
| - message: golang apiextensions/v1/customresourcedefinitions found | ||
| description: "golang apiextensions/v1/customresourcedefinitions found" | ||
| ruleID: go-lang-ref-001 | ||
| effort: 1 | ||
| when: | ||
| go.referenced: | ||
| pattern: "v1beta1.CustomResourceDefinition" | ||
| ---- | ||
|
|
||
| . Create a test `go` file named *example.go* in your `Home` directory. | ||
|
|
||
| . Paste the following code in the *example.go* file: | ||
| + | ||
|
|
||
| [source, go] | ||
| ---- | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" | ||
| ) | ||
|
|
||
| func main() { | ||
| fmt.Println(v1beta1.CustomResourceDefinition{}) | ||
|
|
||
| } | ||
| ---- | ||
|
|
||
| . Run the following command in the {ProductShortName} CLI: | ||
| + | ||
|
|
||
| [source, terminal] | ||
| ---- | ||
| $ ./mta-cli analyze -i _<path_to_Go_project>_ -o _<path_to_report_directory>_ \ --run-local=false --rules _<path_to_go-rule-001.yml>_ | ||
| ---- | ||
| + | ||
|
|
||
| [NOTE] | ||
| ==== | ||
| Add the `--overwrite` option if you want to use the same directory for the report when you run subsequent tests. {ProductShortName} 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 found {{file}}:{{lineNumber}}` issue. | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // Module included in the following assemblies: | ||
| // | ||
| // * docs/rules-development-guide/master.adoc | ||
|
|
||
| :_mod-docs-content-type: PROCEDURE | ||
| [id="create-nodejs-custom-rule_{context}"] | ||
| = Creating a custom Node.js rule | ||
|
|
||
| [role="_abstract"] | ||
| You must create custom rules to analyze `Node.js` applications by using {ProductShortName}. A `Node.js` rule can contain `nodejs.referenced` capability which 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-nodejs` directory. | ||
| + | ||
|
|
||
| [source, terminal] | ||
| ---- | ||
| $ mkdir -p ~/test-nodejs | ||
| ---- | ||
|
|
||
| . Save the following rule as `nodejs-rule-001.yml` in the `test-nodejs` directory: | ||
| + | ||
| [source, yaml] | ||
| ---- | ||
| - ruleID: test-tsx-support-00000 | ||
| description: Found React import in .tsx file | ||
| message: Found React import in .tsx file | ||
| effort: 1 | ||
| when: | ||
| nodejs.referenced: | ||
| pattern: "React" | ||
| ---- | ||
|
|
||
| . Create the following test application in the `Component.tsx` file: | ||
| + | ||
|
|
||
Pkylas007 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| [source, node.js] | ||
| ---- | ||
| import React from 'react'; | ||
| export const MyComponent: React.FC = () => <div>Hello</div>; | ||
| ---- | ||
|
|
||
| . Run the following command in the {ProductShortName} CLI: | ||
| + | ||
|
|
||
| [source, terminal] | ||
| ---- | ||
| $ ./mta-cli analyze -i ~/test-nodejs/ -o \ | ||
Pkylas007 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ~/test-nodejs/report --run-local=false \ | ||
| --rules ~/test-nodejs/nodejs-rule-001.yml | ||
| ---- | ||
| + | ||
|
|
||
Pkylas007 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| [NOTE] | ||
| ==== | ||
| Add the `--overwrite` option if you want to use the same directory for the report when you run subsequent tests. {ProductShortName} 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.html` in your browser. | ||
|
|
||
| . Click the _<application_name>_ to open the Dashboard. | ||
|
|
||
| . Review the incidents in the *Issues* tab. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| // Module included in the following assemblies: | ||
| // | ||
| // * docs/rules-development-guide/master.adoc | ||
|
|
||
| :_mod-docs-content-type: PROCEDURE | ||
| [id="create-python-custom-rule_{context}"] | ||
| = Creating custom Python rules | ||
|
|
||
| [role="_abstract"] | ||
| You must create custom rules to analyze `Python` applications by using {ProductShortName}. A `Python` rule can contain `python.referenced` capability with the supported fields. | ||
|
|
||
| The following example uses two custom rules: | ||
|
|
||
| * The first rule checks if `bad_method` is specified | ||
| * The second rule checks if `hello_world` is specified in `file_a.py` in your project. | ||
|
|
||
| .Procedure | ||
| . Create the directory `test-python`. | ||
| + | ||
| [source, terminal] | ||
| ---- | ||
| $ mkdir -p ~/test-python | ||
| ---- | ||
|
|
||
| . Create a `python-rule-001.yml` file in the directory and add the following rule: | ||
Pkylas007 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| + | ||
| [source, yaml] | ||
| ---- | ||
| - category: mandatory | ||
| ruleID: python-rule-001 | ||
| effort: 1 | ||
| description: "Bad method" | ||
| when: | ||
| python.referenced: | ||
| pattern: "bad_method" | ||
| ---- | ||
|
|
||
| . Create a `python-rule-002.yml` file in the directory and add the following rule: | ||
Pkylas007 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| + | ||
| [source, yaml] | ||
| ---- | ||
| - category: mandatory | ||
| ruleID: python-rule-002 | ||
| effort: 1 | ||
| message: "Found a python" | ||
| when: | ||
| python.referenced: | ||
| pattern: "hello_world" | ||
| ---- | ||
|
|
||
| . Save the following `Python` code as `file_b.py`. | ||
| + | ||
|
|
||
Pkylas007 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| [source, python] | ||
| ---- | ||
| import deprecated | ||
| def hello_world(): | ||
| return "Hello, world!" | ||
| @deprecated.deprecated("This method is bad!") | ||
| def bad_method(): | ||
| return "I'm a bad method!" | ||
| ---- | ||
|
|
||
| . Save the following code as a second file, `file_a.py`. | ||
| + | ||
| [source, python] | ||
| ---- | ||
| import file_b | ||
| print(file_b.hello_world()) | ||
| print(file_b.bad_method()) | ||
| ---- | ||
|
|
||
| . Run the following command in the {ProductShortName} CLI: | ||
| + | ||
| [source, terminal] | ||
| ---- | ||
| $ ./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 | ||
| ---- | ||
| + | ||
|
|
||
Pkylas007 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| [NOTE] | ||
| ==== | ||
| Add the `--overwrite` option if you want to use the same directory for the report when you run subsequent tests. {ProductShortName} 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.html` in your browser. | ||
|
|
||
| . Click the _<application_name>_ to open the Dashboard. | ||
|
|
||
| . Review the incidents in the *Issues* tab. | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.