Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 1 | // 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 | |
| 17 | package reconciler |
| 18 | |
| 19 | import ( |
| 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 | |
| 27 | var ( |
| 28 | clusterRolePSPDefault = builtinRBACName("psp-default") |
| 29 | clusterRoleBindingDefaultPSP = builtinRBACName("default-psp-for-sa") |
| 30 | clusterRoleBindingAPIServerKubeletClient = builtinRBACName("apiserver-kubelet-client") |
Lorenz Brun | cc078df | 2021-12-23 11:51:55 +0100 | [diff] [blame] | 31 | clusterRoleBindingOwnerAdmin = builtinRBACName("owner-admin") |
Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 32 | ) |
| 33 | |
| 34 | type resourceClusterRoles struct { |
| 35 | kubernetes.Interface |
| 36 | } |
| 37 | |
| 38 | func (r resourceClusterRoles) List(ctx context.Context) ([]string, error) { |
| 39 | res, err := r.RbacV1().ClusterRoles().List(ctx, listBuiltins) |
| 40 | if err != nil { |
| 41 | return nil, err |
| 42 | } |
| 43 | objs := make([]string, len(res.Items)) |
| 44 | for i, el := range res.Items { |
| 45 | objs[i] = el.ObjectMeta.Name |
| 46 | } |
| 47 | return objs, nil |
| 48 | } |
| 49 | |
| 50 | func (r resourceClusterRoles) Create(ctx context.Context, el interface{}) error { |
| 51 | _, err := r.RbacV1().ClusterRoles().Create(ctx, el.(*rbac.ClusterRole), meta.CreateOptions{}) |
| 52 | return err |
| 53 | } |
| 54 | |
| 55 | func (r resourceClusterRoles) Delete(ctx context.Context, name string) error { |
| 56 | return r.RbacV1().ClusterRoles().Delete(ctx, name, meta.DeleteOptions{}) |
| 57 | } |
| 58 | |
| 59 | func (r resourceClusterRoles) Expected() map[string]interface{} { |
| 60 | return map[string]interface{}{ |
| 61 | clusterRolePSPDefault: &rbac.ClusterRole{ |
| 62 | ObjectMeta: meta.ObjectMeta{ |
| 63 | Name: clusterRolePSPDefault, |
| 64 | Labels: builtinLabels(nil), |
| 65 | Annotations: map[string]string{ |
| 66 | "kubernetes.io/description": "This role grants access to the \"default\" PSP.", |
| 67 | }, |
| 68 | }, |
| 69 | Rules: []rbac.PolicyRule{ |
| 70 | { |
| 71 | APIGroups: []string{"policy"}, |
| 72 | Resources: []string{"podsecuritypolicies"}, |
| 73 | ResourceNames: []string{"default"}, |
| 74 | Verbs: []string{"use"}, |
| 75 | }, |
| 76 | }, |
| 77 | }, |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | type resourceClusterRoleBindings struct { |
| 82 | kubernetes.Interface |
| 83 | } |
| 84 | |
| 85 | func (r resourceClusterRoleBindings) List(ctx context.Context) ([]string, error) { |
| 86 | res, err := r.RbacV1().ClusterRoleBindings().List(ctx, listBuiltins) |
| 87 | if err != nil { |
| 88 | return nil, err |
| 89 | } |
| 90 | objs := make([]string, len(res.Items)) |
| 91 | for i, el := range res.Items { |
| 92 | objs[i] = el.ObjectMeta.Name |
| 93 | } |
| 94 | return objs, nil |
| 95 | } |
| 96 | |
| 97 | func (r resourceClusterRoleBindings) Create(ctx context.Context, el interface{}) error { |
| 98 | _, err := r.RbacV1().ClusterRoleBindings().Create(ctx, el.(*rbac.ClusterRoleBinding), meta.CreateOptions{}) |
| 99 | return err |
| 100 | } |
| 101 | |
| 102 | func (r resourceClusterRoleBindings) Delete(ctx context.Context, name string) error { |
| 103 | return r.RbacV1().ClusterRoleBindings().Delete(ctx, name, meta.DeleteOptions{}) |
| 104 | } |
| 105 | |
| 106 | func (r resourceClusterRoleBindings) Expected() map[string]interface{} { |
| 107 | return map[string]interface{}{ |
| 108 | clusterRoleBindingDefaultPSP: &rbac.ClusterRoleBinding{ |
| 109 | ObjectMeta: meta.ObjectMeta{ |
| 110 | Name: clusterRoleBindingDefaultPSP, |
| 111 | Labels: builtinLabels(nil), |
| 112 | Annotations: map[string]string{ |
| 113 | "kubernetes.io/description": "This binding grants every service account access to the \"default\" PSP. " + |
| 114 | "Creation of Pods is still restricted by other RBAC roles. Otherwise no pods (unprivileged or not) " + |
| 115 | "can be created.", |
| 116 | }, |
| 117 | }, |
| 118 | RoleRef: rbac.RoleRef{ |
| 119 | APIGroup: rbac.GroupName, |
| 120 | Kind: "ClusterRole", |
| 121 | Name: clusterRolePSPDefault, |
| 122 | }, |
| 123 | Subjects: []rbac.Subject{ |
| 124 | { |
| 125 | APIGroup: rbac.GroupName, |
| 126 | Kind: "Group", |
| 127 | Name: "system:serviceaccounts", |
| 128 | }, |
| 129 | }, |
| 130 | }, |
| 131 | clusterRoleBindingAPIServerKubeletClient: &rbac.ClusterRoleBinding{ |
| 132 | ObjectMeta: meta.ObjectMeta{ |
| 133 | Name: clusterRoleBindingAPIServerKubeletClient, |
| 134 | Labels: builtinLabels(nil), |
| 135 | Annotations: map[string]string{ |
| 136 | "kubernetes.io/description": "This binding grants the apiserver access to the kubelets. This enables " + |
| 137 | "lots of built-in functionality like reading logs or forwarding ports via the API.", |
| 138 | }, |
| 139 | }, |
| 140 | RoleRef: rbac.RoleRef{ |
| 141 | APIGroup: rbac.GroupName, |
| 142 | Kind: "ClusterRole", |
| 143 | Name: "system:kubelet-api-admin", |
| 144 | }, |
| 145 | Subjects: []rbac.Subject{ |
| 146 | { |
| 147 | APIGroup: rbac.GroupName, |
| 148 | Kind: "User", |
| 149 | // TODO(q3k): describe this name's contract, or unify with whatever creates this. |
Serge Bazanski | 662b5b3 | 2020-12-21 13:49:00 +0100 | [diff] [blame] | 150 | Name: "metropolis:apiserver-kubelet-client", |
Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 151 | }, |
| 152 | }, |
| 153 | }, |
Lorenz Brun | cc078df | 2021-12-23 11:51:55 +0100 | [diff] [blame] | 154 | clusterRoleBindingOwnerAdmin: &rbac.ClusterRoleBinding{ |
| 155 | ObjectMeta: meta.ObjectMeta{ |
| 156 | Name: clusterRoleBindingOwnerAdmin, |
| 157 | Labels: builtinLabels(nil), |
| 158 | Annotations: map[string]string{ |
| 159 | "kubernetes.io/description": "This binding grants the Metropolis Cluster owner access to the " + |
| 160 | "cluster-admin role on Kubernetes.", |
| 161 | }, |
| 162 | }, |
| 163 | RoleRef: rbac.RoleRef{ |
| 164 | APIGroup: rbac.GroupName, |
| 165 | Kind: "ClusterRole", |
| 166 | Name: "cluster-admin", |
| 167 | }, |
| 168 | Subjects: []rbac.Subject{ |
| 169 | { |
| 170 | APIGroup: rbac.GroupName, |
| 171 | Kind: "User", |
| 172 | Name: "owner", |
| 173 | }, |
| 174 | }, |
| 175 | }, |
Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 176 | } |
| 177 | } |