# HG changeset patch # User Johannes Bornhold # Date 2016-09-18 18:01:19 # Node ID 175782be77c92f74231ddef6ddb3cdd0683c7c48 # Parent 8170c0f9dc08929e9214cd9014ea3ab3127df8b1 vcs: Minimal change to expose the shadow repository Based on my original research, this was the "minimal" starting point. It shows that three concepts are needed for the "repo_name": * From the security standpoint we think of the shadow repository having the same ACL as the target repository of the pull request. This is because the pull request itself is considered to be a part of the target repository. Out of this thought, the variable "acl_repo_name" is used whenever we want to check permissions or when we need the database configuration of the repository. An alternative name would have been "db_repo_name", but the usage for ACL checking is the most important one. * From the web interaction perspective, we need the URL which was originally used to get to the repository. This is because based on this base URL commands can be identified. Especially for Git this is important, so that the commands are correctly recognized. Since the URL is in the focus, this is called "url_repo_name". * Finally we have to deal with the repository on the file system. This is what the VCS layer deal with normally, so this name is called "vcs_repo_name". The original repository interaction is a special case where all three names are the same. When interacting with a pull request, these three names are typically all different. This change is minimal in a sense that it just makes the interaction with a shadow repository barely work, without checking any special constraints yet. This was the starting point for further work on this topic. diff --git a/rhodecode/lib/middleware/simplevcs.py b/rhodecode/lib/middleware/simplevcs.py --- a/rhodecode/lib/middleware/simplevcs.py +++ b/rhodecode/lib/middleware/simplevcs.py @@ -83,12 +83,15 @@ class SimpleVCS(object): SCM = 'unknown' + acl_repo_name = None + url_repo_name = None + vcs_repo_name = None + def __init__(self, application, config, registry): self.registry = registry self.application = application self.config = config # re-populated by specialized middleware - self.repo_name = None self.repo_vcs_config = base.Config() # base path of repo locations @@ -102,6 +105,11 @@ class SimpleVCS(object): self.ip_addr = '0.0.0.0' @property + def repo_name(self): + # TODO: johbo: Remove, switch to correct repo name attribute + return self.acl_repo_name + + @property def scm_app(self): custom_implementation = self.config.get('vcs.scm_app_implementation') if custom_implementation and custom_implementation != 'pyro4': @@ -340,7 +348,8 @@ class SimpleVCS(object): # REQUEST HANDLING # ====================================================================== str_repo_name = safe_str(self.repo_name) - repo_path = os.path.join(safe_str(self.basepath), str_repo_name) + repo_path = os.path.join( + safe_str(self.basepath), safe_str(self.vcs_repo_name)) log.debug('Repository path is %s', repo_path) fix_PATH() @@ -350,7 +359,7 @@ class SimpleVCS(object): action, self.SCM, str_repo_name, safe_str(username), ip_addr) return self._generate_vcs_response( - environ, start_response, repo_path, self.repo_name, extras, action) + environ, start_response, repo_path, self.url_repo_name, extras, action) @initialize_generator def _generate_vcs_response( @@ -365,7 +374,7 @@ class SimpleVCS(object): the first chunk is produced by the underlying WSGI application. """ callback_daemon, extras = self._prepare_callback_daemon(extras) - config = self._create_config(extras, repo_name) + config = self._create_config(extras, self.acl_repo_name) log.debug('HOOKS extras is %s', extras) app = self._create_wsgi_app(repo_path, repo_name, config) diff --git a/rhodecode/lib/middleware/vcs.py b/rhodecode/lib/middleware/vcs.py --- a/rhodecode/lib/middleware/vcs.py +++ b/rhodecode/lib/middleware/vcs.py @@ -176,18 +176,44 @@ class VCSMiddleware(object): environ['PATH_INFO'] = vcs_handler._get_by_id(environ['PATH_INFO']) repo_name = vcs_handler._get_repository_name(environ) + acl_repo_name = repo_name + vcs_repo_name = repo_name + url_repo_name = repo_name + pr_id = None + + # TODO: johbo: recognize a pull request based on pattern matching + if '/pull-request/' in repo_name: + acl_repo_name, other = repo_name.split('/pull-request/') + # TODO: johbo: Set shadow repo path + basename, repo_segment = acl_repo_name.rsplit('/', 1) + pr_id = int(other[0:-len('/repository')]) + vcs_repo_name = '{basename}/.__shadow_{repo_segment}_pr-{pr_id}'.format( + basename=basename, + repo_segment=repo_segment, + pr_id=pr_id) + + log.debug('repo_names %s', { + 'acl_repo_name': acl_repo_name, + 'vcs_repo_name': vcs_repo_name, + 'url_repo_name': url_repo_name, + }) + log.debug('pull_request %s', pr_id) + # check for type, presence in database and on filesystem if not vcs_handler.is_valid_and_existing_repo( - repo_name, vcs_handler.basepath, vcs_handler.SCM): + acl_repo_name, vcs_handler.basepath, vcs_handler.SCM): return HTTPNotFound()(environ, start_response) # TODO: johbo: Needed for the Pyro4 backend and Mercurial only. # Remove once we fully switched to the HTTP backend. - environ['REPO_NAME'] = repo_name + environ['REPO_NAME'] = url_repo_name # register repo_name and it's config back to the handler - vcs_handler.repo_name = repo_name - vcs_handler.repo_vcs_config = self.vcs_config(repo_name) + vcs_handler.acl_repo_name = acl_repo_name + vcs_handler.url_repo_name = url_repo_name + vcs_handler.vcs_repo_name = vcs_repo_name + vcs_handler.pr_id = pr_id + vcs_handler.repo_vcs_config = self.vcs_config(acl_repo_name) vcs_handler = self.wrap_in_gzip_if_enabled( vcs_handler, self.config) diff --git a/rhodecode/tests/lib/middleware/test_simplevcs.py b/rhodecode/tests/lib/middleware/test_simplevcs.py --- a/rhodecode/tests/lib/middleware/test_simplevcs.py +++ b/rhodecode/tests/lib/middleware/test_simplevcs.py @@ -44,7 +44,9 @@ class StubVCSController(simplevcs.Simple def __init__(self, *args, **kwargs): super(StubVCSController, self).__init__(*args, **kwargs) - self.repo_name = HG_REPO + self.acl_repo_name = HG_REPO + self.url_repo_name = HG_REPO + self.vcs_repo_name = HG_REPO def _get_repository_name(self, environ): return HG_REPO