-
Notifications
You must be signed in to change notification settings - Fork 51
/
ping_http.go
228 lines (190 loc) · 6.36 KB
/
ping_http.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
package httpproxy
import (
"context"
"fmt"
"net"
"net/http"
"time"
"go.aporeto.io/enforcerd/trireme-lib/collector"
"go.aporeto.io/enforcerd/trireme-lib/controller/internal/enforcer/apiauth"
"go.aporeto.io/enforcerd/trireme-lib/controller/internal/enforcer/applicationproxy/common"
"go.aporeto.io/enforcerd/trireme-lib/controller/internal/enforcer/applicationproxy/markedconn"
"go.aporeto.io/enforcerd/trireme-lib/controller/internal/enforcer/applicationproxy/serviceregistry"
"go.aporeto.io/enforcerd/trireme-lib/controller/pkg/packet"
"go.aporeto.io/enforcerd/trireme-lib/controller/pkg/servicetokens"
"go.aporeto.io/enforcerd/trireme-lib/policy"
"go.aporeto.io/gaia"
"go.aporeto.io/gaia/x509extensions"
"go.uber.org/zap"
)
const fourTupleKey = "fourTuple"
type fourTuple struct {
sourceAddress net.IP
destinationAddress net.IP
sourcePort int
destinationPort int
}
// InitiatePing starts an encrypted connection to the given config.
func (p *Config) InitiatePing(ctx context.Context, sctx *serviceregistry.ServiceContext, sdata *serviceregistry.DependentServiceData, pingConfig *policy.PingConfig) error {
zap.L().Debug("Initiating L7 ping")
for i := 0; i < pingConfig.Iterations; i++ {
if err := p.sendPingRequest(ctx, pingConfig, sctx, sdata, i); err != nil {
return err
}
}
return nil
}
func (p *Config) sendPingRequest(
ctx context.Context,
pingConfig *policy.PingConfig,
sctx *serviceregistry.ServiceContext,
sdata *serviceregistry.DependentServiceData,
iterationID int) error {
pingID := pingConfig.ID
destIP := pingConfig.IP
destPort := pingConfig.Port
_, netaction, _ := sctx.PUContext.ApplicationACLPolicyFromAddr(destIP, destPort, packet.IPProtocolTCP)
pingErr := "dial"
if e := pingConfig.Error(); e != "" {
pingErr = e
}
pr := &collector.PingReport{
PingID: pingID,
IterationID: iterationID,
ServiceID: sdata.APICache.ID,
PUID: sctx.PUContext.ManagementID(),
Namespace: sctx.PUContext.ManagementNamespace(),
Protocol: 6,
ServiceType: "L7",
AgentVersion: p.agentVersion.String(),
ApplicationListening: false,
ACLPolicyID: netaction.PolicyID,
ACLPolicyAction: netaction.Action,
Error: pingErr,
TargetTCPNetworks: pingConfig.TargetTCPNetworks,
ExcludedNetworks: pingConfig.ExcludedNetworks,
Type: gaia.PingProbeTypeRequest,
RemoteEndpointType: collector.EndPointTypeExternalIP,
Claims: sctx.PUContext.Identity().GetSlice(),
ClaimsType: gaia.PingProbeClaimsTypeTransmitted,
RemoteNamespaceType: gaia.PingProbeRemoteNamespaceTypePlain,
PayloadSizeType: gaia.PingProbePayloadSizeTypeTransmitted,
}
ft := &fourTuple{}
p.RLock()
encodingKey := p.secrets.EncodingKey()
pubKey := p.secrets.TransmittedKey()
p.RUnlock()
pingPayload := &policy.PingPayload{
PingID: pingID,
IterationID: iterationID,
}
token, err := servicetokens.CreateAndSign(
"",
sctx.PUContext.Identity().GetSlice(),
sctx.PUContext.Scopes(),
sctx.PUContext.ManagementID(),
apiauth.DefaultValidity,
encodingKey,
pingPayload,
)
if err != nil {
return err
}
networkDialerWithContext := func(ctx context.Context, _, addr string) (net.Conn, error) {
conn, err := dial(ctx, addr, p.mark)
if err != nil {
return nil, fmt.Errorf("unable to dial remote: %s", err)
}
if v := ctx.Value(fourTupleKey); v != nil {
if r, ok := v.(*fourTuple); ok {
laddr := conn.LocalAddr().(*net.TCPAddr)
raddr := conn.RemoteAddr().(*net.TCPAddr)
r.sourceAddress = laddr.IP
r.sourcePort = laddr.Port
r.destinationAddress = raddr.IP
r.destinationPort = raddr.Port
}
}
return conn, nil
}
raddr := &net.TCPAddr{
IP: destIP,
Port: int(destPort),
}
// ServerName: Use first configured FQDN or the destination IP
serverName, err := common.GetTLSServerName(raddr.String(), sdata.ServiceObject)
if err != nil {
return fmt.Errorf("unable to get the server name: %s", err)
}
// Used to validate the hostname in the returned server certs.
// TODO: Maybe we should elevate this as first class citizen ?
p.tlsClientConfig.ServerName = serverName
encryptedTransport := &http.Transport{
TLSClientConfig: p.tlsClientConfig,
DialContext: networkDialerWithContext,
MaxIdleConnsPerHost: 2000,
MaxIdleConns: 2000,
ForceAttemptHTTP2: true,
}
client := &http.Client{
Transport: encryptedTransport,
Timeout: 5 * time.Second,
}
host := fmt.Sprintf("https://%s:%d", destIP, destPort)
ctxWithReport := context.WithValue(ctx, fourTupleKey, ft) // nolint: golint,staticcheck
req, err := http.NewRequestWithContext(ctxWithReport, "GET", host, nil)
if err != nil {
return err
}
defer p.collector.CollectPingEvent(pr)
pr.PayloadSize = len(pubKey) + len(token)
req.Header.Add("X-APORETO-KEY", string(pubKey))
req.Header.Add("X-APORETO-AUTH", token)
startTime := time.Now()
res, err := client.Do(req)
if err != nil {
pr.Error = err.Error()
pr.FourTuple = fmt.Sprintf(
"%s:%s:%d:%d",
ft.sourceAddress.String(),
ft.destinationAddress.String(),
ft.sourcePort,
ft.destinationPort,
)
return err
}
res.Body.Close() // nolint: errcheck
pr.Error = ""
pr.RTT = time.Since(startTime).String()
pr.ApplicationListening = true
pr.Type = gaia.PingProbeTypeResponse
pr.FourTuple = fmt.Sprintf(
"%s:%s:%d:%d",
ft.destinationAddress.String(),
ft.sourceAddress.String(),
ft.destinationPort,
ft.sourcePort,
)
if len(res.TLS.PeerCertificates) > 0 {
pr.RemotePUID = res.TLS.PeerCertificates[0].Subject.CommonName
pr.RemoteEndpointType = collector.EndPointTypePU
if len(res.TLS.PeerCertificates[0].Subject.Organization) > 0 {
pr.RemoteNamespace = res.TLS.PeerCertificates[0].Subject.Organization[0]
}
pr.PeerCertIssuer = res.TLS.PeerCertificates[0].Issuer.String()
pr.PeerCertSubject = res.TLS.PeerCertificates[0].Subject.String()
pr.PeerCertExpiry = res.TLS.PeerCertificates[0].NotAfter
if found, controller := common.ExtractExtension(x509extensions.Controller(), res.TLS.PeerCertificates[0].Extensions); found {
pr.RemoteController = string(controller)
}
}
return nil
}
func dial(ctx context.Context, addr string, mark int) (net.Conn, error) {
d := net.Dialer{
Timeout: 5 * time.Second,
Control: markedconn.ControlFunc(mark, false, nil),
}
return d.DialContext(ctx, "tcp", addr)
}