Skip to content

Commit

Permalink
Merge pull request #1220 from justeattakeaway/revert-1144-gh-1139-rem…
Browse files Browse the repository at this point in the history
…ove-sqs-policy

Revert switch to AWSSDK Policy Creation
  • Loading branch information
hwoodiwiss authored Jul 28, 2023
2 parents aa6b03f + 2a1fadd commit 03484e6
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 77 deletions.
23 changes: 23 additions & 0 deletions src/JustSaying/AwsTools/MessageHandling/SqsPolicy.cs
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 src/JustSaying/AwsTools/MessageHandling/SqsPolicyBuilder.cs
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();
}
}
8 changes: 8 additions & 0 deletions src/JustSaying/AwsTools/MessageHandling/SqsPolicyDetails.cs
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; }
}
12 changes: 11 additions & 1 deletion src/JustSaying/AwsTools/QueueCreation/AmazonQueueCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ await SubscribeQueueAndApplyFilterPolicyAsync(snsClient,
sqsClient,
queue.Uri,
queueConfig.FilterPolicy).ConfigureAwait(false);

var sqsDetails = new SqsPolicyDetails
{
SourceArn = eventTopic.Arn,
QueueArn = queue.Arn,
QueueUri = queue.Uri
};
await SqsPolicy
.SaveAsync(sqsDetails, sqsClient)
.ConfigureAwait(false);
}
}

Expand Down Expand Up @@ -112,4 +122,4 @@ await amazonSimpleNotificationService
.SetSubscriptionAttributesAsync(subscriptionArn, "FilterPolicy", actualFilterPolicy)
.ConfigureAwait(false);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
<Using Include="Xunit.Abstractions" />
</ItemGroup>
<ItemGroup>
<Folder Include="Fluent\AwsTools\Approvals" />
<Folder Include="Fluent\Publishing\Approvals" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
"AWS" : "*"
},
"Action" : "sqs:SendMessage",
"Resource" : "arn:aws:sqs:us-east-1:000000000000:<unique-name>",
"Resource" : "",
"Condition" : {
"ArnLike" : {
"aws:SourceArn" : "arn:aws:sns:us-east-1:000000000000:<unique-name>-topic"
"aws:SourceArn" : "arn:aws:sqs:ap-southeast-2:123456789012:*"
}
}
}
Expand Down
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" : "*"
}
}
}
]
}
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>");
}
}
3 changes: 0 additions & 3 deletions tests/JustSaying.UnitTests/JustSaying.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,4 @@
<Using Include="Xunit" />
<Using Include="Xunit.Abstractions" />
</ItemGroup>
<ItemGroup>
<Folder Include="AwsTools\MessageHandling\Sqs\Policy" />
</ItemGroup>
</Project>

0 comments on commit 03484e6

Please sign in to comment.