/
model.go
207 lines (180 loc) · 5.45 KB
/
model.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
package auditlog
import (
"context"
"crypto/rsa"
"crypto/x509"
"encoding/hex"
"time"
"github.com/Laisky/errors/v2"
gutils "github.com/Laisky/go-utils/v4"
gcrypto "github.com/Laisky/go-utils/v4/crypto"
"github.com/Laisky/laisky-blog-graphql/library/db/mongo"
"github.com/Laisky/zap"
"go.mongodb.org/mongo-driver/bson/primitive"
mongoLib "go.mongodb.org/mongo-driver/mongo"
"github.com/Laisky/go-ramjet/library/log"
)
// TaskType type of audit task
type TaskType string
// String convert TaskType to string
func (t TaskType) String() string {
return string(t)
}
const (
// TaskTypeClusterFingerprint check cluster fingerprint
TaskTypeClusterFingerprint TaskType = "cluster_fingerprint"
)
// Task task for audit
type Task struct {
ID primitive.ObjectID `bson:"_id,omitempty" json:"-"`
Type TaskType `bson:"type" json:"type"`
Data string `bson:"data" json:"data"`
}
// NormalLog for normal log
// type NormalLog struct {
// ID primitive.ObjectID `bson:"_id,omitempty" json:"-"`
// Level string `bson:"level" json:"level"`
// Time time.Time `bson:"time" json:"time"`
// Logger string `bson:"logger" json:"logger"`
// Caller string `bson:"caller" json:"caller"`
// Msg string `bson:"msg" json:"msg"`
// }
// Log for audit log
//
// # Example
//
// {
// "level": "info",
// "time": "2023-08-01T09:09:34.377Z",
// "logger": "pki.pki-audit",
// "caller": "audit/log.go:61",
// "msg": "yahoo",
// "cluster_uuid": "63760a3b-2f53-11ee-b5a4-5c6f69c1c020",
// "uuid": "63760a3b-2f53-11ee-b5a4-5c6f69c1c123",
// "ts": "2023-08-01T09:09:28Z",
// "cert_chain": "-----BEGIN CERTIFICATE-xxx",
// "signature": "3282c1160a47c84xxx"
// }
type Log struct {
ID primitive.ObjectID `bson:"_id,omitempty" json:"-"`
Level string `bson:"level" json:"level"`
// DeployEnv where the log from
DeployEnv string `bson:"deploy_env" json:"deploy_env"`
Time time.Time `bson:"time" json:"time"`
Logger string `bson:"logger" json:"logger"`
Caller string `bson:"caller" json:"caller"`
Msg string `bson:"msg" json:"msg"`
ClusterUUID string `bson:"cluster_uuid" json:"cluster_uuid"`
// Ts used for generate signature
Ts string `bson:"ts" json:"ts"`
CertChain string `bson:"cert_chain" json:"cert_chain"`
// Signature generated by RSASHA256(msg + l.clusterUUID + ts)
Signature string `bson:"signature" json:"signature"`
// Verified if signature is valid
Verified bool `bson:"verified" json:"verified"`
UUID string `bson:"uuid" json:"uuid"`
}
// ValidFormat check log format and Signature
func (l *Log) ValidFormat() (err error) {
// check signature
certs, err := gcrypto.Pem2Certs([]byte(l.CertChain))
if err != nil {
return errors.Wrap(err, "try to parse cert chain")
}
pubkey, ok := certs[0].PublicKey.(*rsa.PublicKey)
if !ok {
return errors.New("pubkey must be *rsa.PublicKey")
}
sig, err := hex.DecodeString(l.Signature)
if err != nil {
return errors.Wrap(err, "try to decode signature")
}
if err = gcrypto.VerifyByRSAWithSHA256(
pubkey,
[]byte(l.Ts+l.Msg+l.ClusterUUID+l.UUID),
sig,
); err != nil {
return errors.Wrap(err, "verify signature")
}
return nil
}
// check cert chain must be signed by rootca
func (l *Log) ValidRootCA(rootcaPool *x509.CertPool) (err error) {
certs, err := gcrypto.Pem2Certs([]byte(l.CertChain))
if err != nil {
return errors.Wrap(err, "try to parse cert chain")
}
intermedia := x509.NewCertPool()
for _, cert := range certs[1:] {
intermedia.AddCert(cert)
}
if _, err = certs[0].Verify(x509.VerifyOptions{
Roots: rootcaPool,
Intermediates: intermedia,
}); err != nil {
return errors.Wrap(err, "verify cert chain by rootca")
}
return nil
}
// AuditDB audit db
type AuditDB struct {
db mongo.DB
dbName,
logColName,
normalLogColName,
taskColName string
}
// NewDB new db
func NewDB(ctx context.Context, addr, dbName, user, pwd,
normalLogColName, logColName, taskColName string) (b *AuditDB, err error) {
normalLogColName = gutils.OptionalVal(&normalLogColName, "normal-logs")
dbName = gutils.OptionalVal(&dbName, "auditlog")
logColName = gutils.OptionalVal(&logColName, "logs")
taskColName = gutils.OptionalVal(&taskColName, "tasks")
if addr == "" ||
dbName == "" ||
user == "" ||
pwd == "" ||
normalLogColName == "" ||
logColName == "" ||
taskColName == "" {
return nil, errors.New("empty db config")
}
log.Logger.Info("connect to db",
zap.String("user", user),
zap.String("addr", addr),
zap.String("dbName", dbName),
zap.String("logColName", logColName),
zap.String("normalLogColName", normalLogColName),
zap.String("taskColName", taskColName),
)
b = &AuditDB{
dbName: dbName,
logColName: logColName,
taskColName: taskColName,
normalLogColName: normalLogColName,
}
b.db, err = mongo.NewDB(ctx, mongo.DialInfo{
Addr: addr,
DBName: dbName,
User: user,
Pwd: pwd,
})
if err != nil {
return nil, errors.Wrapf(err, "try to connect to db %s", addr)
}
return b, nil
}
func (b *AuditDB) logCol() *mongoLib.Collection {
return b.db.DB(b.dbName).Collection(b.logColName)
}
func (b *AuditDB) normalLogCol() *mongoLib.Collection {
return b.db.DB(b.dbName).Collection(b.normalLogColName)
}
func (b *AuditDB) taskCol() *mongoLib.Collection {
return b.db.DB(b.dbName).Collection(b.taskColName)
}
// Close close db connection
func (b *AuditDB) Close(ctx context.Context) {
_ = b.db.Close(ctx)
}