-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1220 from justeattakeaway/revert-1144-gh-1139-rem…
…ove-sqs-policy Revert switch to AWSSDK Policy Creation
- Loading branch information
Showing
10 changed files
with
166 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Amazon.SQS; | ||
using Amazon.SQS.Model; | ||
|
||
namespace JustSaying.AwsTools.MessageHandling; | ||
|
||
internal static class SqsPolicy | ||
{ | ||
internal static async Task SaveAsync(SqsPolicyDetails policyDetails, IAmazonSQS client) | ||
{ | ||
var policyJson = SqsPolicyBuilder.BuildPolicyJson(policyDetails); | ||
|
||
var setQueueAttributesRequest = new SetQueueAttributesRequest | ||
{ | ||
QueueUrl = policyDetails.QueueUri.AbsoluteUri, | ||
Attributes = | ||
{ | ||
["Policy"] = policyJson | ||
} | ||
}; | ||
|
||
await client.SetQueueAttributesAsync(setQueueAttributesRequest).ConfigureAwait(false); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/JustSaying/AwsTools/MessageHandling/SqsPolicyBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using Amazon; | ||
|
||
namespace JustSaying.AwsTools.MessageHandling; | ||
|
||
internal static class SqsPolicyBuilder{ | ||
|
||
public static string BuildPolicyJson(SqsPolicyDetails policyDetails) | ||
{ | ||
var sid = Guid.NewGuid().ToString().Replace("-", ""); | ||
|
||
var resource = policyDetails.QueueArn; | ||
|
||
var topicArnWildcard = string.IsNullOrWhiteSpace(policyDetails.SourceArn) | ||
? "*" | ||
: CreateTopicArnWildcard(policyDetails.SourceArn); | ||
|
||
var policyJson = $@"{{ | ||
""Version"" : ""2012-10-17"", | ||
""Statement"" : [ | ||
{{ | ||
""Sid"" : ""{sid}"", | ||
""Effect"" : ""Allow"", | ||
""Principal"" : {{ | ||
""AWS"" : ""*"" | ||
}}, | ||
""Action"" : ""sqs:SendMessage"", | ||
""Resource"" : ""{resource}"", | ||
""Condition"" : {{ | ||
""ArnLike"" : {{ | ||
""aws:SourceArn"" : ""{topicArnWildcard}"" | ||
}} | ||
}} | ||
}} | ||
] | ||
}}"; | ||
return policyJson; | ||
} | ||
|
||
private static string CreateTopicArnWildcard(string topicArn) | ||
{ | ||
var arn = Arn.Parse(topicArn); | ||
arn.Resource = "*"; | ||
return arn.ToString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace JustSaying.AwsTools.MessageHandling; | ||
|
||
internal class SqsPolicyDetails | ||
{ | ||
public string SourceArn { get; set; } | ||
public string QueueArn { get; set; } | ||
public Uri QueueUri { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 0 additions & 70 deletions
70
tests/JustSaying.IntegrationTests/Fluent/AwsTools/WhenEnsuringATopicExistsPolicyIsCreated.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...cyBuilderTests.ShouldGenerateApprovedIamPolicyWithWildcardFromEmptySourceArn.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"Version" : "2012-10-17", | ||
"Statement" : [ | ||
{ | ||
"Sid" : "<sid>", | ||
"Effect" : "Allow", | ||
"Principal" : { | ||
"AWS" : "*" | ||
}, | ||
"Action" : "sqs:SendMessage", | ||
"Resource" : "", | ||
"Condition" : { | ||
"ArnLike" : { | ||
"aws:SourceArn" : "*" | ||
} | ||
} | ||
} | ||
] | ||
} |
58 changes: 58 additions & 0 deletions
58
tests/JustSaying.UnitTests/AwsTools/MessageHandling/Sqs/Policy/SqsPolicyBuilderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using JustSaying.AwsTools.MessageHandling; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace JustSaying.UnitTests.AwsTools.MessageHandling.Sqs.Policy; | ||
|
||
public class SqsPolicyBuilderTests | ||
{ | ||
[Fact] | ||
public void ShouldGenerateApprovedIamPolicy() | ||
{ | ||
// arrange | ||
var sqsPolicyDetails = new SqsPolicyDetails | ||
{ | ||
SourceArn = "arn:aws:sqs:ap-southeast-2:123456789012:topic", | ||
}; | ||
|
||
// act | ||
var policy = SqsPolicyBuilder.BuildPolicyJson(sqsPolicyDetails); | ||
|
||
// assert | ||
policy.ShouldMatchApproved(c => | ||
{ | ||
c.SubFolder("Approvals"); | ||
// Sids are generated from guids on each invocation so must be ignored | ||
// when performing approval tests | ||
c.WithScrubber(ScrubSids); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void ShouldGenerateApprovedIamPolicyWithWildcardFromEmptySourceArn() | ||
{ | ||
// arrange | ||
var sqsPolicyDetails = new SqsPolicyDetails | ||
{ | ||
SourceArn = "", | ||
}; | ||
|
||
// act | ||
var policy = SqsPolicyBuilder.BuildPolicyJson(sqsPolicyDetails); | ||
|
||
// assert | ||
policy.ShouldMatchApproved(c => | ||
{ | ||
c.SubFolder("Approvals"); | ||
// Sids are generated from guids on each invocation so must be ignored | ||
// when performing approval tests | ||
c.WithScrubber(ScrubSids); | ||
}); | ||
} | ||
|
||
private static string ScrubSids(string iamPolicy) | ||
{ | ||
var json = JObject.Parse(iamPolicy); | ||
return iamPolicy | ||
.Replace(json["Statement"]![0]!["Sid"]!.ToString(), "<sid>"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters