Skip to content

Commit a8bd3fc

Browse files
committed
Merge remote-tracking branch 'origin/main' into vnext
2 parents 8598935 + 9cc759f commit a8bd3fc

File tree

15 files changed

+394
-54
lines changed

15 files changed

+394
-54
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
namespace LEGO.AsyncAPI.Bindings.Sns;
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text.Json;
7+
using System.Text.Json.Nodes;
8+
using LEGO.AsyncAPI.Models.Interfaces;
9+
using LEGO.AsyncAPI.Readers.ParseNodes;
10+
using LEGO.AsyncAPI.Writers;
11+
12+
public abstract class Principal : IAsyncApiElement
13+
{
14+
public abstract void Serialize(IAsyncApiWriter writer);
15+
16+
public static Principal Parse(ParseNode node)
17+
{
18+
switch (node)
19+
{
20+
case ValueNode:
21+
var nodeValue = node.GetScalarValue();
22+
if (!IsStarString(nodeValue))
23+
{
24+
throw new ArgumentException($"An error occured while parsing a {nameof(Principal)} node. " +
25+
$"Principal value without a property name can only be a string value of '*'.");
26+
}
27+
28+
return new PrincipalStar();
29+
30+
case MapNode mapNode:
31+
{
32+
var propertyNode = mapNode.First();
33+
if (!IsValidPrincipalProperty(propertyNode.Name))
34+
{
35+
throw new ArgumentException($"An error occured while parsing a {nameof(Principal)} node. " +
36+
$"Node should contain a valid AWS principal property name.");
37+
}
38+
39+
var principalValue = new KeyValuePair<string, StringOrStringList>(
40+
propertyNode.Name,
41+
StringOrStringList.Parse(propertyNode.Value));
42+
43+
return new PrincipalObject(principalValue);
44+
}
45+
46+
default:
47+
throw new ArgumentException($"An error occured while parsing a {nameof(Principal)} node. " +
48+
$"Node should contain a string value of '*' or a valid AWS principal property.");
49+
}
50+
}
51+
52+
private static bool IsStarString(JsonNode value)
53+
{
54+
var element = JsonDocument.Parse(value.ToJsonString()).RootElement;
55+
56+
return element.ValueKind == JsonValueKind.String && element.ValueEquals("*");
57+
}
58+
59+
private static bool IsValidPrincipalProperty(string property)
60+
{
61+
return new[] { "AWS", "Service" }.Contains(property);
62+
}
63+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace LEGO.AsyncAPI.Bindings.Sns;
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using LEGO.AsyncAPI.Writers;
6+
7+
public class PrincipalObject : Principal
8+
{
9+
private KeyValuePair<string, StringOrStringList> PrincipalValue;
10+
11+
public PrincipalObject(KeyValuePair<string, StringOrStringList> principalValue)
12+
{
13+
this.PrincipalValue = principalValue;
14+
}
15+
16+
public override void Serialize(IAsyncApiWriter writer)
17+
{
18+
if (writer is null)
19+
{
20+
throw new ArgumentNullException(nameof(writer));
21+
}
22+
23+
writer.WriteStartObject();
24+
writer.WriteRequiredObject(this.PrincipalValue.Key, this.PrincipalValue.Value, (w, t) => t.Value.Write(w));
25+
writer.WriteEndObject();
26+
}
27+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace LEGO.AsyncAPI.Bindings.Sns;
2+
3+
using System;
4+
using LEGO.AsyncAPI.Writers;
5+
6+
public class PrincipalStar : Principal
7+
{
8+
private string PrincipalValue;
9+
10+
public PrincipalStar()
11+
{
12+
this.PrincipalValue = "*";
13+
}
14+
15+
public override void Serialize(IAsyncApiWriter writer)
16+
{
17+
if (writer is null)
18+
{
19+
throw new ArgumentNullException(nameof(writer));
20+
}
21+
22+
writer.WriteValue(this.PrincipalValue);
23+
}
24+
}

src/LEGO.AsyncAPI.Bindings/Sns/SnsChannelBinding.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ public class SnsChannelBinding : ChannelBinding<SnsChannelBinding>
5757
private static FixedFieldMap<Statement> statementFixedFields = new()
5858
{
5959
{ "effect", (a, n) => { a.Effect = n.GetScalarValue().GetEnumFromDisplayName<Effect>(); } },
60-
{ "principal", (a, n) => { a.Principal = StringOrStringList.Parse(n); } },
60+
{ "principal", (a, n) => { a.Principal = Principal.Parse(n); } },
6161
{ "action", (a, n) => { a.Action = StringOrStringList.Parse(n); } },
62+
{ "resource", (a, n) => { a.Resource = StringOrStringList.Parse(n); } },
63+
{ "condition", (a, n) => { a.Condition = n.CreateAny(); } },
6264
};
6365

6466
/// <inheritdoc/>

src/LEGO.AsyncAPI.Bindings/Sns/Statement.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
11
// Copyright (c) The LEGO Group. All rights reserved.
2-
32
namespace LEGO.AsyncAPI.Bindings.Sns
43
{
54
using System;
65
using System.Collections.Generic;
76
using LEGO.AsyncAPI.Attributes;
7+
using LEGO.AsyncAPI.Models;
88
using LEGO.AsyncAPI.Models.Interfaces;
99
using LEGO.AsyncAPI.Writers;
1010

1111
public class Statement : IAsyncApiExtensible
1212
{
13+
/// <summary>
14+
/// Indicates whether the policy allows or denies access.
15+
/// </summary>
1316
public Effect Effect { get; set; }
1417

1518
/// <summary>
16-
/// The AWS account or resource ARN that this statement applies to.
19+
/// The AWS account(s) or resource ARN(s) that this statement applies to.
1720
/// </summary>
18-
// public StringOrStringList Principal { get; set; }
19-
public StringOrStringList Principal { get; set; }
21+
public Principal Principal { get; set; }
2022

2123
/// <summary>
2224
/// The SNS permission being allowed or denied e.g. sns:Publish.
2325
/// </summary>
2426
public StringOrStringList Action { get; set; }
2527

28+
/// <summary>
29+
/// The resource(s) that this policy applies to.
30+
/// </summary>
31+
public StringOrStringList? Resource { get; set; }
32+
33+
/// <summary>
34+
/// Specific circumstances under which the policy grants permission.
35+
/// </summary>
36+
public AsyncApiAny? Condition { get; set; }
37+
2638
public IDictionary<string, IAsyncApiExtension> Extensions { get; set; } = new Dictionary<string, IAsyncApiExtension>();
2739

2840
public void Serialize(IAsyncApiWriter writer)
@@ -34,8 +46,10 @@ public void Serialize(IAsyncApiWriter writer)
3446

3547
writer.WriteStartObject();
3648
writer.WriteRequiredProperty("effect", this.Effect.GetDisplayName());
37-
writer.WriteRequiredObject("principal", this.Principal, (w, t) => t.Value.Write(w));
49+
writer.WriteRequiredObject("principal", this.Principal, (w, t) => t.Serialize(w));
3850
writer.WriteRequiredObject("action", this.Action, (w, t) => t.Value.Write(w));
51+
writer.WriteOptionalObject("resource", this.Resource, (w, t) => t?.Value.Write(w));
52+
writer.WriteOptionalObject("condition", this.Condition, (w, t) => t?.Write(w));
3953
writer.WriteExtensions(this.Extensions);
4054
writer.WriteEndObject();
4155
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright (c) The LEGO Group. All rights reserved.
2+
3+
namespace LEGO.AsyncAPI.Bindings.Sqs;
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Text.Json;
9+
using System.Text.Json.Nodes;
10+
using LEGO.AsyncAPI.Models.Interfaces;
11+
using LEGO.AsyncAPI.Readers.ParseNodes;
12+
using LEGO.AsyncAPI.Writers;
13+
14+
public abstract class Principal : IAsyncApiElement
15+
{
16+
public abstract void Serialize(IAsyncApiWriter writer);
17+
18+
public static Principal Parse(ParseNode node)
19+
{
20+
switch (node)
21+
{
22+
case ValueNode:
23+
var nodeValue = node.GetScalarValue();
24+
if (!IsStarString(nodeValue))
25+
{
26+
throw new ArgumentException($"An error occured while parsing a {nameof(Principal)} node. " +
27+
$"Principal value without a property name can only be a string value of '*'.");
28+
}
29+
30+
return new PrincipalStar();
31+
32+
case MapNode mapNode:
33+
{
34+
var propertyNode = mapNode.First();
35+
if (!IsValidPrincipalProperty(propertyNode.Name))
36+
{
37+
throw new ArgumentException($"An error occured while parsing a {nameof(Principal)} node. " +
38+
$"Node should contain a valid AWS principal property name.");
39+
}
40+
41+
var principalValue = new KeyValuePair<string, StringOrStringList>(
42+
propertyNode.Name,
43+
StringOrStringList.Parse(propertyNode.Value));
44+
45+
return new PrincipalObject(principalValue);
46+
}
47+
48+
default:
49+
throw new ArgumentException($"An error occured while parsing a {nameof(Principal)} node. " +
50+
$"Node should contain a string value of '*' or a valid AWS principal property.");
51+
}
52+
}
53+
54+
private static bool IsStarString(JsonNode value)
55+
{
56+
var element = JsonDocument.Parse(value.ToJsonString()).RootElement;
57+
58+
return element.ValueKind == JsonValueKind.String && element.ValueEquals("*");
59+
}
60+
61+
private static bool IsValidPrincipalProperty(string property)
62+
{
63+
return new[] { "AWS", "Service" }.Contains(property);
64+
}
65+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace LEGO.AsyncAPI.Bindings.Sqs;
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using LEGO.AsyncAPI.Writers;
6+
7+
public class PrincipalObject : Principal
8+
{
9+
private KeyValuePair<string, StringOrStringList> PrincipalValue;
10+
11+
public PrincipalObject(KeyValuePair<string, StringOrStringList> principalValue)
12+
{
13+
this.PrincipalValue = principalValue;
14+
}
15+
16+
public override void Serialize(IAsyncApiWriter writer)
17+
{
18+
if (writer is null)
19+
{
20+
throw new ArgumentNullException(nameof(writer));
21+
}
22+
23+
writer.WriteStartObject();
24+
writer.WriteRequiredObject(this.PrincipalValue.Key, this.PrincipalValue.Value, (w, t) => t.Value.Write(w));
25+
writer.WriteEndObject();
26+
}
27+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace LEGO.AsyncAPI.Bindings.Sqs;
2+
3+
using System;
4+
using LEGO.AsyncAPI.Writers;
5+
6+
public class PrincipalStar : Principal
7+
{
8+
private string PrincipalValue;
9+
10+
public PrincipalStar()
11+
{
12+
this.PrincipalValue = "*";
13+
}
14+
15+
public override void Serialize(IAsyncApiWriter writer)
16+
{
17+
if (writer is null)
18+
{
19+
throw new ArgumentNullException(nameof(writer));
20+
}
21+
22+
writer.WriteValue(this.PrincipalValue);
23+
}
24+
}

src/LEGO.AsyncAPI.Bindings/Sqs/SqsChannelBinding.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,10 @@ public class SqsChannelBinding : ChannelBinding<SqsChannelBinding>
6464
private static FixedFieldMap<Statement> statementFixedFields = new()
6565
{
6666
{ "effect", (a, n) => { a.Effect = n.GetScalarValue().GetEnumFromDisplayName<Effect>(); } },
67-
{ "principal", (a, n) => { a.Principal = StringOrStringList.Parse(n); } },
67+
{ "principal", (a, n) => { a.Principal = Principal.Parse(n); } },
6868
{ "action", (a, n) => { a.Action = StringOrStringList.Parse(n); } },
69+
{ "resource", (a, n) => { a.Resource = StringOrStringList.Parse(n); } },
70+
{ "condition", (a, n) => { a.Condition = n.CreateAny(); } },
6971
};
7072

7173
public override void SerializeProperties(IAsyncApiWriter writer)

src/LEGO.AsyncAPI.Bindings/Sqs/SqsOperationBinding.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ public class SqsOperationBinding : OperationBinding<SqsOperationBinding>
5656
private static FixedFieldMap<Statement> statementFixedFields = new()
5757
{
5858
{ "effect", (a, n) => { a.Effect = n.GetScalarValue().GetEnumFromDisplayName<Effect>(); } },
59-
{ "principal", (a, n) => { a.Principal = StringOrStringList.Parse(n); } },
59+
{ "principal", (a, n) => { a.Principal = Principal.Parse(n); } },
6060
{ "action", (a, n) => { a.Action = StringOrStringList.Parse(n); } },
61+
{ "resource", (a, n) => { a.Resource = StringOrStringList.Parse(n); } },
62+
{ "condition", (a, n) => { a.Condition = n.CreateAny(); } },
6163
};
6264

6365
public override void SerializeProperties(IAsyncApiWriter writer)

0 commit comments

Comments
 (0)