blob: 15386a6e90bf3ed405bdccc13705a8e45f0d9f20 [file] [log] [blame]
Serge Bazanskie6030f62020-06-03 17:52:59 +02001// Copyright 2020 The Monogon Project Authors.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17package reconciler
18
19import (
20 "context"
21
22 rbac "k8s.io/api/rbac/v1"
23 meta "k8s.io/apimachinery/pkg/apis/meta/v1"
24 "k8s.io/client-go/kubernetes"
25)
26
27var (
28 clusterRolePSPDefault = builtinRBACName("psp-default")
29 clusterRoleBindingDefaultPSP = builtinRBACName("default-psp-for-sa")
30 clusterRoleBindingAPIServerKubeletClient = builtinRBACName("apiserver-kubelet-client")
31)
32
33type resourceClusterRoles struct {
34 kubernetes.Interface
35}
36
37func (r resourceClusterRoles) List(ctx context.Context) ([]string, error) {
38 res, err := r.RbacV1().ClusterRoles().List(ctx, listBuiltins)
39 if err != nil {
40 return nil, err
41 }
42 objs := make([]string, len(res.Items))
43 for i, el := range res.Items {
44 objs[i] = el.ObjectMeta.Name
45 }
46 return objs, nil
47}
48
49func (r resourceClusterRoles) Create(ctx context.Context, el interface{}) error {
50 _, err := r.RbacV1().ClusterRoles().Create(ctx, el.(*rbac.ClusterRole), meta.CreateOptions{})
51 return err
52}
53
54func (r resourceClusterRoles) Delete(ctx context.Context, name string) error {
55 return r.RbacV1().ClusterRoles().Delete(ctx, name, meta.DeleteOptions{})
56}
57
58func (r resourceClusterRoles) Expected() map[string]interface{} {
59 return map[string]interface{}{
60 clusterRolePSPDefault: &rbac.ClusterRole{
61 ObjectMeta: meta.ObjectMeta{
62 Name: clusterRolePSPDefault,
63 Labels: builtinLabels(nil),
64 Annotations: map[string]string{
65 "kubernetes.io/description": "This role grants access to the \"default\" PSP.",
66 },
67 },
68 Rules: []rbac.PolicyRule{
69 {
70 APIGroups: []string{"policy"},
71 Resources: []string{"podsecuritypolicies"},
72 ResourceNames: []string{"default"},
73 Verbs: []string{"use"},
74 },
75 },
76 },
77 }
78}
79
80type resourceClusterRoleBindings struct {
81 kubernetes.Interface
82}
83
84func (r resourceClusterRoleBindings) List(ctx context.Context) ([]string, error) {
85 res, err := r.RbacV1().ClusterRoleBindings().List(ctx, listBuiltins)
86 if err != nil {
87 return nil, err
88 }
89 objs := make([]string, len(res.Items))
90 for i, el := range res.Items {
91 objs[i] = el.ObjectMeta.Name
92 }
93 return objs, nil
94}
95
96func (r resourceClusterRoleBindings) Create(ctx context.Context, el interface{}) error {
97 _, err := r.RbacV1().ClusterRoleBindings().Create(ctx, el.(*rbac.ClusterRoleBinding), meta.CreateOptions{})
98 return err
99}
100
101func (r resourceClusterRoleBindings) Delete(ctx context.Context, name string) error {
102 return r.RbacV1().ClusterRoleBindings().Delete(ctx, name, meta.DeleteOptions{})
103}
104
105func (r resourceClusterRoleBindings) Expected() map[string]interface{} {
106 return map[string]interface{}{
107 clusterRoleBindingDefaultPSP: &rbac.ClusterRoleBinding{
108 ObjectMeta: meta.ObjectMeta{
109 Name: clusterRoleBindingDefaultPSP,
110 Labels: builtinLabels(nil),
111 Annotations: map[string]string{
112 "kubernetes.io/description": "This binding grants every service account access to the \"default\" PSP. " +
113 "Creation of Pods is still restricted by other RBAC roles. Otherwise no pods (unprivileged or not) " +
114 "can be created.",
115 },
116 },
117 RoleRef: rbac.RoleRef{
118 APIGroup: rbac.GroupName,
119 Kind: "ClusterRole",
120 Name: clusterRolePSPDefault,
121 },
122 Subjects: []rbac.Subject{
123 {
124 APIGroup: rbac.GroupName,
125 Kind: "Group",
126 Name: "system:serviceaccounts",
127 },
128 },
129 },
130 clusterRoleBindingAPIServerKubeletClient: &rbac.ClusterRoleBinding{
131 ObjectMeta: meta.ObjectMeta{
132 Name: clusterRoleBindingAPIServerKubeletClient,
133 Labels: builtinLabels(nil),
134 Annotations: map[string]string{
135 "kubernetes.io/description": "This binding grants the apiserver access to the kubelets. This enables " +
136 "lots of built-in functionality like reading logs or forwarding ports via the API.",
137 },
138 },
139 RoleRef: rbac.RoleRef{
140 APIGroup: rbac.GroupName,
141 Kind: "ClusterRole",
142 Name: "system:kubelet-api-admin",
143 },
144 Subjects: []rbac.Subject{
145 {
146 APIGroup: rbac.GroupName,
147 Kind: "User",
148 // TODO(q3k): describe this name's contract, or unify with whatever creates this.
Serge Bazanski662b5b32020-12-21 13:49:00 +0100149 Name: "metropolis:apiserver-kubelet-client",
Serge Bazanskie6030f62020-06-03 17:52:59 +0200150 },
151 },
152 },
153 }
154}