This repository has been archived by the owner on Dec 14, 2023. It is now read-only.
/
identity_model.go
308 lines (269 loc) · 10.1 KB
/
identity_model.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
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 (
"github.com/openziti/edge/controller/persistence"
"github.com/openziti/fabric/controller/models"
"github.com/openziti/foundation/v2/errorz"
"github.com/openziti/sdk-golang/ziti"
"github.com/openziti/storage/boltz"
"go.etcd.io/bbolt"
"time"
)
type EnvInfo struct {
Arch string
Os string
OsRelease string
OsVersion string
}
func (self *EnvInfo) Equals(other *EnvInfo) bool {
if self == nil {
return other == nil
}
if other == nil {
return false
}
return self.Arch == other.Arch &&
self.Os == other.Os &&
self.OsRelease == other.OsRelease &&
self.OsVersion == other.OsVersion
}
type SdkInfo struct {
AppId string
AppVersion string
Branch string
Revision string
Type string
Version string
}
func (self *SdkInfo) Equals(other *SdkInfo) bool {
if self == nil {
return other == nil
}
if other == nil {
return false
}
return self.AppId == other.AppId &&
self.AppVersion == other.AppVersion &&
self.Branch == other.Branch &&
self.Revision == other.Revision &&
self.Type == other.Type &&
self.Version == other.Version
}
type Identity struct {
models.BaseEntity
Name string
IdentityTypeId string
IsDefaultAdmin bool
IsAdmin bool
RoleAttributes []string
EnvInfo *EnvInfo
SdkInfo *SdkInfo
HasErConnection bool
DefaultHostingPrecedence ziti.Precedence
DefaultHostingCost uint16
ServiceHostingPrecedences map[string]ziti.Precedence
ServiceHostingCosts map[string]uint16
AppData map[string]interface{}
AuthPolicyId string
ExternalId *string
Disabled bool
DisabledAt *time.Time
DisabledUntil *time.Time
}
func (entity *Identity) toBoltEntityForCreate(_ *bbolt.Tx, env Env) (*persistence.Identity, error) {
identityType, err := env.GetManagers().IdentityType.ReadByIdOrName(entity.IdentityTypeId)
if err != nil && !boltz.IsErrNotFoundErr(err) {
return nil, err
}
if identityType == nil {
apiErr := errorz.NewNotFound()
apiErr.Cause = errorz.NewFieldError("typeId not found", "typeId", entity.IdentityTypeId)
apiErr.AppendCause = true
return nil, apiErr
}
if identityType.Name == persistence.RouterIdentityType {
fieldErr := errorz.NewFieldError("may not create identities with given typeId", "typeId", entity.IdentityTypeId)
return nil, errorz.NewFieldApiError(fieldErr)
}
entity.IdentityTypeId = identityType.Id
boltEntity := &persistence.Identity{
BaseExtEntity: *boltz.NewExtEntity(entity.Id, entity.Tags),
Name: entity.Name,
IdentityTypeId: entity.IdentityTypeId,
AuthPolicyId: entity.AuthPolicyId,
IsDefaultAdmin: entity.IsDefaultAdmin,
IsAdmin: entity.IsAdmin,
RoleAttributes: entity.RoleAttributes,
DefaultHostingPrecedence: entity.DefaultHostingPrecedence,
DefaultHostingCost: entity.DefaultHostingCost,
ServiceHostingPrecedences: entity.ServiceHostingPrecedences,
ServiceHostingCosts: entity.ServiceHostingCosts,
AppData: entity.AppData,
ExternalId: entity.ExternalId,
DisabledAt: entity.DisabledAt,
DisabledUntil: entity.DisabledUntil,
}
if entity.EnvInfo != nil {
boltEntity.EnvInfo = &persistence.EnvInfo{
Arch: entity.EnvInfo.Arch,
Os: entity.EnvInfo.Os,
OsRelease: entity.EnvInfo.OsRelease,
OsVersion: entity.EnvInfo.OsVersion,
}
}
if entity.SdkInfo != nil {
boltEntity.SdkInfo = &persistence.SdkInfo{
Branch: entity.SdkInfo.Branch,
Revision: entity.SdkInfo.Revision,
Type: entity.SdkInfo.Type,
Version: entity.SdkInfo.Version,
AppId: entity.SdkInfo.AppId,
AppVersion: entity.SdkInfo.AppVersion,
}
}
fillPersistenceInfo(boltEntity, entity.EnvInfo, entity.SdkInfo)
return boltEntity, nil
}
func fillModelInfo(identity *Identity, envInfo *persistence.EnvInfo, sdkInfo *persistence.SdkInfo) {
if envInfo != nil {
identity.EnvInfo = &EnvInfo{
Arch: envInfo.Arch,
Os: envInfo.Os,
OsRelease: envInfo.OsRelease,
OsVersion: envInfo.OsVersion,
}
}
if sdkInfo != nil {
identity.SdkInfo = &SdkInfo{
AppId: sdkInfo.AppId,
AppVersion: sdkInfo.AppVersion,
Branch: sdkInfo.Branch,
Revision: sdkInfo.Revision,
Type: sdkInfo.Type,
Version: sdkInfo.Version,
}
}
}
func fillPersistenceInfo(identity *persistence.Identity, envInfo *EnvInfo, sdkInfo *SdkInfo) {
if envInfo != nil {
identity.EnvInfo = &persistence.EnvInfo{
Arch: envInfo.Arch,
Os: envInfo.Os,
OsRelease: envInfo.OsRelease,
OsVersion: envInfo.OsVersion,
}
}
if sdkInfo != nil {
identity.SdkInfo = &persistence.SdkInfo{
Branch: sdkInfo.Branch,
Revision: sdkInfo.Revision,
Type: sdkInfo.Type,
Version: sdkInfo.Version,
AppId: sdkInfo.AppId,
AppVersion: sdkInfo.AppVersion,
}
}
}
func (entity *Identity) toBoltEntityForUpdate(tx *bbolt.Tx, env Env, checker boltz.FieldChecker) (*persistence.Identity, error) {
if checker == nil || checker.IsUpdated("type") {
identityType, err := env.GetManagers().IdentityType.ReadByIdOrName(entity.IdentityTypeId)
if err != nil && !boltz.IsErrNotFoundErr(err) {
return nil, err
}
if identityType == nil {
apiErr := errorz.NewNotFound()
apiErr.Cause = errorz.NewFieldError("identityTypeId not found", "identityTypeId", entity.IdentityTypeId)
apiErr.AppendCause = true
return nil, apiErr
}
entity.IdentityTypeId = identityType.Id
}
boltEntity := &persistence.Identity{
Name: entity.Name,
IdentityTypeId: entity.IdentityTypeId,
AuthPolicyId: entity.AuthPolicyId,
BaseExtEntity: *boltz.NewExtEntity(entity.Id, entity.Tags),
RoleAttributes: entity.RoleAttributes,
DefaultHostingPrecedence: entity.DefaultHostingPrecedence,
DefaultHostingCost: entity.DefaultHostingCost,
ServiceHostingPrecedences: entity.ServiceHostingPrecedences,
ServiceHostingCosts: entity.ServiceHostingCosts,
AppData: entity.AppData,
ExternalId: entity.ExternalId,
DisabledAt: entity.DisabledAt,
DisabledUntil: entity.DisabledUntil,
IsAdmin: entity.IsAdmin,
}
identityStore := env.GetManagers().Identity.GetStore()
_, currentType := identityStore.GetSymbol(persistence.FieldIdentityType).Eval(tx, []byte(entity.Id))
if string(currentType) == persistence.RouterIdentityType {
if (checker == nil || checker.IsUpdated("identityTypeId")) && entity.IdentityTypeId != persistence.RouterIdentityType {
fieldErr := errorz.NewFieldError("may not change type of router identities", "typeId", entity.IdentityTypeId)
return nil, errorz.NewFieldApiError(fieldErr)
}
_, currentName := identityStore.GetSymbol(persistence.FieldName).Eval(tx, []byte(entity.Id))
if (checker == nil || checker.IsUpdated(persistence.FieldName)) && string(currentName) != entity.Name {
fieldErr := errorz.NewFieldError("may not change name of router identities", "name", entity.Name)
return nil, errorz.NewFieldApiError(fieldErr)
}
} else if (checker == nil || checker.IsUpdated("identityTypeId")) && entity.IdentityTypeId == persistence.RouterIdentityType {
fieldErr := errorz.NewFieldError("may not change type to router", "typeId", entity.IdentityTypeId)
return nil, errorz.NewFieldApiError(fieldErr)
}
fillPersistenceInfo(boltEntity, entity.EnvInfo, entity.SdkInfo)
return boltEntity, nil
}
func (entity *Identity) fillFrom(env Env, _ *bbolt.Tx, boltIdentity *persistence.Identity) error {
entity.FillCommon(boltIdentity)
entity.Name = boltIdentity.Name
entity.IdentityTypeId = boltIdentity.IdentityTypeId
entity.AuthPolicyId = boltIdentity.AuthPolicyId
entity.IsDefaultAdmin = boltIdentity.IsDefaultAdmin
entity.IsAdmin = boltIdentity.IsAdmin
entity.RoleAttributes = boltIdentity.RoleAttributes
entity.HasErConnection = env.GetManagers().Identity.HasErConnection(entity.Id)
entity.DefaultHostingPrecedence = boltIdentity.DefaultHostingPrecedence
entity.DefaultHostingCost = boltIdentity.DefaultHostingCost
entity.ServiceHostingPrecedences = boltIdentity.ServiceHostingPrecedences
entity.ServiceHostingCosts = boltIdentity.ServiceHostingCosts
entity.AppData = boltIdentity.AppData
entity.ExternalId = boltIdentity.ExternalId
entity.DisabledUntil = boltIdentity.DisabledUntil
entity.DisabledAt = boltIdentity.DisabledAt
entity.Disabled = boltIdentity.Disabled
fillModelInfo(entity, boltIdentity.EnvInfo, boltIdentity.SdkInfo)
return nil
}
type ServiceConfig struct {
Service string
Config string
}
func toBoltServiceConfigs(tx *bbolt.Tx, env Env, serviceConfigs []ServiceConfig) ([]persistence.ServiceConfig, error) {
serviceStore := env.GetStores().EdgeService
configStore := env.GetStores().Config
var boltServiceConfigs []persistence.ServiceConfig
for _, serviceConfig := range serviceConfigs {
if !serviceStore.IsEntityPresent(tx, serviceConfig.Service) {
return nil, boltz.NewNotFoundError(serviceStore.GetSingularEntityType(), "id or name", serviceConfig.Service)
}
if !configStore.IsEntityPresent(tx, serviceConfig.Config) {
return nil, boltz.NewNotFoundError(configStore.GetSingularEntityType(), "id or name", serviceConfig.Config)
}
boltServiceConfigs = append(boltServiceConfigs, persistence.ServiceConfig{
ServiceId: serviceConfig.Service,
ConfigId: serviceConfig.Config,
})
}
return boltServiceConfigs, nil
}