Skip to content

Commit 7d23ad1

Browse files
authored
Merge pull request #127 from TraGicCode/chore/update-docs-and-add-validation-for-amazon-sqs-transport
Update docs for amazon-sqs transport along with config validation
2 parents 5d13455 + adf8f11 commit 7d23ad1

7 files changed

Lines changed: 230 additions & 11 deletions

File tree

src/BuslyCLI.Console/Config/AmazonsqsTransportConfig.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
public class AmazonsqsTransportConfig : ITransportConfig
55
{
6+
7+
// Local Stack Only
68
public string ServiceUrl { get; set; }
79
public string RegionName { get; set; }
10+
public string AccessKey { get; set; }
11+
public string SecretKey { get; set; }
12+
813
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using FluentValidation;
2+
3+
namespace BuslyCLI.Config.Validators;
4+
5+
public class AmazonsqsTransportConfigValidator : AbstractValidator<AmazonsqsTransportConfig>
6+
{
7+
public AmazonsqsTransportConfigValidator()
8+
{
9+
RuleFor(x => x.RegionName)
10+
.NotEmpty();
11+
12+
RuleFor(x => x)
13+
.Must(x =>
14+
(string.IsNullOrEmpty(x.AccessKey) && string.IsNullOrEmpty(x.SecretKey)) // both empty
15+
|| (!string.IsNullOrEmpty(x.AccessKey) && !string.IsNullOrEmpty(x.SecretKey)) // both set
16+
)
17+
.WithMessage("AWS AccessKey and SecretKey are mutually dependent: if one is set, the other must also be set.");
18+
}
19+
}

src/BuslyCLI.Console/Config/Validators/TransportConfigValidator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public TransportConfigValidator()
1616
v.Add(new LearningTransportConfigValidator());
1717
v.Add(new RabbitMQTransportConfigValidator());
1818
v.Add(new AzureServiceBusTransportConfigValidator());
19+
v.Add(new AmazonsqsTransportConfigValidator());
1920
});
2021

2122
// RuleFor(x => x.LearningTransportConfig)

src/BuslyCLI.Console/Factories/RawEndpointFactory.cs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Amazon.Runtime;
1+
using Amazon;
2+
using Amazon.Runtime;
23
using Amazon.SimpleNotificationService;
34
using Amazon.SQS;
45
using BuslyCLI.Config;
@@ -73,19 +74,27 @@ private static ManagementApiConfiguration CreateManagementApiConfig(ManagementAp
7374

7475
private TransportDefinition CreateAmazonSQSTransport(AmazonsqsTransportConfig amazonsqsTransportConfig)
7576
{
76-
var credentials = new BasicAWSCredentials("test", "test");
77-
78-
var sqsClient = new AmazonSQSClient(credentials, new AmazonSQSConfig
77+
var credentials = new BasicAWSCredentials(amazonsqsTransportConfig.AccessKey, amazonsqsTransportConfig.SecretKey);
78+
var amazonSqsConfig = new AmazonSQSConfig();
79+
var amazonSnsConfig = new AmazonSimpleNotificationServiceConfig();
80+
if (!string.IsNullOrWhiteSpace(amazonsqsTransportConfig.RegionName))
7981
{
80-
ServiceURL = amazonsqsTransportConfig.ServiceUrl,
81-
AuthenticationRegion = amazonsqsTransportConfig.RegionName,
82-
});
8382

84-
var snsClient = new AmazonSimpleNotificationServiceClient(credentials, new AmazonSimpleNotificationServiceConfig
83+
amazonSqsConfig.RegionEndpoint = RegionEndpoint.GetBySystemName(amazonsqsTransportConfig.RegionName);
84+
amazonSnsConfig.RegionEndpoint = RegionEndpoint.GetBySystemName(amazonsqsTransportConfig.RegionName);
85+
}
86+
87+
// If ServiceUrl is passed, we are assuming we are using LocalStack
88+
// Without this, local stack will try to really authenticate with aws which will fail
89+
if (!string.IsNullOrWhiteSpace(amazonsqsTransportConfig.ServiceUrl))
8590
{
86-
ServiceURL = amazonsqsTransportConfig.ServiceUrl,
87-
AuthenticationRegion = amazonsqsTransportConfig.RegionName,
88-
});
91+
amazonSnsConfig.ServiceURL = amazonsqsTransportConfig.ServiceUrl;
92+
amazonSqsConfig.ServiceURL = amazonsqsTransportConfig.ServiceUrl;
93+
}
94+
95+
var sqsClient = new AmazonSQSClient(credentials, amazonSqsConfig);
96+
97+
var snsClient = new AmazonSimpleNotificationServiceClient(credentials, amazonSnsConfig);
8998

