Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
O
ops_sdk
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PublicSource
ops_sdk
Commits
111da6bd
Commit
111da6bd
authored
Apr 18, 2019
by
shenshuo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加ldap处理模块
parent
3fe42dfe
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
93 additions
and
2 deletions
+93
-2
setup.py
setup.py
+3
-2
consts.py
websdk/consts.py
+11
-0
ldap.py
websdk/ldap.py
+79
-0
No files found.
setup.py
View file @
111da6bd
...
@@ -9,13 +9,14 @@ from distutils.core import setup
...
@@ -9,13 +9,14 @@ from distutils.core import setup
setup
(
setup
(
name
=
'opssdk'
,
name
=
'opssdk'
,
version
=
'0.0.1
1
'
,
version
=
'0.0.1
2
'
,
packages
=
[
'opssdk'
,
'opssdk.logs'
,
'opssdk.operate'
,
'opssdk.install'
,
'opssdk.get_info'
,
'opssdk.utils'
,
'websdk'
],
packages
=
[
'opssdk'
,
'opssdk.logs'
,
'opssdk.operate'
,
'opssdk.install'
,
'opssdk.get_info'
,
'opssdk.utils'
,
'websdk'
],
url
=
'https://github.com/ss1917/ops_sdk/'
,
url
=
'https://github.com/ss1917/ops_sdk/'
,
license
=
''
,
license
=
''
,
install_requires
=
[
'fire'
,
'shortuuid'
,
'pymysql===0.9.3'
,
'sqlalchemy===1.2.15'
,
'python3-pika===0.9.14'
,
'PyJWT'
,
install_requires
=
[
'fire'
,
'shortuuid'
,
'pymysql===0.9.3'
,
'sqlalchemy===1.2.15'
,
'python3-pika===0.9.14'
,
'PyJWT'
,
'Crypto===1.4.1'
,
'requests'
,
'redis===2.10.6'
,
'tornado===5.0'
,
'Crypto===1.4.1'
,
'requests'
,
'redis===2.10.6'
,
'tornado===5.0'
,
'aliyun-python-sdk-core-v3===2.8.6'
,
'aliyun-python-sdk-dysmsapi'
,
'python-dateutil===2.7.5'
],
'aliyun-python-sdk-core-v3===2.8.6'
,
'aliyun-python-sdk-dysmsapi'
,
'python-dateutil===2.7.5'
,
'ldap3===2.6'
],
author
=
'shenshuo'
,
author
=
'shenshuo'
,
author_email
=
'191715030@qq.com'
,
author_email
=
'191715030@qq.com'
,
description
=
'SDK of the operation and maintenance script'
description
=
'SDK of the operation and maintenance script'
...
...
websdk/consts.py
View file @
111da6bd
...
@@ -142,3 +142,14 @@ const.STORAGE_NAME = "STORAGE_NAME"
...
@@ -142,3 +142,14 @@ const.STORAGE_NAME = "STORAGE_NAME"
const
.
STORAGE_PATH
=
"STORAGE_PATH"
const
.
STORAGE_PATH
=
"STORAGE_PATH"
const
.
STORAGE_KEY_ID
=
"STORAGE_KEY_ID"
const
.
STORAGE_KEY_ID
=
"STORAGE_KEY_ID"
const
.
STORAGE_KEY_SECRET
=
"STORAGE_KEY_SECRET"
const
.
STORAGE_KEY_SECRET
=
"STORAGE_KEY_SECRET"
### LDAP
const
.
LDAP_SERVER_HOST
=
"LDAP_SERVER_HOST"
const
.
LDAP_SERVER_PORT
=
"LDAP_SERVER_PORT"
const
.
LDAP_ADMIN_DN
=
"LDAP_ADMIN_DN"
const
.
LDAP_ADMIN_PASSWORD
=
"LDAP_ADMIN_PASSWORD"
const
.
LDAP_SEARCH_BASE
=
"LDAP_SEARCH_BASE"
const
.
LDAP_SEARCH_FILTER
=
"LDAP_SEARCH_FILTER"
const
.
LDAP_ATTRIBUTES
=
"LDAP_ATTRIBUTES"
const
.
LDAP_USE_SSL
=
"LDAP_USE_SSL"
const
.
LDAP_ENABLE
=
"LDAP_ENABLE"
\ No newline at end of file
websdk/ldap.py
0 → 100644
View file @
111da6bd
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Contact : 191715030@qq.com
Author : shenshuo
Date : 2019/4/17
Desc : 对接LDAP登录认证
"""
from
ldap3
import
Server
,
Connection
,
ALL
,
SUBTREE
,
ServerPool
class
LdapApi
:
def
__init__
(
self
,
ldap_server_host
,
ldap_admin_dn
,
ldap_admin_password
,
ldap_server_port
=
389
,
ldap_use_ssl
=
False
):
self
.
_ldap_admin_dn
=
ldap_admin_dn
self
.
_ldap_admin_password
=
ldap_admin_password
# ldap_server_pool = ServerPool(["172.16.0.102",'172.16.0.103'])
self
.
ldap_server
=
Server
(
ldap_server_host
,
port
=
ldap_server_port
,
use_ssl
=
ldap_use_ssl
)
def
ldap_server_test
(
self
):
try
:
conn
=
Connection
(
self
.
ldap_server
,
user
=
self
.
_ldap_admin_dn
,
password
=
self
.
_ldap_admin_password
,
check_names
=
True
,
lazy
=
False
,
raise_exceptions
=
False
)
conn
.
open
()
conn
.
bind
()
return
True
except
Exception
as
e
:
print
(
"auth fail {}"
.
format
(
e
))
return
False
def
ldap_auth
(
self
,
username
,
password
,
search_base
,
search_filter
=
'cn'
):
if
not
self
.
ldap_server_test
():
return
False
,
None
,
None
conn
=
Connection
(
self
.
ldap_server
,
user
=
self
.
_ldap_admin_dn
,
password
=
self
.
_ldap_admin_password
,
check_names
=
True
,
lazy
=
False
,
raise_exceptions
=
False
)
conn
.
open
()
conn
.
bind
()
res
=
conn
.
search
(
search_base
=
search_base
,
search_filter
=
'({}={})'
.
format
(
search_filter
,
username
),
search_scope
=
SUBTREE
,
attributes
=
[
'cn'
,
'givenName'
,
'email'
,
'mail'
,
'sAMAccountName'
],
paged_size
=
5
)
if
res
:
entry
=
conn
.
response
[
0
]
dn
=
entry
[
'dn'
]
attr_dict
=
entry
[
'attributes'
]
# check password by dn
try
:
conn2
=
Connection
(
self
.
ldap_server
,
user
=
dn
,
password
=
password
,
check_names
=
True
,
lazy
=
False
,
raise_exceptions
=
False
)
conn2
.
bind
()
if
conn2
.
result
[
"description"
]
==
"success"
:
if
attr_dict
[
"email"
]:
email
=
attr_dict
[
"email"
][
0
]
elif
attr_dict
[
"mail"
]:
email
=
attr_dict
[
"mail"
][
0
]
else
:
email
=
None
return
True
,
username
,
email
else
:
print
(
"auth fail"
)
return
False
,
None
,
None
except
Exception
as
e
:
print
(
"auth fail {}"
.
format
(
e
))
return
False
,
None
,
None
else
:
return
False
,
None
,
None
if
__name__
==
"__main__"
:
obj
=
LdapApi
(
'172.16.0.102'
,
'cn=Manager,DC=shinezone,DC=com'
,
'070068'
)
print
(
obj
.
ldap_server_test
())
print
(
'____________'
)
print
(
obj
.
ldap_auth
(
"yanghongfei"
,
"123456"
,
'ou=opendevops,dc=shinezone,dc=com'
,
'cn'
))
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment