SecTester.Bus 0.23.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package SecTester.Bus --version 0.23.0                
NuGet\Install-Package SecTester.Bus -Version 0.23.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SecTester.Bus" Version="0.23.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SecTester.Bus --version 0.23.0                
#r "nuget: SecTester.Bus, 0.23.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SecTester.Bus as a Cake Addin
#addin nuget:?package=SecTester.Bus&version=0.23.0

// Install SecTester.Bus as a Cake Tool
#tool nuget:?package=SecTester.Bus&version=0.23.0                

SecTester.Bus

Maintainability Test Coverage Build Status Nuget Downloads

The package includes a simplified implementation of the EventBus, one based on RabbitMQ, to establish synchronous and asynchronous communication between services and agents.

Setup

$ dotnet add package SecTester.Bus

Usage

Overview

To use the RabbitMQ Event Bus, pass the following options object to the constructor method:

const string repeaterId = "your Repeater ID";
var serviceProvider = new ServiceCollection()
  .AddSecTesterConfig("app.neuralegion.com")
  .AddSecTesterBus(repeaterId)
  .BuildServiceProvider();

var bus = serviceProvider.GetService<EventBus>();

The options are specific to the chosen transporter, the package distributes the RabbitMQ implementation by default. The implementation exposes the properties described below:

Option Description
Url EventBus address.
Exchange Exchange name which routes a message to a particular queue.
ClientQueue Queue name which your bus will listen to.
AppQueue Queue name which application will listen to.
PrefetchCount Sets the prefetch count for the channel. By default, 1
ConnectTimeout Time to wait for initial connect. By default, 30 seconds
ReconnectTime The time to wait before trying to reconnect. By default, 20 seconds.
HeartbeatInterval The interval, in seconds, to send heartbeats. By default, 30 seconds.
Username The username to perform authentication.
Password The password to perform authentication.

In case of unrecoverable or operational errors, you will get an exception while initial connecting.

Subscribing to events

To subscribe an event handler to the particular event, you should register the handler in the EventBus as follows:

public record Issue
{
  public string Name;
  public string Details;
  public string Type;
  public string? cvss;
  public string? cwe;
}

public record IssueDetected(Issue Payload) : Event
{
  public Issue Payload = Payload;
}

public class IssueDetectedHandler: EventListener<Issue>
{
  public Task Handle(IssueDetected @event)
  {
    // implementation
  }
}

bus.Register<IssueDetectedHandler, IssueDetected>();

⚡ Make sure that you register the corresponding provider in the IoC. Otherwise, you get an error while receiving an event in the EventBus.

You can also override a event name using the MessageType attribute as follows:

[MessageType(name: "issue-detected")]
public record IssueDetected(Issue Payload) : Event
{
  public Issue Payload = Payload;
}

Now the IssueDetectedHandler event handler listens for the IssueDetected event. As soon as the IssueDetected event appears, the EventBus will call the Handle() method with the payload passed from the application.

To remove subscription, and removes the event handler, you have to call the unregister() method:

await bus.Unregister<IssueDetectedHandler, IssueDetected>();
Publishing events through the event bus

The EventBus exposes a Publish() method. This method publishes an event to the message broker.

public record StatusChanged(string Status): Event
{
  public string Status = Status;
}

var event = new StatusChanged("connected");

await bus.Publish(event);

The Publish() method takes just a single argument, an instance of the derived class of the Event.

⚡ The class name should match one defined event in the application. Otherwise, you should override it by passing the expected name via the constructor or using the MessageType attribute.

For more information, please see SecTester.Core.

Executing RPC methods

The EventBus exposes a Execute() method. This method is intended to perform a command to the application and returns an Task with its response.

public record Version(string Value)
{
  public string Value = Value;
}

public record LastVersion(Version Value)
{
  public Version Value = Value;
}

public record CheckVersion(Version Version): Command<LastVersion>
{
  public Version Version = Version;
}

var command = new CheckVersion(new Version("1.1.1"));
var response = await bus.Execute(command);

This method returns a Task which will eventually be resolved as a response message.

For instance, if you do not expect any response, you can easily make the EventBus resolve a Task immediately to undefined:

public record Record(Version Version) : Command<Unit>(false)
{
  public Version Version = Version;
}

var command = new Record(new Version("1.1.1"));
await bus.Execute(command);

The HttpCommandDispatcher is an alternative way to execute the commands over HTTP. To start, you should create an HttpCommandDispatcher instance by passing the following options to the constructor:

var httpDispatcher = serviceProvider.GetService<HttpCommandDispatcher>();

The command dispatcher can be customized using the following options:

