/
config.py
273 lines (227 loc) · 9.16 KB
/
config.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import os
import copy
import shutil
import datetime
import tempfile
from pathlib import Path
from dateutil.parser import parse as parse_datetime
from flask import request
from flask_restx import Resource, abort
from flask import current_app as ca
from dateutil.tz import tzlocal
from mindsdb.api.http.namespaces.configs.config import ns_conf
from mindsdb.api.http.utils import http_error
from mindsdb.metrics.metrics import api_endpoint_metrics
from mindsdb.utilities import log
from mindsdb.utilities.functions import encrypt, decrypt
from mindsdb.utilities.log_controller import get_logs
from mindsdb.utilities.config import Config
logger = log.getLogger(__name__)
@ns_conf.route('/logs')
@ns_conf.param('name', 'Get logs')
class GetLogs(Resource):
@ns_conf.doc('get_integrations')
@api_endpoint_metrics('GET', '/config/logs')
def get(self):
min_timestamp = parse_datetime(request.args['min_timestamp'])
max_timestamp = request.args.get('max_timestamp', None)
context = request.args.get('context', None)
level = request.args.get('level', None)
log_from = request.args.get('log_from', None)
limit = request.args.get('limit', None)
logs = get_logs(min_timestamp, max_timestamp, context, level, log_from, limit)
return {'data': logs}
@ns_conf.route('/')
@ns_conf.param('name', 'Get config')
class GetConfig(Resource):
@ns_conf.doc('get_config')
@api_endpoint_metrics('GET', '/config')
def get(self):
config = Config()
return {
'auth': {
'http_auth_enabled': config['auth']['http_auth_enabled'],
'username': config['auth']['username'],
'password': config['auth']['password']
}
}
@ns_conf.doc('put_config')
@api_endpoint_metrics('PUT', '/config')
def put(self):
data = request.json
unknown_argumens = list(set(data.keys()) - {'auth'})
if len(unknown_argumens) > 0:
return http_error(
400, 'Wrong arguments',
f'Unknown argumens: {unknown_argumens}'
)
for key in data.keys():
unknown_argumens = list(
set(data[key].keys()) - set(Config()[key].keys())
)
if len(unknown_argumens) > 0:
return http_error(
400, 'Wrong arguments',
f'Unknown argumens: {unknown_argumens}'
)
Config().update(data)
return '', 200
@ns_conf.route('/integrations')
@ns_conf.param('name', 'List all database integration')
class ListIntegration(Resource):
@api_endpoint_metrics('GET', '/config/integrations')
def get(self):
return {
'integrations': [k for k in ca.integration_controller.get_all(sensitive_info=False)]
}
@ns_conf.route('/all_integrations')
@ns_conf.param('name', 'List all database integration')
class AllIntegration(Resource):
@ns_conf.doc('get_all_integrations')
@api_endpoint_metrics('GET', '/config/all_integrations')
def get(self):
integrations = ca.integration_controller.get_all(sensitive_info=False)
return integrations
@ns_conf.route('/integrations/<name>')
@ns_conf.param('name', 'Database integration')
class Integration(Resource):
@ns_conf.doc('get_integration')
@api_endpoint_metrics('GET', '/config/integrations/integration')
def get(self, name):
integration = ca.integration_controller.get(name, sensitive_info=False)
if integration is None:
abort(404, f'Can\'t find database integration: {name}')
integration = copy.deepcopy(integration)
return integration
@ns_conf.doc('put_integration')
@api_endpoint_metrics('PUT', '/config/integrations/integration')
def put(self, name):
params = {}
if request.is_json:
params.update((request.json or {}).get('params', {}))
else:
params.update(request.form or {})
if len(params) == 0:
abort(400, "type of 'params' must be dict")
files = request.files
temp_dir = None
if files is not None and len(files) > 0:
temp_dir = tempfile.mkdtemp(prefix='integration_files_')
for key, file in files.items():
temp_dir_path = Path(temp_dir)
file_name = Path(file.filename)
file_path = temp_dir_path.joinpath(file_name).resolve()
if temp_dir_path not in file_path.parents:
raise Exception(f'Can not save file at path: {file_path}')
file.save(file_path)
params[key] = str(file_path)
is_test = params.get('test', False)
config = Config()
secret_key = config.get('secret_key', 'dummy-key')
if is_test:
del params['test']
handler_type = params.pop('type', None)
params.pop('publish', None)
handler = ca.integration_controller.create_tmp_handler(
handler_type=handler_type,
connection_data=params
)
status = handler.check_connection()
if temp_dir is not None:
shutil.rmtree(temp_dir)
resp = status.to_json()
if status.success and 'code' in params:
if hasattr(handler, 'handler_storage'):
# attach storage if exists
export = handler.handler_storage.export_files()
if export:
# encrypt with flask secret key
encrypted = encrypt(export, secret_key)
resp['storage'] = encrypted.decode()
return resp, 200
integration = ca.integration_controller.get(name, sensitive_info=False)
if integration is not None:
abort(400, f"Integration with name '{name}' already exists")
try:
engine = params['type']
if engine is not None:
del params['type']
params.pop('publish', False)
storage = params.pop('storage', None)
ca.integration_controller.add(name, engine, params)
# copy storage
if storage is not None:
handler = ca.integration_controller.get_data_handler(name)
export = decrypt(storage.encode(), secret_key)
handler.handler_storage.import_files(export)
except Exception as e:
logger.error(str(e))
if temp_dir is not None:
shutil.rmtree(temp_dir)
abort(500, f'Error during config update: {str(e)}')
if temp_dir is not None:
shutil.rmtree(temp_dir)
return {}, 200
@ns_conf.doc('delete_integration')
@api_endpoint_metrics('DELETE', '/config/integrations/integration')
def delete(self, name):
integration = ca.integration_controller.get(name)
if integration is None:
abort(400, f"Nothing to delete. '{name}' not exists.")
try:
ca.integration_controller.delete(name)
except Exception as e:
logger.error(str(e))
abort(500, f"Error during integration delete: {str(e)}")
return "", 200
@ns_conf.doc('modify_integration')
@api_endpoint_metrics('POST', '/config/integrations/integration')
def post(self, name):
params = {}
params.update((request.json or {}).get('params', {}))
params.update(request.form or {})
if not isinstance(params, dict):
abort(400, "type of 'params' must be dict")
integration = ca.integration_controller.get(name)
if integration is None:
abort(400, f"Nothin to modify. '{name}' not exists.")
try:
if 'enabled' in params:
params['publish'] = params['enabled']
del params['enabled']
ca.integration_controller.modify(name, params)
except Exception as e:
logger.error(str(e))
abort(500, f"Error during integration modifycation: {str(e)}")
return "", 200
@ns_conf.route('/integrations/<name>/check')
@ns_conf.param('name', 'Database integration checks')
class Check(Resource):
@ns_conf.doc('check')
@api_endpoint_metrics('GET', '/config/integrations/integration/check')
def get(self, name):
if ca.integration_controller.get(name) is None:
abort(404, f'Can\'t find database integration: {name}')
connections = ca.integration_controller.check_connections()
return connections.get(name, False), 200
@ns_conf.route('/vars')
class Vars(Resource):
@api_endpoint_metrics('GET', '/config/vars')
def get(self):
if os.getenv('CHECK_FOR_UPDATES', '1').lower() in ['0', 'false']:
telemtry = False
else:
telemtry = True
if ca.config_obj.get('disable_mongo', False):
mongo = False
else:
mongo = True
cloud = ca.config_obj.get('cloud', False)
local_time = datetime.datetime.now(tzlocal())
local_timezone = local_time.tzname()
return {
'mongo': mongo,
'telemtry': telemtry,
'cloud': cloud,
'timezone': local_timezone,
}