Skip to content

Commit 1433408

Browse files
authored
Merge pull request #253 from jetstack/rbac_struct_2
Rbac struct 2
2 parents 6321e16 + 39e64be commit 1433408

File tree

2 files changed

+134
-37
lines changed

2 files changed

+134
-37
lines changed

pkg/permissions/generate.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66

77
"github.com/jetstack/preflight/pkg/agent"
88
"github.com/jetstack/preflight/pkg/datagatherer/k8s"
9+
rbac "k8s.io/api/rbac/v1"
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
911
)
1012

1113
func Generate(dataGatherers []agent.DataGatherer) string {
@@ -37,3 +39,37 @@ rules:
3739
ss := strings.TrimSuffix(s, "---")
3840
return strings.TrimSuffix(ss, "\n")
3941
}
42+
43+
func GenerateRoles(dataGatherer []agent.DataGatherer) []rbac.ClusterRole {
44+
out := []rbac.ClusterRole{}
45+
46+
for _, g := range dataGatherer {
47+
if g.Kind != "k8s-dynamic" {
48+
continue
49+
}
50+
51+
genericConfig := g.Config
52+
dyConfig := genericConfig.(*k8s.ConfigDynamic)
53+
54+
metaName := dyConfig.GroupVersionResource.Resource
55+
56+
out = append(out, rbac.ClusterRole{
57+
TypeMeta: metav1.TypeMeta{
58+
Kind: "ClusterRole",
59+
APIVersion: "rbac.authorization.k8s.io/v1",
60+
},
61+
ObjectMeta: metav1.ObjectMeta{
62+
Name: fmt.Sprintf("jetstack-secure-agent-%s-reader", metaName),
63+
},
64+
Rules: []rbac.PolicyRule{
65+
{
66+
Verbs: []string{"get", "list", "watch"},
67+
APIGroups: []string{dyConfig.GroupVersionResource.Group},
68+
Resources: []string{metaName},
69+
},
70+
},
71+
})
72+
73+
}
74+
return out
75+
}

pkg/permissions/generate_test.go

Lines changed: 98 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,116 @@ package permissions
33
import (
44
"testing"
55

6+
"github.com/d4l3k/messagediff"
67
"github.com/jetstack/preflight/pkg/agent"
78
"github.com/jetstack/preflight/pkg/datagatherer/k8s"
9+
rbac "k8s.io/api/rbac/v1"
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
811
"k8s.io/apimachinery/pkg/runtime/schema"
912
)
1013

11-
func TestGenerate(t *testing.T) {
12-
inputDatagatherers := []agent.DataGatherer{
14+
func TestGenerateRBAC(t *testing.T) {
15+
// Use these test cases to check if Generate function is correct
16+
testCases := []struct {
17+
// expectedClusterRoles is the collection of ClusterRole
18+
expectedClusterRoles []rbac.ClusterRole
19+
dataGatherers []agent.DataGatherer
20+
description string
21+
}{
1322
{
14-
Name: "k8s/pods",
15-
Kind: "k8s-dynamic",
16-
Config: &k8s.ConfigDynamic{
17-
GroupVersionResource: schema.GroupVersionResource{
18-
Version: "v1",
19-
Resource: "pods",
23+
description: "Generate RBAC struct for pods datagatherer",
24+
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+
},
35+
{
36+
Name: "k8s/secrets",
37+
Kind: "k8s-dynamic",
38+
Config: &k8s.ConfigDynamic{
39+
GroupVersionResource: schema.GroupVersionResource{
40+
Version: "v1",
41+
Resource: "secrets",
42+
},
43+
},
44+
},
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+
},
2055
},
2156
},
22-
},
23-
{
24-
Name: "k8s/secrets",
25-
Kind: "k8s-dynamic",
26-
Config: &k8s.ConfigDynamic{
27-
GroupVersionResource: schema.GroupVersionResource{
28-
Version: "v1",
29-
Resource: "secrets",
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+
},
74+
{
75+
TypeMeta: metav1.TypeMeta{
76+
Kind: "ClusterRole",
77+
APIVersion: "rbac.authorization.k8s.io/v1",
78+
},
79+
ObjectMeta: metav1.ObjectMeta{
80+
Name: "jetstack-secure-agent-secrets-reader",
81+
},
82+
Rules: []rbac.PolicyRule{
83+
{
84+
Verbs: []string{"get", "list", "watch"},
85+
APIGroups: []string{""},
86+
Resources: []string{"secrets"},
87+
},
88+
},
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+
},
104+
},
30105
},
31106
},
32107
},
108+
// Try adding more test cases
33109
}
34110

35-
expectedOutput := `apiVersion: rbac.authorization.k8s.io/v1
36-
kind: ClusterRole
37-
metadata:
38-
name: jetstack-secure-agent-pods-reader
39-
rules:
40-
- apiGroups: [""]
41-
resources: ["pods"]
42-
verbs: ["get", "list", "watch"]
43-
---
44-
apiVersion: rbac.authorization.k8s.io/v1
45-
kind: ClusterRole
46-
metadata:
47-
name: jetstack-secure-agent-secrets-reader
48-
rules:
49-
- apiGroups: [""]
50-
resources: ["secrets"]
51-
verbs: ["get", "list", "watch"]`
52-
53-
if output := Generate(inputDatagatherers); output != expectedOutput {
54-
t.Fatalf("unexpected output \n%s \n expected: \n%s", output, expectedOutput)
111+
for _, input := range testCases {
112+
got := GenerateRoles(input.dataGatherers)
113+
if diff, equal := messagediff.PrettyDiff(input.expectedClusterRoles, got); !equal {
114+
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)
116+
}
55117
}
56-
57118
}

0 commit comments

Comments
 (0)