-
Notifications
You must be signed in to change notification settings - Fork 51
/
state.go
157 lines (137 loc) · 4.23 KB
/
state.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
package flowstats
import (
"net"
"net/http"
"go.aporeto.io/enforcerd/trireme-lib/controller/internal/enforcer/apiauth"
"go.aporeto.io/enforcerd/trireme-lib/collector"
"go.aporeto.io/enforcerd/trireme-lib/controller/pkg/packet"
"go.aporeto.io/enforcerd/trireme-lib/policy"
)
// ConnectionState captures the connection state. This state
// is passed to the RoundTripper for any last minute adjustments.
type ConnectionState struct {
Stats *collector.FlowRecord
Cookie *http.Cookie
}
// NewAppConnectionState will create the initial connection state object.
func NewAppConnectionState(nativeID string, r *http.Request, authRequest *apiauth.Request, resp *apiauth.AppAuthResponse) *ConnectionState {
sourceIP := "0.0.0.0/0"
sourcePort := 0
if sourceAddress, err := net.ResolveTCPAddr("tcp", r.RemoteAddr); err == nil {
sourceIP = sourceAddress.IP.String()
sourcePort = sourceAddress.Port
}
var tags policy.TagStore
if resp.PUContext.Annotations() != nil {
tags = *resp.PUContext.Annotations()
}
return &ConnectionState{
Stats: &collector.FlowRecord{
ContextID: nativeID,
Destination: collector.EndPoint{
URI: r.Method + " " + r.RequestURI,
HTTPMethod: r.Method,
Type: collector.EndPointTypeExternalIP,
Port: uint16(authRequest.OriginalDestination.Port),
IP: authRequest.OriginalDestination.IP.String(),
ID: resp.NetworkServiceID,
},
Source: collector.EndPoint{
Type: collector.EndPointTypePU,
ID: resp.PUContext.ManagementID(),
IP: sourceIP,
Port: uint16(sourcePort),
HTTPMethod: r.Method,
URI: r.Method + " " + r.RequestURI,
},
Action: resp.Action,
L4Protocol: packet.IPProtocolTCP,
ServiceType: policy.ServiceHTTP,
ServiceID: resp.ServiceID,
Tags: tags.GetSlice(),
Namespace: resp.PUContext.ManagementNamespace(),
PolicyID: resp.NetworkPolicyID,
Count: 1,
},
}
}
// NewNetworkConnectionState will create the initial connection state object.
func NewNetworkConnectionState(nativeID string, userID string, r *apiauth.Request, d *apiauth.NetworkAuthResponse) *ConnectionState {
var mgmtID, namespace, serviceID string
var tags policy.TagStore
if d.PUContext != nil {
mgmtID = d.PUContext.ManagementID()
namespace = d.PUContext.ManagementNamespace()
if d.PUContext.Annotations() != nil {
tags = *d.PUContext.Annotations()
}
serviceID = d.ServiceID
} else {
mgmtID = collector.DefaultEndPoint
namespace = collector.DefaultEndPoint
tags = *policy.NewTagStore()
serviceID = collector.DefaultEndPoint
}
sourceType := collector.EndPointTypeExternalIP
sourceID := collector.DefaultEndPoint
networkPolicyID := collector.DefaultEndPoint
action := policy.Reject | policy.Log
if d != nil {
sourceType = d.SourceType
if sourceType == collector.EndPointTypeClaims {
sourceType = collector.EndPointTypeExternalIP
}
switch d.SourceType {
case collector.EndPointTypePU:
sourceID = d.SourcePUID
case collector.EndPointTypeClaims:
sourceID = d.NetworkServiceID
default:
sourceID = d.NetworkServiceID
}
if d.NetworkPolicyID != "" {
networkPolicyID = d.NetworkPolicyID
}
action = d.Action
}
c := &ConnectionState{
Stats: &collector.FlowRecord{
ContextID: nativeID,
Destination: collector.EndPoint{
ID: mgmtID,
Type: collector.EndPointTypePU,
IP: r.OriginalDestination.IP.String(),
Port: uint16(r.OriginalDestination.Port),
URI: r.Method + " " + r.RequestURI,
HTTPMethod: r.Method,
UserID: userID,
},
Source: collector.EndPoint{
ID: sourceID,
Type: sourceType,
IP: r.SourceAddress.IP.String(),
Port: uint16(r.SourceAddress.Port),
UserID: userID,
},
Action: action,
L4Protocol: packet.IPProtocolTCP,
ServiceType: policy.ServiceHTTP,
PolicyID: networkPolicyID,
ServiceID: serviceID,
Tags: tags.GetSlice(),
Namespace: namespace,
Count: 1,
},
}
if d != nil {
if d.Action.Rejected() {
c.Stats.DropReason = d.DropReason
}
if d.ObservedPolicyID != "" {
c.Stats.ObservedPolicyID = d.ObservedPolicyID
c.Stats.ObservedAction = d.ObservedAction
}
c.Cookie = d.Cookie
}
return c
}