-
Notifications
You must be signed in to change notification settings - Fork 148
/
accesslog.go
132 lines (118 loc) · 2.79 KB
/
accesslog.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
package accesslogger
import (
"bytes"
"fmt"
"html/template"
"net"
"net/http"
"strconv"
"strings"
"time"
"github.com/cloudfoundry/gosteno"
)
const REQUEST_ID_HEADER = "X-Vcap-Request-ID"
var logTemplate *template.Template
type templateContext struct {
Timestamp int64
RequestID string
Path string
Method string
SourceHost string
SourcePort string
DestinationHost string
DestinationPort string
}
type AccessLog struct {
request *http.Request
timestamp time.Time
host string
port uint32
logger *gosteno.Logger
}
func NewAccessLog(req *http.Request, ts time.Time, host string, port uint32, logger *gosteno.Logger) *AccessLog {
return &AccessLog{
request: req,
timestamp: ts,
host: host,
port: port,
logger: logger,
}
}
func (al *AccessLog) String() string {
vcapRequestId := al.request.Header.Get(REQUEST_ID_HEADER)
path := al.request.URL.Path
if al.request.URL.RawQuery != "" {
path = fmt.Sprintf("%s?%s", al.request.URL.Path, al.request.URL.RawQuery)
}
remoteHost, remotePort := al.extractRemoteInfo()
context := templateContext{
toMillis(al.timestamp),
vcapRequestId,
path,
al.request.Method,
remoteHost,
remotePort,
al.host,
strconv.Itoa(int(al.port)),
}
var buf bytes.Buffer
err := logTemplate.Execute(&buf, context)
if err != nil {
al.logger.Errorf("Error executing security access log template: %s\n", err)
return ""
}
return buf.String()
}
func (al *AccessLog) extractRemoteInfo() (string, string) {
remoteAddr := al.request.Header.Get("X-Forwarded-For")
index := strings.Index(remoteAddr, ",")
if index > -1 {
remoteAddr = remoteAddr[:index]
}
if remoteAddr == "" {
remoteAddr = al.request.RemoteAddr
}
if !strings.Contains(remoteAddr, ":") {
remoteAddr += ":"
}
host, port, err := net.SplitHostPort(remoteAddr)
if err != nil {
al.logger.Errorf("Error splitting host and port for access log: %s\n", err)
return "", ""
}
return host, port
}
func toMillis(timestamp time.Time) int64 {
return timestamp.UnixNano() / int64(time.Millisecond)
}
func init() {
extensions := []string{
"rt={{ .Timestamp }}",
"cs1Label=userAuthenticationMechanism",
"cs1=oauth-access-token",
"cs2Label=vcapRequestId",
"cs2={{ .RequestID }}",
"request={{ .Path }}",
"requestMethod={{ .Method }}",
"src={{ .SourceHost }}",
"spt={{ .SourcePort }}",
"dst={{ .DestinationHost }}",
"dpt={{ .DestinationPort }}",
}
fields := []string{
"0",
"cloud_foundry",
"loggregator_trafficcontroller",
"1.0",
"{{ .Method }} {{ .Path }}",
"{{ .Method }} {{ .Path }}",
"0",
strings.Join(extensions, " "),
}
templateSource := "CEF:" + strings.Join(fields, "|")
var err error
logTemplate, err = template.New("access_log").Parse(templateSource)
if err != nil {
panic(err)
}
}