Skip to content

Commit 90444c3

Browse files
author
Arin Ghazarian
authored
Validate AWS region (#872)
1 parent 621a7a6 commit 90444c3

File tree

5 files changed

+26
-6
lines changed

5 files changed

+26
-6
lines changed

RELEASENOTES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
- Adds retry logic during GHES archive generation in cases of transient failure
22
- Added log output linking to migration log URL after migration completes
33
- Add support for specifying `--archive-download-host` with `gh bbs2gh migrate-repo` and `gh bbs2gh generate-script`, rather than taking the host from the `--bbs-server-url`
4-
- Improve handling of GraphQL errors, throwing an exception with the specific error message returned by the API
4+
- Improve handling of GraphQL errors, throwing an exception with the specific error message returned by the API
5+
- Validate AWS region when using Amazon S3 to upload the migration archive in `gh gei` and `gh bbs2gh`

src/Octoshift/AwsApi.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ private static AmazonS3Client BuildAmazonS3Client(string awsAccessKeyId, string
3030
var regionEndpoint = DefaultRegionEndpoint;
3131
if (awsRegion.HasValue())
3232
{
33-
regionEndpoint = RegionEndpoint.GetBySystemName(awsRegion);
33+
regionEndpoint = GetRegionEndpoint(awsRegion);
3434
AWSConfigsS3.UseSignatureVersion4 = true;
3535
}
3636

@@ -39,6 +39,10 @@ private static AmazonS3Client BuildAmazonS3Client(string awsAccessKeyId, string
3939
: new AmazonS3Client(awsAccessKeyId, awsSecretAccessKey, regionEndpoint);
4040
}
4141

42+
private static RegionEndpoint GetRegionEndpoint(string awsRegion) => RegionEndpoint.GetBySystemName(awsRegion) is { DisplayName: not "Unknown" } regionEndpoint
43+
? regionEndpoint
44+
: throw new OctoshiftCliException($"Invalid AWS region \"{awsRegion}\".");
45+
4246
public virtual async Task<string> UploadToBucket(string bucketName, string fileName, string keyName)
4347
{
4448
await _transferUtility.UploadAsync(fileName, bucketName, keyName);

src/OctoshiftCLI.Tests/AwsApiTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,15 @@ public async Task UploadToBucket_Uploads_FileStream()
6161
result.Should().Be(url);
6262
transferUtility.Verify(m => m.UploadAsync(It.IsAny<MemoryStream>(), bucketName, keyName, It.IsAny<CancellationToken>()));
6363
}
64+
65+
[Fact]
66+
public void It_Throws_If_Aws_Region_Is_Invalid()
67+
{
68+
// Arrange, Act
69+
const string awsRegion = "invalid-region";
70+
var awsApi = () => new AwsApi("awsAccessKeyId", "awsSecretAccessKey", awsRegion);
71+
72+
// Assert
73+
awsApi.Should().Throw<OctoshiftCliException>().WithMessage($"*{awsRegion}*");
74+
}
6475
}

src/OctoshiftCLI.Tests/bbs2gh/AwsApiFactoryTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ public void It_Falls_Back_To_Aws_Access_Key_Environment_Variable_If_Aws_Access_K
1919
{
2020
// Arrange
2121
const string awsAccessKey = "AWS_ACCESS_KEY";
22+
const string awsRegion = "us-east-2";
2223
#pragma warning disable CS0618
2324
_mockEnvironmentVariableProvider.Setup(m => m.AwsAccessKey(false)).Returns(awsAccessKey);
2425
#pragma warning restore CS0618
2526

2627
// Act
27-
_awsApiFactory.Create("aws-region", null, "aws-secret-access-key", "aws-session-token");
28+
_awsApiFactory.Create(awsRegion, null, "aws-secret-access-key", "aws-session-token");
2829

2930
// Assert
3031
#pragma warning disable CS0618
@@ -37,12 +38,13 @@ public void It_Falls_Back_To_Aws_Secret_Key_Environment_Variable_If_Aws_Secret_A
3738
{
3839
// Arrange
3940
const string awsSecretKey = "AWS_SECRET_KEY";
41+
const string awsRegion = "us-east-2";
4042
#pragma warning disable CS0618
4143
_mockEnvironmentVariableProvider.Setup(m => m.AwsSecretKey(false)).Returns(awsSecretKey);
4244
#pragma warning restore CS0618
4345

4446
// Act
45-
_awsApiFactory.Create("aws-region", "aws-access-key-id", null, "aws-session-token");
47+
_awsApiFactory.Create(awsRegion, "aws-access-key-id", null, "aws-session-token");
4648

4749
// Assert
4850
#pragma warning disable CS0618

src/OctoshiftCLI.Tests/gei/AwsApiFactoryTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ public void It_Falls_Back_To_Aws_Access_Key_Environment_Variable_If_Aws_Access_K
1919
{
2020
// Arrange
2121
const string awsAccessKey = "AWS_ACCESS_KEY";
22+
const string awsRegion = "us-east-2";
2223
#pragma warning disable CS0618
2324
_mockEnvironmentVariableProvider.Setup(m => m.AwsAccessKey(false)).Returns(awsAccessKey);
2425
#pragma warning restore CS0618
2526

2627
// Act
27-
_awsApiFactory.Create("aws-region", null, "aws-secret-access-key", "aws-session-token");
28+
_awsApiFactory.Create(awsRegion, null, "aws-secret-access-key", "aws-session-token");
2829

2930
// Assert
3031
#pragma warning disable CS0618
@@ -37,12 +38,13 @@ public void It_Falls_Back_To_Aws_Secret_Key_Environment_Variable_If_Aws_Secret_A
3738
{
3839
// Arrange
3940
const string awsSecretKey = "AWS_SECRET_KEY";
41+
const string awsRegion = "us-east-2";
4042
#pragma warning disable CS0618
4143
_mockEnvironmentVariableProvider.Setup(m => m.AwsSecretKey(false)).Returns(awsSecretKey);
4244
#pragma warning restore CS0618
4345

4446
// Act
45-
_awsApiFactory.Create("aws-region", "aws-access-key-id", null, "aws-session-token");
47+
_awsApiFactory.Create(awsRegion, "aws-access-key-id", null, "aws-session-token");
4648

4749
// Assert
4850
#pragma warning disable CS0618

0 commit comments

Comments
 (0)