Skip to content

Commit 1dd58fa

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 1dd58fa

File tree

6 files changed

+184
-24
lines changed

6 files changed

+184
-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 & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,24 @@ 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
26-
* Java
27-
* Go
28-
* External providers (for `Python`, `Dotnet` and `Node.js` applications) initialized by the generic provider binary
26+
* Java
27+
* External providers (for `Python`, `Csharp`,`Go`, and `Node.js` applications)
2928
3029
[NOTE]
3130
====
3231
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].
3332
====
3433

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

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.
36+
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.
3837

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

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 the `nerd-dinner` example project.
13+
14+
.Prerequisites
15+
16+
* You installed `ilspycmd` and `paket` dependencies
17+
* You installed `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 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 run.
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#` 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: 83 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@ 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 provider
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} is a Technology Preview feature only. Technology Preview features are not supported with Red Hat production service level agreements (SLAs) and might not be functionally complete. Red Hat does not recommend using them in production. These features provide early access to upcoming product features, enabling customers to test functionality and provide feedback during the development process.
37+
38+
For more information about the support scope of Red Hat Technology Preview features, see link:https://access.redhat.com/support/offerings/techpreview/[Technology Preview Features Support Scope].
3839
====
3940
:!FeatureName:
4041

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

5152
| Providers that have rules already defined in the product
5253
a|
53-
* `dotnet`
54+
* `csharp`
5455
* `java`
5556

5657
|Providers that require custom rulesets for analysis
@@ -163,6 +164,17 @@ when:
163164
go.dependency:
164165
<fields>
165166
----
167+
168+
|`csharp`
169+
|references
170+
a|Find references to a pattern.
171+
For example,
172+
[source,yaml]
173+
----
174+
when:
175+
csharp.referenced:
176+
<fields>
177+
----
166178
|===
167179

168180
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 +456,75 @@ when:
444456
lowerbound: v4.2.0
445457
----
446458
447-
.3+|xref:yaml-dotnet-provider_rules-prov-cond[dotnet]
459+
.2+|xref:yaml-dotnet-provider_rules-prov-cond[csharp]
448460
.2+|referenced
449461
|pattern
450462
|Yes
451-
|Regular expression to match a reference in the source code. For example, `HttpNotFound`.
463+
|Regular expression to match a reference in the source code. For example, `System.Web.Mvc.*`.
452464

453-
|namespace
465+
|location
454466
|Yes
455-
|Specify the namespace within which the search query must be run. For example, `System.Web.Mvc`.
467+
a|Specify one of the following for which the search query must be run.
468+
469+
* Type reference including classes, interfaces and structure types (struct)
470+
471+
For example,
472+
[subs="+quotes"]
473+
----
474+
when:
475+
csharp.referenced:
476+
location: CLASS
477+
pattern: System.Web.Http.ApiController.*
478+
----
479+
For example,
480+
[subs="+quotes"]
481+
----
482+
when:
483+
csharp.referenced:
484+
location: ALL
485+
pattern: "System.Console.Print*"
486+
----
487+
488+
* Method calls and definitions
489+
490+
For example,
491+
[subs="+quotes"]
492+
----
493+
when:
494+
csharp.referenced:
495+
location: METHOD
496+
pattern: "*.AppDomain.Unload"
497+
----
498+
499+
* Field usages and declaration
500+
501+
For example,
502+
[subs="+quotes"]
503+
----
504+
when:
505+
csharp.referenced:
506+
location: FIELD
507+
pattern: "DotNetOpenAuth.AspNet.AuthenticationResult.Provider"
508+
----
509+
510+
* Namespace imports and usages
511+
512+
For example,
513+
[subs="+quotes"]
514+
----
515+
when:
516+
csharp.referenced:
517+
location: ALL
518+
pattern: "System.Windows.Forms"
519+
----
520+
521+
* `ALL` - You can also specify `ALL` as `location` to run a search query namespaces, structure types, and interfaces.
522+
523+
For example,
524+
[subs="+quotes"]
525+
----
526+
when:
527+
csharp.referenced:
528+
location: ALL
529+
----
456530
|===

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 VS 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)