Skip to content

Commit

Permalink
fix: get user info
Browse files Browse the repository at this point in the history
  • Loading branch information
pycook committed Oct 11, 2023
1 parent 4097d60 commit ef1e3c9
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 221 deletions.
4 changes: 2 additions & 2 deletions acl-api/api/lib/perm/acl/acl.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def clean(cls):

class ACLManager(object):
def __init__(self, app=None):
self.app = AppCache.get(app or 'cmdb')
self.app = AppCache.get(app or 'acl')
if not self.app:
raise Exception(ErrFormat.app_not_found.format(app))
self.app_id = self.app.id
Expand Down Expand Up @@ -246,7 +246,7 @@ def wrapper_has_perm(*args, **kwargs):


def is_app_admin(app=None):
app = app or 'cmdb'
app = app or 'acl'
app = AppCache.get(app)
if app is None:
return False
Expand Down
213 changes: 0 additions & 213 deletions acl-api/api/lib/utils.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
# -*- coding:utf-8 -*-

import base64
import json
import sys
import time
from typing import Set

import elasticsearch
import redis
import six
from Crypto.Cipher import AES
from elasticsearch import Elasticsearch
from flask import current_app


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


class ESHandler(object):
def __init__(self, flask_app=None):
self.flask_app = flask_app
self.es = None
self.index = "cmdb"

def init_app(self, app):
self.flask_app = app
config = self.flask_app.config
if config.get('ES_USER') and config.get('ES_PASSWORD'):
uri = "http://{}:{}@{}:{}/".format(config.get('ES_USER'), config.get('ES_PASSWORD'),
config.get('ES_HOST'), config.get('ES_PORT'))
else:
uri = "{}:{}".format(config.get('ES_HOST'), config.get('ES_PORT') or 9200)
self.es = Elasticsearch(uri,
timeout=10,
max_retries=3,
retry_on_timeout=True,
retry_on_status=(502, 503, 504, "N/A"),
maxsize=10)
try:
if not self.es.indices.exists(index=self.index):
self.es.indices.create(index=self.index)
except elasticsearch.exceptions.RequestError as ex:
if ex.error != 'resource_already_exists_exception':
raise

def update_mapping(self, field, value_type, other):
body = {
"properties": {
field: {"type": value_type},
}}
body['properties'][field].update(other)

self.es.indices.put_mapping(
index=self.index,
body=body
)

def get_index_id(self, ci_id):
try:
return self._get_index_id(ci_id)
except:
return self._get_index_id(ci_id)

def _get_index_id(self, ci_id):
query = {
'query': {
'match': {'ci_id': ci_id}
},
}
res = self.es.search(index=self.index, body=query)
if res['hits']['hits']:
return res['hits']['hits'][-1].get('_id')

def create(self, body):
return self.es.index(index=self.index, body=body).get("_id")

def update(self, ci_id, body):
_id = self.get_index_id(ci_id)

if _id:
return self.es.index(index=self.index, id=_id, body=body).get("_id")

def create_or_update(self, ci_id, body):
try:
self.update(ci_id, body) or self.create(body)
except KeyError:
self.create(body)

def delete(self, ci_id):
try:
_id = self.get_index_id(ci_id)
except KeyError:
return

if _id:
self.es.delete(index=self.index, id=_id)

def read(self, query, filter_path=None):
filter_path = filter_path or []
if filter_path:
filter_path.append('hits.total')

res = self.es.search(index=self.index, body=query, filter_path=filter_path)
if res['hits'].get('hits'):
return res['hits']['total']['value'], \
[i['_source'] for i in res['hits']['hits']], \
res.get("aggregations", {})
else:
return 0, [], {}


class Lock(object):
def __init__(self, name, timeout=10, app=None, need_lock=True):
self.lock_key = name
Expand Down Expand Up @@ -255,118 +157,3 @@ def __enter__(self):
def __exit__(self, exc_type, exc_val, exc_tb):
if self.need_lock:
self.release()


class Redis2Handler(object):
def __init__(self, flask_app=None, prefix=None):
self.flask_app = flask_app
self.prefix = prefix
self.r = None

def init_app(self, app):
self.flask_app = app
config = self.flask_app.config
try:
pool = redis.ConnectionPool(
max_connections=config.get("REDIS_MAX_CONN"),
host=config.get("ONEAGENT_REDIS_HOST"),
port=config.get("ONEAGENT_REDIS_PORT"),
db=config.get("ONEAGENT_REDIS_DB"),
password=config.get("ONEAGENT_REDIS_PASSWORD")
)
self.r = redis.Redis(connection_pool=pool)
except Exception as e:
current_app.logger.warning(str(e))
current_app.logger.error("init redis connection failed")

def get(self, key):
try:
value = json.loads(self.r.get(key))
except:
return

return value

def lrange(self, key, start=0, end=-1):
try:
value = "".join(map(redis_decode, self.r.lrange(key, start, end) or []))
except:
return

return value

def lrange2(self, key, start=0, end=-1):
try:
return list(map(redis_decode, self.r.lrange(key, start, end) or []))
except:
return []

def llen(self, key):
try:
return self.r.llen(key) or 0
except:
return 0

def hget(self, key, field):
try:
return self.r.hget(key, field)
except Exception as e:
current_app.logger.warning("hget redis failed, %s" % str(e))
return

def hset(self, key, field, value):
try:
self.r.hset(key, field, value)
except Exception as e:
current_app.logger.warning("hset redis failed, %s" % str(e))
return

def expire(self, key, timeout):
try:
self.r.expire(key, timeout)
except Exception as e:
current_app.logger.warning("expire redis failed, %s" % str(e))
return


def redis_decode(x):
try:
return x.decode()
except Exception as e:
print(x, e)
try:
return x.decode("gb18030")
except:
return "decode failed"


class AESCrypto(object):
BLOCK_SIZE = 16 # Bytes
pad = lambda s: s + (AESCrypto.BLOCK_SIZE - len(s) % AESCrypto.BLOCK_SIZE) * \
chr(AESCrypto.BLOCK_SIZE - len(s) % AESCrypto.BLOCK_SIZE)
unpad = lambda s: s[:-ord(s[len(s) - 1:])]

iv = '0102030405060708'

@staticmethod
def key():
key = current_app.config.get("SECRET_KEY")[:16]
if len(key) < 16:
key = "{}{}".format(key, (16 - len(key) * "x"))

return key.encode('utf8')

@classmethod
def encrypt(cls, data):
data = cls.pad(data)
cipher = AES.new(cls.key(), AES.MODE_CBC, cls.iv.encode('utf8'))

return base64.b64encode(cipher.encrypt(data.encode('utf8'))).decode('utf8')

@classmethod
def decrypt(cls, data):
encode_bytes = base64.decodebytes(data.encode('utf8'))
cipher = AES.new(cls.key(), AES.MODE_CBC, cls.iv.encode('utf8'))
text_decrypted = cipher.decrypt(encode_bytes)

return cls.unpad(text_decrypted).decode('utf8')
6 changes: 0 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ services:

acl-api:
image: registry.cn-hangzhou.aliyuncs.com/veops/acl-api:1.0
# build:
# context: .
# target: acl-api
container_name: acl-api
environment:
TZ: Asia/Shanghai
Expand All @@ -58,9 +55,6 @@ services:

acl-ui:
image: registry.cn-hangzhou.aliyuncs.com/veops/acl-ui:1.0
# build:
# context: .
# target: acl-ui
container_name: acl-ui
depends_on:
- acl-api
Expand Down

0 comments on commit ef1e3c9

Please sign in to comment.