Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
J
jumpserver
Project
Project
Details
Activity
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
ops
jumpserver
Commits
d6b22e9f
Unverified
Commit
d6b22e9f
authored
6 years ago
by
老广
Committed by
GitHub
6 years ago
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Update] 修改gateway test connection (#2135)
parent
2833f343
No related merge requests found
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
43 additions
and
45 deletions
+43
-45
domain.py
apps/assets/api/domain.py
+1
-2
domain.py
apps/assets/forms/domain.py
+4
-0
domain.py
apps/assets/models/domain.py
+36
-0
label.py
apps/assets/models/label.py
+2
-1
domain.py
apps/assets/serializers/domain.py
+0
-1
utils.py
apps/assets/utils.py
+0
-41
No files found.
apps/assets/api/domain.py
View file @
d6b22e9f
...
...
@@ -9,7 +9,6 @@ from django.views.generic.detail import SingleObjectMixin
from
common.utils
import
get_logger
from
common.permissions
import
IsOrgAdmin
,
IsAppUser
,
IsOrgAdminOrAppUser
from
..models
import
Domain
,
Gateway
from
..utils
import
test_gateway_connectability
from
..
import
serializers
...
...
@@ -54,7 +53,7 @@ class GatewayTestConnectionApi(SingleObjectMixin, APIView):
def
get
(
self
,
request
,
*
args
,
**
kwargs
):
self
.
object
=
self
.
get_object
(
Gateway
.
objects
.
all
())
ok
,
e
=
test_gateway_connectability
(
self
.
object
)
ok
,
e
=
self
.
object
.
test_connective
(
)
if
ok
:
return
Response
(
"ok"
)
else
:
...
...
This diff is collapsed.
Click to expand it.
apps/assets/forms/domain.py
View file @
d6b22e9f
...
...
@@ -36,6 +36,10 @@ class DomainForm(forms.ModelForm):
class
GatewayForm
(
PasswordAndKeyAuthForm
,
OrgModelForm
):
protocol
=
forms
.
ChoiceField
(
choices
=
[
Gateway
.
PROTOCOL_CHOICES
[
0
]],
)
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
()
.
__init__
(
*
args
,
**
kwargs
)
password_field
=
self
.
fields
.
get
(
'password'
)
...
...
This diff is collapsed.
Click to expand it.
apps/assets/models/domain.py
View file @
d6b22e9f
...
...
@@ -4,6 +4,8 @@
import
uuid
import
random
import
paramiko
from
django.db
import
models
from
django.utils.translation
import
ugettext_lazy
as
_
...
...
@@ -57,3 +59,37 @@ class Gateway(AssetUser):
class
Meta
:
unique_together
=
[(
'name'
,
'org_id'
)]
verbose_name
=
_
(
"Gateway"
)
def
test_connective
(
self
):
client
=
paramiko
.
SSHClient
()
client
.
set_missing_host_key_policy
(
paramiko
.
AutoAddPolicy
())
proxy
=
paramiko
.
SSHClient
()
proxy
.
set_missing_host_key_policy
(
paramiko
.
AutoAddPolicy
())
try
:
proxy
.
connect
(
self
.
ip
,
port
=
self
.
port
,
username
=
self
.
username
,
password
=
self
.
password
,
pkey
=
self
.
private_key_obj
)
except
(
paramiko
.
AuthenticationException
,
paramiko
.
BadAuthenticationType
,
paramiko
.
SSHException
)
as
e
:
return
False
,
str
(
e
)
sock
=
proxy
.
get_transport
()
.
open_channel
(
'direct-tcpip'
,
(
'127.0.0.1'
,
self
.
port
),
(
'127.0.0.1'
,
0
)
)
try
:
client
.
connect
(
"127.0.0.1"
,
port
=
self
.
port
,
username
=
self
.
username
,
password
=
self
.
password
,
key_filename
=
self
.
private_key_file
,
sock
=
sock
,
timeout
=
5
)
except
(
paramiko
.
SSHException
,
paramiko
.
ssh_exception
.
SSHException
,
paramiko
.
AuthenticationException
,
TimeoutError
)
as
e
:
return
False
,
str
(
e
)
finally
:
client
.
close
()
return
True
,
None
This diff is collapsed.
Click to expand it.
apps/assets/models/label.py
View file @
d6b22e9f
...
...
@@ -17,7 +17,8 @@ class Label(OrgModelMixin):
id
=
models
.
UUIDField
(
default
=
uuid
.
uuid4
,
primary_key
=
True
)
name
=
models
.
CharField
(
max_length
=
128
,
verbose_name
=
_
(
"Name"
))
value
=
models
.
CharField
(
max_length
=
128
,
verbose_name
=
_
(
"Value"
))
category
=
models
.
CharField
(
max_length
=
128
,
choices
=
CATEGORY_CHOICES
,
default
=
USER_CATEGORY
,
verbose_name
=
_
(
"Category"
))
category
=
models
.
CharField
(
max_length
=
128
,
choices
=
CATEGORY_CHOICES
,
default
=
USER_CATEGORY
,
verbose_name
=
_
(
"Category"
))
is_active
=
models
.
BooleanField
(
default
=
True
,
verbose_name
=
_
(
"Is active"
))
comment
=
models
.
TextField
(
blank
=
True
,
null
=
True
,
verbose_name
=
_
(
"Comment"
))
date_created
=
models
.
DateTimeField
(
...
...
This diff is collapsed.
Click to expand it.
apps/assets/serializers/domain.py
View file @
d6b22e9f
...
...
@@ -23,7 +23,6 @@ class DomainSerializer(serializers.ModelSerializer):
class
GatewaySerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
model
=
Gateway
fields
=
[
...
...
This diff is collapsed.
Click to expand it.
apps/assets/utils.py
View file @
d6b22e9f
...
...
@@ -49,44 +49,3 @@ class LabelFilter:
for
kwargs
in
conditions
:
queryset
=
queryset
.
filter
(
**
kwargs
)
return
queryset
def
test_gateway_connectability
(
gateway
):
"""
Test system cant connect his assets or not.
:param gateway:
:return:
"""
client
=
paramiko
.
SSHClient
()
client
.
set_missing_host_key_policy
(
paramiko
.
AutoAddPolicy
())
proxy
=
paramiko
.
SSHClient
()
proxy
.
set_missing_host_key_policy
(
paramiko
.
AutoAddPolicy
())
try
:
proxy
.
connect
(
gateway
.
ip
,
gateway
.
port
,
username
=
gateway
.
username
,
password
=
gateway
.
password
,
pkey
=
gateway
.
private_key_obj
)
except
(
paramiko
.
AuthenticationException
,
paramiko
.
BadAuthenticationType
,
SSHException
)
as
e
:
return
False
,
str
(
e
)
sock
=
proxy
.
get_transport
()
.
open_channel
(
'direct-tcpip'
,
(
'127.0.0.1'
,
gateway
.
port
),
(
'127.0.0.1'
,
0
)
)
try
:
client
.
connect
(
"127.0.0.1"
,
port
=
gateway
.
port
,
username
=
gateway
.
username
,
password
=
gateway
.
password
,
key_filename
=
gateway
.
private_key_file
,
sock
=
sock
,
timeout
=
5
)
except
(
paramiko
.
SSHException
,
paramiko
.
ssh_exception
.
SSHException
,
paramiko
.
AuthenticationException
,
TimeoutError
)
as
e
:
return
False
,
str
(
e
)
finally
:
client
.
close
()
return
True
,
None
This diff is collapsed.
Click to expand it.
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