Skip to content

Commit 92063d8

Browse files
feat: Add AllExplicit RBAC verb & state all default V1Lease RBAC verbs explicitly (#879)
## Overview Adds new `AllExplicit` RBAC verb to state all RBAC verbs explicitly, alternatively to the current `All` RBAC verb that generates a `*` (wildcard) RBAC verb. ### Motivation As described [here](https://kubernetes.io/docs/reference/access-authn-authz/rbac/#referring-to-resources), using wildcards in verb entries could result in overly permissive access being granted. The [principle of least privilege](https://kubernetes.io/docs/concepts/security/rbac-good-practices/#least-privilege) should be employed, using specific verbs to ensure only the permissions required for the workload to function correctly are applied. --------- Co-authored-by: Christoph Bühler <[email protected]>
1 parent 4e0c205 commit 92063d8

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

src/KubeOps.Abstractions/Rbac/RbacVerbs.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,9 @@ public enum RbacVerb
5050
/// Delete resources on the api.
5151
/// </summary>
5252
Delete = 1 << 7,
53+
54+
/// <summary>
55+
/// All possible permissions (defined explicitly).
56+
/// </summary>
57+
AllExplicit = 1 << 8,
5358
}

src/KubeOps.Cli/Generators/RbacGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ public void Generate(ResultOutput output)
3636
}
3737

3838
[EntityRbac(typeof(Corev1Event), Verbs = RbacVerb.Get | RbacVerb.List | RbacVerb.Create | RbacVerb.Update)]
39-
[EntityRbac(typeof(V1Lease), Verbs = RbacVerb.All)]
39+
[EntityRbac(typeof(V1Lease), Verbs = RbacVerb.AllExplicit)]
4040
private sealed class DefaultRbacAttributes;
4141
}

src/KubeOps.Transpiler/Rbac.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,14 @@ public static IEnumerable<V1PolicyRule> Transpile(
8383
{
8484
RbacVerb.None => Array.Empty<string>(),
8585
_ when verbs.HasFlag(RbacVerb.All) => ["*"],
86+
_ when verbs.HasFlag(RbacVerb.AllExplicit) =>
87+
Enum.GetValues<RbacVerb>()
88+
.Where(v => v != RbacVerb.All && v != RbacVerb.None && v != RbacVerb.AllExplicit)
89+
.Select(v => v.ToString().ToLowerInvariant())
90+
.ToArray(),
8691
_ =>
8792
Enum.GetValues<RbacVerb>()
88-
.Where(v => verbs.HasFlag(v) && v != RbacVerb.All && v != RbacVerb.None)
93+
.Where(v => verbs.HasFlag(v) && v != RbacVerb.All && v != RbacVerb.None && v != RbacVerb.AllExplicit)
8994
.Select(v => v.ToString().ToLowerInvariant())
9095
.ToArray(),
9196
};

test/KubeOps.Transpiler.Test/Rbac.Mlc.Test.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ public void Should_Not_Mix_ApiGroups()
8383
roles.Should().HaveCount(5);
8484
}
8585

86+
[Fact]
87+
public void Should_Correctly_Calculate_All_Verbs_Explicitly()
88+
{
89+
var role = _mlc
90+
.Transpile(_mlc.GetContextType<RbacTest6>().GetCustomAttributesData<EntityRbacAttribute>()).ToList().First();
91+
role.Resources.Should().Contain("leases");
92+
role.Verbs.Should().Contain(new[] { "get", "list", "watch", "create", "update", "patch", "delete" });
93+
}
94+
8695
[KubernetesEntity(Group = "test", ApiVersion = "v1")]
8796
[EntityRbac(typeof(RbacTest1), Verbs = RbacVerb.Get)]
8897
[EntityRbac(typeof(RbacTest1), Verbs = RbacVerb.Update)]
@@ -115,6 +124,10 @@ public class RbacTest4 : CustomKubernetesEntity;
115124
[EntityRbac(typeof(V1Lease), Verbs = RbacVerb.All)]
116125
public class RbacTest5 : CustomKubernetesEntity;
117126

127+
[KubernetesEntity(Group = "test", ApiVersion = "v1")]
128+
[EntityRbac(typeof(V1Lease), Verbs = RbacVerb.AllExplicit)]
129+
public class RbacTest6 : CustomKubernetesEntity;
130+
118131
[KubernetesEntity(Group = "test", ApiVersion = "v1")]
119132
[GenericRbac(Urls = ["url", "foobar"], Resources = ["configmaps"], Groups = ["group"],
120133
Verbs = RbacVerb.Delete | RbacVerb.Get)]

0 commit comments

Comments
 (0)