Skip to content

Commit aa7534b

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 f7a8ab9 commit aa7534b

File tree

6 files changed

+181
-21
lines changed

6 files changed

+181
-21
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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ Currently, {ProductShortName} supports the following providers:
2525
* Builtin
2626
* Java
2727
* Go
28-
* External providers (for `Python`, `Dotnet` and `Node.js` applications) initialized by the generic provider binary
28+
* External providers (for `Python`, `C#` 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: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 `.NET` SDK 9.x or higher
17+
* You installed `ilspycmd` and `paket` for dependency analysis
18+
* You exported the dotnet tools path by using the `export PATH="$PATH:<path/to/.dotnet/tools"` command
19+
20+
.Procedure
21+
. Create a `csharp-rule.yaml` file in your `Home` directory.
22+
23+
. Copy the following rule in the `csharp-rule.yaml` file:
24+
+
25+
26+
[source, yaml]
27+
----
28+
- category: mandatory
29+
customVariables: []
30+
description: WebMatrix.WebData.WebSecurity is not available in .NET Core
31+
effort: 8
32+
labels:
33+
- konveyor.io/source=dotnet
34+
- konveyor.io/target=dotnet-core
35+
links:
36+
- title: Introduction to Identity on ASP.NET Core
37+
url: https://learn.microsoft.com/en-us/aspnet/core/security/authentication/identity
38+
- title: Migrate Authentication and Identity to ASP.NET Core
39+
url: https://learn.microsoft.com/en-us/aspnet/core/migration/identity
40+
message: |
41+
WebMatrix.WebData.WebSecurity is not available in .NET Core and must be replaced with ASP.NET Core Identity.
42+
43+
Migration actions:
44+
- Add Microsoft.AspNetCore.Identity.EntityFrameworkCore NuGet package
45+
- Create ApplicationUser class inheriting from IdentityUser
46+
- Update DbContext to inherit from IdentityDbContext<ApplicationUser>
47+
- Replace WebSecurity.Login with SignInManager.PasswordSignInAsync
48+
- Replace WebSecurity.Logout with SignInManager.SignOutAsync
49+
- Replace WebSecurity.CreateUserAndAccount with UserManager.CreateAsync
50+
- Replace WebSecurity.ChangePassword with UserManager.ChangePasswordAsync
51+
- Configure Identity in Startup.ConfigureServices with AddIdentity or AddDefaultIdentity
52+
ruleID: dotnet-core-websecurity-01
53+
when:
54+
csharp.referenced:
55+
location: ALL
56+
pattern: WebMatrix.WebData.WebSecurity
57+
----
58+
59+
. Clone the `c-sharp-analyzer-provider` respository that has the application in the `test-data/nerd-dinner` path:
60+
+
61+
62+
[subs="+quotes"]
63+
----
64+
$ git clone https://github.com/konveyor/c-sharp-analyzer-provider.git
65+
----
66+
67+
. Run the following command in the {ProductShortName} CLI:
68+
+
69+
70+
[source, terminal]
71+
----
72+
$ ./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
73+
----
74+
+
75+
76+
[NOTE]
77+
====
78+
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.
79+
====
80+
81+
. Open the static report at _path_to_report_ in your browser.
82+
83+
. Navigate to the issues to verify the *`WebMatrix.WebData.WebSecurity` is not available in `.NET Core`* issue.
84+

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,29 @@
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+
The `csharp` provider you to install `.NET` software development kit (SDK) 9.x or later.
15+
16+
== `referenced`
17+
18+
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.
1519

1620
[source,yaml]
1721
----
1822
when:
19-
dotnet.referenced:
23+
csharp.referenced:
2024
pattern: "<pattern>"
21-
namespace: "<namespace>"
25+
location: CLASS
2226
----
2327
where:
2428

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`.
29+
`pattern` :: A regular expression pattern to match the needed reference. For example, `System.Web.Http.ApiController.*`
30+
`location` :: Specifies the location in the source code to search the pattern. For example, `CLASS`.
31+
32+
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 & 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
@@ -444,13 +445,75 @@ when:
444445
lowerbound: v4.2.0
445446
----
446447
447-
.3+|xref:yaml-dotnet-provider_rules-prov-cond[dotnet]
448+
.2+|xref:yaml-dotnet-provider_rules-prov-cond[csharp]
448449
.2+|referenced
449450
|pattern
450451
|Yes
451-
|Regular expression to match a reference in the source code. For example, `HttpNotFound`.
452+
|Regular expression to match a reference in the source code. For example, `System.Web.Mvc.*`.
452453

453-
|namespace
454+
|location
454455
|Yes
455-
|Specify the namespace within which the search query must be run. For example, `System.Web.Mvc`.
456+
a|Specify one of the following for which the search query must be run.
457+
458+
* Type reference including classes, interfaces and structure types (struct)
459+
460+
For example,
461+
[subs="+quotes"]
462+
----
463+
when:
464+
csharp.referenced:
465+
location: CLASS
466+
pattern: System.Web.Http.ApiController.*
467+
----
468+
For example,
469+
[subs="+quotes"]
470+
----
471+
when:
472+
csharp.referenced:
473+
location: ALL
474+
pattern: "public struct *"
475+
----
476+
477+
* Method calls and definitions
478+
479+
For example,
480+
[subs="+quotes"]
481+
----
482+
when:
483+
csharp.referenced:
484+
location: METHOD
485+
pattern: "*.AppDomain.Unload"
486+
----
487+
488+
* Field usages and declaration
489+
490+
For example,
491+
[subs="+quotes"]
492+
----
493+
when:
494+
csharp.referenced:
495+
location: FIELD
496+
pattern: "public DateTime"
497+
----
498+
499+
* Namespace imports and usages
500+
501+
For example,
502+
[subs="+quotes"]
503+
----
504+
when:
505+
csharp.referenced:
506+
location: ALL
507+
pattern: "System.Windows.Forms"
508+
----
509+
510+
* `ALL` - You can also specify `ALL` as `location` to run a search query namespaces, structure types, and interfaces.
511+
512+
For example,
513+
[subs="+quotes"]
514+
----
515+
when:
516+
csharp.referenced:
517+
location: ALL
518+
----
456519
|===

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ 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} `C#` extension
13+
.. `Rust` 1.70+ with `cargo`
14+
.. Protocol Buffers compiler (protoc)
15+
.. `.NET` SDK 9.x or higher
16+
.. Optional: `ilspycmd` and `paket` for dependency analysis
1117
* 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].
1218
1319
.Procedure

0 commit comments

Comments
 (0)