Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ratelimiter_test.go « acceptance « test - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6f0fd6567cf1eec6b5f39f11a95310798cf80634 (plain)
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
package acceptance_test

import (
	"fmt"
	"net/http"
	"testing"
	"time"

	"github.com/stretchr/testify/require"
)

var ratelimitedListeners = map[string]struct {
	listener ListenSpec
	header   http.Header
	clientIP string
}{
	"http_listener": {
		listener: httpListener,
		clientIP: "127.0.0.1",
	},
	"https_listener": {
		listener: httpsListener,
		clientIP: "127.0.0.1",
	},
	"proxy_listener": {
		listener: proxyListener,
		header: http.Header{
			"X-Forwarded-For":  []string{"172.16.123.1"},
			"X-Forwarded-Host": []string{"group.gitlab-example.com"},
		},
		clientIP: "172.16.123.1",
	},
	"proxyv2_listener": {
		listener: httpsProxyv2Listener,
		clientIP: "10.1.1.1",
	},
}

func TestIPRateLimits(t *testing.T) {
	for name, tc := range ratelimitedListeners {
		t.Run(name, func(t *testing.T) {
			rateLimit := 5
			logBuf := RunPagesProcess(t,
				withListeners([]ListenSpec{tc.listener}),
				withExtraArgument("rate-limit-source-ip", fmt.Sprint(rateLimit)),
				withExtraArgument("rate-limit-source-ip-burst", fmt.Sprint(rateLimit)),
			)

			for i := 0; i < 10; i++ {
				rsp, err := GetPageFromListenerWithHeaders(t, tc.listener, "group.gitlab-example.com", "project/", tc.header)
				require.NoError(t, err)
				require.NoError(t, rsp.Body.Close())

				if i >= rateLimit {
					require.Equal(t, http.StatusTooManyRequests, rsp.StatusCode, "group.gitlab-example.com request: %d failed", i)
					assertLogFound(t, logBuf, []string{"request hit rate limit", "\"source_ip\":\"" + tc.clientIP + "\""})
				} else {
					require.Equal(t, http.StatusOK, rsp.StatusCode, "request: %d failed", i)
				}
			}
		})
	}
}

func TestDomainRateLimits(t *testing.T) {
	for name, tc := range ratelimitedListeners {
		t.Run(name, func(t *testing.T) {
			rateLimit := 5
			logBuf := RunPagesProcess(t,
				withListeners([]ListenSpec{tc.listener}),
				withExtraArgument("rate-limit-domain", fmt.Sprint(rateLimit)),
				withExtraArgument("rate-limit-domain-burst", fmt.Sprint(rateLimit)),
			)

			for i := 0; i < 10; i++ {
				rsp, err := GetPageFromListenerWithHeaders(t, tc.listener, "group.gitlab-example.com", "project/", tc.header)
				require.NoError(t, err)
				require.NoError(t, rsp.Body.Close())

				if i >= rateLimit {
					require.Equal(t, http.StatusTooManyRequests, rsp.StatusCode, "group.gitlab-example.com request: %d failed", i)
					assertLogFound(t, logBuf, []string{"request hit rate limit", "\"source_ip\":\"" + tc.clientIP + "\""})
				} else {
					require.Equal(t, http.StatusOK, rsp.StatusCode, "request: %d failed", i)
				}
			}

			// make sure that requests to other domains are passing
			rsp, err := GetPageFromListener(t, tc.listener, "CapitalGroup.gitlab-example.com", "project/")
			require.NoError(t, err)
			require.NoError(t, rsp.Body.Close())

			require.Equal(t, http.StatusOK, rsp.StatusCode, "request to unrelated domain failed")
		})
	}
}

func TestTLSRateLimits(t *testing.T) {
	tests := map[string]struct {
		spec        ListenSpec
		domainLimit bool
		sourceIP    string
	}{
		"https_with_domain_limit": {
			spec:        httpsListener,
			domainLimit: true,
			sourceIP:    "127.0.0.1",
		},
		"https_with_ip_limit": {
			spec:     httpsListener,
			sourceIP: "127.0.0.1",
		},
		"proxyv2_with_domain_limit": {
			spec:        httpsProxyv2Listener,
			domainLimit: true,
			sourceIP:    "10.1.1.1",
		},
		"proxyv2_with_ip_limit": {
			spec:     httpsProxyv2Listener,
			sourceIP: "10.1.1.1",
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			rateLimit := 5

			options := []processOption{
				withListeners([]ListenSpec{tt.spec}),
				withExtraArgument("metrics-address", ":42345"),
			}

			limitName := "tls_connections_by_source_ip"

			if tt.domainLimit {
				options = append(options,
					withExtraArgument("rate-limit-tls-domain", fmt.Sprint(rateLimit)),
					withExtraArgument("rate-limit-tls-domain-burst", fmt.Sprint(rateLimit)))

				limitName = "tls_connections_by_domain"
			} else {
				options = append(options,
					withExtraArgument("rate-limit-tls-source-ip", fmt.Sprint(rateLimit)),
					withExtraArgument("rate-limit-tls-source-ip-burst", fmt.Sprint(rateLimit)))
			}

			logBuf := RunPagesProcess(t, options...)

			// when we start the process we make 1 requests to verify that process is up
			// it gets counted in the rate limit for IP, but host is different
			if !tt.domainLimit {
				rateLimit--
			}

			for i := 0; i < 10; i++ {
				rsp, err := makeTLSRequest(t, tt.spec)

				if i >= rateLimit {
					assertLogFound(t, logBuf, []string{
						"TLS connection rate-limited",
						"\"req_host\":\"group.gitlab-example.com\"",
						fmt.Sprintf("\"source_ip\":\"%s\"", tt.sourceIP)})

					require.Error(t, err)
					require.Contains(t, err.Error(), "remote error: tls: internal error")

					continue
				}

				require.NoError(t, err, "request: %d failed", i)
				require.NoError(t, rsp.Body.Close())
				require.Equal(t, http.StatusOK, rsp.StatusCode, "request: %d failed", i)
			}
			expectedMetric := fmt.Sprintf(
				"gitlab_pages_rate_limit_blocked_count{limit_name=\"%s\"} %v",
				limitName, 10-rateLimit)

			RequireMetricEqual(t, "127.0.0.1:42345", expectedMetric)
		})
	}
}

func makeTLSRequest(t *testing.T, spec ListenSpec) (*http.Response, error) {
	req, err := http.NewRequest("GET", "https://group.gitlab-example.com/project", nil)
	require.NoError(t, err)

	return spec.Client().Do(req)
}

func assertLogFound(t *testing.T, logBuf *LogCaptureBuffer, expectedLogs []string) {
	t.Helper()

	// give the process enough time to write the log message
	require.Eventually(t, func() bool {
		for _, e := range expectedLogs {
			require.Contains(t, logBuf.String(), e, "log mismatch")
		}
		return true
	}, 100*time.Millisecond, 10*time.Millisecond)
}