Show More
@@ -23,8 +23,9 b' import re' | |||
|
23 | 23 | import logging |
|
24 | 24 | import datetime |
|
25 | 25 | import configparser |
|
26 | from sqlalchemy import Table | |
|
26 | 27 | |
|
27 | from rhodecode.model.db import Session, User, UserSshKeys | |
|
28 | from rhodecode.lib.utils2 import AttributeDict | |
|
28 | 29 | from rhodecode.model.scm import ScmModel |
|
29 | 30 | |
|
30 | 31 | from .hg import MercurialServer |
@@ -39,11 +40,11 b' class SshWrapper(object):' | |||
|
39 | 40 | svn_cmd_pat = re.compile(r'^svnserve -t') |
|
40 | 41 | |
|
41 | 42 | def __init__(self, command, connection_info, mode, |
|
42 | user, user_id, key_id, shell, ini_path, env): | |
|
43 | user, user_id, key_id: int, shell, ini_path: str, env): | |
|
43 | 44 | self.command = command |
|
44 | 45 | self.connection_info = connection_info |
|
45 | 46 | self.mode = mode |
|
46 | self.user = user | |
|
47 | self.username = user | |
|
47 | 48 | self.user_id = user_id |
|
48 | 49 | self.key_id = key_id |
|
49 | 50 | self.shell = shell |
@@ -59,14 +60,38 b' class SshWrapper(object):' | |||
|
59 | 60 | return parser |
|
60 | 61 | |
|
61 | 62 | def update_key_access_time(self, key_id): |
|
62 | key = UserSshKeys().query().filter( | |
|
63 | UserSshKeys.ssh_key_id == key_id).scalar() | |
|
64 | if key: | |
|
65 | key.accessed_on = datetime.datetime.utcnow() | |
|
66 | Session().add(key) | |
|
67 | Session().commit() | |
|
63 | from rhodecode.model.meta import raw_query_executor, Base | |
|
64 | ||
|
65 | table = Table('user_ssh_keys', Base.metadata, autoload=False) | |
|
66 | stmt = ( | |
|
67 | table.update() | |
|
68 | .where(table.c.ssh_key_id == key_id) | |
|
69 | .values(accessed_on=datetime.datetime.utcnow()) | |
|
70 | .returning(table.c.accessed_on, table.c.ssh_key_fingerprint) | |
|
71 | ) | |
|
72 | ||
|
73 | scalar_res = None | |
|
74 | with raw_query_executor() as session: | |
|
75 | result = session.execute(stmt) | |
|
76 | if result.rowcount: | |
|
77 | scalar_res = result.first() | |
|
78 | ||
|
79 | if scalar_res: | |
|
80 | atime, ssh_key_fingerprint = scalar_res | |
|
68 | 81 | log.debug('Update key id:`%s` fingerprint:`%s` access time', |
|
69 |
key_id, |
|
|
82 | key_id, ssh_key_fingerprint) | |
|
83 | ||
|
84 | def get_user(self, user_id): | |
|
85 | user = AttributeDict() | |
|
86 | # lazy load db imports | |
|
87 | from rhodecode.model.db import User | |
|
88 | dbuser = User.get(user_id) | |
|
89 | if not dbuser: | |
|
90 | return None | |
|
91 | user.user_id = dbuser.user_id | |
|
92 | user.username = dbuser.username | |
|
93 | user.auth_user = dbuser.AuthUser() | |
|
94 | return user | |
|
70 | 95 | |
|
71 | 96 | def get_connection_info(self): |
|
72 | 97 | """ |
@@ -186,7 +211,7 b' class SshWrapper(object):' | |||
|
186 | 211 | |
|
187 | 212 | def wrap(self): |
|
188 | 213 | mode = self.mode |
|
189 | user = self.user | |
|
214 | username = self.username | |
|
190 | 215 | user_id = self.user_id |
|
191 | 216 | key_id = self.key_id |
|
192 | 217 | shell = self.shell |
@@ -194,15 +219,16 b' class SshWrapper(object):' | |||
|
194 | 219 | scm_detected, scm_repo, scm_mode = self.get_repo_details(mode) |
|
195 | 220 | |
|
196 | 221 | log.debug( |
|
197 | 'Mode: `%s` User: `%s:%s` Shell: `%s` SSH Command: `\"%s\"` ' | |
|
222 | 'Mode: `%s` User: `name:%s : id:%s` Shell: `%s` SSH Command: `\"%s\"` ' | |
|
198 | 223 | 'SCM_DETECTED: `%s` SCM Mode: `%s` SCM Repo: `%s`', |
|
199 | mode, user, user_id, shell, self.command, | |
|
224 | mode, username, user_id, shell, self.command, | |
|
200 | 225 | scm_detected, scm_mode, scm_repo) |
|
201 | 226 | |
|
227 | log.debug('SSH Connection info %s', self.get_connection_info()) | |
|
228 | ||
|
202 | 229 | # update last access time for this key |
|
203 | self.update_key_access_time(key_id) | |
|
204 | ||
|
205 | log.debug('SSH Connection info %s', self.get_connection_info()) | |
|
230 | if key_id: | |
|
231 | self.update_key_access_time(key_id) | |
|
206 | 232 | |
|
207 | 233 | if shell and self.command is None: |
|
208 | 234 | log.info('Dropping to shell, no command given and shell is allowed') |
@@ -210,13 +236,13 b' class SshWrapper(object):' | |||
|
210 | 236 | exit_code = 1 |
|
211 | 237 | |
|
212 | 238 | elif scm_detected: |
|
213 |
user = |
|
|
239 | user = self.get_user(user_id) | |
|
214 | 240 | if not user: |
|
215 | 241 | log.warning('User with id %s not found', user_id) |
|
216 | 242 | exit_code = -1 |
|
217 | 243 | return exit_code |
|
218 | 244 | |
|
219 |
auth_user = user. |
|
|
245 | auth_user = user.auth_user | |
|
220 | 246 | permissions = auth_user.permissions['repositories'] |
|
221 | 247 | repo_branch_permissions = auth_user.get_branch_permissions(scm_repo) |
|
222 | 248 | try: |
@@ -24,10 +24,10 b' SQLAlchemy Metadata and Session object' | |||
|
24 | 24 | |
|
25 | 25 | from sqlalchemy.orm import declarative_base |
|
26 | 26 | from sqlalchemy.orm import scoped_session, sessionmaker |
|
27 | ||
|
27 | from sqlalchemy.orm import Session as SASession | |
|
28 | 28 | from rhodecode.lib import caching_query |
|
29 | 29 | |
|
30 | __all__ = ['Base', 'Session'] | |
|
30 | __all__ = ['Base', 'Session', 'raw_query_executor'] | |
|
31 | 31 | |
|
32 | 32 | # scoped_session. Apply our custom CachingQuery class to it, |
|
33 | 33 | # using a callable that will associate the dictionary |
@@ -43,3 +43,9 b' Session = scoped_session(' | |||
|
43 | 43 | |
|
44 | 44 | # The declarative Base |
|
45 | 45 | Base = declarative_base() |
|
46 | ||
|
47 | ||
|
48 | def raw_query_executor(): | |
|
49 | engine = Base.metadata.bind | |
|
50 | session = SASession(engine) | |
|
51 | return session |
General Comments 0
You need to be logged in to leave comments.
Login now