This repository has been archived by the owner on Mar 20, 2024. It is now read-only.
forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
402 lines (344 loc) · 11 KB
/
logger.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// Copyright 2016-2018 Authors of Cilium
//
// 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 logger
import (
"encoding/json"
"net"
"strconv"
"time"
"github.com/cilium/cilium/pkg/flowdebug"
"github.com/cilium/cilium/pkg/identity"
"github.com/cilium/cilium/pkg/lock"
"github.com/cilium/cilium/pkg/logging"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/node"
"github.com/cilium/cilium/pkg/proxy/accesslog"
"github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
)
var (
log = logging.DefaultLogger.WithField(logfields.LogSubsys, "proxy-logger")
logMutex lock.Mutex
logger *lumberjack.Logger
notifier LogRecordNotifier
logPath string
metadata []string
)
// fields used for structured logging
const (
FieldType = "type"
FieldVerdict = "verdict"
FieldCode = "code"
FieldMethod = "method"
FieldURL = "url"
FieldProtocol = "protocol"
FieldHeader = "header"
FieldFilePath = logfields.Path
FieldMessage = "message"
)
// fields used for structured logging of Kafka messages
const (
FieldKafkaAPIKey = "kafkaApiKey"
FieldKafkaAPIVersion = "kafkaApiVersion"
FieldKafkaCorrelationID = "kafkaCorrelationID"
)
// LogRecord is a proxy log record based off accesslog.LogRecord.
type LogRecord struct {
accesslog.LogRecord
// endpointInfoRegistry provides access to any endpoint's information given
// its IP address.
endpointInfoRegistry EndpointInfoRegistry
// localEndpointInfo is the information on the local endpoint which
// either sent the request (for egress) or is receiving the request
// (for ingress)
localEndpointInfo *accesslog.EndpointInfo
}
// NewLogRecord creates a new log record and applies optional tags
//
// Example:
// record := logger.NewLogRecord(localEndpointInfoSource, flowType,
// observationPoint, logger.LogTags.Timestamp(time.Now()))
func NewLogRecord(endpointInfoRegistry EndpointInfoRegistry, localEndpointInfoSource EndpointInfoSource, t accesslog.FlowType, ingress bool, tags ...LogTag) *LogRecord {
var observationPoint accesslog.ObservationPoint
if ingress {
observationPoint = accesslog.Ingress
} else {
observationPoint = accesslog.Egress
}
lr := LogRecord{
LogRecord: accesslog.LogRecord{
Type: t,
ObservationPoint: observationPoint,
IPVersion: accesslog.VersionIPv4,
TransportProtocol: 6,
Timestamp: time.Now().UTC().Format(time.RFC3339Nano),
NodeAddressInfo: accesslog.NodeAddressInfo{},
},
endpointInfoRegistry: endpointInfoRegistry,
localEndpointInfo: getEndpointInfo(localEndpointInfoSource),
}
if ip := node.GetExternalIPv4(); ip != nil {
lr.LogRecord.NodeAddressInfo.IPv4 = ip.String()
}
if ip := node.GetIPv6(); ip != nil {
lr.LogRecord.NodeAddressInfo.IPv6 = ip.String()
}
for _, tagFn := range tags {
tagFn(&lr)
}
return &lr
}
// fillEndpointInfo tries to resolve the IP address and fills the EndpointInfo
// fields with either ReservedIdentityHost or ReservedIdentityWorld
func (lr *LogRecord) fillEndpointInfo(info *accesslog.EndpointInfo, ip net.IP) {
if ip.To4() != nil {
info.IPv4 = ip.String()
// first we try to resolve and check if the IP is
// same as Host
if node.IsHostIPv4(ip) {
lr.endpointInfoRegistry.FillEndpointIdentityByID(identity.ReservedIdentityHost, info)
} else if !lr.endpointInfoRegistry.FillEndpointIdentityByIP(ip, info) {
// If we are unable to resolve the HostIP as well
// as the cluster IP we mark this as a 'world' identity.
lr.endpointInfoRegistry.FillEndpointIdentityByID(identity.ReservedIdentityWorld, info)
}
} else {
info.IPv6 = ip.String()
if node.IsHostIPv6(ip) {
lr.endpointInfoRegistry.FillEndpointIdentityByID(identity.ReservedIdentityHost, info)
} else if !lr.endpointInfoRegistry.FillEndpointIdentityByIP(ip, info) {
lr.endpointInfoRegistry.FillEndpointIdentityByID(identity.ReservedIdentityWorld, info)
}
}
}
// fillIngressSourceInfo fills the EndpointInfo fields using identity sent by
// source. This is needed in ingress proxy while logging the source endpoint
// info. Since there will be 2 proxies on the same host, if both egress and
// ingress policies are set, the ingress policy cannot determine the source
// endpoint info based on ip address, as the ip address would be that of the
// egress proxy i.e host.
func (lr *LogRecord) fillIngressSourceInfo(info *accesslog.EndpointInfo, ip *net.IP, srcIdentity uint32) {
if srcIdentity != 0 {
if ip != nil {
if ip.To4() != nil {
info.IPv4 = ip.String()
} else {
info.IPv6 = ip.String()
}
}
lr.endpointInfoRegistry.FillEndpointIdentityByID(identity.NumericIdentity(srcIdentity), info)
} else {
// source security identity 0 is possible when somebody else other than
// the BPF datapath attempts to
// connect to the proxy.
// We should try to resolve if the identity belongs to reserved_host
// or reserved_world.
if ip != nil {
lr.fillEndpointInfo(info, *ip)
} else {
log.Warn("Missing security identity in source endpoint info")
}
}
}
// fillEgressDestinationInfo returns the destination EndpointInfo for a flow
// leaving the proxy at egress.
func (lr *LogRecord) fillEgressDestinationInfo(info *accesslog.EndpointInfo, ipstr string) {
ip := net.ParseIP(ipstr)
if ip != nil {
lr.fillEndpointInfo(info, ip)
}
}
// LogTag attaches a tag to a log record
type LogTag func(lr *LogRecord)
// LogTags are optional structured tags that can be attached to log records.
// See NewLogRecord() and ApplyTags() for example usage.
var LogTags logTags
type logTags struct{}
// Verdict attachs verdict information to the log record
func (logTags) Verdict(v accesslog.FlowVerdict, info string) LogTag {
return func(lr *LogRecord) {
lr.Verdict = v
lr.Info = info
}
}
// Timestamp overwrites the starting timestamp of the log record
func (logTags) Timestamp(ts time.Time) LogTag {
return func(lr *LogRecord) {
lr.Timestamp = ts.UTC().Format(time.RFC3339Nano)
}
}
// AddressingInfo is the information passed in via the Addressing() tag
type AddressingInfo struct {
SrcIPPort string
DstIPPort string
SrcIdentity uint32
}
// Addressing attaches addressing information about the source and destination
// to the logrecord
func (logTags) Addressing(i AddressingInfo) LogTag {
return func(lr *LogRecord) {
switch lr.ObservationPoint {
case accesslog.Ingress:
lr.DestinationEndpoint = *lr.localEndpointInfo
case accesslog.Egress:
lr.SourceEndpoint = *lr.localEndpointInfo
}
ipstr, port, err := net.SplitHostPort(i.SrcIPPort)
if err == nil {
ip := net.ParseIP(ipstr)
if ip != nil && ip.To4() == nil {
lr.IPVersion = accesslog.VersionIPV6
}
p, err := strconv.ParseUint(port, 10, 16)
if err == nil {
lr.SourceEndpoint.Port = uint16(p)
if lr.ObservationPoint == accesslog.Ingress {
lr.fillIngressSourceInfo(&lr.SourceEndpoint, &ip, i.SrcIdentity)
}
}
}
ipstr, port, err = net.SplitHostPort(i.DstIPPort)
if err == nil {
p, err := strconv.ParseUint(port, 10, 16)
if err == nil {
lr.DestinationEndpoint.Port = uint16(p)
if lr.ObservationPoint == accesslog.Egress {
lr.fillEgressDestinationInfo(&lr.DestinationEndpoint, ipstr)
}
}
}
}
}
// HTTP attaches HTTP information to the log record
func (logTags) HTTP(h *accesslog.LogRecordHTTP) LogTag {
return func(lr *LogRecord) {
lr.HTTP = h
}
}
// Kafka attaches Kafka information to the log record
func (logTags) Kafka(k *accesslog.LogRecordKafka) LogTag {
return func(lr *LogRecord) {
lr.Kafka = k
}
}
// DNS attaches DNS information to the log record
func (logTags) DNS(d *accesslog.LogRecordDNS) LogTag {
return func(lr *LogRecord) {
lr.DNS = d
}
}
// L7 attaches generic L7 information to the log record
func (logTags) L7(h *accesslog.LogRecordL7) LogTag {
return func(lr *LogRecord) {
lr.L7 = h
}
}
// ApplyTags applies tags to an existing log record
//
// Example:
// lr.ApplyTags(logger.LogTags.Verdict(verdict, info))
func (lr *LogRecord) ApplyTags(tags ...LogTag) {
for _, tagFn := range tags {
tagFn(lr)
}
}
func (lr *LogRecord) getLogFields() *logrus.Entry {
fields := log.WithFields(logrus.Fields{
FieldType: lr.Type,
FieldVerdict: lr.Verdict,
FieldMessage: lr.Info,
})
if lr.HTTP != nil {
fields = fields.WithFields(logrus.Fields{
FieldCode: lr.HTTP.Code,
FieldMethod: lr.HTTP.Method,
FieldURL: lr.HTTP.URL,
FieldProtocol: lr.HTTP.Protocol,
FieldHeader: lr.HTTP.Headers,
})
}
if lr.Kafka != nil {
fields = fields.WithFields(logrus.Fields{
FieldCode: lr.Kafka.ErrorCode,
FieldKafkaAPIKey: lr.Kafka.APIKey,
FieldKafkaAPIVersion: lr.Kafka.APIVersion,
FieldKafkaCorrelationID: lr.Kafka.CorrelationID,
})
}
return fields
}
func (lr *LogRecord) getRawLogMessage() []byte {
b, err := json.Marshal(*lr)
if err != nil {
return []byte(err.Error())
}
return append(b, byte('\n'))
}
// Log logs a record to the logfile and flushes the buffer
func (lr *LogRecord) Log() {
flowdebug.Log(lr.getLogFields(), "Logging flow record")
// Lock while writing access log so we serialize writes as we may have
// to reopen the logfile and parallel writes could fail because of that
logMutex.Lock()
defer logMutex.Unlock()
lr.Metadata = metadata
if notifier != nil {
notifier.NewProxyLogRecord(lr)
}
if logger == nil {
flowdebug.Log(log.WithField(FieldFilePath, logPath),
"Skipping writing to access log (logger nil)")
return
}
if _, err := logger.Write(lr.getRawLogMessage()); err != nil {
log.WithError(err).WithField(FieldFilePath, logPath).
Errorf("Error writing to access file")
}
}
// Called with lock held
func openLogfileLocked(lf string) error {
logPath = lf
log.WithField(FieldFilePath, logPath).Info("Opened access log")
logger = &lumberjack.Logger{
Filename: lf,
MaxSize: 100, // megabytes
MaxBackups: 3,
MaxAge: 28, //days
Compress: true, // disabled by default
}
return nil
}
// LogRecordNotifier is the interface to implement LogRecord notifications
type LogRecordNotifier interface {
// NewProxyLogRecord is called for each new log record
NewProxyLogRecord(l *LogRecord) error
}
// OpenLogfile opens a file for logging
func OpenLogfile(lf string) error {
logMutex.Lock()
defer logMutex.Unlock()
return openLogfileLocked(lf)
}
// SetNotifier sets the notifier to call for all L7 records
func SetNotifier(n LogRecordNotifier) {
logMutex.Lock()
notifier = n
logMutex.Unlock()
}
// SetMetadata sets the metadata to include in each record
func SetMetadata(md []string) {
logMutex.Lock()
defer logMutex.Unlock()
metadata = md
}