Skip to content
Open
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 @@ -30,6 +30,7 @@ include::topics/rules-development/create-nodejs-custom-rule.adoc[leveloffset=+1]

include::topics/rules-development/create-python-custom-rule.adoc[leveloffset=+1]

include::topics/rules-development/create-csharp-custom-rule.adoc[leveloffset=+1]

ifdef::parent-context-of-creating-rule[:context: {parent-context-of-creating-rule}]
ifndef::parent-context-of-creating-rule[:!context:]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@
:context: rules-prov-cond

[role="_abstract"]
The providers are the modular components in charge of analyzing a given language. Providers are able to analyze code by leveraging the Language Server Protocol (LSP). Through the LSP, all code analysis is abstracted away from the analysis engine and left to the specific LSP server to run the search query defined in the rule on the source code.
The providers are the modular components in charge of analyzing a given language. The external providers, except `csharp`, are able to analyze code by leveraging the Language Server Protocol (LSP). Through the LSP, all code analysis is abstracted away from the analysis engine and left to the specific LSP server to run the search query defined in the rule on the source code.

Additionally, {ProductShortName} provides a built-in provider with abilities such as XML parsing, running regular expressions on files, and so on.

Currently, {ProductShortName} supports the following providers:

* builtin

Check failure on line 25 in assemblies/rules-development-guide/assembly_rule-yaml-conditions.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [RedHat.TermsErrors] Use 'built-in' rather than 'builtin'. Raw Output: {"message": "[RedHat.TermsErrors] Use 'built-in' rather than 'builtin'.", "location": {"path": "assemblies/rules-development-guide/assembly_rule-yaml-conditions.adoc", "range": {"start": {"line": 25, "column": 3}}}, "severity": "ERROR"}
* Java
* Go
* External providers (for `Python`, `Dotnet` and `Node.js` applications) initialized by the generic provider binary
* Csharp
* External providers (for `Python`, `Go`, and `Node.js` applications) initialized by the generic provider binary

[NOTE]
====
You can use the generic provider binary to create an external provider for any language that is compliant with link:https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/[LSP 3.17 specifications].
====

.Using the provider capability in custom rules
== Using the provider capability in custom rules

In a rule, the when block is where the conditions for matching the rule are specified. Each provider offers a series of capabilities to do matching.. The search query in the rule condition can contain patterns, code locations, specific dependencies to be found, and so on, to evaluate the source code and dependencies. The provider sends the LSP server a request to check the search query against the application being analyzed. When the LSP server returns a match for the search in the source code, the analyzer triggers a violation.
In a rule, the when block is where the conditions for matching the rule are specified. Each provider offers a series of capabilities to do matching. The search query in the rule condition can contain patterns, code locations, specific dependencies to be found, and so on, to evaluate the source code and dependencies. The provider sends the LSP server a request to check the search query against the application being analyzed. When the LSP server returns a match for the search in the source code, the analyzer triggers a violation.

The syntax for the when block is as follows: contains one condition, but that condition can have multiple conditions nested under it.

Expand Down
77 changes: 77 additions & 0 deletions docs/topics/rules-development/create-csharp-custom-rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Module included in the following assemblies:
//
// * docs/rules-development-guide/master.adoc

:_mod-docs-content-type: PROCEDURE
[id="create-csharp-custom-rule_{context}"]
= Creating a custom C# rule

[role="_abstract"]
You can create custom rules for `C#` applications based on the following example.

You can use the following custom rule to check if {ProductShortName} triggers an incident when it detects the `WebMatrix.WebData.WebSecurity` class in a `C#` example project.

.Prerequisites

* You installed the `ilspycmd` and `paket` dependencies.
* You installed the `dotnet tools` and exported the `dotnet tools` path by using the `export PATH="$PATH:<path/to/.dotnet/tools"` command.

.Procedure
. Create a `csharp-rule.yaml` file in your `Home` directory.

. Copy the following rule in the `csharp-rule.yaml` file:
+

