forked from jly8866/archer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request jly8866#1 from jly8866/master
更新master代码
- Loading branch information
Showing
14 changed files
with
279 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -128,6 +128,91 @@ | |
INCEPTION_REMOTE_BACKUP_USER='inception' | ||
INCEPTION_REMOTE_BACKUP_PASSWORD='inception' | ||
|
||
# 账户登录失败锁定时间(秒) | ||
LOCK_TIME_THRESHOLD = 300 | ||
# 账户登录失败 几次 锁账户 | ||
LOCK_CNT_THRESHOLD = 5 | ||
|
||
# LDAP | ||
ENABLE_LDAP = False | ||
if ENABLE_LDAP: | ||
import ldap | ||
# from django_auth_ldap.config import LDAPSearch, GroupOfNamesType | ||
from django_auth_ldap.config import LDAPSearch, GroupOfUniqueNamesType | ||
# if use self signed certificate, Remove AUTH_LDAP_GLOBAL_OPTIONS annotations | ||
#AUTH_LDAP_GLOBAL_OPTIONS={ | ||
# ldap.OPT_X_TLS_REQUIRE_CERT: ldap.OPT_X_TLS_NEVER | ||
#} | ||
AUTH_LDAP_BIND_DN = "cn=ro,dc=xxx,dc=cn" | ||
AUTH_LDAP_BIND_PASSWORD = "xxxxxx" | ||
AUTH_LDAP_SERVER_URI = "ldap://auth.xxx.com" | ||
AUTH_LDAP_BASEDN = "ou=users,dc=xxx,dc=cn" | ||
AUTH_LDAP_USER_DN_TEMPLATE = "cn=%(user)s,ou=users,dc=xxx,dc=cn" | ||
AUTH_LDAP_GROUP_SEARCH = LDAPSearch("ou=groups,dc=xxx,dc=cn", | ||
ldap.SCOPE_SUBTREE, "(objectClass=groupOfUniqueNames)" | ||
) | ||
AUTH_LDAP_GROUP_TYPE = GroupOfUniqueNamesType() | ||
AUTH_LDAP_USER_ATTRLIST = ["cn", "sn", "mail"] | ||
AUTH_LDAP_USER_ATTR_MAP = { | ||
"username": "cn", | ||
"display": "sn", | ||
"email": "mail" | ||
} | ||
|
||
# AUTH_LDAP_MIRROR_GROUPS = True # 直接把ldap的组复制到django一份,和AUTH_LDAP_FIND_GROUP_PERMS互斥.用户每次登录会根据ldap来更新数据库的组关系 | ||
# AUTH_LDAP_FIND_GROUP_PERMS = True # django从ldap的组权限中获取权限,这种方式,django自身不创建组,每次请求都调用ldap | ||
# AUTH_LDAP_CACHE_GROUPS = True # 如打开FIND_GROUP_PERMS后,此配置生效,对组关系进行缓存,不用每次请求都调用ldap | ||
# AUTH_LDAP_GROUP_CACHE_TIMEOUT = 600 # 缓存时间 | ||
|
||
#开启以下配置注释,可以帮助调试ldap集成 | ||
LDAP_LOGS = '/tmp/ldap.log' | ||
DEFAULT_LOGS = '/tmp/default.log' | ||
stamdard_format = '[%(asctime)s][%(threadName)s:%(thread)d]' + \ | ||
'[task_id:%(name)s][%(filename)s:%(lineno)d] ' + \ | ||
'[%(levelname)s]- %(message)s' | ||
LOGGING = { | ||
'version': 1, | ||
'disable_existing_loggers': False, | ||
'formatters': { | ||
'standard': { # 详细 | ||
'format': stamdard_format | ||
}, | ||
}, | ||
'handlers': { | ||
'default': { | ||
'level': 'DEBUG', | ||
'class': 'logging.handlers.RotatingFileHandler', | ||
'filename': DEFAULT_LOGS, | ||
'maxBytes': 1024 * 1024 * 100, # 5 MB | ||
'backupCount': 5, | ||
'formatter': 'standard', | ||
}, | ||
'ldap': { | ||
'level': 'DEBUG', | ||
'class': 'logging.handlers.RotatingFileHandler', | ||
'filename': LDAP_LOGS, | ||
'maxBytes': 1024 * 1024 * 100, # 5 MB | ||
'backupCount': 5, | ||
'formatter': 'standard', | ||
}, | ||
'console': { | ||
'level': 'DEBUG', | ||
'class': 'logging.StreamHandler', | ||
} | ||
}, | ||
'loggers': { | ||
'default': { # default日志,存放于log中 | ||
'handlers': ['default'], | ||
'level': 'DEBUG', | ||
}, | ||
'django_auth_ldap': { # django_auth_ldap模块相关日志打印到console | ||
'handlers': ['ldap'], | ||
'level': 'DEBUG', | ||
'propagate': True, # 选择关闭继承,不然这个logger继承自默认,日志就会被记录2次了(''一次,自己一次) | ||
} | ||
} | ||
} | ||
|
||
#是否开启邮件提醒功能:发起SQL上线后会发送邮件提醒审核人审核,执行完毕会发送给DBA. on是开,off是关,配置为其他值均会被archer认为不开启邮件功能 | ||
MAIL_ON_OFF='on' | ||
|
||
|
@@ -136,6 +221,7 @@ | |
MAIL_REVIEW_FROM_ADDR='[email protected]' #发件人,也是登录SMTP server需要提供的用户名 | ||
MAIL_REVIEW_FROM_PASSWORD='' #发件人邮箱密码,如果为空则不需要login SMTP server | ||
MAIL_REVIEW_DBA_ADDR=['[email protected]', '[email protected]'] #DBA地址,执行完毕会发邮件给DBA,以list形式保存 | ||
MAIL_REVIEW_SECURE_ADDR=['[email protected]', '[email protected]'] #登录失败,等安全相关发送地址 | ||
#是否过滤【DROP DATABASE】|【DROP TABLE】|【TRUNCATE PARTITION】|【TRUNCATE TABLE】等高危DDL操作: | ||
#on是开,会首先用正则表达式匹配sqlContent,如果匹配到高危DDL操作,则判断为“自动审核不通过”;off是关,直接将所有的SQL语句提交给inception,对于上述高危DDL操作,只备份元数据 | ||
CRITICAL_DDL_ON_OFF='off' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--- views.py 2018-01-23 11:53:00.179201491 +0800 | ||
+++ python/site-packages/django/contrib/auth/views.py 2018-01-23 11:58:10.668286140 +0800 | ||
@@ -24,7 +24,14 @@ | ||
from django.views.decorators.cache import never_cache | ||
from django.views.decorators.csrf import csrf_protect | ||
from django.views.decorators.debug import sensitive_post_parameters | ||
- | ||
+# 账户锁定 | ||
+from django.conf import settings | ||
+from sql.sendmail import MailSender | ||
+import datetime | ||
+import logging | ||
+logger = logging.getLogger('default') | ||
+login_failure_counter = {} | ||
+# 账户锁定end | ||
|
||
@sensitive_post_parameters() | ||
@csrf_protect | ||
@@ -41,8 +48,22 @@ | ||
|
||
if request.method == "POST": | ||
form = authentication_form(request, data=request.POST) | ||
- if form.is_valid(): | ||
- | ||
+ | ||
+ # 增加账户锁定 | ||
+ failed_cnt = settings.LOCK_CNT_THRESHOLD | ||
+ locking_time = settings.LOCK_TIME_THRESHOLD | ||
+ username = request.POST['username'] | ||
+ mailSender = MailSender() | ||
+ now_time = datetime.datetime.now() | ||
+ mail_title = 'login inception admin' | ||
+ login_failed_message = '' | ||
+ | ||
+ if username in login_failure_counter and login_failure_counter[username]['cnt'] >= failed_cnt and (now_time - login_failure_counter[username]["last_failure_time"]).seconds <= locking_time: | ||
+ login_failed_message = 'user:{},login /admin failed, account locking...'.format(username) | ||
+ logger.warning(login_failed_message) | ||
+ mailSender.sendEmail(mail_title, login_failed_message, getattr(settings, 'MAIL_REVIEW_SECURE_ADDR')) | ||
+ elif form.is_valid(): | ||
+ logger.info('user:{},login /admin success'.format(username)) | ||
# Ensure the user-originating redirection url is safe. | ||
if not is_safe_url(url=redirect_to, host=request.get_host()): | ||
redirect_to = resolve_url(settings.LOGIN_REDIRECT_URL) | ||
@@ -51,6 +72,15 @@ | ||
auth_login(request, form.get_user()) | ||
|
||
return HttpResponseRedirect(redirect_to) | ||
+ else: | ||
+ if username in login_failure_counter and (now_time - login_failure_counter[username]["last_failure_time"]).seconds <= locking_time: | ||
+ login_failure_counter[username]["cnt"] += 1 | ||
+ else: | ||
+ login_failure_counter[username] = {"cnt":1, "last_failure_time": datetime.datetime.now()} | ||
+ login_failed_message = 'user:{},login /admin failed, fail count:{}'.format(username, login_failure_counter[username]["cnt"]) | ||
+ logger.warning(login_failed_message) | ||
+ mailSender.sendEmail(mail_title, login_failed_message, getattr(settings, 'MAIL_REVIEW_SECURE_ADDR')) | ||
+ #账户锁定end | ||
else: | ||
form = authentication_form(request) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
Django==1.8.17 | ||
python==3.4.1 | ||
django-auth-ldap==1.3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{% extends "base.html" %} | ||
|
||
{% block content %} | ||
<button type="button" id="btnSync" class="btn btn-primary">同步LDAP用户</button> | ||
{% endblock content %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
$('#btnSync').click(function(){ | ||
syncUser(); | ||
}); | ||
|
||
function syncUser() { | ||
$.ajax({ | ||
type: "post", | ||
url: "/syncldapuser/", | ||
dataType: "json", | ||
data: {}, | ||
complete: function () { | ||
}, | ||
success: function (data) { | ||
$('#wrongpwd-modal-body').html(data.msg); | ||
$('#wrongpwd-modal').modal({ | ||
keyboard: true | ||
}); | ||
}, | ||
error: function (XMLHttpRequest, textStatus, errorThrown) { | ||
alert(errorThrown); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.