-
Notifications
You must be signed in to change notification settings - Fork 273
/
pgaudit.go
100 lines (84 loc) · 3.06 KB
/
pgaudit.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
/*
Copyright The CloudNativePG Contributors
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
http://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 logpipe
// PgAuditFieldsPerRecord is the number of fields in a pgaudit log line
const PgAuditFieldsPerRecord int = 9
// PgAuditRecordName is the value of the logger field for pgaudit
const PgAuditRecordName = "pgaudit"
// PgAuditLoggingDecorator stores all the fields of pgaudit CSV format
type PgAuditLoggingDecorator struct {
*LoggingRecord
Audit *PgAuditRecord `json:"audit,omitempty"`
CSVReadWriter `json:"-"`
}
// NewPgAuditLoggingDecorator builds PgAuditLoggingDecorator
func NewPgAuditLoggingDecorator() *PgAuditLoggingDecorator {
return &PgAuditLoggingDecorator{
LoggingRecord: &LoggingRecord{},
Audit: &PgAuditRecord{},
CSVReadWriter: NewCSVRecordReadWriter(PgAuditFieldsPerRecord),
}
}
// GetName implements the NamedRecord interface
func (r *PgAuditLoggingDecorator) GetName() string {
return PgAuditRecordName
}
func getTagAndContent(record *LoggingRecord) (string, string) {
if record != nil && tagRegex.MatchString(record.Message) {
matches := tagRegex.FindStringSubmatch(record.Message)
return matches[1], matches[2]
}
return "", ""
}
// FromCSV implements the CSVRecordParser interface, parsing a LoggingRecord and then
func (r *PgAuditLoggingDecorator) FromCSV(content []string) NamedRecord {
r.LoggingRecord.FromCSV(content)
tag, record := getTagAndContent(r.LoggingRecord)
if tag != "AUDIT" || record == "" {
return r.LoggingRecord
}
_, err := r.CSVReadWriter.Write([]byte(record))
if err != nil {
return r.LoggingRecord
}
auditContent, err := r.Read()
if err != nil {
return r.LoggingRecord
}
r.LoggingRecord.Message = ""
r.Audit.fromCSV(auditContent)
return r
}
func (r *PgAuditRecord) fromCSV(auditContent []string) {
r.AuditType = auditContent[0]
r.StatementID = auditContent[1]
r.SubstatementID = auditContent[2]
r.Class = auditContent[3]
r.Command = auditContent[4]
r.ObjectType = auditContent[5]
r.ObjectName = auditContent[6]
r.Statement = auditContent[7]
r.Parameter = auditContent[8]
}
// PgAuditRecord stores all the fields of a pgaudit log line
type PgAuditRecord struct {
AuditType string `json:"audit_type,omitempty"`
StatementID string `json:"statement_id,omitempty"`
SubstatementID string `json:"substatement_id,omitempty"`
Class string `json:"class,omitempty"`
Command string `json:"command,omitempty"`
ObjectType string `json:"object_type,omitempty"`
ObjectName string `json:"object_name,omitempty"`
Statement string `json:"statement,omitempty"`
Parameter string `json:"parameter,omitempty"`
}