-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
model_adaptor_loader.go
116 lines (106 loc) · 3.16 KB
/
model_adaptor_loader.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
111
112
113
114
115
116
// Copyright 2022 The Casbin Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package model
import (
"context"
"path/filepath"
"strings"
"github.com/casbin/casbin/v2/model"
"github.com/casbin/casbin/v2/persist"
"github.com/casbin/k8s-gatekeeper/pkg/crdadaptor"
"github.com/casbin/k8s-gatekeeper/pkg/generated/clientset/versioned"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
)
type ModelLoader struct {
namespace string
isExternalClient bool
clientset *versioned.Clientset
}
type ModelAdaptorPair struct {
Name string
Model model.Model
Adaptor persist.Adapter
}
func NewModelLoader(namespace string, isExternalClient bool) (*ModelLoader, error) {
var res = &ModelLoader{
namespace: namespace,
isExternalClient: isExternalClient,
}
var err error
if isExternalClient {
err = res.establishExternalClient()
} else {
err = res.establishInternalClient()
}
return res, err
}
func (m *ModelLoader) GetModelAndAdaptors() ([]ModelAdaptorPair, error) {
list, err := m.clientset.AuthV1().CasbinModels(m.namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
res := make([]ModelAdaptorPair, 0)
for _, crdModel := range list.Items {
modelText := ModelMacroSubstitution(crdModel.Spec.ModelText)
casbinModel, err := model.NewModelFromString(modelText)
if err != nil {
return nil, err
}
modelName := crdModel.ObjectMeta.Name
casbinAdaptor, err := crdadaptor.NewK8sAdaptor(m.namespace, modelName, m.isExternalClient)
if err != nil {
return nil, err
}
res = append(res, ModelAdaptorPair{
Model: casbinModel,
Adaptor: casbinAdaptor,
Name: modelName,
})
}
return res, nil
}
func (m *ModelLoader) establishInternalClient() error {
config, err := rest.InClusterConfig()
if err != nil {
return err
}
clientset, err := versioned.NewForConfig(config)
if err != nil {
return err
}
m.clientset = clientset
return nil
}
func (m *ModelLoader) establishExternalClient() error {
home := homedir.HomeDir()
config, err := clientcmd.BuildConfigFromFlags("", filepath.Join(home, ".kube", "config"))
if err != nil {
return err
}
clientset, err := versioned.NewForConfig(config)
if err != nil {
return err
}
m.clientset = clientset
return nil
}
func ModelMacroSubstitution(originalModelText string) string {
res := strings.ReplaceAll(originalModelText, "${OBJECT}", "r.obj.Request.Object.Object")
res = strings.ReplaceAll(res, "${NAMESPACE}", "r.obj.Request.Namespace")
res = strings.ReplaceAll(res, "${RESOURCE}", "r.obj.Request.Resource.Resource")
return res
}