[source, yaml]
----
- category: mandatory
customVariables: []
description: WebMatrix.WebData.WebSecurity is not available in .NET Core
effort: 8
labels:
- konveyor.io/source=dotnet
- konveyor.io/target=dotnet-core
links:
- title: Introduction to Identity on ASP.NET Core
url: https://learn.microsoft.com/en-us/aspnet/core/security/authentication/identity
- title: Migrate Authentication and Identity to ASP.NET Core
url: https://learn.microsoft.com/en-us/aspnet/core/migration/identity
message: |
WebMatrix.WebData.WebSecurity is not available in .NET Core and must be replaced with ASP.NET Core Identity.

Migration actions:
- Add Microsoft.AspNetCore.Identity.EntityFrameworkCore NuGet package
- Create ApplicationUser class inheriting from IdentityUser
- Update DbContext to inherit from IdentityDbContext<ApplicationUser>
- Replace WebSecurity.Login with SignInManager.PasswordSignInAsync
- Replace WebSecurity.Logout with SignInManager.SignOutAsync
- Replace WebSecurity.CreateUserAndAccount with UserManager.CreateAsync
- Replace WebSecurity.ChangePassword with UserManager.ChangePasswordAsync
- Configure Identity in Startup.ConfigureServices with AddIdentity or AddDefaultIdentity
ruleID: dotnet-core-websecurity-01
when:
csharp.referenced:
location: ALL
pattern: WebMatrix.WebData.WebSecurity
----

. Open a C# project that has the `WebMatrix.WebData.WebSecurity` class.

. Run an analysis with the following command in the {ProductShortName} CLI:
+

[source, terminal]
----
$ ./mta-cli analyze -i _path_to_nerd-dinner_ -o _path_to_report_ --overwrite --run-local=false --enable-default-rulesets=false --mode source-only --rules ~/csharp-rule.yaml
----
+

