/
accesslog.go
87 lines (72 loc) · 1.89 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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package envoy
import (
"fmt"
"net/http"
"net/url"
"strings"
cilium "github.com/cilium/proxy/go/cilium/api"
"github.com/cilium/cilium/pkg/proxy/accesslog"
)
// ParseURL returns the URL as *net.url.URL
func ParseURL(scheme, host, path string) *url.URL {
u, err := url.Parse(fmt.Sprintf("%s://%s/%s", scheme, host, strings.TrimPrefix(path, "/")))
if err != nil {
u = &url.URL{
Scheme: scheme,
Host: host,
Path: path,
}
}
return u
}
// getNetHttpHeaders returns the Headers as net.http.Header
func GetNetHttpHeaders(httpHeaders []*cilium.KeyValue) http.Header {
headers := make(http.Header)
for _, header := range httpHeaders {
headers.Add(header.Key, header.Value)
}
return headers
}
// getProtocol returns the HTTP protocol in the format that Cilium understands
func GetProtocol(httpProtocol cilium.HttpProtocol) string {
switch httpProtocol {
case cilium.HttpProtocol_HTTP10:
return "HTTP/1"
case cilium.HttpProtocol_HTTP11:
return "HTTP/1.1"
case cilium.HttpProtocol_HTTP2:
return "HTTP/2"
default:
return "Unknown"
}
}
// GetFlowType returns the type of flow (request|response)
func GetFlowType(m *cilium.LogEntry) accesslog.FlowType {
// the fall back type is request
result := accesslog.TypeRequest
if m != nil {
switch m.EntryType {
case cilium.EntryType_Denied:
result = accesslog.TypeRequest
case cilium.EntryType_Request:
result = accesslog.TypeRequest
case cilium.EntryType_Response:
result = accesslog.TypeResponse
}
}
return result
}
// GetVerdict returns the verdict performed on the flow (forwarded|denied)
func GetVerdict(m *cilium.LogEntry) accesslog.FlowVerdict {
// the default verdict is forwarded
result := accesslog.VerdictForwarded
if m != nil {
switch m.EntryType {
case cilium.EntryType_Denied:
result = accesslog.VerdictDenied
}
}
return result
}