Skip to content

Commit f55bc9d

Browse files
author
Prabha Kylasamiyer Sundara Rajan
committed
MTA-6471 C# Provider Documentation
Signed-off-by: Prabha Kylasamiyer Sundara Rajan <pkylasam@pkylasam-thinkpadp16vgen1.bengluru.csb>
1 parent 5e9a352 commit f55bc9d

File tree

6 files changed

+173
-24
lines changed

6 files changed

+173
-24
lines changed

assemblies/rules-development-guide/assembly_creating-rule.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ include::topics/rules-development/create-nodejs-custom-rule.adoc[leveloffset=+1]
3030

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

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

3435
ifdef::parent-context-of-creating-rule[:context: {parent-context-of-creating-rule}]
3536
ifndef::parent-context-of-creating-rule[:!context:]

assemblies/rules-development-guide/assembly_rule-yaml-conditions.adoc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,25 @@ endif::[]
1616
:context: rules-prov-cond
1717

1818
[role="_abstract"]
19-
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.
19+
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.
2020

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

2323
Currently, {ProductShortName} supports the following providers:
2424

2525
* Builtin
2626
* Java
27-
* Go
28-
* External providers (for `Python`, `Dotnet` and `Node.js` applications) initialized by the generic provider binary
27+
* Csharp
28+
* External providers (for `Python`, `Go`, and `Node.js` applications) initialized by the generic provider binary
2929
3030
[NOTE]
3131
====
3232
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].
3333
====
3434

35-
.Using the provider capability in custom rules
35+
== Using the provider capability in custom rules
3636

37-
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.
37+
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.
3838

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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Module included in the following assemblies:
2+
//
3+
// * docs/rules-development-guide/master.adoc
4+
5+
:_mod-docs-content-type: PROCEDURE
6+
[id="create-csharp-custom-rule_{context}"]
7+
= Creating a custom C# rule
8+
9+
[role="_abstract"]
10+
You can create custom rules for `C#` applications based on the following example.
11+
12+
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.
13+
14+
.Prerequisites
15+
16+
* You installed the `ilspycmd` and `paket` dependencies.
17+
* You installed the `dotnet tools` and exported the `dotnet tools` path by using the `export PATH="$PATH:<path/to/.dotnet/tools"` command.
18+
19+
.Procedure
20+
. Create a `csharp-rule.yaml` file in your `Home` directory.
21+
22+
. Copy the following rule in the `csharp-rule.yaml` file:
23+
+
24+
25+
[source, yaml]
26+
----
27+
- category: mandatory
28+
customVariables: []
29+
description: WebMatrix.WebData.WebSecurity is not available in .NET Core
30+
effort: 8
31+
labels:
32+
- konveyor.io/source=dotnet
33+
- konveyor.io/target=dotnet-core
34+
links:
35+
- title: Introduction to Identity on ASP.NET Core
36+
url: https://learn.microsoft.com/en-us/aspnet/core/security/authentication/identity
37+
- title: Migrate Authentication and Identity to ASP.NET Core
38+
url: https://learn.microsoft.com/en-us/aspnet/core/migration/identity
39+
message: |
40+
WebMatrix.WebData.WebSecurity is not available in .NET Core and must be replaced with ASP.NET Core Identity.
41+
42+
Migration actions:
43+
- Add Microsoft.AspNetCore.Identity.EntityFrameworkCore NuGet package
44+
- Create ApplicationUser class inheriting from IdentityUser
45+
- Update DbContext to inherit from IdentityDbContext<ApplicationUser>
46+
- Replace WebSecurity.Login with SignInManager.PasswordSignInAsync
47+
- Replace WebSecurity.Logout with SignInManager.SignOutAsync
48+
- Replace WebSecurity.CreateUserAndAccount with UserManager.CreateAsync
49+
- Replace WebSecurity.ChangePassword with UserManager.ChangePasswordAsync
50+
- Configure Identity in Startup.ConfigureServices with AddIdentity or AddDefaultIdentity
51+
ruleID: dotnet-core-websecurity-01
52+
when:
53+
csharp.referenced:
54+
location: ALL
55+
pattern: WebMatrix.WebData.WebSecurity
56+
----
57+
58+
. Open a C# project that has the `WebMatrix.WebData.WebSecurity` class.
59+
60+
. Run an analysis with the following command in the {ProductShortName} CLI:
61+
+
62+
63+
[source, terminal]
64+
----
65+
$ ./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
66+
----
67+
+
68+
69+
[NOTE]
70+
====
71+
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.
72+
====
73+
74+
. Open the static report at _path_to_report_ in your browser.
75+
76+
. Navigate to the issues to verify the *`WebMatrix.WebData.WebSecurity` is not available in `.NET Core`* issue.
77+

docs/topics/rules-development/yaml-dotnet-provider.adoc

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,27 @@
44

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

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

12-
.`referenced`
12+
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.
1313

