-
Notifications
You must be signed in to change notification settings - Fork 1
/
dbUtils.go
110 lines (101 loc) · 2.98 KB
/
dbUtils.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
package vermouth
import (
"context"
"github.com/ipifony/vermouth/logger"
"github.com/jackc/pgx/v5/pgxpool"
)
const (
InsertSigningSql = "INSERT INTO signing_record (src_tn, dst_tn, attestation, orig_id) VALUES ($1,$2,$3,$4)"
InsertVerificationSql = "INSERT INTO verification_record (sip_from, sip_pai, sip_to, sip_ruri, identity, x5u_url, " +
"cert_chain_raw, status_no_signature, status_bad_format, status_not_trusted, status_invalid_signature, " +
"status_tn_mismatch, status_stale, status_valid, attestation, orig_ocn) " +
"VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16)"
)
var pool *pgxpool.Pool
func ClosePool() {
if pool != nil {
pool.Close()
}
}
func InitPool() error {
if !GlobalConfig.isDbEnabled() {
return nil
}
var dbErr error
config, dbErr := pgxpool.ParseConfig(GlobalConfig.PrepareDbUrl() + "?pool_max_conns=5&pool_min_conns=2")
if dbErr != nil {
return dbErr
}
pool, dbErr = pgxpool.NewWithConfig(context.Background(), config)
if dbErr != nil {
pool = nil
return dbErr
}
dbErr = pool.Ping(context.Background())
if dbErr != nil {
pool = nil
return dbErr
}
logger.LogChan <- &logger.LogMessage{Severity: logger.INFO, MsgStr: "DB Logging Active"}
return nil
}
func WriteSigningRecord(srcTn *string, dstTn *string, attestation *string, origId *string) {
if pool == nil {
return
}
dbConn, err := pool.Acquire(context.Background())
if err != nil || dbConn == nil {
logger.LogChan <- &logger.LogMessage{Severity: logger.ERROR, MsgStr: "Unable to get a db connection. Error: " + err.Error()}
return
}
defer dbConn.Release()
ret, err := dbConn.Query(context.Background(), InsertSigningSql, srcTn, dstTn, attestation, origId)
if err != nil {
logger.LogChan <- &logger.LogMessage{Severity: logger.ERROR, MsgStr: "Error inserting into signing_record. Error: " + err.Error()}
return
}
ret.Close()
}
func WriteVerificationRecord(record *VerificationRecord) {
if pool == nil {
return
}
dbConn, err := pool.Acquire(context.Background())
if err != nil || dbConn == nil {
logger.LogChan <- &logger.LogMessage{Severity: logger.ERROR, MsgStr: "Unable to get a db connection. Error: " + err.Error()}
return
}
defer dbConn.Release()
var certChain string
if record.CertBytes != nil {
certChain = string(*record.CertBytes)
}
ret, err := dbConn.Query(context.Background(), InsertVerificationSql,
record.From,
record.Pai,
record.To,
record.Ruri,
record.Identity,
record.X5U,
certChain,
boolToInt(record.StatusNoSignature),
boolToInt(record.StatusBadFormat),
boolToInt(record.StatusNotTrusted),
boolToInt(record.StatusInvalidSignature),
boolToInt(record.StatusTnMismatch),
boolToInt(record.StatusStale),
boolToInt(record.StatusValid),
record.Attestation,
record.OrigOcn)
if err != nil {
logger.LogChan <- &logger.LogMessage{Severity: logger.ERROR, MsgStr: "Error inserting into verification_record. Error: " + err.Error()}
return
}
ret.Close()
}
func boolToInt(val bool) int8 {
if val {
return 1
}
return 0
}