1919from rest_framework import status
2020from rest_framework .test import APIClient
2121
22+ from aap_eda .api .views .external_event_stream import (
23+ REDACTED_STRING ,
24+ UNSAFE_HEADER_KEYS ,
25+ )
2226from aap_eda .core import enums
2327from tests .integration .api .test_event_stream import (
2428 create_event_stream ,
@@ -80,47 +84,160 @@ def test_post_event_stream_with_token(
8084 assert response .status_code == auth_status
8185
8286
87+ BASE_HEADERS = {
88+ "X-Gitlab-Event-Uuid" : "c2675c66-7e6e-4fe2-9ac3-288534ef34b9" ,
89+ "X-Gitlab-Instance" : "https://gitlab.com" ,
90+ "X-Gitlab-Token" : secrets .token_hex (32 ),
91+ "X-Gitlab-Uuid" : "b697868f-3b59-4a1f-985d-47f79e2b05ff" ,
92+ "X-Gitlab-Event" : "Push Hook" ,
93+ "X-Envoy-abc" : "abc" ,
94+ "X-Trusted-Proxy" : "gobbledegook" ,
95+ "X-Forwarded-For" : "fred" ,
96+ "X-Real-IP" : "barney" ,
97+ }
98+
99+
100+ @pytest .mark .parametrize (
101+ ("test_args" ),
102+ [
103+ (
104+ {
105+ "auth_header" : "X-Gitlab-Token" ,
106+ "additional_data_headers" : (
107+ "x-gitlab-event, x-gitlab-event-uuid , x-gitlab-uuid"
108+ ),
109+ "headers" : BASE_HEADERS ,
110+ "required_header_keys" : [
111+ "X-Gitlab-Event" ,
112+ "X-Gitlab-Event-Uuid" ,
113+ "X-Gitlab-Uuid" ,
114+ ],
115+ "keys_should_not_exist" : list (UNSAFE_HEADER_KEYS )
116+ + [
117+ "X-Envoy-abc" ,
118+ "X-Gitlab-Instance" ,
119+ "X-Gitlab-Token" ,
120+ ],
121+ "redacted" : True ,
122+ "key_remap" : {
123+ "X-Gitlab-Event" : "x-gitlab-event" ,
124+ "X-Gitlab-Event-Uuid" : "x-gitlab-event-uuid" ,
125+ "X-Gitlab-Uuid" : "x-gitlab-uuid" ,
126+ },
127+ "test_name" : "lowercase data headers with extra spaces" ,
128+ }
129+ ),
130+ (
131+ {
132+ "auth_header" : "X-Gitlab-Token" ,
133+ "additional_data_headers" : (
134+ "X-Gitlab-Event, X-Gitlab-Event-Uuid, X-Gitlab-Uuid"
135+ ),
136+ "headers" : BASE_HEADERS ,
137+ "required_header_keys" : [
138+ "X-Gitlab-Event" ,
139+ "X-Gitlab-Event-Uuid" ,
140+ "X-Gitlab-Uuid" ,
141+ ],
142+ "keys_should_not_exist" : list (UNSAFE_HEADER_KEYS )
143+ + [
144+ "X-Envoy-abc" ,
145+ "X-Gitlab-Instance" ,
146+ "X-Gitlab-Token" ,
147+ ],
148+ "redacted" : True ,
149+ "key_remap" : {},
150+ "test_name" : "data headers with extra spaces" ,
151+ }
152+ ),
153+ (
154+ {
155+ "auth_header" : "X-Gitlab-Token" ,
156+ "additional_data_headers" : " X-Gitlab-Event " ,
157+ "headers" : BASE_HEADERS ,
158+ "required_header_keys" : ["X-Gitlab-Event" ],
159+ "keys_should_not_exist" : list (UNSAFE_HEADER_KEYS )
160+ + [
161+ "X-Gitlab-Event-Uuid" ,
162+ "X-Gitlab-Uuid" ,
163+ "X-Gitlab-Token" ,
164+ "X-Gitlab-Instance" ,
165+ ],
166+ "redacted" : True ,
167+ "key_remap" : {},
168+ "test_name" : "single data headers with surrounding spaces" ,
169+ }
170+ ),
171+ (
172+ {
173+ "auth_header" : "X-Gitlab-Token" ,
174+ "additional_data_headers" : " X-Gitlab-Token " ,
175+ "headers" : BASE_HEADERS ,
176+ "required_header_keys" : ["X-Gitlab-Token" ],
177+ "keys_should_not_exist" : list (UNSAFE_HEADER_KEYS )
178+ + [
179+ "X-Envoy-abc" ,
180+ "X-Gitlab-Event-Uuid" ,
181+ "X-Gitlab-Uuid" ,
182+ "X-Gitlab-Instance" ,
183+ "X-Gitlab-Event" ,
184+ ],
185+ "redacted" : False ,
186+ "key_remap" : {},
187+ "test_name" : "single data header with exposed auth_header" ,
188+ }
189+ ),
190+ (
191+ {
192+ "auth_header" : "X-Gitlab-Token" ,
193+ "additional_data_headers" : "*" ,
194+ "headers" : BASE_HEADERS ,
195+ "required_header_keys" : [
196+ "X-Gitlab-Event" ,
197+ "X-Gitlab-Event-Uuid" ,
198+ "X-Gitlab-Instance" ,
199+ "X-Gitlab-Uuid" ,
200+ "X-Gitlab-Token" ,
201+ ],
202+ "keys_should_not_exist" : list (UNSAFE_HEADER_KEYS )
203+ + ["X-Envoy-abc" ],
204+ "redacted" : True ,
205+ "key_remap" : {},
206+ "test_name" : "wild card data header" ,
207+ }
208+ ),
209+ ],
210+ )
83211@pytest .mark .django_db
84212def test_post_event_stream_with_test_mode_extra_headers (
85213 admin_client : APIClient ,
86214 preseed_credential_types ,
215+ test_args ,
87216):
88- secret = secrets .token_hex (32 )
89- signature_header_name = "X-Gitlab-Token"
217+ auth_header = test_args ["auth_header" ]
90218 inputs = {
91219 "auth_type" : "token" ,
92- "token" : secret ,
93- "http_header_key" : signature_header_name ,
220+ "token" : test_args [ "headers" ][ auth_header ] ,
221+ "http_header_key" : auth_header ,
94222 }
95223
96224 obj = create_event_stream_credential (
97225 admin_client , enums .EventStreamCredentialType .TOKEN .value , inputs
98226 )
99227
100- additional_data_headers = (
101- "X-Gitlab-Event,X-Gitlab-Event-Uuid,X-Gitlab-Uuid"
102- )
103228 data_in = {
104229 "name" : "test-es-1" ,
105230 "eda_credential_id" : obj ["id" ],
106231 "event_stream_type" : obj ["credential_type" ]["kind" ],
107232 "organization_id" : get_default_test_org ().id ,
108233 "test_mode" : True ,
109- "additional_data_headers" : additional_data_headers ,
234+ "additional_data_headers" : test_args [ " additional_data_headers" ] ,
110235 }
111236 event_stream = create_event_stream (admin_client , data_in )
112237 data = {"a" : 1 , "b" : 2 }
113- headers = {
114- "X-Gitlab-Event-Uuid" : "c2675c66-7e6e-4fe2-9ac3-288534ef34b9" ,
115- "X-Gitlab-Instance" : "https://gitlab.com" ,
116- signature_header_name : secret ,
117- "X-Gitlab-Uuid" : "b697868f-3b59-4a1f-985d-47f79e2b05ff" ,
118- "X-Gitlab-Event" : "Push Hook" ,
119- }
120-
121238 response = admin_client .post (
122239 event_stream_post_url (event_stream .uuid ),
123- headers = headers ,
240+ headers = test_args [ " headers" ] ,
124241 data = data ,
125242 )
126243 assert response .status_code == status .HTTP_200_OK
@@ -129,15 +246,21 @@ def test_post_event_stream_with_test_mode_extra_headers(
129246 test_data = yaml .safe_load (event_stream .test_content )
130247 assert test_data ["a" ] == 1
131248 assert test_data ["b" ] == 2
249+
132250 test_headers = yaml .safe_load (event_stream .test_headers )
133- assert (
134- test_headers ["X-Gitlab-Event-Uuid" ]
135- == "c2675c66-7e6e-4fe2-9ac3-288534ef34b9"
136- )
137- assert (
138- test_headers ["X-Gitlab-Uuid" ] == "b697868f-3b59-4a1f-985d-47f79e2b05ff"
139- )
140- assert test_headers ["X-Gitlab-Event" ] == "Push Hook"
251+
252+ for key in test_args ["required_header_keys" ]:
253+ if key == auth_header and test_args ["redacted" ]:
254+ assert test_headers [key ] == REDACTED_STRING
255+ else :
256+ assert (
257+ test_headers [test_args ["key_remap" ].get (key , key )]
258+ == test_args ["headers" ][key ]
259+ )
260+
261+ for key in test_args ["keys_should_not_exist" ]:
262+ assert key not in test_headers
263+
141264 assert event_stream .test_content_type == "application/json"
142265 assert event_stream .events_received == 1
143266 assert event_stream .last_event_received_at is not None
0 commit comments