Skip to content

Commit 1d3fbe8

Browse files
Weeblinribbybibby
authored andcommitted
WIP
1 parent f2d056f commit 1d3fbe8

File tree

2 files changed

+58
-64
lines changed

2 files changed

+58
-64
lines changed

pkg/permissions/generate.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,39 @@ func GenerateRoles(dataGatherer []agent.DataGatherer) []rbac.ClusterRole {
7373
}
7474
return out
7575
}
76+
77+
// func SelectNamespace(all, include, exclude) selected {
78+
79+
// }
80+
81+
func GenerateBindings(clusterRoles []rbac.ClusterRole) []rbac.ClusterRoleBinding {
82+
out := []rbac.ClusterRoleBinding{}
83+
for _, cr := range clusterRoles {
84+
out = append(out, rbac.ClusterRoleBinding{
85+
TypeMeta: metav1.TypeMeta{
86+
Kind: "ClusterRoleBinding",
87+
APIVersion: "rbac.authorization.k8s.io/v1",
88+
},
89+
90+
ObjectMeta: metav1.ObjectMeta{
91+
Name: fmt.Sprintf("jetstack-secure-agent-%s-reader", cr.TypeMeta.Kind),
92+
},
93+
94+
Subjects: []rbac.Subject{
95+
{
96+
Kind: "ServiceAccount",
97+
Name: "agent",
98+
Namespace: "jetstack-secure",
99+
},
100+
},
101+
102+
RoleRef: rbac.RoleRef{
103+
Kind: "ClusterRole",
104+
Name: cr.ObjectMeta.Name,
105+
APIGroup: "rbac.authorization.k8s.io",
106+
},
107+
})
108+
109+
}
110+
return out
111+
}

pkg/permissions/generate_test.go

Lines changed: 22 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,14 @@ func TestGenerateRBAC(t *testing.T) {
1515
// Use these test cases to check if Generate function is correct
1616
testCases := []struct {
1717
// expectedClusterRoles is the collection of ClusterRole
18-
expectedClusterRoles []rbac.ClusterRole
19-
dataGatherers []agent.DataGatherer
20-
description string
18+
expectedClusterRoleBindings []rbac.ClusterRoleBinding
19+
dataGatherers []agent.DataGatherer
20+
description string
2121
}{
2222
{
2323
description: "Generate RBAC struct for pods datagatherer",
2424
dataGatherers: []agent.DataGatherer{
25-
{
26-
Name: "k8s/pods",
27-
Kind: "k8s-dynamic",
28-
Config: &k8s.ConfigDynamic{
29-
GroupVersionResource: schema.GroupVersionResource{
30-
Version: "v1",
31-
Resource: "pods",
32-
},
33-
},
34-
},
25+
3526
{
3627
Name: "k8s/secrets",
3728
Kind: "k8s-dynamic",
@@ -42,65 +33,31 @@ func TestGenerateRBAC(t *testing.T) {
4233
},
4334
},
4435
},
45-
{
46-
Name: "k8s/awspcaissuer",
47-
Kind: "k8s-dynamic",
48-
Config: &k8s.ConfigDynamic{
49-
GroupVersionResource: schema.GroupVersionResource{
50-
Group: "awspca.cert-manager.io",
51-
Version: "v1",
52-
Resource: "awspcaissuers",
53-
},
54-
},
55-
},
5636
},
57-
expectedClusterRoles: []rbac.ClusterRole{
58-
{
59-
TypeMeta: metav1.TypeMeta{
60-
Kind: "ClusterRole",
61-
APIVersion: "rbac.authorization.k8s.io/v1",
62-
},
63-
ObjectMeta: metav1.ObjectMeta{
64-
Name: "jetstack-secure-agent-pods-reader",
65-
},
66-
Rules: []rbac.PolicyRule{
67-
{
68-
Verbs: []string{"get", "list", "watch"},
69-
APIGroups: []string{""},
70-
Resources: []string{"pods"},
71-
},
72-
},
73-
},
37+
expectedClusterRoleBindings: []rbac.ClusterRoleBinding{
38+
7439
{
7540
TypeMeta: metav1.TypeMeta{
76-
Kind: "ClusterRole",
41+
Kind: "ClusterRoleBinding",
7742
APIVersion: "rbac.authorization.k8s.io/v1",
7843
},
44+
7945
ObjectMeta: metav1.ObjectMeta{
80-
Name: "jetstack-secure-agent-secrets-reader",
46+
Name: "jetstack-secure-agent-ClusterRole-reader",
8147
},
82-
Rules: []rbac.PolicyRule{
48+
49+
Subjects: []rbac.Subject{
8350
{
84-
Verbs: []string{"get", "list", "watch"},
85-
APIGroups: []string{""},
86-
Resources: []string{"secrets"},
51+
Kind: "ServiceAccount",
52+
Name: "agent",
53+
Namespace: "jetstack-secure",
8754
},
8855
},
89-
},
90-
{
91-
TypeMeta: metav1.TypeMeta{
92-
Kind: "ClusterRole",
93-
APIVersion: "rbac.authorization.k8s.io/v1",
94-
},
95-
ObjectMeta: metav1.ObjectMeta{
96-
Name: "jetstack-secure-agent-awspcaissuers-reader",
97-
},
98-
Rules: []rbac.PolicyRule{
99-
{
100-
Verbs: []string{"get", "list", "watch"},
101-
APIGroups: []string{"awspca.cert-manager.io"},
102-
Resources: []string{"awspcaissuers"},
103-
},
56+
57+
RoleRef: rbac.RoleRef{
58+
Kind: "ClusterRole",
59+
Name: "jetstack-secure-agent-secret-reader",
60+
APIGroup: "rbac.authorization.k8s.io",
10461
},
10562
},
10663
},
@@ -110,9 +67,10 @@ func TestGenerateRBAC(t *testing.T) {
11067

11168
for _, input := range testCases {
11269
got := GenerateRoles(input.dataGatherers)
113-
if diff, equal := messagediff.PrettyDiff(input.expectedClusterRoles, got); !equal {
70+
toBeTest := GenerateBindings(got)
71+
if diff, equal := messagediff.PrettyDiff(input.expectedClusterRoleBindings, toBeTest); !equal {
11472
t.Errorf("%s:\n%s", input.description, diff)
115-
t.Fatalf("unexpected difference in RBAC cluster role: \ngot \n%v\nwant\n%v", got, input.expectedClusterRoles)
73+
t.Fatalf("unexpected difference in RBAC cluster role: \ngot \n%v\nwant\n%v", got, input.expectedClusterRoleBindings)
11674
}
11775
}
11876
}

0 commit comments

Comments
 (0)