-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
144 lines (128 loc) · 3.88 KB
/
client.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
package policyclient
import (
"context"
"go.uber.org/fx"
"log"
"sync"
"time"
"github.com/ScoreTrak/ScoreTrak/pkg/policy"
"github.com/ScoreTrak/ScoreTrak/pkg/policy/policyrepo"
"github.com/ScoreTrak/ScoreTrak/pkg/queue"
"github.com/ScoreTrak/ScoreTrak/pkg/queue/queueing"
"github.com/gofrs/uuid"
"github.com/jinzhu/copier"
)
// Client Policy allows for eventually consistent way to distribute pkg/Policy struct throughout ScoreTrak instances.
// This is needed because certain API routes evaluate Policy on every call, and retrieving Policy from database is very expensive, hence having eventually consistent copy is much more efficient.
type Client struct {
policy *policy.Policy
policyMutex *sync.RWMutex
repo policyrepo.Repo
pubsub queue.MasterStreamPubSub
cnf queueing.MasterConfig
signal map[uuid.UUID]chan struct{}
signalMutex *sync.RWMutex
}
func NewPolicyClient(policy *policy.Policy, cnf queueing.MasterConfig, repo policyrepo.Repo, pubsub queue.MasterStreamPubSub) *Client {
return &Client{policy: policy, policyMutex: &sync.RWMutex{}, repo: repo, cnf: cnf, signalMutex: &sync.RWMutex{}, pubsub: pubsub, signal: make(map[uuid.UUID]chan struct{})}
}
func (a *Client) GetAllowUnauthenticatedUsers() bool {
a.policyMutex.RLock()
defer a.policyMutex.RUnlock()
return *a.policy.AllowUnauthenticatedUsers
}
func (a *Client) GetAllowChangingUsernamesAndPasswords() bool {
a.policyMutex.RLock()
defer a.policyMutex.RUnlock()
return *a.policy.AllowChangingUsernamesAndPasswords
}
func (a *Client) GetAllowRedTeamLaunchingServiceTestsManually() bool {
a.policyMutex.RLock()
defer a.policyMutex.RUnlock()
return *a.policy.AllowRedTeamLaunchingServiceTestsManually
}
func (a *Client) GetShowPoints() bool {
a.policyMutex.RLock()
defer a.policyMutex.RUnlock()
return *a.policy.ShowPoints
}
func (a *Client) GetShowAddresses() bool {
a.policyMutex.RLock()
defer a.policyMutex.RUnlock()
return *a.policy.ShowAddresses
}
func (a *Client) Subscribe() (uuid.UUID, <-chan struct{}) {
a.signalMutex.Lock()
defer a.signalMutex.Unlock()
channel := make(chan struct{}, 1)
uid, err := uuid.NewV4()
if err != nil {
log.Panicln("Unable to generate random UUID")
}
a.signal[uid] = channel
return uid, channel
}
func (a *Client) Unsubscribe(uid uuid.UUID) {
a.signalMutex.Lock()
defer a.signalMutex.Unlock()
delete(a.signal, uid)
}
func (a *Client) Publish() {
a.signalMutex.RLock()
defer a.signalMutex.RUnlock()
for _, ch := range a.signal {
ch <- struct{}{}
}
}
func (a *Client) Notify() {
a.pubsub.NotifyTopic(a.cnf.ChannelPrefix + "_policy")
}
func (a *Client) RefreshLocalPolicy() {
tempPolicy, err := a.repo.Get(context.Background())
if err != nil {
log.Panicf("Unable to retrieve policy. Make sure database is reachable")
}
a.policyMutex.Lock()
defer a.policyMutex.Unlock()
err = copier.Copy(a.policy, tempPolicy)
if err != nil {
log.Panicf("Unable to copy policy into destination policy. This is likely a bug")
}
}
func (a *Client) PolicyClient() {
recvChannel := a.pubsub.ReceiveUpdateFromTopic(a.cnf.ChannelPrefix + "_policy")
forceSync := time.NewTimer(time.Duration(a.cnf.ReportForceRefreshSeconds) * time.Second)
a.RefreshLocalPolicy()
for {
select {
case <-forceSync.C:
a.RefreshLocalPolicy()
case <-recvChannel:
a.RefreshLocalPolicy()
a.Publish()
}
}
}
func (a *Client) GetPolicy() *policy.Policy {
a.policyMutex.RLock()
defer a.policyMutex.RUnlock()
cp := &policy.Policy{}
err := copier.Copy(&cp, a.policy)
if err != nil {
log.Panicf("Unable to copy policy into destination policy. This is likely a bug")
}
return cp
}
func InitPolicyClient(lc fx.Lifecycle, policyClient *Client) {
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
log.Println("Starting Policy Client")
go policyClient.PolicyClient()
return nil
},
OnStop: func(ctx context.Context) error {
log.Println("Stopping Policy Client")
return nil
},
})
}