Skip to content

Commit 0b8cbab

Browse files
committed
fix(rbac): do not mix apigroups.
This fixes [bug]: RBAC roles get mixed between apigroups #583.
1 parent dd94ff8 commit 0b8cbab

File tree

11 files changed

+32
-21
lines changed

11 files changed

+32
-21
lines changed

.editorconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ insert_final_newline = true
2727
dotnet_diagnostic.S3604.severity = none
2828

2929
dotnet_diagnostic.SA0001.severity = none
30+
dotnet_diagnostic.SA1010.severity = none
3031
dotnet_diagnostic.SA1600.severity = none
3132
dotnet_diagnostic.CS1591.severity = none
3233
dotnet_diagnostic.SA1008.severity = suggestion

src/KubeOps.Generator/SyntaxReceiver/EntityControllerSyntaxReceiver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace KubeOps.Generator.SyntaxReceiver;
55

66
internal class EntityControllerSyntaxReceiver : ISyntaxContextReceiver
77
{
8-
public List<(ClassDeclarationSyntax Controller, string EntityName)> Controllers { get; } = new();
8+
public List<(ClassDeclarationSyntax Controller, string EntityName)> Controllers { get; } = [];
99

1010
public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
1111
{

src/KubeOps.Generator/SyntaxReceiver/EntityFinalizerSyntaxReceiver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace KubeOps.Generator.SyntaxReceiver;
55

66
internal class EntityFinalizerSyntaxReceiver : ISyntaxContextReceiver
77
{
8-
public List<(ClassDeclarationSyntax Finalizer, string EntityName)> Finalizer { get; } = new();
8+
public List<(ClassDeclarationSyntax Finalizer, string EntityName)> Finalizer { get; } = [];
99

1010
public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
1111
{

src/KubeOps.Generator/SyntaxReceiver/KubernetesEntitySyntaxReceiver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ internal class KubernetesEntitySyntaxReceiver : ISyntaxContextReceiver
1111
private const string VersionName = "ApiVersion";
1212
private const string DefaultVersion = "v1";
1313

14-
public List<AttributedEntity> Entities { get; } = new();
14+
public List<AttributedEntity> Entities { get; } = [];
1515

1616
public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
1717
{

src/KubeOps.Transpiler/Crds.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static class Crds
3131
private const string Decimal = "decimal";
3232
private const string DateTime = "date-time";
3333

34-
private static readonly string[] IgnoredToplevelProperties = { "metadata", "apiversion", "kind" };
34+
private static readonly string[] IgnoredToplevelProperties = ["metadata", "apiversion", "kind"];
3535

3636
/// <summary>
3737
/// Transpile a single type to a CRD.

src/KubeOps.Transpiler/Rbac.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,13 @@ public static IEnumerable<V1PolicyRule> Transpile(
4848
group => (
4949
Crd: context.ToEntityMetadata(group.Key),
5050
Verbs: group.Aggregate(RbacVerb.None, (accumulator, element) => accumulator | element.Verbs)))
51-
.GroupBy(group => group.Verbs)
52-
.Select(
53-
group => (
54-
Verbs: group.Key,
55-
Crds: group.Select(element => element.Crd).ToList()))
51+
.GroupBy(group => (group.Crd.Metadata.Group, group.Verbs))
5652
.Select(
5753
group => new V1PolicyRule
5854
{
59-
ApiGroups = group.Crds.Select(crd => crd.Metadata.Group).Distinct().ToList(),
60-
Resources = group.Crds.Select(crd => crd.Metadata.PluralName).Distinct().ToList(),
61-
Verbs = ConvertToStrings(group.Verbs),
55+
ApiGroups = [group.Key.Group],
56+
Resources = group.Select(crd => crd.Crd.Metadata.PluralName).Distinct().ToList(),
57+
Verbs = ConvertToStrings(group.Key.Verbs),
6258
});
6359

6460
var entityStatus = list
@@ -75,8 +71,8 @@ public static IEnumerable<V1PolicyRule> Transpile(
7571
.Select(
7672
crd => new V1PolicyRule
7773
{
78-
ApiGroups = new[] { crd.Metadata.Group },
79-
Resources = new[] { $"{crd.Metadata.PluralName}/status" },
74+
ApiGroups = [crd.Metadata.Group],
75+
Resources = [$"{crd.Metadata.PluralName}/status"],
8076
Verbs = ConvertToStrings(RbacVerb.Get | RbacVerb.Patch | RbacVerb.Update),
8177
});
8278

@@ -86,7 +82,7 @@ public static IEnumerable<V1PolicyRule> Transpile(
8682
private static string[] ConvertToStrings(RbacVerb verbs) => verbs switch
8783
{
8884
RbacVerb.None => Array.Empty<string>(),
89-
_ when verbs.HasFlag(RbacVerb.All) => new[] { "*" },
85+
_ when verbs.HasFlag(RbacVerb.All) => ["*"],
9086
_ =>
9187
Enum.GetValues<RbacVerb>()
9288
.Where(v => verbs.HasFlag(v) && v != RbacVerb.All && v != RbacVerb.None)

src/KubeOps.Transpiler/Utilities.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public static IList<T> GetCustomAttributeNamedArrayArg<T>(this CustomAttributeDa
9292
attr.NamedArguments.FirstOrDefault(a => a.MemberName == name).TypedValue.Value is
9393
ReadOnlyCollection<CustomAttributeTypedArgument> value
9494
? value.Select(v => (T)v.Value!).ToList()
95-
: new List<T>();
95+
: [];
9696

9797
/// <summary>
9898
/// Load a specific constructor argument from a custom attribute.
@@ -145,7 +145,7 @@ public static IList<T> GetCustomAttributeCtorArrayArg<T>(
145145
attr.ConstructorArguments[index].Value is
146146
ReadOnlyCollection<CustomAttributeTypedArgument> value
147147
? value.Select(v => (T)v.Value!).ToList()
148-
: new List<T>();
148+
: [];
149149

150150
/// <summary>
151151
/// Load a type from a metadata load context.

test/KubeOps.Operator.Test/IntegrationTestCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public async Task DisposeAsync()
7272

7373
public sealed class CrdInstaller : IAsyncLifetime
7474
{
75-
private List<V1CustomResourceDefinition> _crds = new();
75+
private List<V1CustomResourceDefinition> _crds = [];
7676

7777
public async Task InitializeAsync()
7878
{

test/KubeOps.Operator.Test/InvocationCounter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class InvocationCounter<TEntity>
99
where TEntity : IKubernetesObject<V1ObjectMeta>
1010
{
1111
private TaskCompletionSource _task = new();
12-
public readonly List<(string Method, TEntity Entity)> Invocations = new();
12+
public readonly List<(string Method, TEntity Entity)> Invocations = [];
1313

1414
public Task WaitForInvocations => _task.Task;
1515

test/KubeOps.Operator.Web.Test/IntegrationTestCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public abstract class IntegrationTestBase;
3030

3131
public sealed class CrdInstaller : IAsyncLifetime
3232
{
33-
private List<V1CustomResourceDefinition> _crds = new();
33+
private List<V1CustomResourceDefinition> _crds = [];
3434

3535
public async Task InitializeAsync()
3636
{

0 commit comments

Comments
 (0)