This repository has been archived by the owner on Dec 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
service_handlers.go
248 lines (214 loc) · 7.98 KB
/
service_handlers.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
Copyright NetFoundry, Inc.
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
https://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 (
"fmt"
"github.com/michaelquigley/pfxlog"
"github.com/netfoundry/ziti-edge/controller/persistence"
"github.com/netfoundry/ziti-fabric/controller/models"
"github.com/netfoundry/ziti-foundation/storage/ast"
"github.com/netfoundry/ziti-foundation/storage/boltz"
"go.etcd.io/bbolt"
)
func NewEdgeServiceHandler(env Env) *EdgeServiceHandler {
handler := &EdgeServiceHandler{
baseHandler: newBaseHandler(env, env.GetStores().EdgeService),
}
handler.impl = handler
return handler
}
type EdgeServiceHandler struct {
baseHandler
}
func (handler *EdgeServiceHandler) newModelEntity() boltEntitySink {
return &ServiceDetail{}
}
func (handler *EdgeServiceHandler) Create(service *Service) (string, error) {
return handler.createEntity(service)
}
func (handler *EdgeServiceHandler) Read(id string) (*Service, error) {
entity := &Service{}
if err := handler.readEntity(id, entity); err != nil {
return nil, err
}
return entity, nil
}
func (handler *EdgeServiceHandler) readInTx(tx *bbolt.Tx, id string) (*ServiceDetail, error) {
entity := &ServiceDetail{}
if err := handler.readEntityInTx(tx, id, entity); err != nil {
return nil, err
}
return entity, nil
}
func (handler *EdgeServiceHandler) ReadForIdentity(id string, identityId string, configTypes map[string]struct{}) (*ServiceDetail, error) {
var service *ServiceDetail
err := handler.GetDb().View(func(tx *bbolt.Tx) error {
identity, err := handler.GetEnv().GetHandlers().Identity.readInTx(tx, identityId)
if err != nil {
return err
}
if identity.IsAdmin {
service, err = handler.readInTx(tx, id)
if err == nil && service != nil {
service.Permissions = []string{persistence.PolicyTypeBindName, persistence.PolicyTypeDialName}
}
} else {
service, err = handler.ReadForIdentityInTx(tx, id, identityId)
}
if err == nil && len(configTypes) > 0 {
identityServiceConfigs := handler.env.GetStores().Identity.LoadServiceConfigsByServiceAndType(tx, identityId, configTypes)
handler.mergeConfigs(tx, configTypes, service, identityServiceConfigs)
}
return err
})
return service, err
}
func (handler *EdgeServiceHandler) ReadForIdentityInTx(tx *bbolt.Tx, id string, identityId string) (*ServiceDetail, error) {
query := `id = "%v" and not isEmpty(from servicePolicies where (type = %v and anyOf(identities.id) = "%v"))`
dialQuery := fmt.Sprintf(query, id, persistence.PolicyTypeDial, identityId)
_, dialCount, err := handler.GetStore().QueryIds(tx, dialQuery)
if err != nil {
return nil, err
}
bindQuery := fmt.Sprintf(query, id, persistence.PolicyTypeBind, identityId)
_, bindCount, err := handler.GetStore().QueryIds(tx, bindQuery)
if err != nil {
return nil, err
}
result, err := handler.readInTx(tx, id)
if err != nil {
return nil, err
}
if result == nil {
return nil, boltz.NewNotFoundError(handler.GetStore().GetSingularEntityType(), "id", id)
}
if bindCount > 0 {
result.Permissions = append(result.Permissions, persistence.PolicyTypeBindName)
} else if dialCount == 0 {
return nil, boltz.NewNotFoundError(handler.GetStore().GetSingularEntityType(), "id", id)
}
if dialCount > 0 {
result.Permissions = append(result.Permissions, persistence.PolicyTypeDialName)
}
return result, nil
}
func (handler *EdgeServiceHandler) Delete(id string) error {
return handler.deleteEntity(id)
}
func (handler *EdgeServiceHandler) Update(service *Service) error {
return handler.updateEntity(service, nil)
}
func (handler *EdgeServiceHandler) Patch(service *Service, checker boltz.FieldChecker) error {
return handler.patchEntity(service, checker)
}
func (handler *EdgeServiceHandler) PublicQueryForIdentity(sessionIdentity *Identity, configTypes map[string]struct{}, query ast.Query) (*ServiceListResult, error) {
if sessionIdentity.IsAdmin {
return handler.queryServices(query, sessionIdentity.Id, configTypes, true)
}
return handler.QueryForIdentity(sessionIdentity.Id, configTypes, query)
}
func (handler *EdgeServiceHandler) QueryForIdentity(identityId string, configTypes map[string]struct{}, query ast.Query) (*ServiceListResult, error) {
idFilterQueryString := fmt.Sprintf(`not isEmpty(from servicePolicies where anyOf(identities) = "%v")`, identityId)
idFilterQuery, err := ast.Parse(handler.Store, idFilterQueryString)
if err != nil {
return nil, err
}
query.SetPredicate(ast.NewAndExprNode(query.GetPredicate(), idFilterQuery.GetPredicate()))
return handler.queryServices(query, identityId, configTypes, false)
}
func (handler *EdgeServiceHandler) queryServices(query ast.Query, identityId string, configTypes map[string]struct{}, isAdmin bool) (*ServiceListResult, error) {
result := &ServiceListResult{
handler: handler,
identityId: identityId,
configTypes: configTypes,
isAdmin: isAdmin,
}
err := handler.preparedList(query, result.collect)
if err != nil {
return nil, err
}
return result, nil
}
func (handler *EdgeServiceHandler) QueryRoleAttributes(queryString string) ([]string, *models.QueryMetaData, error) {
index := handler.env.GetStores().EdgeService.GetRoleAttributesIndex()
return handler.queryRoleAttributes(index, queryString)
}
type ServiceListResult struct {
handler *EdgeServiceHandler
Services []*ServiceDetail
identityId string
configTypes map[string]struct{}
isAdmin bool
models.QueryMetaData
}
func (result *ServiceListResult) collect(tx *bbolt.Tx, ids []string, queryMetaData *models.QueryMetaData) error {
result.QueryMetaData = *queryMetaData
var service *ServiceDetail
var err error
identityServiceConfigs := result.handler.env.GetStores().Identity.LoadServiceConfigsByServiceAndType(tx, result.identityId, result.configTypes)
for _, key := range ids {
if !result.isAdmin && result.identityId != "" {
service, err = result.handler.ReadForIdentityInTx(tx, key, result.identityId)
} else {
service, err = result.handler.readInTx(tx, key)
if service != nil && result.isAdmin {
service.Permissions = []string{persistence.PolicyTypeBindName, persistence.PolicyTypeDialName}
}
}
if err != nil {
return err
}
result.handler.mergeConfigs(tx, result.configTypes, service, identityServiceConfigs)
result.Services = append(result.Services, service)
}
return nil
}
func (handler *EdgeServiceHandler) mergeConfigs(tx *bbolt.Tx, configTypes map[string]struct{}, service *ServiceDetail,
identityServiceConfigs map[string]map[string]map[string]interface{}) {
service.Config = map[string]map[string]interface{}{}
_, wantsAll := configTypes["all"]
configTypeStore := handler.env.GetStores().ConfigType
if len(configTypes) > 0 && len(service.Configs) > 0 {
configStore := handler.env.GetStores().Config
for _, configId := range service.Configs {
config, _ := configStore.LoadOneById(tx, configId)
if config != nil {
_, wantsConfig := configTypes[config.Type]
if wantsAll || wantsConfig {
service.Config[config.Type] = config.Data
}
}
}
}
// inject overrides
if serviceMap, ok := identityServiceConfigs[service.Id]; ok {
for configTypeId, config := range serviceMap {
wantsConfig := wantsAll
if !wantsConfig {
_, wantsConfig = configTypes[configTypeId]
}
if wantsConfig {
service.Config[configTypeId] = config
}
}
}
for configTypeId, config := range service.Config {
configTypeName := configTypeStore.GetName(tx, configTypeId)
if configTypeName != nil {
delete(service.Config, configTypeId)
service.Config[*configTypeName] = config
} else {
pfxlog.Logger().Errorf("name for config type %v not found!", configTypeId)
}
}
}