[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 ran.
====

. Open the static report at _path_to_report_ in your browser.

. Navigate to the issues to verify the *`WebMatrix.WebData.WebSecurity` is not available in `.NET Core`* issue.

14 changes: 8 additions & 6 deletions docs/topics/rules-development/yaml-dotnet-provider.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@

:_mod-docs-content-type: REFERENCE
[id="yaml-dotnet-provider_{context}"]
= Dotnet provider
= Csharp provider

[role="_abstract"]
The `dotnet` provider is an external provider used to analyze .NET and C# source code. Currently, the provider supports the `referenced` capability.
The `csharp` provider is an external provider used to analyze `.NET` and `C#` source code. Currently, the provider supports the `referenced` capability.

.`referenced`
The `csharp` provider uses a gRPC interface to perform a semantic analysis of an application source code in the `source-only` mode. The provider parses the source code by using tree-sitter and uses stack graph for the analysis to find references to types, methods, classes, and fields. Based on the `C#` custom rule definition, the analyzer identifies violations in your code that you must resolve before the application migration.

By using the `referenced` capability, the provider finds references in the source code.
`referenced`::

The `csharp` provider supports `referenced` capability in rules to define fields such as `pattern` and `location` based on which the provider searches the code for violations.

[source,yaml]
----
when:
dotnet.referenced:
csharp.referenced:
pattern: "<pattern>"
namespace: "<namespace>"
location: CLASS
----
where:

Expand Down
82 changes: 72 additions & 10 deletions docs/topics/rules-development/yaml-provider-conditions.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ The analyzer currently supports the following `provider` conditions:
* `go`
* `nodejs`
* `python`
* `dotnet`

:FeatureName: Dotnet provider
* `csharp`

:FeatureName: Csharp, Python, and Nodejs providers
[IMPORTANT]
====
[subs="attributes+"]
{FeatureName} is a Developer Preview feature only. Developer Preview features are not supported by Red Hat in any way and are not functionally complete or production-ready. Do not use Developer Preview features for production or business-critical workloads. Developer Preview features provide early access to upcoming product features in advance of their possible inclusion in a Red Hat product offering, enabling customers to test functionality and provide feedback during the development process. These features might not have any documentation, are subject to change or removal at any time, and testing is limited. Red Hat might provide ways to submit feedback on Developer Preview features without an associated SLA.
{FeatureName} are Developer Preview features only. Developer Preview features are not supported by Red Hat in any way and are not functionally complete or production-ready. Do not use Developer Preview features for production or business-critical workloads. Developer Preview features provide early access to upcoming product features in advance of their possible inclusion in a Red Hat product offering, enabling customers to test functionality and provide feedback during the development process. These features might not have any documentation, are subject to change or removal at any time, and testing is limited. Red Hat might provide ways to submit feedback on Developer Preview features without an associated SLA.
====
:!FeatureName:

Expand All @@ -50,7 +49,7 @@ The analyzer currently supports the following `provider` conditions:

| Providers that have rules already defined in the product
a|
* `dotnet`
* `csharp`
* `java`

|Providers that require custom rulesets for analysis
Expand Down Expand Up @@ -102,7 +101,7 @@ when:
|Search XML files using xpath queries.


|`json`
|json
a|Search JSON files using `jsonpath` queries.
For example,
[source, yaml]
Expand Down Expand Up @@ -163,6 +162,17 @@ when:
go.dependency:
<fields>
----

|`csharp`
|referenced
a|Find references to a pattern.
For example,
[source,yaml]
----
when:
csharp.referenced:
<fields>
----
|===

Following the example in the previous table, you can create the first part of the condition that does not contain any of the condition fields.
Expand Down Expand Up @@ -444,13 +454,65 @@ when:
lowerbound: v4.2.0
----

.3+|xref:yaml-dotnet-provider_rules-prov-cond[dotnet]
.2+|xref:yaml-dotnet-provider_rules-prov-cond[csharp]
.2+|referenced
|pattern
|Yes
|Regular expression to match a reference in the source code. For example, `HttpNotFound`.
|Regular expression to match a reference in the source code. For example, `System.Web.Mvc.*`.

|namespace
|location
|Yes
|Specify the namespace within which the search query must be run. For example, `System.Web.Mvc`.
a|Specify one of the following for which {ProductShortName} runs a search query:

* Type reference including classes, interfaces and structure types (struct). For example:
+

[subs="+quotes"]
----
when:
csharp.referenced:
location: CLASS
pattern: System.Web.Http.ApiController.*
----
For example,

[subs="+quotes"]
----
when:
csharp.referenced:
location: ALL
pattern: "System.Console.Print*"
----

* Method calls and definitions. For example:

[subs="+quotes"]
----
when:
csharp.referenced:
location: METHOD
pattern: "*.AppDomain.Unload"
----

* Field usages and declaration. For example:

[subs="+quotes"]
----
when:
csharp.referenced:
location: FIELD
pattern: "DotNetOpenAuth.AspNet.AuthenticationResult.Provider"
----

* Namespace imports and usages. For example:

[subs="+quotes"]
----
when:
csharp.referenced:
location: ALL
pattern: "System.Windows.Forms"
----

* `ALL` - You can also specify `ALL` as `location` to run a search query on any location in the code, including namespaces, structure types, and interfaces.
|===
7 changes: 6 additions & 1 deletion docs/topics/vscode/proc_vscode-analyzing-application.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ You can run a static code analysis of an application with or without enabling th

.Prerequisites

* You opened a Java project in your Visual Studio Code workspace.
* You opened a project in your Visual Studio Code workspace.
* You installed the following for `.NET` or `C#` application analysis:
.. The {ProductShortName} Core and `C#` extensions
.. `dotnet tools` and added it to the `$PATH` environment variable
.. `ilspycmd` command line tool for the `ILSpy.NET` decompiler
.. `paket` package manager
* You configured an analysis profile on the *{ProductShortName} Analysis View* page. For more information, see xref:configuring-profile-settings_vsc-extension-guide[Configuring the {ProductShortName} profile settings].

.Procedure
Expand Down
Loading