Skip to content

Commit ef1e3c9

Browse files
committed
fix: get user info
1 parent 4097d60 commit ef1e3c9

File tree

3 files changed

+2
-221
lines changed

3 files changed

+2
-221
lines changed

acl-api/api/lib/perm/acl/acl.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def clean(cls):
5757

5858
class ACLManager(object):
5959
def __init__(self, app=None):
60-
self.app = AppCache.get(app or 'cmdb')
60+
self.app = AppCache.get(app or 'acl')
6161
if not self.app:
6262
raise Exception(ErrFormat.app_not_found.format(app))
6363
self.app_id = self.app.id
@@ -246,7 +246,7 @@ def wrapper_has_perm(*args, **kwargs):
246246

247247

248248
def is_app_admin(app=None):
249-
app = app or 'cmdb'
249+
app = app or 'acl'
250250
app = AppCache.get(app)
251251
if app is None:
252252
return False

acl-api/api/lib/utils.py

-213
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
# -*- coding:utf-8 -*-
22

3-
import base64
4-
import json
53
import sys
64
import time
75
from typing import Set
86

9-
import elasticsearch
107
import redis
118
import six
12-
from Crypto.Cipher import AES
13-
from elasticsearch import Elasticsearch
149
from flask import current_app
1510

1611

@@ -118,99 +113,6 @@ def delete(self, key_id, prefix):
118113
current_app.logger.error("delete redis key error, {0}".format(str(e)))
119114

120115

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-
214116
class Lock(object):
215117
def __init__(self, name, timeout=10, app=None, need_lock=True):
216118
self.lock_key = name
@@ -255,118 +157,3 @@ def __enter__(self):
255157
def __exit__(self, exc_type, exc_val, exc_tb):
256158
if self.need_lock:
257159
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')

docker-compose.yml

-6
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ services:
3131

3232
acl-api:
3333
image: registry.cn-hangzhou.aliyuncs.com/veops/acl-api:1.0
34-
# build:
35-
# context: .
36-
# target: acl-api
3734
container_name: acl-api
3835
environment:
3936
TZ: Asia/Shanghai
@@ -58,9 +55,6 @@ services:
5855

5956
acl-ui:
6057
image: registry.cn-hangzhou.aliyuncs.com/veops/acl-ui:1.0
61-
# build:
62-
# context: .
63-
# target: acl-ui
6458
container_name: acl-ui
6559
depends_on:
6660
- acl-api

0 commit comments

Comments
 (0)