Преглед изворни кода

Add tags for ip blocklisted metric

tags/v2.1.6^2
9seconds пре 4 година
родитељ
комит
534d5b755e
7 измењених фајлова са 68 додато и 16 уклоњено
  1. 16
    2
      mtglib/events.go
  2. 9
    0
      mtglib/events_test.go
  3. 1
    1
      mtglib/proxy.go
  4. 14
    9
      stats/prometheus.go
  5. 12
    1
      stats/prometheus_test.go
  6. 7
    2
      stats/statsd.go
  7. 9
    1
      stats/statsd_test.go

+ 16
- 2
mtglib/events.go Прегледај датотеку

@@ -83,7 +83,8 @@ type EventConcurrencyLimited struct {
83 83
 type EventIPBlocklisted struct {
84 84
 	eventBase
85 85
 
86
-	RemoteIP net.IP
86
+	RemoteIP    net.IP
87
+	IsBlockList bool
87 88
 }
88 89
 
89 90
 // EventReplayAttack is emitted when mtg detects a replay attack on a
@@ -172,7 +173,20 @@ func NewEventIPBlocklisted(remoteIP net.IP) EventIPBlocklisted {
172 173
 		eventBase: eventBase{
173 174
 			timestamp: time.Now(),
174 175
 		},
175
-		RemoteIP: remoteIP,
176
+		RemoteIP:    remoteIP,
177
+		IsBlockList: true,
178
+	}
179
+}
180
+
181
+// NewEventIPAllowlisted creates a NewEventIPBlocklisted event with a mark that
182
+// it is supposed to be for allow list.
183
+func NewEventIPAllowlisted(remoteIP net.IP) EventIPBlocklisted {
184
+	return EventIPBlocklisted{
185
+		eventBase: eventBase{
186
+			timestamp: time.Now(),
187
+		},
188
+		RemoteIP:    remoteIP,
189
+		IsBlockList: false,
176 190
 	}
177 191
 }
178 192
 

+ 9
- 0
mtglib/events_test.go Прегледај датотеку

@@ -60,6 +60,15 @@ func (suite *EventsTestSuite) TestEventIPBlocklisted() {
60 60
 
61 61
 	suite.Empty(evt.StreamID())
62 62
 	suite.WithinDuration(time.Now(), evt.Timestamp(), 10*time.Millisecond)
63
+	suite.True(evt.IsBlockList)
64
+}
65
+
66
+func (suite *EventsTestSuite) TestEventIPAllowlisted() {
67
+	evt := mtglib.NewEventIPAllowlisted(net.ParseIP("10.0.0.10"))
68
+
69
+	suite.Empty(evt.StreamID())
70
+	suite.WithinDuration(time.Now(), evt.Timestamp(), 10*time.Millisecond)
71
+	suite.False(evt.IsBlockList)
63 72
 }
64 73
 
