Skip to content

Commit 1efc94a

Browse files
authored
Merge pull request #200 from hasbro17/haseeb/restrict-rbac-rules
generator: restrict default RBAC rules
2 parents 4d00a62 + 8491bbd commit 1efc94a

File tree

4 files changed

+62
-16
lines changed

4 files changed

+62
-16
lines changed

pkg/generator/deploy_tmpl.go

+21
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,30 @@ metadata:
5656
name: {{.ProjectName}}
5757
rules:
5858
- apiGroups:
59+
- {{.GroupName}}
60+
resources:
61+
- "*"
62+
verbs:
5963
- "*"
64+
- apiGroups:
65+
- ""
6066
resources:
67+
- pods
68+
- services
69+
- endpoints
70+
- persistentvolumeclaims
71+
- events
72+
- configmaps
73+
- secrets
74+
verbs:
6175
- "*"
76+
- apiGroups:
77+
- apps
78+
resources:
79+
- deployments
80+
- daemonsets
81+
- replicasets
82+
- statefulsets
6283
verbs:
6384
- "*"
6485

pkg/generator/gen_deploy.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,21 @@ func renderOperatorYaml(w io.Writer, kind, apiVersion, projectName, image string
6666
// when pairing with rbacYamlTmpl template.
6767
type RBACYaml struct {
6868
ProjectName string
69+
GroupName string
6970
}
7071

7172
// renderRBACYaml generates deploy/rbac.yaml.
72-
func renderRBACYaml(w io.Writer, projectName string) error {
73+
func renderRBACYaml(w io.Writer, projectName, groupName string) error {
7374
t := template.New(rbacTmplName)
7475
t, err := t.Parse(rbacYamlTmpl)
7576
if err != nil {
7677
return fmt.Errorf("failed to parse rbac yaml template: %v", err)
7778
}
7879

79-
r := RBACYaml{ProjectName: projectName}
80+
r := RBACYaml{
81+
ProjectName: projectName,
82+
GroupName: groupName,
83+
}
8084
return t.Execute(w, r)
8185
}
8286

pkg/generator/generator.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,17 @@ func (g *Generator) renderDeploy() error {
167167
return renderDeployFiles(dp, g.projectName, g.apiVersion, g.kind)
168168
}
169169

170-
func renderRBAC(deployDir, projectName string) error {
170+
func renderRBAC(deployDir, projectName, groupName string) error {
171171
buf := &bytes.Buffer{}
172-
if err := renderRBACYaml(buf, projectName); err != nil {
172+
if err := renderRBACYaml(buf, projectName, groupName); err != nil {
173173
return err
174174
}
175175
return writeFileAndPrint(filepath.Join(deployDir, rbacYaml), buf.Bytes(), defaultFileMode)
176176
}
177177

178178
func renderDeployFiles(deployDir, projectName, apiVersion, kind string) error {
179179
buf := &bytes.Buffer{}
180-
if err := renderRBACYaml(buf, projectName); err != nil {
180+
if err := renderRBACYaml(buf, projectName, groupName(apiVersion)); err != nil {
181181
return err
182182
}
183183
if err := writeFileAndPrint(filepath.Join(deployDir, rbacYaml), buf.Bytes(), defaultFileMode); err != nil {

pkg/generator/generator_test.go

+32-11
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ import (
2121

2222
const (
2323
// test constants for app-operator
24-
appRepoPath = "github.com/example-inc/app-operator"
25-
appKind = "App"
26-
appApiDirName = "app"
27-
appAPIVersion = appGroupName + "/" + appVersion
28-
appVersion = "v1alpha1"
29-
appGroupName = "app.example.com"
24+
appRepoPath = "github.com/example-inc/app-operator"
25+
appKind = "AppService"
26+
appApiDirName = "app"
27+
appAPIVersion = appGroupName + "/" + appVersion
28+
appVersion = "v1alpha1"
29+
appGroupName = "app.example.com"
30+
appProjectName = "app-operator"
3031
)
3132

3233
const mainExp = `package main
@@ -50,7 +51,7 @@ func printVersion() {
5051
5152
func main() {
5253
printVersion()
53-
sdk.Watch("app.example.com/v1alpha1", "App", "default", 5)
54+
sdk.Watch("app.example.com/v1alpha1", "AppService", "default", 5)
5455
sdk.Handle(stub.NewHandler())
5556
sdk.Run(context.TODO())
5657
}
@@ -120,7 +121,7 @@ func newbusyBoxPod(cr *v1alpha1.App) *v1.Pod {
120121
*metav1.NewControllerRef(cr, schema.GroupVersionKind{
121122
Group: v1alpha1.SchemeGroupVersion.Group,
122123
Version: v1alpha1.SchemeGroupVersion.Version,
123-
Kind: "App",
124+
Kind: "AppService",
124125
}),
125126
},
126127
Labels: labels,
@@ -421,9 +422,30 @@ metadata:
421422
name: app-operator
422423
rules:
423424
- apiGroups:
425+
- app.example.com
426+
resources:
427+
- "*"
428+
verbs:
424429
- "*"
430+
- apiGroups:
431+
- ""
425432
resources:
433+
- pods
434+
- services
435+
- endpoints
436+
- persistentvolumeclaims
437+
- events
438+
- configmaps
439+
- secrets
440+
verbs:
426441
- "*"
442+
- apiGroups:
443+
- apps
444+
resources:
445+
- deployments
446+
- daemonsets
447+
- replicasets
448+
- statefulsets
427449
verbs:
428450
- "*"
429451
@@ -444,16 +466,15 @@ roleRef:
444466

445467
func TestGenDeploy(t *testing.T) {
446468
buf := &bytes.Buffer{}
447-
projectName := "app-operator"
448-
if err := renderOperatorYaml(buf, "AppService", "app.example.com/v1alpha1", projectName, "quay.io/coreos/operator-sdk-dev:app-operator"); err != nil {
469+
if err := renderOperatorYaml(buf, appKind, appAPIVersion, appProjectName, "quay.io/coreos/operator-sdk-dev:app-operator"); err != nil {
449470
t.Error(err)
450471
}
451472
if operatorYamlExp != buf.String() {
452473
t.Errorf("want %v, got %v", operatorYamlExp, buf.String())
453474
}
454475

455476
buf = &bytes.Buffer{}
456-
if err := renderRBACYaml(buf, projectName); err != nil {
477+
if err := renderRBACYaml(buf, appProjectName, appGroupName); err != nil {
457478
t.Error(err)
458479
}
459480
if rbacYamlExp != buf.String() {

0 commit comments

Comments
 (0)