-
Notifications
You must be signed in to change notification settings - Fork 153
/
posture_response_manager.go
355 lines (288 loc) · 11.1 KB
/
posture_response_manager.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
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/openziti/ziti/controller/persistence"
"github.com/openziti/ziti/controller/change"
"github.com/openziti/ziti/controller/db"
"github.com/openziti/storage/ast"
"go.etcd.io/bbolt"
"runtime/debug"
"time"
)
func NewPostureResponseManager(env Env) *PostureResponseManager {
manager := &PostureResponseManager{
env: env,
postureCache: newPostureCache(env),
}
manager.AddPostureDataListener(manager.postureDataUpdated)
return manager
}
type PostureResponseManager struct {
env Env
postureCache *PostureCache
}
func (self *PostureResponseManager) Create(identityId string, postureResponses []*PostureResponse) {
self.postureCache.Add(identityId, postureResponses)
}
// SetMfaPosture sets the MFA passing status a specific API Session owned by an identity
func (self *PostureResponseManager) SetMfaPosture(identityId string, apiSessionId string, isPassed bool) {
postureResponse := &PostureResponse{
PostureCheckId: MfaProviderZiti,
TypeId: PostureCheckTypeMFA,
TimedOut: false,
LastUpdatedAt: time.Now().UTC(),
}
var passedAt *time.Time = nil
if isPassed {
passedAt = &postureResponse.LastUpdatedAt
}
postureSubType := &PostureResponseMfa{
ApiSessionId: apiSessionId,
PassedMfaAt: passedAt,
}
postureResponse.SubType = postureSubType
postureSubType.PostureResponse = postureResponse
self.Create(identityId, []*PostureResponse{postureResponse})
}
// SetMfaPostureForIdentity sets the MFA passing status for all API Sessions associated to an identity
func (self *PostureResponseManager) SetMfaPostureForIdentity(identityId string, isPassed bool) {
postureResponse := &PostureResponse{
PostureCheckId: MfaProviderZiti,
TypeId: PostureCheckTypeMFA,
TimedOut: false,
LastUpdatedAt: time.Now().UTC(),
}
var passedAt *time.Time = nil
if isPassed {
passedAt = &postureResponse.LastUpdatedAt
}
self.postureCache.Upsert(identityId, true, func(exist bool, valueInMap *PostureData, newValue *PostureData) *PostureData {
var pd *PostureData
if exist {
pd = valueInMap
} else {
pd = newValue
}
if pd == nil {
pfxlog.Logger().Errorf("attempting to set MFA status for identity [%s] but existing/new values was not posture data: existing [%v] new [%v]", identityId, valueInMap, newValue)
pd = newPostureData()
}
for apiSessionId := range pd.ApiSessions {
postureSubType := &PostureResponseMfa{
ApiSessionId: apiSessionId,
PassedMfaAt: passedAt,
}
postureResponse.SubType = postureSubType
postureSubType.PostureResponse = postureResponse
postureResponse.Apply(pd)
}
return pd
})
}
func (self *PostureResponseManager) AddPostureDataListener(cb func(env Env, identityId string)) {
self.postureCache.AddListener(EventIdentityPostureDataAltered, func(i ...interface{}) {
defer func() {
if r := recover(); r != nil {
fmt.Printf("panic during posture data listener execution: %v\n", r)
fmt.Println(string(debug.Stack()))
}
}()
if identityId, ok := i[0].(string); ok {
cb(self.env, identityId)
}
})
}
// Kills active sessions that do not have passing posture checks. Run when posture data is updated
// via posture response or posture data timeout.
func (self *PostureResponseManager) postureDataUpdated(env Env, identityId string) {
var sessionIdsToDelete []string
//todo: Be smarter about this? Store a cache of service-> result on postureDataCach detect changes?
// Only an issue when timeouts are being used - which aren't right now.
env.HandleServiceUpdatedEventForIdentityId(identityId)
err := self.env.GetDbProvider().GetDb().View(func(tx *bbolt.Tx) error {
apiSessionIds, _, err := self.env.GetStores().ApiSession.QueryIds(tx, fmt.Sprintf(`identity = "%v"`, identityId))
if err != nil {
return fmt.Errorf("could not query for api sessions for identity: %v", err)
}
for _, apiSessionId := range apiSessionIds {
result, err := self.env.GetManagers().Session.Query(fmt.Sprintf(`apiSession = "%v"`, apiSessionId))
if err != nil {
return fmt.Errorf("could not query for sessions: %v", err)
}
validServices := map[string]bool{} //cache services access, scoped by apiSessionId
validPolicies := map[string]bool{} //cache policy posture check results, scoped by apiSessionId
for _, session := range result.Sessions {
isValidService, isEvaluatedService := validServices[session.ServiceId]
//if we have evaluated positive access before, don't do it again
if !isEvaluatedService {
validServices[session.ServiceId] = false
policyPostureChecks := self.env.GetManagers().EdgeService.GetPolicyPostureChecks(identityId, session.ServiceId)
if len(policyPostureChecks) == 0 {
isValidService = true
} else {
for policyId, checks := range policyPostureChecks {
isValidPolicy, isEvaluatedPolicy := validPolicies[policyId]
if !isEvaluatedPolicy { //not checked yet
validPolicies[policyId] = false
isValidPolicy = false
if ok, _ := self.postureCache.Evaluate(identityId, apiSessionId, checks.PostureChecks); ok {
isValidService = true
isValidPolicy = true
}
}
validPolicies[policyId] = isValidPolicy
if isValidPolicy {
break //found 1 valid policy, stop
}
}
}
if isValidService {
validServices[session.ServiceId] = true
break //found the service valid stop checking
}
}
if !isValidService {
sessionIdsToDelete = append(sessionIdsToDelete, session.Id)
}
}
}
return nil
})
if err != nil {
pfxlog.Logger().Errorf("could not process posture data update: %v", err)
return
}
for _, sessionId := range sessionIdsToDelete {
//todo: delete batch?
_ = self.env.GetManagers().Session.Delete(sessionId, change.New().SetSourceType("posture.cache").SetChangeAuthorType(change.AuthorTypeController))
}
}
func (self *PostureResponseManager) Evaluate(identityId, apiSessionId string, check *PostureCheck) (bool, *PostureCheckFailure) {
isValid, failures := self.postureCache.Evaluate(identityId, apiSessionId, []*PostureCheck{check})
if isValid {
return true, nil
} else {
return false, failures[0]
}
}
func (self *PostureResponseManager) PostureData(id string) *PostureData {
return self.postureCache.PostureData(id)
}
func (self *PostureResponseManager) WithPostureData(id string, f func(data *PostureData)) {
self.postureCache.WithPostureData(id, f)
}
func (self *PostureResponseManager) SetSdkInfo(identityId, apiSessionId string, sdkInfo *SdkInfo) {
if identityId == "" || apiSessionId == "" || sdkInfo == nil {
return
}
self.postureCache.Upsert(identityId, false, func(exist bool, valueInMap *PostureData, newValue *PostureData) *PostureData {
var postureData *PostureData
if exist {
postureData = valueInMap
} else {
postureData = newValue
}
if _, ok := postureData.ApiSessions[apiSessionId]; !ok {
postureData.ApiSessions[apiSessionId] = &ApiSessionPostureData{}
}
apiSessionData := postureData.ApiSessions[apiSessionId]
apiSessionData.SdkInfo = sdkInfo
return postureData
})
}
type ServiceWithTimeout struct {
Service *Service
Timeout int64
}
func shouldPostureCheckTimeoutBeAltered(mfaCheck *persistence.PostureCheckMfa, timeSinceLastMfa, gracePeriod time.Duration, onWake, onUnlock bool) bool {
if mfaCheck == nil {
return false
}
if (mfaCheck.PromptOnUnlock && onUnlock) || (mfaCheck.PromptOnWake && onWake) {
//no time out the remaining time was bigger than the grace period
timeSinceLastMfaSeconds := int64(timeSinceLastMfa.Seconds())
gracePeriodSeconds := int64(gracePeriod.Seconds())
timeoutShouldBeAltered := mfaCheck.TimeoutSeconds == -1 || (mfaCheck.TimeoutSeconds-timeSinceLastMfaSeconds > gracePeriodSeconds)
return timeoutShouldBeAltered
}
return false
}
func (self *PostureResponseManager) GetEndpointStateChangeAffectedServices(timeSinceLastMfa, gracePeriod time.Duration, onWake bool, onUnlock bool) []*ServiceWithTimeout {
affectedChecks := map[string]int64{} //check id -> timeout
if onWake || onUnlock {
queryStr := fmt.Sprintf("%s=true or %s=true", persistence.FieldPostureCheckMfaPromptOnUnlock, persistence.FieldPostureCheckMfaPromptOnWake)
query, err := ast.Parse(self.env.GetStores().PostureCheck, queryStr)
if err != nil {
pfxlog.Logger().Errorf("error querying for onWake/onUnlock posture checks: %v", err)
} else {
err = self.env.GetDbProvider().GetDb().View(func(tx *bbolt.Tx) error {
cursor := self.env.GetStores().PostureCheck.IterateIds(tx, query)
for cursor.IsValid() {
if check, err := self.env.GetStores().PostureCheck.LoadOneById(tx, string(cursor.Current())); err == nil {
if mfaCheck, ok := check.SubType.(*persistence.PostureCheckMfa); ok {
if shouldPostureCheckTimeoutBeAltered(mfaCheck, timeSinceLastMfa, gracePeriod, onWake, onUnlock) {
affectedChecks[check.Id] = mfaCheck.TimeoutSeconds
}
}
} else {
pfxlog.Logger().Errorf("error querying for onWake/onUnlock posture checks by id: %v", err)
}
cursor.Next()
}
return nil
})
if err != nil {
pfxlog.Logger().WithError(err).Error("error querying for onWake/onUnlock posture by id")
}
}
}
services := map[string]*ServiceWithTimeout{}
if len(affectedChecks) > 0 {
_ = self.env.GetDbProvider().GetDb().View(func(tx *bbolt.Tx) error {
for checkId, timeout := range affectedChecks {
policyCursor := self.env.GetStores().PostureCheck.GetRelatedEntitiesCursor(tx, checkId, persistence.EntityTypeServicePolicies, true)
for policyCursor.IsValid() {
serviceCursor := self.env.GetStores().ServicePolicy.GetRelatedEntitiesCursor(tx, string(policyCursor.Current()), db.EntityTypeServices, true)
for serviceCursor.IsValid() {
if _, ok := services[string(serviceCursor.Current())]; !ok {
service, err := self.env.GetStores().EdgeService.LoadOneById(tx, string(serviceCursor.Current()))
if err == nil {
modelService := &Service{}
if err := modelService.fillFrom(self.env, tx, service); err == nil {
//use the lowest configured timeout (which is some timeout or no timeout)
if existingService, ok := services[service.Id]; !ok || timeout < existingService.Timeout {
services[service.Id] = &ServiceWithTimeout{
Service: modelService,
Timeout: timeout,
}
}
}
}
}
serviceCursor.Next()
}
policyCursor.Next()
}
}
return nil
})
}
var serviceSlice []*ServiceWithTimeout
for _, service := range services {
serviceSlice = append(serviceSlice, service)
}
return serviceSlice
}