14-
By using the `referenced` capability, the provider finds references in the source code.
14+
`referenced`::
15+
16+
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.
1517

1618
[source,yaml]
1719
----
1820
when:
19-
dotnet.referenced:
21+
csharp.referenced:
2022
pattern: "<pattern>"
21-
namespace: "<namespace>"
23+
location: CLASS
2224
----
2325
where:
2426

25-
`pattern` :: A regular expression pattern to match the desired reference. For example, `HttpNotFound`.
26-
`namespace` :: Specifies the namespace to search within. For example, `System.Web.Mvc`.
27+
* `pattern` :: A regular expression pattern to match the needed reference. For example, `System.Web.Http.ApiController.*`
28+
* `location` :: Specifies the location in the source code to search the pattern. For example, `CLASS`.
29+
+
30+
NOTE: You can also specify `location` as `METHOD`, `FIELD`, or `ALL`. When the `location` is `ALL`, the `C#` provider runs a search query for namespaces, structure types, and interfaces.

docs/topics/rules-development/yaml-provider-conditions.adoc

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,13 @@ The analyzer currently supports the following `provider` conditions:
2727
* `go`
2828
* `nodejs`
2929
* `python`
30-
* `dotnet`
31-
32-
:FeatureName: Dotnet provider
30+
* `csharp`
3331
32+
:FeatureName: Csharp, Python, and Nodejs providers
3433
[IMPORTANT]
3534
====
3635
[subs="attributes+"]
37-
{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.
36+
{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.
3837
====
3938
:!FeatureName:
4039

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

5150
| Providers that have rules already defined in the product
5251
a|
53-
* `dotnet`
52+
* `csharp`
5453
* `java`
5554

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

104103

105-
|`json`
104+
|json
106105
a|Search JSON files using `jsonpath` queries.
107106
For example,
108107
[source, yaml]
@@ -163,6 +162,17 @@ when:
163162
go.dependency:
164163
<fields>
165164
----
165+
166+
|`csharp`
167+
|referenced
168+
a|Find references to a pattern.
169+
For example,
170+
[source,yaml]
171+
----
172+
when:
173+
csharp.referenced:
174+
<fields>
175+
----
166176
|===
167177

168178
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.
@@ -444,13 +454,65 @@ when:
444454
lowerbound: v4.2.0
445455
----
446456
447-
.3+|xref:yaml-dotnet-provider_rules-prov-cond[dotnet]
457+
.2+|xref:yaml-dotnet-provider_rules-prov-cond[csharp]
448458
.2+|referenced
449459
|pattern
450460
|Yes
451-
|Regular expression to match a reference in the source code. For example, `HttpNotFound`.
461+
|Regular expression to match a reference in the source code. For example, `System.Web.Mvc.*`.
452462

453-
|namespace
463+
|location
454464
|Yes
455-
|Specify the namespace within which the search query must be run. For example, `System.Web.Mvc`.
465+
a|Specify one of the following for which {ProductShortName} runs a search query:
466+
467+
* Type reference including classes, interfaces and structure types (struct). For example:
468+
+
469+
470+
[subs="+quotes"]
471+
----
472+
when:
473+
csharp.referenced:
474+
location: CLASS
475+
pattern: System.Web.Http.ApiController.*
476+
----
477+
For example,
478+
479+
[subs="+quotes"]
480+
----
481+
when:
482+
csharp.referenced:
483+
location: ALL
484+
pattern: "System.Console.Print*"
485+
----
486+
487+
* Method calls and definitions. For example:
488+
489+
[subs="+quotes"]
490+
----
491+
when:
492+
csharp.referenced:
493+
location: METHOD
494+
pattern: "*.AppDomain.Unload"
495+
----
496+
497+
* Field usages and declaration. For example:
498+
499+
[subs="+quotes"]
500+
----
501+
when:
502+
csharp.referenced:
503+
location: FIELD
504+
pattern: "DotNetOpenAuth.AspNet.AuthenticationResult.Provider"
505+
----
506+
507+
* Namespace imports and usages. For example:
508+
509+
[subs="+quotes"]
510+
----
511+
when:
512+
csharp.referenced:
513+
location: ALL
514+
pattern: "System.Windows.Forms"
515+
----
516+
517+
* `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.
456518
|===

docs/topics/vscode/proc_vscode-analyzing-application.adoc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ You can run a static code analysis of an application with or without enabling th
77

88
.Prerequisites
99

10-
* You opened a Java project in your VS Code workspace.
10+
* You opened a project in your Visual Studio Code workspace.
11+
* You installed the following for `.NET` or `C#` application analysis:
12+
.. The {ProductShortName} Core and `C#` extensions
13+
.. `dotnet tools` and added it to the `$PATH` environment variable
14+
.. `ilspycmd` command line tool for the `ILSpy.NET` decompiler
15+
.. `paket` package manager
1116
* 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].
1217
1318
.Procedure

0 commit comments

Comments
 (0)