|
1 | 1 | # -*- coding:utf-8 -*-
|
2 | 2 |
|
3 |
| -import base64 |
4 |
| -import json |
5 | 3 | import sys
|
6 | 4 | import time
|
7 | 5 | from typing import Set
|
8 | 6 |
|
9 |
| -import elasticsearch |
10 | 7 | import redis
|
11 | 8 | import six
|
12 |
| -from Crypto.Cipher import AES |
13 |
| -from elasticsearch import Elasticsearch |
14 | 9 | from flask import current_app
|
15 | 10 |
|
16 | 11 |
|
@@ -118,99 +113,6 @@ def delete(self, key_id, prefix):
|
118 | 113 | current_app.logger.error("delete redis key error, {0}".format(str(e)))
|
119 | 114 |
|
120 | 115 |
|
121 |
| -class ESHandler(object): |
122 |
| - def __init__(self, flask_app=None): |
123 |
| - self.flask_app = flask_app |
124 |
| - self.es = None |
125 |
| - self.index = "cmdb" |
126 |
| - |
127 |
| - def init_app(self, app): |
128 |
| - self.flask_app = app |
129 |
| - config = self.flask_app.config |
130 |
| - if config.get('ES_USER') and config.get('ES_PASSWORD'): |
131 |
| - uri = "http://{}:{}@{}:{}/".format(config.get('ES_USER'), config.get('ES_PASSWORD'), |
132 |
| - config.get('ES_HOST'), config.get('ES_PORT')) |
133 |
| - else: |
134 |
| - uri = "{}:{}".format(config.get('ES_HOST'), config.get('ES_PORT') or 9200) |
135 |
| - self.es = Elasticsearch(uri, |
136 |
| - timeout=10, |
137 |
| - max_retries=3, |
138 |
| - retry_on_timeout=True, |
139 |
| - retry_on_status=(502, 503, 504, "N/A"), |
140 |
| - maxsize=10) |
141 |
| - try: |
142 |
| - if not self.es.indices.exists(index=self.index): |
143 |
| - self.es.indices.create(index=self.index) |
144 |
| - except elasticsearch.exceptions.RequestError as ex: |
145 |
| - if ex.error != 'resource_already_exists_exception': |
146 |
| - raise |
147 |
| - |
148 |
| - def update_mapping(self, field, value_type, other): |
149 |
| - body = { |
150 |
| - "properties": { |
151 |
| - field: {"type": value_type}, |
152 |
| - }} |
153 |
| - body['properties'][field].update(other) |
154 |
| - |
155 |
| - self.es.indices.put_mapping( |
156 |
| - index=self.index, |
157 |
| - body=body |
158 |
| - ) |
159 |
| - |
160 |
| - def get_index_id(self, ci_id): |
161 |
| - try: |
162 |
| - return self._get_index_id(ci_id) |
163 |
| - except: |
164 |
| - return self._get_index_id(ci_id) |
165 |
| - |
166 |
| - def _get_index_id(self, ci_id): |
167 |
| - query = { |
168 |
| - 'query': { |
169 |
| - 'match': {'ci_id': ci_id} |
170 |
| - }, |
171 |
| - } |
172 |
| - res = self.es.search(index=self.index, body=query) |
173 |
| - if res['hits']['hits']: |
174 |
| - return res['hits']['hits'][-1].get('_id') |
175 |
| - |
176 |
| - def create(self, body): |
177 |
| - return self.es.index(index=self.index, body=body).get("_id") |
178 |
| - |
179 |
| - def update(self, ci_id, body): |
180 |
| - _id = self.get_index_id(ci_id) |
181 |
| - |
182 |
| - if _id: |
183 |
| - return self.es.index(index=self.index, id=_id, body=body).get("_id") |
184 |
| - |
185 |
| - def create_or_update(self, ci_id, body): |
186 |
| - try: |
187 |
| - self.update(ci_id, body) or self.create(body) |
188 |
| - except KeyError: |
189 |
| - self.create(body) |
190 |
| - |
191 |
| - def delete(self, ci_id): |
192 |
| - try: |
193 |
| - _id = self.get_index_id(ci_id) |
194 |
| - except KeyError: |
195 |
| - return |
196 |
| - |
197 |
| - if _id: |
198 |
| - self.es.delete(index=self.index, id=_id) |
199 |
| - |
200 |
| - def read(self, query, filter_path=None): |
201 |
| - filter_path = filter_path or [] |
202 |
| - if filter_path: |
203 |
| - filter_path.append('hits.total') |
204 |
| - |
205 |
| - res = self.es.search(index=self.index, body=query, filter_path=filter_path) |
206 |
| - if res['hits'].get('hits'): |
207 |
| - return res['hits']['total']['value'], \ |
208 |
| - [i['_source'] for i in res['hits']['hits']], \ |
209 |
| - res.get("aggregations", {}) |
210 |
| - else: |
211 |
| - return 0, [], {} |
212 |
| - |
213 |
| - |
214 | 116 | class Lock(object):
|
215 | 117 | def __init__(self, name, timeout=10, app=None, need_lock=True):
|
216 | 118 | self.lock_key = name
|
@@ -255,118 +157,3 @@ def __enter__(self):
|
255 | 157 | def __exit__(self, exc_type, exc_val, exc_tb):
|
256 | 158 | if self.need_lock:
|
257 | 159 | self.release()
|
258 |
| - |
259 |
| - |
260 |
| -class Redis2Handler(object): |
261 |
| - def __init__(self, flask_app=None, prefix=None): |
262 |
| - self.flask_app = flask_app |
263 |
| - self.prefix = prefix |
264 |
| - self.r = None |
265 |
| - |
266 |
| - def init_app(self, app): |
267 |
| - self.flask_app = app |
268 |
| - config = self.flask_app.config |
269 |
| - try: |
270 |
| - pool = redis.ConnectionPool( |
271 |
| - max_connections=config.get("REDIS_MAX_CONN"), |
272 |
| - host=config.get("ONEAGENT_REDIS_HOST"), |
273 |
| - port=config.get("ONEAGENT_REDIS_PORT"), |
274 |
| - db=config.get("ONEAGENT_REDIS_DB"), |
275 |
| - password=config.get("ONEAGENT_REDIS_PASSWORD") |
276 |
| - ) |
277 |
| - self.r = redis.Redis(connection_pool=pool) |
278 |
| - except Exception as e: |
279 |
| - current_app.logger.warning(str(e)) |
280 |
| - current_app.logger.error("init redis connection failed") |
281 |
| - |
282 |
| - def get(self, key): |
283 |
| - try: |
284 |
| - value = json.loads(self.r.get(key)) |
285 |
| - except: |
286 |
| - return |
287 |
| - |
288 |
| - return value |
289 |
| - |
290 |
| - def lrange(self, key, start=0, end=-1): |
291 |
| - try: |
292 |
| - value = "".join(map(redis_decode, self.r.lrange(key, start, end) or [])) |
293 |
| - except: |
294 |
| - return |
295 |
| - |
296 |
| - return value |
297 |
| - |
298 |
| - def lrange2(self, key, start=0, end=-1): |
299 |
| - try: |
300 |
| - return list(map(redis_decode, self.r.lrange(key, start, end) or [])) |
301 |
| - except: |
302 |
| - return [] |
303 |
| - |
304 |
| - def llen(self, key): |
305 |
| - try: |
306 |
| - return self.r.llen(key) or 0 |
307 |
| - except: |
308 |
| - return 0 |
309 |
| - |
310 |
| - def hget(self, key, field): |
311 |
| - try: |
312 |
| - return self.r.hget(key, field) |
313 |
| - except Exception as e: |
314 |
| - current_app.logger.warning("hget redis failed, %s" % str(e)) |
315 |
| - return |
316 |
| - |
317 |
| - def hset(self, key, field, value): |
318 |
| - try: |
319 |
| - self.r.hset(key, field, value) |
320 |
| - except Exception as e: |
321 |
| - current_app.logger.warning("hset redis failed, %s" % str(e)) |
322 |
| - return |
323 |
| - |
324 |
| - def expire(self, key, timeout): |
325 |
| - try: |
326 |
| - self.r.expire(key, timeout) |
327 |
| - except Exception as e: |
328 |
| - current_app.logger.warning("expire redis failed, %s" % str(e)) |
329 |
| - return |
330 |
| - |
331 |
| - |
332 |
| -def redis_decode(x): |
333 |
| - try: |
334 |
| - return x.decode() |
335 |
| - except Exception as e: |
336 |
| - print(x, e) |
337 |
| - try: |
338 |
| - return x.decode("gb18030") |
339 |
| - except: |
340 |
| - return "decode failed" |
341 |
| - |
342 |
| - |
343 |
| -class AESCrypto(object): |
344 |
| - BLOCK_SIZE = 16 # Bytes |
345 |
| - pad = lambda s: s + (AESCrypto.BLOCK_SIZE - len(s) % AESCrypto.BLOCK_SIZE) * \ |
346 |
| - chr(AESCrypto.BLOCK_SIZE - len(s) % AESCrypto.BLOCK_SIZE) |
347 |
| - unpad = lambda s: s[:-ord(s[len(s) - 1:])] |
348 |
| - |
349 |
| - iv = '0102030405060708' |
350 |
| - |
351 |
| - @staticmethod |
352 |
| - def key(): |
353 |
| - key = current_app.config.get("SECRET_KEY")[:16] |
354 |
| - if len(key) < 16: |
355 |
| - key = "{}{}".format(key, (16 - len(key) * "x")) |
356 |
| - |
357 |
| - return key.encode('utf8') |
358 |
| - |
359 |
| - @classmethod |
360 |
| - def encrypt(cls, data): |
361 |
| - data = cls.pad(data) |
362 |
| - cipher = AES.new(cls.key(), AES.MODE_CBC, cls.iv.encode('utf8')) |
363 |
| - |
364 |
| - return base64.b64encode(cipher.encrypt(data.encode('utf8'))).decode('utf8') |
365 |
| - |
366 |
| - @classmethod |
367 |
| - def decrypt(cls, data): |
368 |
| - encode_bytes = base64.decodebytes(data.encode('utf8')) |
369 |
| - cipher = AES.new(cls.key(), AES.MODE_CBC, cls.iv.encode('utf8')) |
370 |
| - text_decrypted = cipher.decrypt(encode_bytes) |
371 |
| - |
372 |
| - return cls.unpad(text_decrypted).decode('utf8') |
0 commit comments