-
Notifications
You must be signed in to change notification settings - Fork 159
/
session_store.go
208 lines (168 loc) · 6.92 KB
/
session_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
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
/*
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 db
import (
"github.com/openziti/foundation/v2/errorz"
"github.com/openziti/foundation/v2/stringz"
"github.com/openziti/storage/ast"
"github.com/openziti/storage/boltz"
"github.com/pkg/errors"
"go.etcd.io/bbolt"
)
const (
FieldSessionToken = "token"
FieldSessionApiSession = "apiSession"
FieldSessionService = "service"
FieldSessionIdentity = "identity"
FieldSessionType = "type"
FieldSessionServicePolicies = "servicePolicies"
SessionTypeDial = "Dial"
SessionTypeBind = "Bind"
)
var validSessionTypes = []string{SessionTypeDial, SessionTypeBind}
type Session struct {
boltz.BaseExtEntity
Token string `json:"-"`
IdentityId string `json:"identityId"`
ApiSessionId string `json:"apiSessionId"`
ServiceId string `json:"serviceId"`
Type string `json:"type"`
ApiSession *ApiSession `json:"-"`
ServicePolicies []string `json:"servicePolicies"`
}
func (entity *Session) GetEntityType() string {
return EntityTypeSessions
}
var _ SessionStore = (*sessionStoreImpl)(nil)
type SessionStore interface {
Store[*Session]
LoadOneByToken(tx *bbolt.Tx, token string) (*Session, error)
GetTokenIndex() boltz.ReadIndex
}
func newSessionStore(stores *stores) *sessionStoreImpl {
store := &sessionStoreImpl{}
store.baseStore = newBaseStore[*Session](stores, store)
store.InitImpl(store)
return store
}
type sessionStoreImpl struct {
*baseStore[*Session]
indexToken boltz.ReadIndex
symbolApiSession boltz.EntitySymbol
symbolService boltz.EntitySymbol
symbolServicePolicies boltz.EntitySetSymbol
}
func (store *sessionStoreImpl) NewEntity() *Session {
return &Session{}
}
func (store *sessionStoreImpl) GetTokenIndex() boltz.ReadIndex {
return store.indexToken
}
func (store *sessionStoreImpl) initializeLocal() {
store.AddExtEntitySymbols()
symbolToken := store.AddSymbol(FieldSessionToken, ast.NodeTypeString)
store.indexToken = store.AddUniqueIndex(symbolToken)
store.symbolApiSession = store.AddFkSymbol(FieldSessionApiSession, store.stores.apiSession)
store.symbolService = store.AddFkSymbol(FieldSessionService, store.stores.edgeService)
store.symbolServicePolicies = store.AddFkSetSymbol(FieldSessionServicePolicies, store.stores.servicePolicy)
sessionTypeSymbol := store.AddSymbol(FieldSessionType, ast.NodeTypeString)
store.AddFkConstraint(store.symbolApiSession, false, boltz.CascadeCreateUpdate)
store.AddFkConstraint(store.symbolService, false, boltz.CascadeDelete)
store.AddConstraint(&sessionApiSessionIndex{
apiSessionSymbol: store.symbolApiSession,
sessionTypeSymbol: sessionTypeSymbol,
serviceSymbol: store.symbolService,
apiSessionStore: store.stores.apiSession,
})
}
func (store *sessionStoreImpl) initializeLinked() {}
func (*sessionStoreImpl) FillEntity(entity *Session, bucket *boltz.TypedBucket) {
entity.LoadBaseValues(bucket)
entity.Token = bucket.GetStringOrError(FieldSessionToken)
entity.ApiSessionId = bucket.GetStringOrError(FieldSessionApiSession)
entity.ServiceId = bucket.GetStringOrError(FieldSessionService)
entity.IdentityId = bucket.GetStringWithDefault(FieldSessionIdentity, "")
entity.Type = bucket.GetStringWithDefault(FieldSessionType, "Dial")
entity.ServicePolicies = bucket.GetStringList(FieldSessionServicePolicies)
}
func (*sessionStoreImpl) PersistEntity(entity *Session, ctx *boltz.PersistContext) {
if entity.Type == "" {
entity.Type = SessionTypeDial
}
if !stringz.Contains(validSessionTypes, entity.Type) {
ctx.Bucket.SetError(errorz.NewFieldError("invalid session type", FieldSessionType, entity.Type))
return
}
entity.SetBaseValues(ctx)
if ctx.IsCreate {
ctx.SetString(FieldSessionToken, entity.Token)
ctx.SetString(FieldSessionApiSession, entity.ApiSessionId)
ctx.SetString(FieldSessionService, entity.ServiceId)
sessionStore := ctx.Store.(*sessionStoreImpl)
_, identityId := sessionStore.stores.apiSession.symbolIdentity.Eval(ctx.Tx(), []byte(entity.ApiSessionId))
entity.IdentityId = string(identityId)
ctx.SetString(FieldSessionIdentity, entity.IdentityId)
ctx.SetString(FieldSessionType, entity.Type)
}
ctx.SetStringList(FieldSessionServicePolicies, entity.ServicePolicies)
if entity.ApiSession == nil {
entity.ApiSession, _ = ctx.Store.(*sessionStoreImpl).stores.apiSession.LoadOneById(ctx.Bucket.Tx(), entity.ApiSessionId)
}
}
func (store *sessionStoreImpl) LoadOneByToken(tx *bbolt.Tx, token string) (*Session, error) {
id := store.indexToken.Read(tx, []byte(token))
if id != nil {
return store.LoadOneById(tx, string(id))
}
return nil, boltz.NewNotFoundError(store.GetSingularEntityType(), "token", token)
}
type sessionApiSessionIndex struct {
apiSessionSymbol boltz.EntitySymbol
serviceSymbol boltz.EntitySymbol
sessionTypeSymbol boltz.EntitySymbol
apiSessionStore boltz.Store
}
func (self *sessionApiSessionIndex) ProcessBeforeUpdate(*boltz.IndexingContext) {}
func (self *sessionApiSessionIndex) ProcessAfterUpdate(ctx *boltz.IndexingContext) {
if ctx.IsCreate && !ctx.ErrHolder.HasError() {
_, sessionType := self.sessionTypeSymbol.Eval(ctx.Tx(), ctx.RowId)
_, apiSessionId := self.apiSessionSymbol.Eval(ctx.Tx(), ctx.RowId)
_, serviceId := self.serviceSymbol.Eval(ctx.Tx(), ctx.RowId)
if len(apiSessionId) == 0 {
ctx.ErrHolder.SetError(errors.Errorf("index on %v.%v does not allow null or empty values",
self.apiSessionSymbol.GetStore().GetEntityType(), self.apiSessionSymbol.GetName()))
return
}
bucket := boltz.GetOrCreatePath(ctx.Tx(),
RootBucket,
boltz.IndexesBucket,
EntityTypeApiSessions,
EntityTypeSessions,
string(apiSessionId),
string(sessionType),
)
if existingSessionId := bucket.GetString(string(serviceId)); existingSessionId != nil {
if self.sessionTypeSymbol.GetStore().IsEntityPresent(ctx.Tx(), *existingSessionId) {
ctx.ErrHolder.SetError(errors.Errorf("session for api-session %v, service %v and type: %v already exists",
string(apiSessionId), string(serviceId), string(sessionType)))
}
}
bucket.SetString(string(serviceId), string(ctx.RowId), nil)
ctx.ErrHolder.SetError(bucket.GetError())
}
}
func (self *sessionApiSessionIndex) ProcessBeforeDelete(*boltz.IndexingContext) {}
func (self *sessionApiSessionIndex) Initialize(*bbolt.Tx, errorz.ErrorHolder) {}
func (self *sessionApiSessionIndex) CheckIntegrity(boltz.MutateContext, bool, func(err error, fixed bool)) error {
return nil
}