65 74
 func (suite *EventsTestSuite) TestEventReplayAttack() {

+ 1
- 1
mtglib/proxy.go Прегледај датотеку

@@ -112,7 +112,7 @@ func (p *Proxy) Serve(listener net.Listener) error {
112 112
 		if !p.allowlist.Contains(ipAddr) {
113 113
 			conn.Close()
114 114
 			logger.Info("ip was rejected by allowlist")
115
-			p.eventStream.Send(p.ctx, NewEventIPBlocklisted(ipAddr))
115
+			p.eventStream.Send(p.ctx, NewEventIPAllowlisted(ipAddr))
116 116
 
117 117
 			continue
118 118
 		}

+ 14
- 9
stats/prometheus.go Прегледај датотеку

@@ -110,8 +110,13 @@ func (p prometheusProcessor) EventConcurrencyLimited(_ mtglib.EventConcurrencyLi
110 110
 	p.factory.metricConcurrencyLimited.Inc()
111 111
 }
112 112
 
113
-func (p prometheusProcessor) EventIPBlocklisted(_ mtglib.EventIPBlocklisted) {
114
-	p.factory.metricIPBlocklisted.Inc()
113
+func (p prometheusProcessor) EventIPBlocklisted(evt mtglib.EventIPBlocklisted) {
114
+	tag := TagIPListBlock
115
+	if !evt.IsBlockList {
116
+		tag = TagIPListAllow
117
+	}
118
+
119
+	p.factory.metricIPBlocklisted.WithLabelValues(tag).Inc()
115 120
 }
116 121
 
117 122
 func (p prometheusProcessor) EventReplayAttack(_ mtglib.EventReplayAttack) {
@@ -150,10 +155,10 @@ type PrometheusFactory struct {
150 155
 
151 156
 	metricTelegramTraffic       *prometheus.CounterVec
152 157
 	metricDomainFrontingTraffic *prometheus.CounterVec
158
+	metricIPBlocklisted         *prometheus.CounterVec
153 159
 
154 160
 	metricDomainFronting     prometheus.Counter
155 161
 	metricConcurrencyLimited prometheus.Counter
156
-	metricIPBlocklisted      prometheus.Counter
157 162
 	metricReplayAttacks      prometheus.Counter
158 163
 }
159 164
 
@@ -223,6 +228,11 @@ func NewPrometheus(metricPrefix, httpPath string) *PrometheusFactory { // nolint
223 228
 			Name:      MetricDomainFrontingTraffic,
224 229
 			Help:      "Traffic which is generated talking with front domain.",
225 230
 		}, []string{TagDirection}),
231
+		metricIPBlocklisted: prometheus.NewCounterVec(prometheus.CounterOpts{
232
+			Namespace: metricPrefix,
233
+			Name:      MetricIPBlocklisted,
234
+			Help:      "A number of rejected sessions due to ip blocklisting.",
235
+		}, []string{TagIPList}),
226 236
 
227 237
 		metricDomainFronting: prometheus.NewCounter(prometheus.CounterOpts{
228 238
 			Namespace: metricPrefix,
@@ -234,11 +244,6 @@ func NewPrometheus(metricPrefix, httpPath string) *PrometheusFactory { // nolint
234 244
 			Name:      MetricConcurrencyLimited,
235 245
 			Help:      "A number of sessions that were rejected by concurrency limiter.",
236 246
 		}),
237
-		metricIPBlocklisted: prometheus.NewCounter(prometheus.CounterOpts{
238
-			Namespace: metricPrefix,
239
-			Name:      MetricIPBlocklisted,
240
-			Help:      "A number of rejected sessions due to ip blocklisting.",
241
-		}),
242 247
 		metricReplayAttacks: prometheus.NewCounter(prometheus.CounterOpts{
243 248
 			Namespace: metricPrefix,
244 249
 			Name:      MetricReplayAttacks,
@@ -253,10 +258,10 @@ func NewPrometheus(metricPrefix, httpPath string) *PrometheusFactory { // nolint
253 258
 
254 259
 	registry.MustRegister(factory.metricTelegramTraffic)
255 260
 	registry.MustRegister(factory.metricDomainFrontingTraffic)
261
+	registry.MustRegister(factory.metricIPBlocklisted)
256 262
 
257 263
 	registry.MustRegister(factory.metricDomainFronting)
258 264
 	registry.MustRegister(factory.metricConcurrencyLimited)
259
-	registry.MustRegister(factory.metricIPBlocklisted)
260 265
 	registry.MustRegister(factory.metricReplayAttacks)
261 266
 
262 267
 	return factory

+ 12
- 1
stats/prometheus_test.go Прегледај датотеку

@@ -156,7 +156,18 @@ func (suite *PrometheusTestSuite) TestEventIPBlocklisted() {
156 156
 
157 157
 	data, err := suite.Get()
158 158
 	suite.NoError(err)
159
-	suite.Contains(data, `mtg_ip_blocklisted 1`)
159
+	suite.Contains(data, `mtg_ip_blocklisted{ip_list="blocklist"} 1`)
160
+}
161
+
162
+func (suite *PrometheusTestSuite) TestEventIPAllowlisted() {
163
+	suite.prometheus.EventIPBlocklisted(
164
+		mtglib.NewEventIPAllowlisted(net.ParseIP("2001:db8::68")))
165
+
166
+	time.Sleep(100 * time.Millisecond)
167
+
168
+	data, err := suite.Get()
169
+	suite.NoError(err)
170
+	suite.Contains(data, `mtg_ip_blocklisted{ip_list="allowlist"} 1`)
160 171
 }
161 172
 
162 173
 func (suite *PrometheusTestSuite) TestEventReplayAttack() {

+ 7
- 2
stats/statsd.go Прегледај датотеку

@@ -113,8 +113,13 @@ func (s statsdProcessor) EventConcurrencyLimited(_ mtglib.EventConcurrencyLimite
113 113
 	s.client.Incr(MetricConcurrencyLimited, 1)
114 114
 }
115 115
 
116
-func (s statsdProcessor) EventIPBlocklisted(_ mtglib.EventIPBlocklisted) {
117
-	s.client.Incr(MetricIPBlocklisted, 1)
116
+func (s statsdProcessor) EventIPBlocklisted(evt mtglib.EventIPBlocklisted) {
117
+	tag := TagIPListBlock
118
+	if !evt.IsBlockList {
119
+		tag = TagIPListAllow
120
+	}
121
+
122
+	s.client.Incr(MetricIPBlocklisted, 1, statsd.StringTag(TagIPList, tag))
118 123
 }
119 124
 
120 125
 func (s statsdProcessor) EventReplayAttack(_ mtglib.EventReplayAttack) {

+ 9
- 1
stats/statsd_test.go Прегледај датотеку

@@ -186,7 +186,15 @@ func (suite *StatsdTestSuite) TestEventIPBlocklisted() {
186 186
 		mtglib.NewEventIPBlocklisted(net.ParseIP("10.0.0.10")))
187 187
 
188 188
 	time.Sleep(statsdSleepTime)
189
-	suite.Equal("mtg.ip_blocklisted:1|c", suite.statsdServer.String())
189
+	suite.Equal("mtg.ip_blocklisted:1|c|#ip_list:blocklist", suite.statsdServer.String())
190
+}
191
+
192
+func (suite *StatsdTestSuite) TestEventIPAllowlisted() {
193
+	suite.statsd.EventIPBlocklisted(
194
+		mtglib.NewEventIPAllowlisted(net.ParseIP("10.0.0.10")))
195
+
196
+	time.Sleep(statsdSleepTime)
197
+	suite.Equal("mtg.ip_blocklisted:1|c|#ip_list:allowlist", suite.statsdServer.String())
190 198
 }
191 199
 
192 200
 func (suite *StatsdTestSuite) TestEventReplayAttack() {

Loading…
Откажи
Сачувај