This repository has been archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
hook.go
83 lines (69 loc) · 1.92 KB
/
hook.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
package findy
import (
"io"
"github.com/findy-network/findy-agent-api/grpc/agency"
"github.com/findy-network/findy-agent-api/grpc/ops"
"github.com/findy-network/findy-agent-vault/utils"
"github.com/findy-network/findy-grpc/agency/client"
auth "github.com/findy-network/findy-grpc/jwt"
"github.com/findy-network/findy-grpc/rpc"
"github.com/golang/glog"
"github.com/google/uuid"
"github.com/lainio/err2"
"google.golang.org/grpc"
)
func (f *Agency) adminClient(user string) (conn *grpc.ClientConn, err error) {
defer err2.Return(&err)
utils.LogLow().Infoln("client with user:", user)
cfg := client.BuildClientConnBase(f.tlsPath, f.agencyHost, f.agencyPort, f.options)
token := auth.BuildJWT(user)
cfg.JWT = token
conn, err = rpc.ClientConn(*cfg)
err2.Check(err)
return
}
func (f *Agency) listenAdminHook() (err error) {
defer err2.Return(&err)
// TODO: cancellation, reconnect
glog.Info("Start listening to PSM events.")
conn, err := f.adminClient("findy-root")
err2.Check(err)
opsClient := ops.NewAgencyClient(conn)
statusCh := make(chan *agency.ProtocolStatus)
// Error in registration is not notified here, instead all relevant info comes
// in stream callback from now on
stream, err := opsClient.PSMHook(f.ctx, &ops.DataHook{Id: uuid.New().String()})
err2.Check(err)
utils.LogMed().Infoln("successful start of listen PSM hook id:")
go func() {
defer err2.CatchTrace(func(err error) {
glog.Warningln("error when reading response:", err)
close(statusCh)
conn.Close()
// TODO: reconnect logic
})
for {
status, err := stream.Recv()
if err == io.EOF {
glog.Warningln("status stream end")
close(statusCh)
conn.Close()
break
}
err2.Check(err)
statusCh <- status.ProtocolStatus
}
}()
go func() {
for {
_, ok := <-statusCh
if !ok {
glog.Warning("closed from server")
break
}
// TODO: get agent ID from agency status
// store data
}
}()
return nil
}