9099
return new SqsTransport(sqsClient, snsClient);
91100
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using BuslyCLI.Config;
2+
using BuslyCLI.Config.Validators;
3+
using FluentValidation.TestHelper;
4+
5+
namespace BuslyCLI.Console.Tests.Config.Validators;
6+
7+
[TestFixture]
8+
public class AmazonsqsTransportConfigValidatorTests
9+
{
10+
private readonly AmazonsqsTransportConfigValidator _validator;
11+
12+
public AmazonsqsTransportConfigValidatorTests()
13+
{
14+
_validator = new AmazonsqsTransportConfigValidator();
15+
}
16+
17+
[Test]
18+
public async Task ShouldErrorWhenRegionNameIsNotPassed()
19+
{
20+
// Arrange
21+
var amazonsqsTransportConfig = new AmazonsqsTransportConfig
22+
{
23+
RegionName = null
24+
};
25+
26+
// Act
27+
var result = await _validator.TestValidateAsync(amazonsqsTransportConfig);
28+
29+
// Assert
30+
result.ShouldHaveValidationErrorFor(c => c.RegionName)
31+
.WithErrorMessage("'Region Name' must not be empty.");
32+
}
33+
34+
[Test]
35+
public async Task ShouldNotErrorWhenRegionNameIsPassed()
36+
{
37+
// Arrange
38+
var amazonsqsTransportConfig = new AmazonsqsTransportConfig
39+
{
40+
RegionName = "us-east-1"
41+
};
42+
// Act
43+
var result = await _validator.TestValidateAsync(amazonsqsTransportConfig);
44+
45+
// Assert
46+
result.ShouldNotHaveValidationErrorFor(c => c.RegionName);
47+
}
48+
49+
[Test]
50+
public async Task ShouldNotErrorWhenServiceUrlIsPassed()
51+
{
52+
// Arrange
53+
var amazonsqsTransportConfig = new AmazonsqsTransportConfig
54+
{
55+
ServiceUrl = "us-east-1"
56+
};
57+
// Act
58+
var result = await _validator.TestValidateAsync(amazonsqsTransportConfig);
59+
60+
// Assert
61+
result.ShouldNotHaveValidationErrorFor(c => c.ServiceUrl);
62+
}
63+
64+
[Test]
65+
public async Task ShouldErrorWhenAccessKeyIsPassedWithoutSecretKey()
66+
{
67+
// Arrange
68+
var amazonsqsTransportConfig = new AmazonsqsTransportConfig()
69+
{
70+
AccessKey = "BLAHBLAHBLAH",
71+
SecretKey = null
72+
};
73+
// Act
74+
var result = await _validator.TestValidateAsync(amazonsqsTransportConfig);
75+
76+
// Assert
77+
result.ShouldHaveValidationErrors().WithErrorMessage("AWS AccessKey and SecretKey are mutually dependent: if one is set, the other must also be set.");
78+
}
79+
80+
[Test]
81+
public async Task ShouldErrorWhenSecretIsPassedWithoutAccessKey()
82+
{
83+
// Arrange
84+
var amazonsqsTransportConfig = new AmazonsqsTransportConfig()
85+
{
86+
AccessKey = null,
87+
SecretKey = "BLAHBLAHBLAH"
88+
};
89+
// Act
90+
var result = await _validator.TestValidateAsync(amazonsqsTransportConfig);
91+
92+
// Assert
93+
result.ShouldHaveValidationErrors().WithErrorMessage("AWS AccessKey and SecretKey are mutually dependent: if one is set, the other must also be set.");
94+
}
95+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Amazon SQS
2+
3+
The **Amazon SQS Transport** is used to communicate to Amazon SQS. It is suitable for development, testing, and production environments.
4+
5+
## Configuration
6+
7+
To use the Amazon SQS Transport, define it under `transports` and reference it as `current-transport`.
8+
9+
### Example
10+
11+
```yaml
12+
current-transport: local-stack-amazon-sqs
13+
14+
transports:
15+
- name: local-stack-amazon-sqs
16+
amazonsqs-transport-config:
17+
region-name: us-east-1
18+
access-key: test
19+
secret-key: test
20+
service-url: http://127.0.0.1:32813/ # (optional) Only used when connecting to local-stack
21+
```
22+
23+
:::info
24+
25+
The Amazon SQS transport implementation currently works only with **AWS access key and secret key authentication**. Pull requests that add support for additional authentication methods are welcome and greatly appreciated!
26+
27+
:::
28+
29+
---
30+
31+
## `amazonsqs-transport-config` Fields
32+
33+
| Field | Required | Type | Default | Description |
34+
| ------------- | -------- | ------ | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
35+
| `region-name` | **Yes** | string | — | The AWS region. All Region codes can be found [here](https://docs.aws.amazon.com/global-infrastructure/latest/regions/aws-regions.html) (EX: us-east-1, us-east2, us-west1..etc). |
36+
| `access-key` | **Yes** | string | — | The AWS Access Key. |
37+
| `secret-key` | **Yes** | string | — | The AWS Secret Key. |
38+
| `service-url` | No | string | — | The service URL used to connect to LocalStack for local development. |
39+
40+
---
41+
42+
## Field Details
43+
44+
### `region-name` (required)
45+
46+
The AWS Region SQS is hosted in.
47+
48+
Examples:
49+
50+
```yaml
51+
region-name: us-east-1
52+
```
53+
54+
---
55+
56+
### `access-key` (required)
57+
58+
The AWS Access Key.
59+
60+
Examples:
61+
62+
```yaml
63+
access-key: test
64+
```
65+
66+
---
67+
68+
### `secret-key` (required)
69+
70+
The AWS Secret Key.
71+
72+
Examples:
73+
74+
```yaml
75+
secret-key: test
76+
```
77+
78+
---
79+
80+
### `service-url` (optional)
81+
82+
The service URL used to connect to LocalStack for local development.
83+
84+
Examples:
85+
86+
```yaml
87+
service-url: http://127.0.0.1:32813/
88+
```

website/docs/transports/rabbitmq.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ amqp-connection-string: amqp://guest:guest@localhost:5672/
5555
amqp-connection-string: amqps://user:pass@rabbitmq.example.com:5671/my-vhost
5656
```
5757

58+
---
59+
5860
### `management-api` (optional)
5961

6062
Allows Busly to interact with the RabbitMQ Management API for monitoring or queue management.

0 commit comments

Comments
 (0)