forked from gravitational/teleport
/
clusterrole.go
110 lines (100 loc) · 3.11 KB
/
clusterrole.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* Teleport
* Copyright (C) 2023 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package kubeserver
import (
"net/http"
"sort"
"github.com/gravitational/trace"
"github.com/julienschmidt/httprouter"
authv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/gravitational/teleport/api/types"
)
var clusterRoleList = authv1.ClusterRoleList{
TypeMeta: metav1.TypeMeta{
Kind: "ClusterRoleList",
APIVersion: "rbac.authorization.k8s.io/v1",
},
ListMeta: metav1.ListMeta{
ResourceVersion: "1231415",
},
Items: []authv1.ClusterRole{
newClusterRole("nginx-1"),
newClusterRole("nginx-2"),
newClusterRole("test"),
},
}
func newClusterRole(name string) authv1.ClusterRole {
return authv1.ClusterRole{
TypeMeta: metav1.TypeMeta{
Kind: "ClusterRole",
APIVersion: "rbac.authorization.k8s.io/v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
}
}
func (s *KubeMockServer) listClusterRoles(w http.ResponseWriter, req *http.Request, p httprouter.Params) (any, error) {
return &clusterRoleList, nil
}
func (s *KubeMockServer) getClusterRole(w http.ResponseWriter, req *http.Request, p httprouter.Params) (any, error) {
name := p.ByName("name")
filter := func(role authv1.ClusterRole) bool {
return role.Name == name
}
for _, role := range clusterRoleList.Items {
if filter(role) {
return role, nil
}
}
return nil, trace.NotFound("cluster role %q not found", name)
}
func (s *KubeMockServer) deleteClusterRole(w http.ResponseWriter, req *http.Request, p httprouter.Params) (any, error) {
name := p.ByName("name")
deleteOpts, err := parseDeleteCollectionBody(req.Body)
if err != nil {
return nil, trace.Wrap(err)
}
reqID := ""
if deleteOpts.Preconditions != nil && deleteOpts.Preconditions.UID != nil {
reqID = string(*deleteOpts.Preconditions.UID)
}
filter := func(role authv1.ClusterRole) bool {
return role.Name == name
}
for _, role := range clusterRoleList.Items {
if filter(role) {
s.mu.Lock()
key := deletedResource{reqID, types.KindKubeClusterRole}
s.deletedResources[key] = append(s.deletedResources[key], name)
s.mu.Unlock()
return role, nil
}
}
return nil, trace.NotFound("cluster %q not found", name)
}
func (s *KubeMockServer) DeletedClusterRoles(reqID string) []string {
s.mu.Lock()
key := deletedResource{reqID, types.KindKubeClusterRole}
deleted := make([]string, len(s.deletedResources[key]))
copy(deleted, s.deletedResources[key])
s.mu.Unlock()
sort.Strings(deleted)
return deleted
}