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
d615eb80
Commit
d615eb80
authored
May 22, 2018
by
ibuler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Update] 优化使用storage sdk
parent
46520287
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
41 additions
and
79 deletions
+41
-79
api.py
apps/terminal/api.py
+18
-36
__init__.py
apps/terminal/backends/__init__.py
+8
-8
es.py
apps/terminal/backends/command/es.py
+6
-25
serializers.py
apps/terminal/serializers.py
+2
-2
terminal_tags.py
apps/terminal/templatetags/terminal_tags.py
+2
-2
command.py
apps/terminal/views/command.py
+2
-2
session.py
apps/terminal/views/session.py
+2
-2
requirements.txt
requirements/requirements.txt
+1
-2
No files found.
apps/terminal/api.py
View file @
d615eb80
...
@@ -9,6 +9,7 @@ from django.core.cache import cache
...
@@ -9,6 +9,7 @@ from django.core.cache import cache
from
django.shortcuts
import
get_object_or_404
,
redirect
from
django.shortcuts
import
get_object_or_404
,
redirect
from
django.utils
import
timezone
from
django.utils
import
timezone
from
django.core.files.storage
import
default_storage
from
django.core.files.storage
import
default_storage
from
django.http.response
import
HttpResponseRedirectBase
from
django.http
import
HttpResponseNotFound
from
django.http
import
HttpResponseNotFound
from
django.conf
import
settings
from
django.conf
import
settings
...
@@ -25,7 +26,7 @@ from .serializers import TerminalSerializer, StatusSerializer, \
...
@@ -25,7 +26,7 @@ from .serializers import TerminalSerializer, StatusSerializer, \
SessionSerializer
,
TaskSerializer
,
ReplaySerializer
SessionSerializer
,
TaskSerializer
,
ReplaySerializer
from
.hands
import
IsSuperUserOrAppUser
,
IsAppUser
,
\
from
.hands
import
IsSuperUserOrAppUser
,
IsAppUser
,
\
IsSuperUserOrAppUserOrUserReadonly
IsSuperUserOrAppUserOrUserReadonly
from
.backends
import
get_command_stor
e
,
get_multi_command_stor
e
,
\
from
.backends
import
get_command_stor
age
,
get_multi_command_storag
e
,
\
SessionCommandSerializer
SessionCommandSerializer
logger
=
logging
.
getLogger
(
__file__
)
logger
=
logging
.
getLogger
(
__file__
)
...
@@ -227,8 +228,8 @@ class CommandViewSet(viewsets.ViewSet):
...
@@ -227,8 +228,8 @@ class CommandViewSet(viewsets.ViewSet):
}
}
"""
"""
command_store
=
get_command_store
()
command_store
=
get_command_stor
ag
e
()
multi_command_storage
=
get_multi_command_store
()
multi_command_storage
=
get_multi_command_stor
ag
e
()
serializer_class
=
SessionCommandSerializer
serializer_class
=
SessionCommandSerializer
permission_classes
=
(
IsSuperUserOrAppUser
,)
permission_classes
=
(
IsSuperUserOrAppUser
,)
...
@@ -291,19 +292,20 @@ class SessionReplayViewSet(viewsets.ViewSet):
...
@@ -291,19 +292,20 @@ class SessionReplayViewSet(viewsets.ViewSet):
url
=
default_storage
.
url
(
path
)
url
=
default_storage
.
url
(
path
)
return
redirect
(
url
)
return
redirect
(
url
)
else
:
else
:
configs
=
settings
.
TERMINAL_REPLAY_STORAGE
.
items
()
configs
=
settings
.
TERMINAL_REPLAY_STORAGE
configs
=
[
cfg
for
cfg
in
configs
if
cfg
[
'TYPE'
]
!=
'server'
]
if
not
configs
:
if
not
configs
:
return
HttpResponseNotFound
()
return
HttpResponseNotFound
()
for
name
,
config
in
configs
:
date
=
self
.
session
.
date_start
.
strftime
(
'
%
Y-
%
m-
%
d'
)
client
=
jms_storage
.
init
(
config
)
file_path
=
os
.
path
.
join
(
date
,
str
(
self
.
session
.
id
)
+
'.replay.gz'
)
date
=
self
.
session
.
date_start
.
strftime
(
'
%
Y-
%
m-
%
d'
)
target_path
=
default_storage
.
base_location
+
'/'
+
path
file_path
=
os
.
path
.
join
(
date
,
str
(
self
.
session
.
id
)
+
'.replay.gz'
)
storage
=
jms_storage
.
get_multi_object_storage
(
configs
)
target_path
=
default_storage
.
base_location
+
'/'
+
path
ok
,
err
=
storage
.
download
(
file_path
,
target_path
)
if
ok
:
if
client
and
client
.
has_file
(
file_path
)
and
\
return
redirect
(
default_storage
.
url
(
path
))
client
.
download_file
(
file_path
,
target_path
)
:
else
:
return
redirect
(
default_storage
.
url
(
path
))
logger
.
error
(
"Failed download replay file: {}"
.
format
(
err
))
return
HttpResponseNotFound
()
return
HttpResponseNotFound
()
...
@@ -313,34 +315,14 @@ class SessionReplayV2ViewSet(SessionReplayViewSet):
...
@@ -313,34 +315,14 @@ class SessionReplayV2ViewSet(SessionReplayViewSet):
session
=
None
session
=
None
def
retrieve
(
self
,
request
,
*
args
,
**
kwargs
):
def
retrieve
(
self
,
request
,
*
args
,
**
kwargs
):
session_id
=
kwargs
.
get
(
'pk'
)
response
=
super
()
.
retrieve
(
request
,
*
args
,
**
kwargs
)
self
.
session
=
get_object_or_404
(
Session
,
id
=
session_id
)
path
=
self
.
gen_session_path
()
data
=
{
data
=
{
'type'
:
'guacamole'
if
self
.
session
.
protocol
==
'rdp'
else
'json'
,
'type'
:
'guacamole'
if
self
.
session
.
protocol
==
'rdp'
else
'json'
,
'src'
:
''
,
'src'
:
''
,
}
}
if
isinstance
(
response
,
HttpResponseRedirectBase
):
if
default_storage
.
exists
(
path
):
data
[
'src'
]
=
response
.
url
url
=
default_storage
.
url
(
path
)
data
[
'src'
]
=
url
return
Response
(
data
)
return
Response
(
data
)
else
:
configs
=
settings
.
TERMINAL_REPLAY_STORAGE
.
items
()
if
not
configs
:
return
HttpResponseNotFound
()
for
name
,
config
in
configs
:
client
=
jms_storage
.
init
(
config
)
date
=
self
.
session
.
date_start
.
strftime
(
'
%
Y-
%
m-
%
d'
)
file_path
=
os
.
path
.
join
(
date
,
str
(
self
.
session
.
id
)
+
'.replay.gz'
)
target_path
=
default_storage
.
base_location
+
'/'
+
path
if
client
and
client
.
has_file
(
file_path
)
and
\
client
.
download_file
(
file_path
,
target_path
):
url
=
default_storage
.
url
(
path
)
data
[
'src'
]
=
url
return
Response
(
data
)
return
HttpResponseNotFound
()
return
HttpResponseNotFound
()
...
...
apps/terminal/backends/__init__.py
View file @
d615eb80
...
@@ -7,19 +7,19 @@ TYPE_ENGINE_MAPPING = {
...
@@ -7,19 +7,19 @@ TYPE_ENGINE_MAPPING = {
}
}
def
get_command_store
():
def
get_command_stor
ag
e
():
params
=
settings
.
COMMAND_STORAGE
config
=
settings
.
COMMAND_STORAGE
engine_class
=
import_module
(
params
[
'ENGINE'
])
engine_class
=
import_module
(
config
[
'ENGINE'
])
storage
=
engine_class
.
CommandStore
(
params
)
storage
=
engine_class
.
CommandStore
(
config
)
return
storage
return
storage
def
get_terminal_command_stor
e
():
def
get_terminal_command_stor
ages
():
storage_list
=
{}
storage_list
=
{}
for
name
,
params
in
settings
.
TERMINAL_COMMAND_STORAGE
.
items
():
for
name
,
params
in
settings
.
TERMINAL_COMMAND_STORAGE
.
items
():
tp
=
params
[
'TYPE'
]
tp
=
params
[
'TYPE'
]
if
tp
==
'server'
:
if
tp
==
'server'
:
storage
=
get_command_store
()
storage
=
get_command_stor
ag
e
()
else
:
else
:
if
not
TYPE_ENGINE_MAPPING
.
get
(
tp
):
if
not
TYPE_ENGINE_MAPPING
.
get
(
tp
):
continue
continue
...
@@ -29,9 +29,9 @@ def get_terminal_command_store():
...
@@ -29,9 +29,9 @@ def get_terminal_command_store():
return
storage_list
return
storage_list
def
get_multi_command_store
():
def
get_multi_command_stor
ag
e
():
from
.command.multi
import
CommandStore
from
.command.multi
import
CommandStore
storage_list
=
get_terminal_command_stor
e
()
.
values
()
storage_list
=
get_terminal_command_stor
ages
()
.
values
()
storage
=
CommandStore
(
storage_list
)
storage
=
CommandStore
(
storage_list
)
return
storage
return
storage
...
...
apps/terminal/backends/command/es.py
View file @
d615eb80
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
#
#
from
jms_
es_sdk
import
ESStor
e
from
jms_
storage.es
import
ESStorag
e
from
.base
import
CommandBase
from
.base
import
CommandBase
from
.models
import
AbstractSessionCommand
from
.models
import
AbstractSessionCommand
class
CommandStore
(
CommandBase
,
ESStor
e
):
class
CommandStore
(
ESStorage
,
CommandBas
e
):
def
__init__
(
self
,
params
):
def
__init__
(
self
,
params
):
hosts
=
params
.
get
(
'HOSTS'
,
[
'http://localhost'
])
super
()
.
__init__
(
params
)
ESStore
.
__init__
(
self
,
hosts
=
hosts
)
def
save
(
self
,
command
):
return
ESStore
.
save
(
self
,
command
)
def
bulk_save
(
self
,
commands
):
return
ESStore
.
bulk_save
(
self
,
commands
)
def
filter
(
self
,
date_from
=
None
,
date_to
=
None
,
def
filter
(
self
,
date_from
=
None
,
date_to
=
None
,
user
=
None
,
asset
=
None
,
system_user
=
None
,
user
=
None
,
asset
=
None
,
system_user
=
None
,
input
=
None
,
session
=
None
):
input
=
None
,
session
=
None
):
data
=
ESStore
.
filter
(
data
=
super
()
.
filter
(
date_from
=
date_from
,
date_to
=
date_to
,
self
,
date_from
=
date_from
,
date_to
=
date_to
,
user
=
user
,
asset
=
asset
,
system_user
=
system_user
,
user
=
user
,
asset
=
asset
,
system_user
=
system_user
,
input
=
input
,
session
=
session
)
input
=
input
,
session
=
session
)
return
AbstractSessionCommand
.
from_multi_dict
(
return
AbstractSessionCommand
.
from_multi_dict
(
[
item
[
"_source"
]
for
item
in
data
[
"hits"
]
if
item
]
[
item
[
"_source"
]
for
item
in
data
[
"hits"
]
if
item
]
)
)
def
count
(
self
,
date_from
=
None
,
date_to
=
None
,
user
=
None
,
asset
=
None
,
system_user
=
None
,
input
=
None
,
session
=
None
):
amount
=
ESStore
.
count
(
self
,
date_from
=
date_from
,
date_to
=
date_to
,
user
=
user
,
asset
=
asset
,
system_user
=
system_user
,
input
=
input
,
session
=
session
)
return
amount
apps/terminal/serializers.py
View file @
d615eb80
...
@@ -9,7 +9,7 @@ from rest_framework_bulk.serializers import BulkListSerializer
...
@@ -9,7 +9,7 @@ from rest_framework_bulk.serializers import BulkListSerializer
from
common.mixins
import
BulkSerializerMixin
from
common.mixins
import
BulkSerializerMixin
from
common.utils
import
get_object_or_none
from
common.utils
import
get_object_or_none
from
.models
import
Terminal
,
Status
,
Session
,
Task
from
.models
import
Terminal
,
Status
,
Session
,
Task
from
.backends
import
get_multi_command_store
from
.backends
import
get_multi_command_stor
ag
e
class
TerminalSerializer
(
serializers
.
ModelSerializer
):
class
TerminalSerializer
(
serializers
.
ModelSerializer
):
...
@@ -47,7 +47,7 @@ class TerminalSerializer(serializers.ModelSerializer):
...
@@ -47,7 +47,7 @@ class TerminalSerializer(serializers.ModelSerializer):
class
SessionSerializer
(
serializers
.
ModelSerializer
):
class
SessionSerializer
(
serializers
.
ModelSerializer
):
command_amount
=
serializers
.
SerializerMethodField
()
command_amount
=
serializers
.
SerializerMethodField
()
command_store
=
get_multi_command_store
()
command_store
=
get_multi_command_stor
ag
e
()
class
Meta
:
class
Meta
:
model
=
Session
model
=
Session
...
...
apps/terminal/templatetags/terminal_tags.py
View file @
d615eb80
# ~*~ coding: utf-8 ~*~
# ~*~ coding: utf-8 ~*~
from
django
import
template
from
django
import
template
from
..backends
import
get_multi_command_store
from
..backends
import
get_multi_command_stor
ag
e
register
=
template
.
Library
()
register
=
template
.
Library
()
command_store
=
get_multi_command_store
()
command_store
=
get_multi_command_stor
ag
e
()
@register.filter
@register.filter
...
...
apps/terminal/views/command.py
View file @
d615eb80
...
@@ -9,10 +9,10 @@ from django.utils.translation import ugettext as _
...
@@ -9,10 +9,10 @@ from django.utils.translation import ugettext as _
from
common.mixins
import
DatetimeSearchMixin
,
AdminUserRequiredMixin
from
common.mixins
import
DatetimeSearchMixin
,
AdminUserRequiredMixin
from
..models
import
Command
from
..models
import
Command
from
..
import
utils
from
..
import
utils
from
..backends
import
get_multi_command_store
from
..backends
import
get_multi_command_stor
ag
e
__all__
=
[
'CommandListView'
]
__all__
=
[
'CommandListView'
]
common_storage
=
get_multi_command_store
()
common_storage
=
get_multi_command_stor
ag
e
()
class
CommandListView
(
DatetimeSearchMixin
,
AdminUserRequiredMixin
,
ListView
):
class
CommandListView
(
DatetimeSearchMixin
,
AdminUserRequiredMixin
,
ListView
):
...
...
apps/terminal/views/session.py
View file @
d615eb80
...
@@ -10,7 +10,7 @@ from django.conf import settings
...
@@ -10,7 +10,7 @@ from django.conf import settings
from
users.utils
import
AdminUserRequiredMixin
from
users.utils
import
AdminUserRequiredMixin
from
common.mixins
import
DatetimeSearchMixin
from
common.mixins
import
DatetimeSearchMixin
from
..models
import
Session
,
Command
,
Terminal
from
..models
import
Session
,
Command
,
Terminal
from
..backends
import
get_multi_command_store
from
..backends
import
get_multi_command_stor
ag
e
from
..
import
utils
from
..
import
utils
...
@@ -19,7 +19,7 @@ __all__ = [
...
@@ -19,7 +19,7 @@ __all__ = [
'SessionDetailView'
,
'SessionDetailView'
,
]
]
command_store
=
get_multi_command_store
()
command_store
=
get_multi_command_stor
ag
e
()
class
SessionListView
(
AdminUserRequiredMixin
,
DatetimeSearchMixin
,
ListView
):
class
SessionListView
(
AdminUserRequiredMixin
,
DatetimeSearchMixin
,
ListView
):
...
...
requirements/requirements.txt
View file @
d615eb80
...
@@ -40,7 +40,6 @@ itsdangerous==0.24
...
@@ -40,7 +40,6 @@ itsdangerous==0.24
itypes==1.1.0
itypes==1.1.0
Jinja2==2.10
Jinja2==2.10
jmespath==0.9.3
jmespath==0.9.3
jms-es-sdk
kombu==4.0.2
kombu==4.0.2
ldap3==2.4
ldap3==2.4
MarkupSafe==1.0
MarkupSafe==1.0
...
@@ -62,7 +61,7 @@ pytz==2017.3
...
@@ -62,7 +61,7 @@ pytz==2017.3
PyYAML==3.12
PyYAML==3.12
redis==2.10.6
redis==2.10.6
requests==2.18.4
requests==2.18.4
jms-storage==0.0.1
3
jms-storage==0.0.1
5
s3transfer==0.1.13
s3transfer==0.1.13
simplejson==3.13.2
simplejson==3.13.2
six==1.11.0
six==1.11.0
...
...
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