Skip to content

Commit

Permalink
Merge pull request momosecurity#69 from Flynnon/feature-menu-key-conv…
Browse files Browse the repository at this point in the history
…ert-logic

补充名单维度映射逻辑
  • Loading branch information
leohowell authored Apr 6, 2021
2 parents 379d2d9 + 6f1271d commit 3f00da6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
28 changes: 24 additions & 4 deletions risk_models/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
logging.basicConfig()


def build_redis_key(event_code, dimension, menu_type):
def build_redis_key(event_code: str, dimension: str, menu_type: str):
"""
:param event_code: 名单code
:param dimension: 名单维度
Expand All @@ -22,17 +22,37 @@ def build_redis_key(event_code, dimension, menu_type):
return ''


def get_element(req_body: dict, dimension: str) -> str:
""" 从req_body中得到所需维度的数据 """
# 目前全部从业务传参中获取
return req_body.get(dimension, '')


def convert_dimension(dimension: str) -> str:
""" 将配置中的某个key映射到某个名单维度 """

# 暂时写死,之后可以配置化
convert_map = {
"reg_ip": "ip",
"reg_uid": "uid"
}
return convert_map.get(dimension, dimension)


def hit_menu(req_body, op_name, event, dimension, menu_type):
if dimension not in req_body:
logger.error('req_body(%s) does not contain %s', req_body, dimension)
element = get_element(req_body, dimension)

if not element:
logger.warning(f'req_body({req_body}) does not contain {dimension}')
return False

dimension = convert_dimension(dimension)
redis_key = build_redis_key(event, dimension, menu_type)

if not redis_key:
return False

rv = req_body[dimension] in menu_cache[redis_key]
rv = element in menu_cache[redis_key]
if op_name == 'is_not':
rv = not rv

Expand Down
4 changes: 0 additions & 4 deletions wiki/上报API.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
| key | 类型 | 含义 | 是否必填 |
| :----: | :----: | :----: | :----: |
| user_id | string | 账号ID ||
| token | string | 标识业务方, 接入时分发 ||
| app_name | string | 标识应用, 接入时分发 ||
| source_name | string | 数据源名称,每个接入点单独指定 ||
| timestamp | long | 用户动作发生时间(秒级时间戳) ||
| ip | string | 用户当前IP地址 ||
Expand All @@ -31,11 +29,9 @@
import requests

data = {
'app_name': 'test_app',
'ip': '1.1.1.1',
'source_name': 'test_test',
'timestamp': 1568271589,
'token': '11111111111111111',
'uid': '11111111111111111111',
'user_id': '1111'
}
Expand Down
6 changes: 1 addition & 5 deletions wiki/查询API.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
| key | 类型 | 含义 | 是否必填 |
| ---- | ---- | ---- | ---- |
| user_id | string | 账号ID ||
| token | string | 标识业务方, 接入时分发 ||
| app_name | string | 业务方标识,接入时分发 ||
| rule_id | string | 规则ID, 每个调用点可能不同 ||
| ip | string | 用户当前IP地址 ||
| uid | string | 用户当前设备号 ||
Expand All @@ -30,10 +28,8 @@
import requests

data = {
'app_name': 'test_app',
'ip': '1.1.1.1',
'rule_id': '1',
'token': '111111111111111',
'uid': '111111111111111',
'user_id': '11111'
}
Expand All @@ -51,7 +47,7 @@ requests.post(f'http://{ip}:{port}/query/', json=data)
'{"result":{"control":"deny","weight":100},"em":"OK","ec":0}'

# 失败结果示例
'{"error":"invalid token","em":"invalid token","ec":70}'
'{"error":"invalid token","em":"invalid user_id","ec":70}'

# warning 规则停用后,会返回错误
```
Expand Down

0 comments on commit 3f00da6

Please sign in to comment.