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_policy_enforcer.go
160 lines (127 loc) · 4.2 KB
/
service_policy_enforcer.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
/*
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 policy
import (
"fmt"
"github.com/michaelquigley/pfxlog"
"time"
"github.com/openziti/edge/controller/env"
"github.com/openziti/edge/controller/persistence"
"github.com/openziti/edge/runner"
"go.etcd.io/bbolt"
)
type ServicePolicyEnforcer struct {
appEnv *env.AppEnv
*runner.BaseOperation
notify chan struct{}
}
func NewServicePolicyEnforcer(appEnv *env.AppEnv, f time.Duration) *ServicePolicyEnforcer {
result := &ServicePolicyEnforcer{
appEnv: appEnv,
BaseOperation: runner.NewBaseOperation("ServicePolicyEnforcer", f),
notify: make(chan struct{}, 1),
}
result.notify <- struct{}{} // ensure we do a full scan on startup
persistence.ServiceEvents.AddServiceEventHandler(result.handleServiceEvent)
return result
}
func (enforcer *ServicePolicyEnforcer) handleServiceEvent(event *persistence.ServiceEvent) {
policyType := ""
if event.Type == persistence.ServiceDialAccessLost {
policyType = persistence.PolicyTypeDialName
}
if event.Type == persistence.ServiceBindAccessLost {
policyType = persistence.PolicyTypeBindName
}
if policyType == "" {
return
}
log := pfxlog.Logger().WithField("event", event.String())
log.Debug("event received")
var sessionsToDelete []string
err := enforcer.appEnv.GetDbProvider().GetDb().View(func(tx *bbolt.Tx) error {
var err error
identity := &persistence.Identity{}
if _, err := enforcer.appEnv.GetStores().Identity.BaseLoadOneById(tx, event.IdentityId, identity); err != nil {
return err
}
if identity.IsAdmin {
return nil
}
query := fmt.Sprintf(`apiSession.identity="%v" and service="%v" and type="%v"`, event.IdentityId, event.ServiceId, policyType)
sessionsToDelete, _, err = enforcer.appEnv.GetStores().Session.QueryIds(tx, query)
return err
})
if err != nil {
pfxlog.Logger().WithError(err).Errorf("error while processing event: %v", event)
// notify enforcer that it should run on the next cycle
select {
case enforcer.notify <- struct{}{}:
default:
}
}
for _, sessionId := range sessionsToDelete {
_ = enforcer.appEnv.GetHandlers().Session.Delete(sessionId)
log.Debugf("session %v deleted", sessionId)
}
}
func (enforcer *ServicePolicyEnforcer) Run() error {
// if we haven't been notified to run b/c of startup or handler error, skip run
select {
case <-enforcer.notify:
default:
return nil
}
result, err := enforcer.appEnv.GetHandlers().Session.Query("")
if err != nil {
return err
}
var sessionsToRemove []string
err = enforcer.appEnv.GetDbProvider().GetDb().View(func(tx *bbolt.Tx) error {
for _, session := range result.Sessions {
apiSession := &persistence.ApiSession{}
_, err := enforcer.appEnv.GetStores().ApiSession.BaseLoadOneById(tx, session.ApiSessionId, apiSession)
if err != nil {
return err
}
identity := &persistence.Identity{}
_, err = enforcer.appEnv.GetStores().Identity.BaseLoadOneById(tx, apiSession.IdentityId, identity)
if err != nil {
return err
}
if identity.IsAdmin {
continue
}
policyType := persistence.PolicyTypeDial
if session.Type == persistence.SessionTypeBind {
policyType = persistence.PolicyTypeBind
}
query := fmt.Sprintf(`id = "%v" and not isEmpty(from servicePolicies where type = %v and anyOf(services) = "%v")`, identity.Id, int32(policyType), session.ServiceId)
_, count, err := enforcer.appEnv.GetStores().Identity.QueryIds(tx, query)
if err != nil {
return err
}
if count == 0 {
sessionsToRemove = append(sessionsToRemove, session.Id)
}
}
return nil
})
if err != nil {
return err
}
for _, sessionId := range sessionsToRemove {
_ = enforcer.appEnv.GetHandlers().Session.Delete(sessionId)
}
return nil
}