Option Description
BaseUrl Base URL for your application instance, e.g. https://app.neuralegion.com
Token API key to access the API. Find out how to obtain personal and organization API keys in the knowledgebase
Timeout Time to wait for a server to send response headers (and start the response body) before aborting the request. Default 10000 ms

Then you have to create an instance of HttpRequest instead of a custom command, specifying the Url and Method in addition to the Body that a command accepts by default:

var body = JsonContent.Create(new { Foo = "bar" });
var command = new HttpRequest<Unit>(url: "/api/v1/repeaters",
  method: HttpMethods.Post,
  body: body);

Once it is done, you can perform a request using HttpComandDispatcher as follows:

var response = await httpDispatcher.Execute(command);

Below you will find a list of parameters that can be used to configure a command:

Option Description
Url Absolute URL or path that will be used for the request. By default, /
Method HTTP method that is going to be used when making the request. By default, HttpMethod.Get
Params Use to set query parameters.
Body Message that we want to transmit to the remote service.
ExpectReply Indicates whether to wait for a reply. By default true.
Ttl Period of time that command should be handled before being discarded. By default 10000 ms.
Type The name of a command. By default, it is the name of specific class.
CorrelationId Used to ensure atomicity while working with EventBus. By default, random UUID.
CreatedAt The exact date and time the command was created.

For more information, please see SecTester.Core.

Retry Strategy

For some noncritical operations, it is better to fail as soon as possible rather than retry a coupe of times. For example, it is better to fail right after a smaller number of retries with only a short delay between retry attempts, and display a message to the user.

By default, you can use the Exponential backoff retry strategy to retry an action when errors like SocketException appear.

You can implement your own to match the business requirements and the nature of the failure:

public class CustomRetryStrategy: RetryStrategy
{
  public async Task<TResult> Acquire<TResult>(task: Func<Task<TResult>>) {
    var times = 0;

    for (;;) {
      try
      {
        return await task();
      } catch
      {
        times++;

        if (times == 3)
        {
          throw;
        }
      }
    }
  }
}

Once a retry strategy is implemented, you can register it in the IoC container:

collection.AddSingleton<RetryStrategy, CustomRetryStrategy>();

License

Copyright © 2022 Bright Security.

This project is licensed under the MIT License - see the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on SecTester.Bus:

Package Downloads
SecTester.Scan

This SDK is designed to provide all the basic tools and functions that will allow you to easily integrate the Bright security testing engine into your own project.

SecTester.Repeater

This SDK is designed to provide all the basic tools and functions that will allow you to easily integrate the Bright security testing engine into your own project.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.41.4 250 6/8/2024
0.41.3 364 10/4/2023
0.41.2 311 10/4/2023
0.41.1 308 10/4/2023
0.41.0 313 10/4/2023
0.40.0 383 8/3/2023
0.39.1 388 8/1/2023
0.39.0 367 7/31/2023
0.38.0 382 7/28/2023
0.37.0 367 7/20/2023
0.36.0 383 6/5/2023
0.35.1 423 5/2/2023
0.35.0 532 4/11/2023
0.34.0 816 2/8/2023
0.33.7 1,000 12/20/2022
0.33.6 997 12/16/2022
0.33.5 1,002 12/16/2022
0.33.4 1,041 12/15/2022
0.33.3 996 12/14/2022
0.33.2 1,030 12/14/2022
0.33.1 1,018 12/14/2022
0.33.0 975 12/14/2022
0.32.8 1,005 12/13/2022
0.32.7 1,005 12/13/2022
0.32.6 1,009 12/13/2022
0.32.5 989 12/13/2022
0.32.4 990 12/13/2022
0.32.3 1,010 12/13/2022
0.32.2 987 12/13/2022
0.32.1 1,016 12/13/2022
0.32.0 1,016 12/13/2022
0.31.0 1,033 12/11/2022
0.30.1 842 12/10/2022
0.30.0 838 12/9/2022
0.29.2 667 12/9/2022
0.29.1 702 12/9/2022
0.29.0 686 12/8/2022
0.28.0 694 12/8/2022
0.27.0 627 12/8/2022
0.26.0 671 12/7/2022
0.25.0 662 12/7/2022
0.24.0 653 12/6/2022
0.23.0 704 12/5/2022
0.22.0 734 12/2/2022
0.21.0 746 12/1/2022
0.20.0 795 12/1/2022
0.19.0 766 11/28/2022
0.18.0 550 11/28/2022
0.17.0 569 11/28/2022
0.16.0 344 11/28/2022
0.15.0 370 11/21/2022
0.14.0 365 11/16/2022
0.13.0 366 11/16/2022
0.12.0 366 11/16/2022
0.11.0 375 11/14/2022
0.10.0 352 11/14/2022
0.9.0 379 11/14/2022