-
-
Notifications
You must be signed in to change notification settings - Fork 181
/
service_definition_interface.py
240 lines (210 loc) · 7.77 KB
/
service_definition_interface.py
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
229
230
231
232
233
234
235
236
237
238
239
240
import json
import os
import re
from abc import ABCMeta, abstractmethod
import constance
import requests
from ssrf_protect.ssrf_protect import SSRFProtect, SSRFProtectException
from kpi.utils.log import logging
from kpi.utils.strings import split_lines_to_list
from .hook import Hook
from .hook_log import HookLog
from ..constants import (
HOOK_LOG_FAILED,
HOOK_LOG_SUCCESS,
KOBO_INTERNAL_ERROR_STATUS_CODE,
RETRIABLE_STATUS_CODES,
)
from ..exceptions import HookRemoteServerDownError
class ServiceDefinitionInterface(metaclass=ABCMeta):
def __init__(self, hook, submission_id):
self._hook = hook
self._submission_id = submission_id
self._data = self._get_data()
def _get_data(self):
"""
Retrieves data from deployment backend of the asset.
"""
try:
submission = self._hook.asset.deployment.get_submission(
self._submission_id,
user=self._hook.asset.owner,
format_type=self._hook.export_type,
)
return self._parse_data(submission, self._hook.subset_fields)
except Exception as e:
logging.error(
'service_json.ServiceDefinition._get_data: '
f'Hook #{self._hook.uid} - Data #{self._submission_id} - '
f'{str(e)}',
exc_info=True,
)
return None
@abstractmethod
def _parse_data(self, submission, fields):
"""
Data must be parsed to include only `self._hook.subset_fields` if there are any.
:param submission: json|xml
:param fields: list
:return: mixed: json|xml
"""
if len(fields) > 0:
pass
return submission
@abstractmethod
def _prepare_request_kwargs(self):
"""
Prepares params to pass to `Requests.post` in `send` method.
It defines headers and data.
For example:
{
"headers": {"Content-Type": "application/json"},
"json": self._data
}
:return: dict
"""
pass
def send(self) -> bool:
"""
Sends data to external endpoint.
Raise an exception if something is wrong. Retries are only allowed
when `HookRemoteServerDownError` is raised.
"""
if not self._data:
self.save_log(
KOBO_INTERNAL_ERROR_STATUS_CODE,
'Submission has been deleted',
allow_retries=False,
)
return False
# Need to declare response before requests.post assignment in case of
# RequestException
response = None
try:
request_kwargs = self._prepare_request_kwargs()
# Add custom headers
request_kwargs.get('headers').update(
self._hook.settings.get('custom_headers', {})
)
# Add user agent
public_domain = (
'- {} '.format(os.getenv('PUBLIC_DOMAIN_NAME'))
if os.getenv('PUBLIC_DOMAIN_NAME')
else ''
)
request_kwargs.get('headers').update(
{
'User-Agent': 'KoboToolbox external service {}#{}'.format(
public_domain, self._hook.uid
)
}
)
# If the request needs basic authentication with username and
# password, let's provide them
if self._hook.auth_level == Hook.BASIC_AUTH:
request_kwargs.update(
{
'auth': (
self._hook.settings.get('username'),
self._hook.settings.get('password'),
)
}
)
ssrf_protect_options = {}
if constance.config.SSRF_ALLOWED_IP_ADDRESS.strip():
ssrf_protect_options['allowed_ip_addresses'] = (
split_lines_to_list(
constance.config.SSRF_ALLOWED_IP_ADDRESS
)
)
if constance.config.SSRF_DENIED_IP_ADDRESS.strip():
ssrf_protect_options['denied_ip_addresses'] = (
split_lines_to_list(
constance.config.SSRF_DENIED_IP_ADDRESS
)
)
SSRFProtect.validate(self._hook.endpoint, options=ssrf_protect_options)
response = requests.post(self._hook.endpoint, timeout=30, **request_kwargs)
response.raise_for_status()
self.save_log(response.status_code, response.text, success=True)
return True
except requests.exceptions.RequestException as e:
# If request fails to communicate with remote server.
# Exception is raised before request.post can return something.
# Thus, response equals None
status_code = KOBO_INTERNAL_ERROR_STATUS_CODE
text = str(e)
if response is not None:
text = response.text
status_code = response.status_code
if status_code in RETRIABLE_STATUS_CODES:
self.save_log(status_code, text, allow_retries=True)
raise HookRemoteServerDownError
self.save_log(status_code, text)
raise
except SSRFProtectException as e:
logging.error(
'service_json.ServiceDefinition.send: '
f'Hook #{self._hook.uid} - '
f'Data #{self._submission_id} - '
f'{str(e)}',
exc_info=True,
)
self.save_log(
KOBO_INTERNAL_ERROR_STATUS_CODE, f'{self._hook.endpoint} is not allowed'
)
raise
except Exception as e:
logging.error(
'service_json.ServiceDefinition.send: '
f'Hook #{self._hook.uid} - '
f'Data #{self._submission_id} - '
f'{str(e)}',
exc_info=True,
)
self.save_log(
KOBO_INTERNAL_ERROR_STATUS_CODE,
'An error occurred when sending '
f'data to external endpoint: {str(e)}',
)
raise
def save_log(
self,
status_code: int,
message: str,
success: bool = False,
allow_retries: bool = False,
):
"""
Updates/creates log entry with:
- `status_code` as the HTTP status code of the remote server response
- `message` as the content of the remote server response
"""
fields = {'hook': self._hook, 'submission_id': self._submission_id}
try:
# Try to load the log with a multiple field FK because
# we don't know the log `uid` in this context, but we do know
# its `hook` FK and its `submission_id`
log = HookLog.objects.get(**fields)
except HookLog.DoesNotExist:
log = HookLog(**fields)
if success:
log.status = HOOK_LOG_SUCCESS
elif not allow_retries or log.tries >= constance.config.HOOK_MAX_RETRIES:
log.status = HOOK_LOG_FAILED
log.status_code = status_code
# We want to clean up HTML, so first, we try to create a json object.
# In case of failure, it should be HTML (or plaintext), we can remove
# tags
try:
json.loads(message)
except ValueError:
message = re.sub(r'<[^>]*>', ' ', message).strip()
log.message = message
try:
log.save()
except Exception as e:
logging.error(
f'ServiceDefinitionInterface.save_log - {str(e)}',
exc_info=True,
)