-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
131 lines (107 loc) · 3.28 KB
/
store.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package userscope
import (
"fmt"
"strings"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
v1 "github.com/rancher/types/apis/core/v1"
client "github.com/rancher/types/client/management/v3"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
NamespaceID = client.PreferenceFieldNamespaceId
)
type Store struct {
Store types.Store
nsClient v1.NamespaceInterface
}
func NewStore(nsClient v1.NamespaceInterface, store types.Store) *Store {
return &Store{
Store: store,
nsClient: nsClient,
}
}
func (s *Store) Context() types.StorageContext {
return s.Store.Context()
}
func (s *Store) ByID(apiContext *types.APIContext, schema *types.Schema, id string) (map[string]interface{}, error) {
user, err := getUser(apiContext)
if err != nil {
return nil, err
}
return s.Store.ByID(apiContext, schema, addNamespace(user, id))
}
func (s *Store) List(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) ([]map[string]interface{}, error) {
user, err := getUser(apiContext)
if err != nil {
return nil, err
}
if opt == nil {
return nil, nil
}
opt.Conditions = append(opt.Conditions, types.EQ(NamespaceID, getNamespace(user)))
return s.Store.List(apiContext, schema, opt)
}
func (s *Store) Create(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}) (map[string]interface{}, error) {
user, err := getUser(apiContext)
if err != nil || data == nil {
return nil, err
}
ns := getNamespace(user)
_, err = s.nsClient.Get(ns, metav1.GetOptions{})
if err != nil {
s.nsClient.Create(&corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"management.cattle.io/system-namespace": "true",
},
Name: ns,
},
})
}
data[NamespaceID] = getNamespace(user)
return s.Store.Create(apiContext, schema, data)
}
func (s *Store) Update(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}, id string) (map[string]interface{}, error) {
user, err := getUser(apiContext)
if err != nil {
return nil, err
}
return s.Store.Update(apiContext, schema, data, addNamespace(user, id))
}
func (s *Store) Delete(apiContext *types.APIContext, schema *types.Schema, id string) (map[string]interface{}, error) {
user, err := getUser(apiContext)
if err != nil {
return nil, err
}
return s.Store.Delete(apiContext, schema, addNamespace(user, id))
}
func (s *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) (chan map[string]interface{}, error) {
user, err := getUser(apiContext)
if err != nil {
return nil, err
}
if opt == nil {
return nil, nil
}
opt.Conditions = append(opt.Conditions, types.EQ(NamespaceID, getNamespace(user)))
return s.Store.Watch(apiContext, schema, opt)
}
func getUser(apiContext *types.APIContext) (string, error) {
user := apiContext.Request.Header.Get("Impersonate-User")
if user == "" {
return "", httperror.NewAPIError(httperror.NotFound, "missing user")
}
return user, nil
}
func addNamespace(user, id string) string {
parts := strings.SplitN(id, ":", 2)
if len(parts) == 1 {
return fmt.Sprintf("%s:%s", getNamespace(user), parts[0])
}
return fmt.Sprintf("%s:%s", getNamespace(user), parts[1])
}
func getNamespace(user string) string {
return user
}