##// END OF EJS Templates
vcs: Minimal change to expose the shadow repository...
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.

File last commit:

r868:2073cad3 merge default
r887:175782be default
Show More
gist_schema.py
183 lines | 5.6 KiB | text/x-python | PythonLexer
gists: use colander schema to validate input data....
r523 # -*- coding: utf-8 -*-
# Copyright (C) 2016-2016 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License, version 3
# (only), as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# This program is dual-licensed. If you wish to learn more about the
# RhodeCode Enterprise Edition, including its added features, Support services,
# and proprietary license terms, please see https://rhodecode.com/licenses/
import os
import colander
from rhodecode.translation import _
Martin Bornhold
imports: Remove unused imports.
r850 from rhodecode.model.validation_schema import preparers
gists: use colander schema to validate input data....
r523
def nodes_to_sequence(nodes, colander_node=None):
"""
Converts old style dict nodes to new list of dicts
:param nodes: dict with key beeing name of the file
"""
if not isinstance(nodes, dict):
msg = 'Nodes needs to be a dict, got {}'.format(type(nodes))
raise colander.Invalid(colander_node, msg)
out = []
for key, val in nodes.items():
val = (isinstance(val, dict) and val) or {}
out.append(dict(
filename=key,
content=val.get('content'),
mimetype=val.get('mimetype')
))
out = Nodes().deserialize(out)
return out
def sequence_to_nodes(nodes, colander_node=None):
if not isinstance(nodes, list):
msg = 'Nodes needs to be a list, got {}'.format(type(nodes))
raise colander.Invalid(colander_node, msg)
nodes = Nodes().deserialize(nodes)
out = {}
try:
for file_data in nodes:
file_data_skip = file_data.copy()
# if we got filename_org we use it as a key so we keep old
# name as input and rename is-reflected inside the values as
# filename and filename_org differences.
filename_org = file_data.get('filename_org')
filename = filename_org or file_data['filename']
out[filename] = {}
out[filename].update(file_data_skip)
except Exception as e:
msg = 'Invalid data format org_exc:`{}`'.format(repr(e))
raise colander.Invalid(colander_node, msg)
return out
@colander.deferred
def deferred_lifetime_validator(node, kw):
options = kw.get('lifetime_options', [])
return colander.All(
colander.Range(min=-1, max=60 * 24 * 30 * 12),
colander.OneOf([x for x in options]))
def unique_gist_validator(node, value):
from rhodecode.model.db import Gist
existing = Gist.get_by_access_id(value)
if existing:
msg = _(u'Gist with name {} already exists').format(value)
raise colander.Invalid(node, msg)
def filename_validator(node, value):
if value != os.path.basename(value):
msg = _(u'Filename {} cannot be inside a directory').format(value)
raise colander.Invalid(node, msg)
class NodeSchema(colander.MappingSchema):
# if we perform rename this will be org filename
filename_org = colander.SchemaNode(
colander.String(),
preparer=[preparers.strip_preparer,
preparers.non_ascii_strip_preparer],
validator=filename_validator,
missing=None)
filename = colander.SchemaNode(
colander.String(),
preparer=[preparers.strip_preparer,
preparers.non_ascii_strip_preparer],
validator=filename_validator)
content = colander.SchemaNode(
colander.String())
mimetype = colander.SchemaNode(
colander.String(),
missing=None)
class Nodes(colander.SequenceSchema):
filenames = NodeSchema()
def validator(self, node, cstruct):
if not isinstance(cstruct, list):
return
found_filenames = []
for data in cstruct:
filename = data['filename']
if filename in found_filenames:
msg = _('Duplicated value for filename found: `{}`').format(
filename)
raise colander.Invalid(node, msg)
found_filenames.append(filename)
class GistSchema(colander.MappingSchema):
"""
schema = GistSchema()
schema.bind(
lifetime_options = [1,2,3]
)
out = schema.deserialize(dict(
nodes=[
{'filename': 'x', 'content': 'xxx', },
{'filename': 'docs/Z', 'content': 'xxx', 'mimetype': 'x'},
]
))
"""
from rhodecode.model.db import Gist
gistid = colander.SchemaNode(
colander.String(),
missing=None,
preparer=[preparers.strip_preparer,
preparers.non_ascii_strip_preparer,
preparers.slugify_preparer],
validator=colander.All(
colander.Length(min=3),
unique_gist_validator
))
description = colander.SchemaNode(
colander.String(),
missing=u'')
lifetime = colander.SchemaNode(
colander.Integer(),
validator=deferred_lifetime_validator)
gist_acl_level = colander.SchemaNode(
colander.String(),
validator=colander.OneOf([Gist.ACL_LEVEL_PUBLIC,
Gist.ACL_LEVEL_PRIVATE]))
gist_type = colander.SchemaNode(
colander.String(),
Martin Bornhold
gists: Fix wrong default value in colander schema. #4243
r854 missing=Gist.GIST_PUBLIC,
gists: use colander schema to validate input data....
r523 validator=colander.OneOf([Gist.GIST_PRIVATE, Gist.GIST_PUBLIC]))
nodes = Nodes()