1 package opaqueports
2
3 import (
4 "fmt"
5 "regexp"
6
7 "github.com/linkerd/linkerd2/testutil/prommatch"
8 )
9
10 var (
11 authorityRE = regexp.MustCompile(`[a-zA-Z\-]+\.[a-zA-Z\-]+\.svc\.cluster\.local:[0-9]+`)
12 )
13
14
15
16 func hasNoOutboundHTTPRequest(metrics, ns string) error {
17 m := prommatch.NewMatcher("request_total",
18 prommatch.Labels{
19 "direction": prommatch.Equals("outbound"),
20 })
21 ok, err := m.HasMatchInString(metrics)
22 if err != nil {
23 return fmt.Errorf("failed to run a check of against the provided metrics: %w", err)
24 }
25 if ok {
26 return fmt.Errorf("expected not to find HTTP outbound requests \n%s", metrics)
27 }
28 return nil
29 }
30
31
32
33
34
35
36
37
38
39
40
41 func hasOutboundHTTPRequestWithTLS(metrics, ns string) error {
42 m := prommatch.NewMatcher("request_total",
43 prommatch.Labels{
44 "direction": prommatch.Equals("outbound"),
45 "tls": prommatch.Equals("true"),
46 "server_id": prommatch.Equals(fmt.Sprintf("default.%s.serviceaccount.identity.linkerd.cluster.local", ns)),
47 "dst_namespace": prommatch.Equals(ns),
48 "dst_serviceaccount": prommatch.Equals("default"),
49 },
50 prommatch.TargetAddrLabels(),
51 prommatch.HasPositiveValue())
52 ok, err := m.HasMatchInString(metrics)
53 if err != nil {
54 return fmt.Errorf("failed to run a check of against the provided metrics: %w", err)
55 }
56 if !ok {
57 return fmt.Errorf("expected to find HTTP TLS outbound requests \n%s", metrics)
58 }
59 return nil
60 }
61
62
63
64
65
66
67
68
69
70
71
72
73 func hasOutboundHTTPRequestNoTLS(metrics, ns string) error {
74 m := prommatch.NewMatcher("request_total",
75 prommatch.Labels{
76 "direction": prommatch.Equals("outbound"),
77 "tls": prommatch.Equals("no_identity"),
78 "no_tls_reason": prommatch.Equals("not_provided_by_service_discovery"),
79 "dst_namespace": prommatch.Equals(ns),
80 "dst_serviceaccount": prommatch.Equals("default"),
81 },
82 prommatch.TargetAddrLabels(),
83 prommatch.HasPositiveValue())
84 ok, err := m.HasMatchInString(metrics)
85 if err != nil {
86 return fmt.Errorf("failed to run a check of against the provided metrics: %w", err)
87 }
88 if !ok {
89 return fmt.Errorf("expected to find HTTP non-TLS outbound requests\n%s", metrics)
90 }
91 return nil
92 }
93
94
95
96
97
98
99
100
101
102
103
104
105
106 func hasInboundTCPTrafficWithTLS(metrics, ns string) error {
107 m := prommatch.NewMatcher(
108 "tcp_open_total",
109 prommatch.Labels{
110 "direction": prommatch.Equals("inbound"),
111 "peer": prommatch.Equals("src"),
112 "tls": prommatch.Equals("true"),
113 "client_id": prommatch.Equals(fmt.Sprintf("default.%s.serviceaccount.identity.linkerd.cluster.local", ns)),
114 "srv_kind": prommatch.Equals("default"),
115 "srv_name": prommatch.Equals("all-unauthenticated"),
116 },
117 prommatch.TargetAddrLabels(),
118 prommatch.HasPositiveValue(),
119 )
120 ok, err := m.HasMatchInString(metrics)
121 if err != nil {
122 return fmt.Errorf("failed to run a check of against the provided metrics: %w", err)
123 }
124 if !ok {
125 return fmt.Errorf("failed to find expected metric for inbound TLS TCP traffic\n%s", metrics)
126 }
127 return nil
128 }
129
130
131
132
133
134
135
136
137
138
139 func hasOutboundTCPWithAuthorityAndNoTLS(metrics, ns string) error {
140 m := prommatch.NewMatcher("tcp_open_total",
141 prommatch.Labels{
142 "direction": prommatch.Equals("outbound"),
143 "peer": prommatch.Equals("dst"),
144 "tls": prommatch.Equals("no_identity"),
145 "no_tls_reason": prommatch.Equals("not_provided_by_service_discovery"),
146 "authority": prommatch.Like(authorityRE),
147 },
148 prommatch.HasPositiveValue())
149 ok, err := m.HasMatchInString(metrics)
150 if err != nil {
151 return fmt.Errorf("failed to run a check of against the provided metrics: %w", err)
152 }
153 if !ok {
154 return fmt.Errorf("failed to find expected metric for outbound non-TLS TCP traffic\n%s", metrics)
155 }
156 return nil
157 }
158
159
160
161
162
163
164
165
166
167
168 func hasOutboundTCPWithNoTLSAndNoAuthority(metrics, ns string) error {
169 m := prommatch.NewMatcher("tcp_open_total",
170 prommatch.Labels{
171 "direction": prommatch.Equals("outbound"),
172 "peer": prommatch.Equals("dst"),
173 "tls": prommatch.Equals("no_identity"),
174 "no_tls_reason": prommatch.Equals("not_provided_by_service_discovery"),
175 "authority": prommatch.Absent(),
176 })
177 ok, err := m.HasMatchInString(metrics)
178 if err != nil {
179 return fmt.Errorf("failed to run a check of against the provided metrics: %w", err)
180 }
181 if !ok {
182 return fmt.Errorf("failed to find expected metric for outbound non-TLS TCP traffic\n%s", metrics)
183 }
184 return nil
185 }
186
187
188
189
190
191
192
193
194
195
196 func hasOutboundTCPWithTLSAndAuthority(metrics, ns string) error {
197 m := prommatch.NewMatcher("tcp_open_total",
198 prommatch.Labels{
199 "direction": prommatch.Equals("outbound"),
200 "peer": prommatch.Equals("dst"),
201 "tls": prommatch.Equals("true"),
202 "authority": prommatch.Like(authorityRE),
203 },
204 prommatch.TargetAddrLabels(),
205 prommatch.HasPositiveValue())
206 ok, err := m.HasMatchInString(metrics)
207 if err != nil {
208 return fmt.Errorf("failed to run a check against the provided metrics: %w", err)
209 }
210 if !ok {
211 return fmt.Errorf("failed to find expected metric for outbound TLS TCP traffic\n%s", metrics)
212 }
213 return nil
214 }
215
216
217
218
219
220
221
222
223
224
225 func hasOutboundTCPWithTLSAndNoAuthority(metrics, ns string) error {
226 m := prommatch.NewMatcher("tcp_open_total",
227 prommatch.Labels{
228 "direction": prommatch.Equals("outbound"),
229 "peer": prommatch.Equals("dst"),
230 "tls": prommatch.Equals("true"),
231 "authority": prommatch.Absent(),
232 },
233 prommatch.TargetAddrLabels(),
234 prommatch.HasPositiveValue())
235 ok, err := m.HasMatchInString(metrics)
236 if err != nil {
237 return fmt.Errorf("failed to run a check of against the provided metrics: %w", err)
238 }
239 if !ok {
240 return fmt.Errorf("failed to find expected metric for outbound TLS TCP traffic\n%s", metrics)
241 }
242 return nil
243 }
244
View as plain text