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
e0a3fafb
Commit
e0a3fafb
authored
Dec 29, 2017
by
ibuler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Bugfix] 详见trello
parent
3804ab53
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
75 additions
and
66 deletions
+75
-66
tasks.py
apps/assets/tasks.py
+3
-1
celery.py
apps/common/celery.py
+7
-0
forms.py
apps/perms/forms.py
+8
-3
_user_profile.html
apps/templates/_user_profile.html
+3
-3
apps.py
apps/terminal/apps.py
+3
-3
models.py
apps/terminal/backends/command/models.py
+3
-3
signals_handler.py
apps/terminal/signals_handler.py
+18
-30
command_list.html
apps/terminal/templates/terminal/command_list.html
+9
-9
session_list.html
apps/terminal/templates/terminal/session_list.html
+1
-1
command.py
apps/terminal/views/command.py
+10
-7
session.py
apps/terminal/views/session.py
+1
-1
user.py
apps/users/models/user.py
+7
-3
login.py
apps/users/views/login.py
+2
-2
No files found.
apps/assets/tasks.py
View file @
e0a3fafb
# ~*~ coding: utf-8 ~*~
import
json
import
re
from
celery
import
shared_task
from
django.core.cache
import
cache
...
...
@@ -18,6 +19,7 @@ FORKS = 10
TIMEOUT
=
60
logger
=
get_logger
(
__file__
)
CACHE_MAX_TIME
=
60
*
60
*
60
disk_pattern
=
re
.
compile
(
r'^hd|sd'
)
@shared_task
...
...
@@ -57,7 +59,7 @@ def set_assets_hardware_info(result, **kwargs):
___memory
=
'
%
s
%
s'
%
capacity_convert
(
'{} MB'
.
format
(
info
[
'ansible_memtotal_mb'
]))
disk_info
=
{}
for
dev
,
dev_info
in
info
[
'ansible_devices'
]
.
items
():
if
dev_info
[
'removable'
]
==
'0'
:
if
d
isk_pattern
.
match
(
dev
)
and
d
ev_info
[
'removable'
]
==
'0'
:
disk_info
[
dev
]
=
dev_info
[
'size'
]
___disk_total
=
'
%
s
%
s'
%
sum_capacity
(
disk_info
.
values
())
___disk_info
=
json
.
dumps
(
disk_info
)
...
...
apps/common/celery.py
View file @
e0a3fafb
...
...
@@ -16,6 +16,7 @@ logger = get_logger(__file__)
os
.
environ
.
setdefault
(
'DJANGO_SETTINGS_MODULE'
,
'jumpserver.settings'
)
from
django.conf
import
settings
from
django.core.cache
import
cache
app
=
Celery
(
'jumpserver'
)
...
...
@@ -176,6 +177,9 @@ def after_app_shutdown_clean(func):
@worker_ready.connect
def
on_app_ready
(
sender
=
None
,
headers
=
None
,
body
=
None
,
**
kwargs
):
if
cache
.
get
(
"CELERY_APP_READY"
,
0
)
==
1
:
return
cache
.
set
(
"CELERY_APP_READY"
,
1
,
10
)
logger
.
debug
(
"App ready signal recv"
)
logger
.
debug
(
"Start need start task: [{}]"
.
format
(
", "
.
join
(
__AFTER_APP_READY_RUN_TASKS
))
...
...
@@ -186,6 +190,9 @@ def on_app_ready(sender=None, headers=None, body=None, **kwargs):
@worker_shutdown.connect
def
after_app_shutdown
(
sender
=
None
,
headers
=
None
,
body
=
None
,
**
kwargs
):
if
cache
.
get
(
"CELERY_APP_SHUTDOWN"
,
0
)
==
1
:
return
cache
.
set
(
"CELERY_APP_SHUTDOWN"
,
1
,
10
)
from
django_celery_beat.models
import
PeriodicTask
logger
.
debug
(
"App shutdown signal recv"
)
logger
.
debug
(
"Clean need cleaned period tasks: [{}]"
.
format
(
...
...
apps/perms/forms.py
View file @
e0a3fafb
...
...
@@ -6,9 +6,17 @@ from django.utils.translation import ugettext_lazy as _
# from .hands import User, UserGroup, Asset, AssetGroup, SystemUser
from
.models
import
AssetPermission
from
users.models
import
User
class
AssetPermissionForm
(
forms
.
ModelForm
):
users
=
forms
.
ModelMultipleChoiceField
(
queryset
=
User
.
objects
.
exclude
(
role
=
User
.
ROLE_APP
),
widget
=
forms
.
SelectMultiple
(
attrs
=
{
'class'
:
'select2'
,
'data-placeholder'
:
_
(
'Select users'
)},
)
)
class
Meta
:
model
=
AssetPermission
fields
=
[
...
...
@@ -16,9 +24,6 @@ class AssetPermissionForm(forms.ModelForm):
'system_users'
,
'is_active'
,
'date_expired'
,
'comment'
,
]
widgets
=
{
'users'
:
forms
.
SelectMultiple
(
attrs
=
{
'class'
:
'select2'
,
'data-placeholder'
:
_
(
'Select users'
)}),
'user_groups'
:
forms
.
SelectMultiple
(
attrs
=
{
'class'
:
'select2'
,
'data-placeholder'
:
_
(
'Select user groups'
)}),
...
...
apps/templates/_user_profile.html
View file @
e0a3fafb
...
...
@@ -3,15 +3,15 @@
<li
class=
"nav-header"
>
<div
class=
"dropdown profile-element"
>
<span>
<img
alt=
"image"
class=
"img-circle"
width=
"48"
height=
"48"
src=
"{{ user.avatar_url }}"
/>
<img
alt=
"image"
class=
"img-circle"
width=
"48"
height=
"48"
src=
"{{
request.
user.avatar_url }}"
/>
</span>
<a
data-toggle=
"dropdown"
class=
"dropdown-toggle"
href=
"#"
>
<span
class=
"clear"
>
<span
class=
"block m-t-xs"
>
<strong
class=
"font-bold"
>
{{ user.name }}
<span
style=
"color: #8095a8"
></span></strong>
<strong
class=
"font-bold"
>
{{
request.
user.name }}
<span
style=
"color: #8095a8"
></span></strong>
</span>
<span
class=
"text-muted text-xs block"
>
{{ user.get_role_display | default:_('User') }}
<b
class=
"caret"
></b>
{{
request.
user.get_role_display | default:_('User') }}
<b
class=
"caret"
></b>
</span>
</span>
</a>
...
...
apps/terminal/apps.py
View file @
e0a3fafb
...
...
@@ -7,6 +7,5 @@ class TerminalConfig(AppConfig):
name
=
'terminal'
def
ready
(
self
):
from
.signals
import
on_app_ready
on_app_ready
.
send
(
self
.
__class__
)
super
()
.
ready
()
from
.
import
signals_handler
return
super
()
.
ready
()
\ No newline at end of file
apps/terminal/backends/command/models.py
View file @
e0a3fafb
...
...
@@ -7,9 +7,9 @@ from django.utils.translation import ugettext_lazy as _
class
AbstractSessionCommand
(
models
.
Model
):
id
=
models
.
UUIDField
(
default
=
uuid
.
uuid4
,
primary_key
=
True
)
user
=
models
.
CharField
(
max_length
=
64
,
verbose_name
=
_
(
"User"
))
asset
=
models
.
CharField
(
max_length
=
128
,
verbose_name
=
_
(
"Asset"
))
system_user
=
models
.
CharField
(
max_length
=
64
,
verbose_name
=
_
(
"System user"
))
user
=
models
.
CharField
(
max_length
=
64
,
db_index
=
True
,
verbose_name
=
_
(
"User"
))
asset
=
models
.
CharField
(
max_length
=
128
,
db_index
=
True
,
verbose_name
=
_
(
"Asset"
))
system_user
=
models
.
CharField
(
max_length
=
64
,
db_index
=
True
,
verbose_name
=
_
(
"System user"
))
input
=
models
.
CharField
(
max_length
=
128
,
db_index
=
True
,
verbose_name
=
_
(
"Input"
))
output
=
models
.
CharField
(
max_length
=
1024
,
blank
=
True
,
verbose_name
=
_
(
"Output"
))
session
=
models
.
CharField
(
max_length
=
36
,
db_index
=
True
,
verbose_name
=
_
(
"Session"
))
...
...
apps/terminal/signals.py
→
apps/terminal/signals
_handler
.py
View file @
e0a3fafb
...
...
@@ -3,47 +3,35 @@
import
threading
import
time
from
celery
import
shared_task
from
django.core.cache
import
cache
from
django.dispatch
import
Signal
,
receiver
from
django.db.utils
import
ProgrammingError
,
OperationalError
from
common.utils
import
get_logger
from
common.celery
import
after_app_ready_start
,
register_as_period_task
,
\
after_app_shutdown_clean
from
.const
import
ASSETS_CACHE_KEY
,
USERS_CACHE_KEY
,
SYSTEM_USER_CACHE_KEY
RUNNING
=
False
logger
=
get_logger
(
__file__
)
on_app_ready
=
Signal
()
@receiver
(
on_app_ready
)
def
on_app_ready_set_cache
(
sender
,
**
kwargs
):
@shared_task
@register_as_period_task
(
interval
=
3600
)
@after_app_ready_start
@after_app_shutdown_clean
def
set_session_info_cache
():
logger
.
debug
(
""
)
from
.utils
import
get_session_asset_list
,
get_session_user_list
,
\
get_session_system_user_list
global
RUNNING
def
set_cache
():
while
True
:
try
:
assets
=
get_session_asset_list
()
users
=
get_session_user_list
()
system_users
=
get_session_system_user_list
()
cache
.
set
(
ASSETS_CACHE_KEY
,
assets
)
cache
.
set
(
USERS_CACHE_KEY
,
users
)
cache
.
set
(
SYSTEM_USER_CACHE_KEY
,
system_users
)
except
(
ProgrammingError
,
OperationalError
):
pass
finally
:
time
.
sleep
(
10
)
if
RUNNING
:
return
threads
=
[]
thread
=
threading
.
Thread
(
target
=
set_cache
)
threads
.
append
(
thread
)
try
:
assets
=
get_session_asset_list
()
users
=
get_session_user_list
()
system_users
=
get_session_system_user_list
()
logger
.
debug
(
"Receive app ready signal: set cache task start"
)
for
t
in
threads
:
t
.
daemon
=
True
t
.
start
()
RUNNING
=
True
cache
.
set
(
ASSETS_CACHE_KEY
,
assets
)
cache
.
set
(
USERS_CACHE_KEY
,
users
)
cache
.
set
(
SYSTEM_USER_CACHE_KEY
,
system_users
)
except
(
ProgrammingError
,
OperationalError
):
pass
apps/terminal/templates/terminal/command_list.html
View file @
e0a3fafb
...
...
@@ -28,15 +28,15 @@
</div>
</div>
<div
class=
"input-group"
>
<select
class=
"select2 form-control"
name=
"user
name
"
>
<select
class=
"select2 form-control"
name=
"user"
>
<option
value=
""
>
{% trans 'User' %}
</option>
{% for u in user_list %}
<option
value=
"{{ u }}"
{%
if
u =
=
user
name
%}
selected
{%
endif
%}
>
{{ u }}
</option>
<option
value=
"{{ u }}"
{%
if
u =
=
user
%}
selected
{%
endif
%}
>
{{ u }}
</option>
{% endfor %}
</select>
</div>
<div
class=
"input-group"
>
<select
class=
"select2 form-control"
name=
"
ip
"
>
<select
class=
"select2 form-control"
name=
"
asset
"
>
<option
value=
""
>
{% trans 'Asset' %}
</option>
{% for a in asset_list %}
<option
value=
"{{ a }}"
{%
if
a =
=
asset
%}
selected
{%
endif
%}
>
{{ a }}
</option>
...
...
@@ -68,12 +68,12 @@
<thead>
<tr>
<th
data-toggle=
"true"
>
ID
</th>
<th>
Command
</th>
<th>
Username
</th>
<th>
IP
</th>
<th>
System user
</th>
<th>
Session
</th>
<th>
Datetime
</th>
<th>
{% trans 'Command' %}
</th>
<th>
{% trans 'User' %}
</th>
<th>
{% trans 'Asset' %}
</th>
<th>
{% trans 'System user'%}
</th>
<th>
{% trans 'Session' %}
</th>
<th>
{% trans 'Datetime' %}
</th>
<th
data-hide=
"all"
></th>
</tr>
</thead>
...
...
apps/terminal/templates/terminal/session_list.html
View file @
e0a3fafb
...
...
@@ -31,7 +31,7 @@
<select
class=
"select2 form-control"
name=
"user"
>
<option
value=
""
>
{% trans 'User' %}
</option>
{% for u in user_list %}
<option
value=
"{{ u }}"
{%
if
u =
=
user
name
%}
selected
{%
endif
%}
>
{{ u }}
</option>
<option
value=
"{{ u }}"
{%
if
u =
=
user
%}
selected
{%
endif
%}
>
{{ u }}
</option>
{% endfor %}
</select>
</div>
...
...
apps/terminal/views/command.py
View file @
e0a3fafb
...
...
@@ -25,6 +25,10 @@ class CommandListView(DatetimeSearchMixin, ListView):
date_format
=
'
%
m/
%
d/
%
Y'
def
get_queryset
(
self
):
self
.
command
=
self
.
request
.
GET
.
get
(
'command'
,
''
)
self
.
user
=
self
.
request
.
GET
.
get
(
"user"
,
''
)
self
.
asset
=
self
.
request
.
GET
.
get
(
'asset'
,
''
)
self
.
system_user
=
self
.
request
.
GET
.
get
(
'system_user'
,
''
)
filter_kwargs
=
dict
()
filter_kwargs
[
'date_from'
]
=
self
.
date_from
filter_kwargs
[
'date_to'
]
=
self
.
date_to
...
...
@@ -36,21 +40,20 @@ class CommandListView(DatetimeSearchMixin, ListView):
filter_kwargs
[
'system_user'
]
=
self
.
system_user
if
self
.
command
:
filter_kwargs
[
'input'
]
=
self
.
command
if
filter_kwargs
:
self
.
queryset
=
command_store
.
filter
(
**
filter_kwargs
)
return
self
.
queryset
queryset
=
command_store
.
filter
(
**
filter_kwargs
)
return
queryset
def
get_context_data
(
self
,
**
kwargs
):
context
=
{
'app'
:
_
(
'Terminal'
),
'action'
:
_
(
'Command list'
),
'user_list'
:
utils
.
get_
user_list_from_cache
(),
'asset_list'
:
utils
.
get_
asset_list_from_cache
(),
'system_user_list'
:
utils
.
get_s
ystem_user_list_from_cache
(),
'user_list'
:
utils
.
get_
session_user_list
(),
'asset_list'
:
utils
.
get_
session_asset_list
(),
'system_user_list'
:
utils
.
get_s
ession_system_user_list
(),
'command'
:
self
.
command
,
'date_from'
:
self
.
date_from
,
'date_to'
:
self
.
date_to
,
'user
name
'
:
self
.
user
,
'user'
:
self
.
user
,
'asset'
:
self
.
asset
,
'system_user'
:
self
.
system_user
,
}
...
...
apps/terminal/views/session.py
View file @
e0a3fafb
...
...
@@ -59,7 +59,7 @@ class SessionListView(AdminUserRequiredMixin, DatetimeSearchMixin, ListView):
'system_user_list'
:
utils
.
get_system_user_list_from_cache
(),
'date_from'
:
self
.
date_from
,
'date_to'
:
self
.
date_to
,
'user
name
'
:
self
.
user
,
'user'
:
self
.
user
,
'asset'
:
self
.
asset
,
'system_user'
:
self
.
system_user
,
}
...
...
apps/users/models/user.py
View file @
e0a3fafb
...
...
@@ -22,10 +22,14 @@ signer = get_signer()
class
User
(
AbstractUser
):
ROLE_ADMIN
=
'Admin'
ROLE_USER
=
'User'
ROLE_APP
=
'App'
ROLE_CHOICES
=
(
(
'Admin'
,
'Administrator'
),
(
'User'
,
'User'
),
(
'App'
,
'Application'
)
(
ROLE_ADMIN
,
'Administrator'
),
(
ROLE_USER
,
'User'
),
(
ROLE_APP
,
'Application'
)
)
id
=
models
.
UUIDField
(
default
=
uuid
.
uuid4
,
primary_key
=
True
)
username
=
models
.
CharField
(
max_length
=
128
,
unique
=
True
,
verbose_name
=
_
(
'Username'
))
...
...
apps/users/views/login.py
View file @
e0a3fafb
...
...
@@ -220,7 +220,7 @@ class LoginLogListView(DatetimeSearchMixin, ListView):
date_format
=
'
%
m/
%
d/
%
Y'
def
get_queryset
(
self
):
self
.
username
=
self
.
request
.
GET
.
get
(
'user
name
'
,
''
)
self
.
username
=
self
.
request
.
GET
.
get
(
'user'
,
''
)
self
.
keyword
=
self
.
request
.
GET
.
get
(
"keyword"
,
''
)
queryset
=
super
()
.
get_queryset
()
...
...
@@ -228,7 +228,7 @@ class LoginLogListView(DatetimeSearchMixin, ListView):
datetime__gt
=
self
.
date_from
,
datetime__lt
=
self
.
date_to
)
if
self
.
username
:
queryset
=
self
.
queryset
.
filter
(
username
=
self
.
username
)
queryset
=
queryset
.
filter
(
username
=
self
.
username
)
if
self
.
keyword
:
queryset
=
self
.
queryset
.
filter
(
Q
(
ip__contains
=
self
.
keyword
)
|
...
...
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