author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 27 Apr 2010 18:27:43 +0200 | |
branch | stable |
changeset 5419 | 0b7805928a27 |
parent 5168 | 1ab032df5ca3 |
child 5421 | 8167de96c523 |
permissions | -rw-r--r-- |
0 | 1 |
"""Helper classes to execute RQL queries on a set of sources, performing |
2 |
security checking and data aggregation. |
|
3 |
||
4 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
5 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 6 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1954
diff
changeset
|
7 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 8 |
""" |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4764
diff
changeset
|
9 |
from __future__ import with_statement |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4764
diff
changeset
|
10 |
|
0 | 11 |
__docformat__ = "restructuredtext en" |
12 |
||
13 |
from itertools import repeat |
|
14 |
||
15 |
from logilab.common.cache import Cache |
|
16 |
from logilab.common.compat import any |
|
4719
aaed3f813ef8
kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4654
diff
changeset
|
17 |
from rql import RQLSyntaxError |
0 | 18 |
from rql.stmts import Union, Select |
4192 | 19 |
from rql.nodes import Relation, VariableRef, Constant, SubQuery |
0 | 20 |
|
21 |
from cubicweb import Unauthorized, QueryError, UnknownEid, typed_eid |
|
22 |
from cubicweb import server |
|
23 |
from cubicweb.rset import ResultSet |
|
24 |
||
25 |
from cubicweb.server.utils import cleanup_solutions |
|
26 |
from cubicweb.server.rqlannotation import SQLGenAnnotator, set_qdata |
|
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4759
diff
changeset
|
27 |
from cubicweb.server.ssplanner import READ_ONLY_RTYPES, add_types_restriction |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4764
diff
changeset
|
28 |
from cubicweb.server.session import security_enabled |
2601
5196d213f4e3
[R querier] new READ_ONLY_RTYPES constant
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2593
diff
changeset
|
29 |
|
4722
9c13d5db03d9
pylint suggested refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
30 |
def empty_rset(rql, args, rqlst=None): |
0 | 31 |
"""build an empty result set object""" |
32 |
return ResultSet([], rql, args, rqlst=rqlst) |
|
33 |
||
34 |
def update_varmap(varmap, selected, table): |
|
35 |
"""return a sql schema to store RQL query result""" |
|
36 |
for i, term in enumerate(selected): |
|
37 |
key = term.as_string() |
|
38 |
value = '%s.C%s' % (table, i) |
|
39 |
if varmap.get(key, value) != value: |
|
40 |
raise Exception('variable name conflict on %s' % key) |
|
41 |
varmap[key] = value |
|
42 |
||
43 |
# permission utilities ######################################################## |
|
44 |
||
45 |
def check_no_password_selected(rqlst): |
|
46 |
"""check that Password entities are not selected""" |
|
47 |
for solution in rqlst.solutions: |
|
48 |
if 'Password' in solution.itervalues(): |
|
49 |
raise Unauthorized('Password selection is not allowed') |
|
50 |
||
5419
0b7805928a27
[repo security] deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
51 |
def term_etype(session, term, solution, args): |
0b7805928a27
[repo security] deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
52 |
"""return the entity type for the given term (a VariableRef or a Constant |
0b7805928a27
[repo security] deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
53 |
node) |
0b7805928a27
[repo security] deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
54 |
""" |
0b7805928a27
[repo security] deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
55 |
try: |
0b7805928a27
[repo security] deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
56 |
return solution[term.name] |
0b7805928a27
[repo security] deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
57 |
except AttributeError: |
0b7805928a27
[repo security] deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
58 |
return session.describe(term.eval(args))[0] |
0b7805928a27
[repo security] deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
59 |
|
0b7805928a27
[repo security] deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
60 |
def check_read_access(session, rqlst, solution, args): |
0 | 61 |
"""check that the given user has credentials to access data read the |
62 |
query |
|
63 |
||
64 |
return a dict defining necessary local checks (due to use of rql expression |
|
65 |
in the schema), keys are variable names and values associated rql expression |
|
66 |
for the associated variable with the given solution |
|
67 |
""" |
|
5419
0b7805928a27
[repo security] deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
68 |
# use `term_etype` since we've to deal with rewritten constants here, |
0b7805928a27
[repo security] deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
69 |
# when used as an external source by another repository. |
0b7805928a27
[repo security] deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
70 |
# XXX what about local read security w/ those rewritten constants... |
0b7805928a27
[repo security] deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
71 |
schema = session.repo.schema |
0 | 72 |
if rqlst.where is not None: |
73 |
for rel in rqlst.where.iget_nodes(Relation): |
|
74 |
# XXX has_text may have specific perm ? |
|
2601
5196d213f4e3
[R querier] new READ_ONLY_RTYPES constant
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2593
diff
changeset
|
75 |
if rel.r_type in READ_ONLY_RTYPES: |
0 | 76 |
continue |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
77 |
rschema = schema.rschema(rel.r_type) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
78 |
if rschema.final: |
5419
0b7805928a27
[repo security] deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
79 |
eschema = schema.eschema(term_etype(session, rel.children[0], |
0b7805928a27
[repo security] deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
80 |
solution, args)) |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
81 |
rdef = eschema.rdef(rschema) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
82 |
else: |
5419
0b7805928a27
[repo security] deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
83 |
rdef = rschema.rdef(term_etype(session, rel.children[0], |
0b7805928a27
[repo security] deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
84 |
solution, args), |
0b7805928a27
[repo security] deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
85 |
term_etype(session, rel.children[1].children[0], |
0b7805928a27
[repo security] deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
86 |
solution, args)) |
0b7805928a27
[repo security] deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
87 |
if not session.user.matching_groups(rdef.get_groups('read')): |
4955 | 88 |
# XXX rqlexpr not allowed |
0 | 89 |
raise Unauthorized('read', rel.r_type) |
90 |
localchecks = {} |
|
91 |
# iterate on defined_vars and not on solutions to ignore column aliases |
|
92 |
for varname in rqlst.defined_vars: |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
93 |
eschema = schema.eschema(solution[varname]) |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
94 |
if eschema.final: |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
95 |
continue |
5419
0b7805928a27
[repo security] deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
96 |
if not session.user.matching_groups(eschema.get_groups('read')): |
0 | 97 |
erqlexprs = eschema.get_rqlexprs('read') |
98 |
if not erqlexprs: |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
99 |
ex = Unauthorized('read', solution[varname]) |
0 | 100 |
ex.var = varname |
101 |
raise ex |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
102 |
localchecks[varname] = erqlexprs |
0 | 103 |
return localchecks |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
104 |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
105 |
def add_noinvariant(noinvariant, restricted, select, nbtrees): |
0 | 106 |
# a variable can actually be invariant if it has not been restricted for |
107 |
# security reason or if security assertion hasn't modified the possible |
|
108 |
# solutions for the query |
|
109 |
if nbtrees != 1: |
|
110 |
for vname in restricted: |
|
111 |
try: |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
112 |
noinvariant.add(select.defined_vars[vname]) |
0 | 113 |
except KeyError: |
114 |
# this is an alias |
|
115 |
continue |
|
116 |
else: |
|
117 |
for vname in restricted: |
|
118 |
try: |
|
119 |
var = select.defined_vars[vname] |
|
120 |
except KeyError: |
|
121 |
# this is an alias |
|
122 |
continue |
|
123 |
if len(var.stinfo['possibletypes']) != 1: |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
124 |
noinvariant.add(var) |
0 | 125 |
|
126 |
def _expand_selection(terms, selected, aliases, select, newselect): |
|
127 |
for term in terms: |
|
128 |
for vref in term.iget_nodes(VariableRef): |
|
129 |
if not vref.name in selected: |
|
130 |
select.append_selected(vref) |
|
131 |
colalias = newselect.get_variable(vref.name, len(aliases)) |
|
132 |
aliases.append(VariableRef(colalias)) |
|
133 |
selected.add(vref.name) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
134 |
|
0 | 135 |
# Plans ####################################################################### |
136 |
||
137 |
class ExecutionPlan(object): |
|
138 |
"""the execution model of a rql query, composed of querier steps""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
139 |
|
0 | 140 |
def __init__(self, querier, rqlst, args, session): |
141 |
# original rql syntax tree |
|
142 |
self.rqlst = rqlst |
|
143 |
self.args = args or {} |
|
144 |
# session executing the query |
|
145 |
self.session = session |
|
146 |
# quick reference to the system source |
|
147 |
self.syssource = session.pool.source('system') |
|
148 |
# execution steps |
|
149 |
self.steps = [] |
|
150 |
# index of temporary tables created during execution |
|
151 |
self.temp_tables = {} |
|
152 |
# various resource accesors |
|
153 |
self.querier = querier |
|
154 |
self.schema = querier.schema |
|
155 |
self.sqlannotate = querier.sqlgen_annotate |
|
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3171
diff
changeset
|
156 |
self.rqlhelper = session.vreg.rqlhelper |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
157 |
|
0 | 158 |
def annotate_rqlst(self): |
159 |
if not self.rqlst.annotated: |
|
160 |
self.rqlhelper.annotate(self.rqlst) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
161 |
|
0 | 162 |
def add_step(self, step): |
163 |
"""add a step to the plan""" |
|
164 |
self.steps.append(step) |
|
165 |
||
166 |
def clean(self): |
|
167 |
"""remove temporary tables""" |
|
168 |
self.syssource.clean_temp_data(self.session, self.temp_tables) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
169 |
|
0 | 170 |
def sqlexec(self, sql, args=None): |
171 |
return self.syssource.sqlexec(self.session, sql, args) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
172 |
|
0 | 173 |
def execute(self): |
174 |
"""execute a plan and return resulting rows""" |
|
175 |
try: |
|
176 |
for step in self.steps: |
|
177 |
result = step.execute() |
|
178 |
# the latest executed step contains the full query result |
|
179 |
return result |
|
180 |
finally: |
|
181 |
self.clean() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
182 |
|
5168
1ab032df5ca3
SQL Server port: temporary table handling
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5072
diff
changeset
|
183 |
def make_temp_table_name(self, table): |
1ab032df5ca3
SQL Server port: temporary table handling
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5072
diff
changeset
|
184 |
""" |
1ab032df5ca3
SQL Server port: temporary table handling
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5072
diff
changeset
|
185 |
return a temp table name according to db backend |
1ab032df5ca3
SQL Server port: temporary table handling
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5072
diff
changeset
|
186 |
""" |
1ab032df5ca3
SQL Server port: temporary table handling
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5072
diff
changeset
|
187 |
return self.syssource.make_temp_table_name(table) |
1ab032df5ca3
SQL Server port: temporary table handling
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5072
diff
changeset
|
188 |
|
1ab032df5ca3
SQL Server port: temporary table handling
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5072
diff
changeset
|
189 |
|
0 | 190 |
def init_temp_table(self, table, selected, sol): |
191 |
"""initialize sql schema and variable map for a temporary table which |
|
192 |
will be used to store result for the given rqlst |
|
193 |
""" |
|
194 |
try: |
|
195 |
outputmap, sqlschema, _ = self.temp_tables[table] |
|
196 |
update_varmap(outputmap, selected, table) |
|
197 |
except KeyError: |
|
198 |
sqlschema, outputmap = self.syssource.temp_table_def(selected, sol, |
|
199 |
table) |
|
200 |
self.temp_tables[table] = [outputmap, sqlschema, False] |
|
201 |
return outputmap |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
202 |
|
0 | 203 |
def create_temp_table(self, table): |
204 |
"""create a temporary table to store result for the given rqlst""" |
|
205 |
if not self.temp_tables[table][-1]: |
|
206 |
sqlschema = self.temp_tables[table][1] |
|
207 |
self.syssource.create_temp_table(self.session, table, sqlschema) |
|
208 |
self.temp_tables[table][-1] = True |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
209 |
|
0 | 210 |
def preprocess(self, union, security=True): |
211 |
"""insert security when necessary then annotate rql st for sql generation |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
212 |
|
0 | 213 |
return rqlst to actually execute |
214 |
""" |
|
4954
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
215 |
cached = None |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4764
diff
changeset
|
216 |
if security and self.session.read_security: |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4764
diff
changeset
|
217 |
# ensure security is turned of when security is inserted, |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4764
diff
changeset
|
218 |
# else we may loop for ever... |
4954
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
219 |
if self.session.transaction_data.get('security-rqlst-cache'): |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
220 |
key = self.cache_key |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
221 |
else: |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
222 |
key = None |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
223 |
if key is not None and key in self.session.transaction_data: |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
224 |
cachedunion, args = self.session.transaction_data[key] |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
225 |
union.children[:] = [] |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
226 |
for select in cachedunion.children: |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
227 |
union.append(select) |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
228 |
union.has_text_query = cachedunion.has_text_query |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
229 |
args.update(self.args) |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
230 |
self.args = args |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
231 |
cached = True |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
232 |
else: |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
233 |
noinvariant = set() |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
234 |
with security_enabled(self.session, read=False): |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
235 |
self._insert_security(union, noinvariant) |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
236 |
if key is not None: |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
237 |
self.session.transaction_data[key] = (union, self.args) |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
238 |
else: |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
239 |
noinvariant = () |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
240 |
if cached is None: |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
241 |
self.rqlhelper.simplify(union) |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
242 |
self.sqlannotate(union) |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
243 |
set_qdata(self.schema.rschema, union, noinvariant) |
0 | 244 |
if union.has_text_query: |
245 |
self.cache_key = None |
|
246 |
||
247 |
def _insert_security(self, union, noinvariant): |
|
248 |
for select in union.children[:]: |
|
249 |
for subquery in select.with_: |
|
250 |
self._insert_security(subquery.query, noinvariant) |
|
251 |
localchecks, restricted = self._check_permissions(select) |
|
252 |
if any(localchecks): |
|
253 |
rewrite = self.session.rql_rewriter.rewrite |
|
254 |
nbtrees = len(localchecks) |
|
255 |
myunion = union |
|
256 |
# transform in subquery when len(localchecks)>1 and groups |
|
257 |
if nbtrees > 1 and (select.orderby or select.groupby or |
|
258 |
select.having or select.has_aggregat or |
|
259 |
select.limit or select.offset): |
|
260 |
newselect = Select() |
|
261 |
# only select variables in subqueries |
|
262 |
origselection = select.selection |
|
263 |
select.select_only_variables() |
|
264 |
select.has_aggregat = False |
|
265 |
# create subquery first so correct node are used on copy |
|
266 |
# (eg ColumnAlias instead of Variable) |
|
267 |
aliases = [VariableRef(newselect.get_variable(vref.name, i)) |
|
268 |
for i, vref in enumerate(select.selection)] |
|
269 |
selected = set(vref.name for vref in aliases) |
|
270 |
# now copy original selection and groups |
|
271 |
for term in origselection: |
|
272 |
newselect.append_selected(term.copy(newselect)) |
|
273 |
if select.orderby: |
|
274 |
newselect.set_orderby([s.copy(newselect) for s in select.orderby]) |
|
275 |
_expand_selection(select.orderby, selected, aliases, select, newselect) |
|
276 |
select.orderby = () # XXX dereference? |
|
277 |
if select.groupby: |
|
278 |
newselect.set_groupby([g.copy(newselect) for g in select.groupby]) |
|
279 |
_expand_selection(select.groupby, selected, aliases, select, newselect) |
|
280 |
select.groupby = () # XXX dereference? |
|
281 |
if select.having: |
|
282 |
newselect.set_having([g.copy(newselect) for g in select.having]) |
|
283 |
_expand_selection(select.having, selected, aliases, select, newselect) |
|
284 |
select.having = () # XXX dereference? |
|
285 |
if select.limit: |
|
286 |
newselect.limit = select.limit |
|
287 |
select.limit = None |
|
288 |
if select.offset: |
|
289 |
newselect.offset = select.offset |
|
290 |
select.offset = 0 |
|
291 |
myunion = Union() |
|
292 |
newselect.set_with([SubQuery(aliases, myunion)], check=False) |
|
293 |
solutions = [sol.copy() for sol in select.solutions] |
|
294 |
cleanup_solutions(newselect, solutions) |
|
295 |
newselect.set_possible_types(solutions) |
|
296 |
# if some solutions doesn't need rewriting, insert original |
|
297 |
# select as first union subquery |
|
298 |
if () in localchecks: |
|
299 |
myunion.append(select) |
|
300 |
# we're done, replace original select by the new select with |
|
301 |
# subqueries (more added in the loop below) |
|
302 |
union.replace(select, newselect) |
|
303 |
elif not () in localchecks: |
|
304 |
union.remove(select) |
|
305 |
for lcheckdef, lchecksolutions in localchecks.iteritems(): |
|
306 |
if not lcheckdef: |
|
307 |
continue |
|
308 |
myrqlst = select.copy(solutions=lchecksolutions) |
|
309 |
myunion.append(myrqlst) |
|
310 |
# in-place rewrite + annotation / simplification |
|
4955 | 311 |
lcheckdef = [((var, 'X'), rqlexprs) for var, rqlexprs in lcheckdef] |
0 | 312 |
rewrite(myrqlst, lcheckdef, lchecksolutions, self.args) |
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
313 |
add_noinvariant(noinvariant, restricted, myrqlst, nbtrees) |
0 | 314 |
if () in localchecks: |
315 |
select.set_possible_types(localchecks[()]) |
|
316 |
add_types_restriction(self.schema, select) |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
317 |
add_noinvariant(noinvariant, restricted, select, nbtrees) |
0 | 318 |
|
319 |
def _check_permissions(self, rqlst): |
|
320 |
"""return a dict defining "local checks", e.g. RQLExpression defined in |
|
321 |
the schema that should be inserted in the original query |
|
322 |
||
323 |
solutions where a variable has a type which the user can't definitly read |
|
324 |
are removed, else if the user may read it (eg if an rql expression is |
|
325 |
defined for the "read" permission of the related type), the local checks |
|
326 |
dict for the solution is updated |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
327 |
|
0 | 328 |
return a dict with entries for each different local check necessary, |
329 |
with associated solutions as value. A local check is defined by a list |
|
330 |
of 2-uple, with variable name as first item and the necessary rql |
|
331 |
expression as second item for each variable which has to be checked. |
|
332 |
So solutions which don't require local checks will be associated to |
|
333 |
the empty tuple key. |
|
334 |
||
335 |
note: rqlst should not have been simplified at this point |
|
336 |
""" |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
337 |
session = self.session |
0 | 338 |
msgs = [] |
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
339 |
neweids = session.transaction_data.get('neweids', ()) |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
340 |
varkwargs = {} |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
341 |
if not session.transaction_data.get('security-rqlst-cache'): |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
342 |
for var in rqlst.defined_vars.itervalues(): |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
343 |
for rel in var.stinfo['uidrels']: |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
344 |
const = rel.children[1].children[0] |
4957
25f20adba80f
[security] take care uidrels may contains a relation targetting something else than a constant node (eg IN function)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4955
diff
changeset
|
345 |
try: |
25f20adba80f
[security] take care uidrels may contains a relation targetting something else than a constant node (eg IN function)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4955
diff
changeset
|
346 |
varkwargs[var.name] = typed_eid(const.eval(self.args)) |
25f20adba80f
[security] take care uidrels may contains a relation targetting something else than a constant node (eg IN function)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4955
diff
changeset
|
347 |
break |
25f20adba80f
[security] take care uidrels may contains a relation targetting something else than a constant node (eg IN function)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4955
diff
changeset
|
348 |
except AttributeError: |
25f20adba80f
[security] take care uidrels may contains a relation targetting something else than a constant node (eg IN function)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4955
diff
changeset
|
349 |
#from rql.nodes import Function |
25f20adba80f
[security] take care uidrels may contains a relation targetting something else than a constant node (eg IN function)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4955
diff
changeset
|
350 |
#assert isinstance(const, Function) |
25f20adba80f
[security] take care uidrels may contains a relation targetting something else than a constant node (eg IN function)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4955
diff
changeset
|
351 |
# X eid IN(...) |
25f20adba80f
[security] take care uidrels may contains a relation targetting something else than a constant node (eg IN function)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4955
diff
changeset
|
352 |
pass |
0 | 353 |
# dictionnary of variables restricted for security reason |
354 |
localchecks = {} |
|
355 |
restricted_vars = set() |
|
356 |
newsolutions = [] |
|
357 |
for solution in rqlst.solutions: |
|
358 |
try: |
|
5419
0b7805928a27
[repo security] deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
359 |
localcheck = check_read_access(session, rqlst, solution, self.args) |
0 | 360 |
except Unauthorized, ex: |
361 |
msg = 'remove %s from solutions since %s has no %s access to %s' |
|
5419
0b7805928a27
[repo security] deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
362 |
msg %= (solution, session.user.login, ex.args[0], ex.args[1]) |
0 | 363 |
msgs.append(msg) |
364 |
LOGGER.info(msg) |
|
365 |
else: |
|
366 |
newsolutions.append(solution) |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
367 |
# try to benefit of rqlexpr.check cache for entities which |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
368 |
# are specified by eid in query'args |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
369 |
for varname, eid in varkwargs.iteritems(): |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
370 |
try: |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
371 |
rqlexprs = localcheck.pop(varname) |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
372 |
except KeyError: |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
373 |
continue |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
374 |
if eid in neweids: |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
375 |
continue |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
376 |
for rqlexpr in rqlexprs: |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
377 |
if rqlexpr.check(session, eid): |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
378 |
break |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
379 |
else: |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
380 |
raise Unauthorized() |
0 | 381 |
restricted_vars.update(localcheck) |
382 |
localchecks.setdefault(tuple(localcheck.iteritems()), []).append(solution) |
|
383 |
# raise Unautorized exception if the user can't access to any solution |
|
384 |
if not newsolutions: |
|
385 |
raise Unauthorized('\n'.join(msgs)) |
|
386 |
rqlst.set_possible_types(newsolutions) |
|
387 |
return localchecks, restricted_vars |
|
388 |
||
389 |
def finalize(self, select, solutions, insertedvars): |
|
390 |
rqlst = Union() |
|
391 |
rqlst.append(select) |
|
392 |
for mainvarname, rschema, newvarname in insertedvars: |
|
393 |
nvartype = str(rschema.objects(solutions[0][mainvarname])[0]) |
|
394 |
for sol in solutions: |
|
395 |
sol[newvarname] = nvartype |
|
396 |
select.clean_solutions(solutions) |
|
397 |
self.rqlhelper.annotate(rqlst) |
|
398 |
self.preprocess(rqlst, security=False) |
|
399 |
return rqlst |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
400 |
|
4759 | 401 |
|
0 | 402 |
class InsertPlan(ExecutionPlan): |
403 |
"""an execution model specific to the INSERT rql query |
|
404 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
405 |
|
0 | 406 |
def __init__(self, querier, rqlst, args, session): |
407 |
ExecutionPlan.__init__(self, querier, rqlst, args, session) |
|
408 |
# save originaly selected variable, we may modify this |
|
409 |
# dictionary for substitution (query parameters) |
|
410 |
self.selected = rqlst.selection |
|
411 |
# list of new or updated entities definition (utils.Entity) |
|
412 |
self.e_defs = [[]] |
|
413 |
# list of new relation definition (3-uple (from_eid, r_type, to_eid) |
|
414 |
self.r_defs = [] |
|
415 |
# indexes to track entity definitions bound to relation definitions |
|
416 |
self._r_subj_index = {} |
|
417 |
self._r_obj_index = {} |
|
418 |
self._expanded_r_defs = {} |
|
419 |
||
420 |
def add_entity_def(self, edef): |
|
421 |
"""add an entity definition to build""" |
|
422 |
edef.querier_pending_relations = {} |
|
423 |
self.e_defs[-1].append(edef) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
424 |
|
0 | 425 |
def add_relation_def(self, rdef): |
426 |
"""add an relation definition to build""" |
|
427 |
self.r_defs.append(rdef) |
|
428 |
if not isinstance(rdef[0], int): |
|
429 |
self._r_subj_index.setdefault(rdef[0], []).append(rdef) |
|
430 |
if not isinstance(rdef[2], int): |
|
431 |
self._r_obj_index.setdefault(rdef[2], []).append(rdef) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
432 |
|
0 | 433 |
def substitute_entity_def(self, edef, edefs): |
434 |
"""substitute an incomplete entity definition by a list of complete |
|
435 |
equivalents |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
436 |
|
0 | 437 |
e.g. on queries such as :: |
438 |
INSERT Personne X, Societe Y: X nom N, Y nom 'toto', X travaille Y |
|
439 |
WHERE U login 'admin', U login N |
|
440 |
||
441 |
X will be inserted as many times as U exists, and so the X travaille Y |
|
442 |
relations as to be added as many time as X is inserted |
|
443 |
""" |
|
444 |
if not edefs or not self.e_defs: |
|
445 |
# no result, no entity will be created |
|
446 |
self.e_defs = () |
|
447 |
return |
|
448 |
# first remove the incomplete entity definition |
|
449 |
colidx = self.e_defs[0].index(edef) |
|
450 |
for i, row in enumerate(self.e_defs[:]): |
|
451 |
self.e_defs[i][colidx] = edefs[0] |
|
452 |
samplerow = self.e_defs[i] |
|
453 |
for edef in edefs[1:]: |
|
454 |
row = samplerow[:] |
|
455 |
row[colidx] = edef |
|
456 |
self.e_defs.append(row) |
|
457 |
# now, see if this entity def is referenced as subject in some relation |
|
458 |
# definition |
|
459 |
if self._r_subj_index.has_key(edef): |
|
460 |
for rdef in self._r_subj_index[edef]: |
|
461 |
expanded = self._expanded(rdef) |
|
462 |
result = [] |
|
463 |
for exp_rdef in expanded: |
|
464 |
for edef in edefs: |
|
465 |
result.append( (edef, exp_rdef[1], exp_rdef[2]) ) |
|
466 |
self._expanded_r_defs[rdef] = result |
|
467 |
# and finally, see if this entity def is referenced as object in some |
|
468 |
# relation definition |
|
469 |
if self._r_obj_index.has_key(edef): |
|
470 |
for rdef in self._r_obj_index[edef]: |
|
471 |
expanded = self._expanded(rdef) |
|
472 |
result = [] |
|
473 |
for exp_rdef in expanded: |
|
474 |
for edef in edefs: |
|
475 |
result.append( (exp_rdef[0], exp_rdef[1], edef) ) |
|
476 |
self._expanded_r_defs[rdef] = result |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
477 |
|
0 | 478 |
def _expanded(self, rdef): |
479 |
"""return expanded value for the given relation definition""" |
|
480 |
try: |
|
481 |
return self._expanded_r_defs[rdef] |
|
482 |
except KeyError: |
|
483 |
self.r_defs.remove(rdef) |
|
484 |
return [rdef] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
485 |
|
0 | 486 |
def relation_defs(self): |
487 |
"""return the list for relation definitions to insert""" |
|
488 |
for rdefs in self._expanded_r_defs.values(): |
|
489 |
for rdef in rdefs: |
|
490 |
yield rdef |
|
491 |
for rdef in self.r_defs: |
|
492 |
yield rdef |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
493 |
|
0 | 494 |
def insert_entity_defs(self): |
495 |
"""return eids of inserted entities in a suitable form for the resulting |
|
496 |
result set, e.g.: |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
497 |
|
0 | 498 |
e.g. on queries such as :: |
499 |
INSERT Personne X, Societe Y: X nom N, Y nom 'toto', X travaille Y |
|
500 |
WHERE U login 'admin', U login N |
|
501 |
||
502 |
if there is two entities matching U, the result set will look like |
|
503 |
[(eidX1, eidY1), (eidX2, eidY2)] |
|
504 |
""" |
|
505 |
session = self.session |
|
506 |
repo = session.repo |
|
507 |
results = [] |
|
508 |
for row in self.e_defs: |
|
509 |
results.append([repo.glob_add_entity(session, edef) |
|
510 |
for edef in row]) |
|
511 |
return results |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
512 |
|
0 | 513 |
def insert_relation_defs(self): |
514 |
session = self.session |
|
515 |
repo = session.repo |
|
516 |
for subj, rtype, obj in self.relation_defs(): |
|
517 |
# if a string is given into args instead of an int, we get it here |
|
518 |
if isinstance(subj, basestring): |
|
519 |
subj = typed_eid(subj) |
|
520 |
elif not isinstance(subj, (int, long)): |
|
521 |
subj = subj.eid |
|
522 |
if isinstance(obj, basestring): |
|
523 |
obj = typed_eid(obj) |
|
524 |
elif not isinstance(obj, (int, long)): |
|
525 |
obj = obj.eid |
|
526 |
if repo.schema.rschema(rtype).inlined: |
|
2680
66472d85d548
[R] use req.entity_from_eid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2646
diff
changeset
|
527 |
entity = session.entity_from_eid(subj) |
0 | 528 |
entity[rtype] = obj |
3171
b10f0c1f3fe4
[repo] oops, glob_update_entity takes a list of edited attributes has third argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3109
diff
changeset
|
529 |
repo.glob_update_entity(session, entity, set((rtype,))) |
0 | 530 |
else: |
531 |
repo.glob_add_relation(session, subj, rtype, obj) |
|
532 |
||
533 |
||
534 |
class QuerierHelper(object): |
|
535 |
"""helper class to execute rql queries, putting all things together""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
536 |
|
0 | 537 |
def __init__(self, repo, schema): |
538 |
# system info helper |
|
539 |
self._repo = repo |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
540 |
# instance schema |
0 | 541 |
self.set_schema(schema) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
542 |
|
0 | 543 |
def set_schema(self, schema): |
544 |
self.schema = schema |
|
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3171
diff
changeset
|
545 |
repo = self._repo |
0 | 546 |
# rql parsing / analysing helper |
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3171
diff
changeset
|
547 |
self.solutions = repo.vreg.solutions |
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3171
diff
changeset
|
548 |
self._rql_cache = Cache(repo.config['rql-cache-size']) |
0 | 549 |
self.cache_hit, self.cache_miss = 0, 0 |
550 |
# rql planner |
|
551 |
# note: don't use repo.sources, may not be built yet, and also "admin" |
|
552 |
# isn't an actual source |
|
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3171
diff
changeset
|
553 |
rqlhelper = repo.vreg.rqlhelper |
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3171
diff
changeset
|
554 |
self._parse = rqlhelper.parse |
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3171
diff
changeset
|
555 |
self._annotate = rqlhelper.annotate |
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3171
diff
changeset
|
556 |
if len([uri for uri in repo.config.sources() if uri != 'admin']) < 2: |
0 | 557 |
from cubicweb.server.ssplanner import SSPlanner |
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3171
diff
changeset
|
558 |
self._planner = SSPlanner(schema, rqlhelper) |
0 | 559 |
else: |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
560 |
from cubicweb.server.msplanner import MSPlanner |
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3171
diff
changeset
|
561 |
self._planner = MSPlanner(schema, rqlhelper) |
0 | 562 |
# sql generation annotator |
563 |
self.sqlgen_annotate = SQLGenAnnotator(schema).annotate |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
564 |
|
0 | 565 |
def parse(self, rql, annotate=False): |
566 |
"""return a rql syntax tree for the given rql""" |
|
567 |
try: |
|
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3171
diff
changeset
|
568 |
return self._parse(unicode(rql), annotate=annotate) |
0 | 569 |
except UnicodeError: |
570 |
raise RQLSyntaxError(rql) |
|
571 |
||
572 |
def plan_factory(self, rqlst, args, session): |
|
573 |
"""create an execution plan for an INSERT RQL query""" |
|
574 |
if rqlst.TYPE == 'insert': |
|
575 |
return InsertPlan(self, rqlst, args, session) |
|
576 |
return ExecutionPlan(self, rqlst, args, session) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
577 |
|
0 | 578 |
def execute(self, session, rql, args=None, eid_key=None, build_descr=True): |
579 |
"""execute a rql query, return resulting rows and their description in |
|
580 |
a `ResultSet` object |
|
581 |
||
4654
717310b3d576
docstring improvement
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4533
diff
changeset
|
582 |
* `rql` should be an Unicode string or a plain ASCII string |
0 | 583 |
* `args` the optional parameters dictionary associated to the query |
584 |
* `build_descr` is a boolean flag indicating if the description should |
|
585 |
be built on select queries (if false, the description will be en empty |
|
586 |
list) |
|
587 |
* `eid_key` must be both a key in args and a substitution in the rql |
|
588 |
query. It should be used to enhance cacheability of rql queries. |
|
589 |
It may be a tuple for keys in args. |
|
4654
717310b3d576
docstring improvement
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4533
diff
changeset
|
590 |
`eid_key` must be provided in cases where a eid substitution is provided |
717310b3d576
docstring improvement
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4533
diff
changeset
|
591 |
and resolves ambiguities in the possible solutions inferred for each |
0 | 592 |
variable in the query. |
593 |
||
4654
717310b3d576
docstring improvement
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4533
diff
changeset
|
594 |
on INSERT queries, there will be one row with the eid of each inserted |
0 | 595 |
entity |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
596 |
|
0 | 597 |
result for DELETE and SET queries is undefined yet |
598 |
||
599 |
to maximize the rql parsing/analyzing cache performance, you should |
|
4654
717310b3d576
docstring improvement
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4533
diff
changeset
|
600 |
always use substitute arguments in queries (i.e. avoid query such as |
0 | 601 |
'Any X WHERE X eid 123'!) |
602 |
""" |
|
2621
1b9d08840a0e
R [querier] debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2601
diff
changeset
|
603 |
if server.DEBUG & (server.DBG_RQL | server.DBG_SQL): |
2629
0d445c2171e4
R [querier] debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2621
diff
changeset
|
604 |
if server.DEBUG & (server.DBG_MORE | server.DBG_SQL): |
2621
1b9d08840a0e
R [querier] debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2601
diff
changeset
|
605 |
print '*'*80 |
2646
d2874ddd4347
#F [querier] debugging tweak
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2629
diff
changeset
|
606 |
print 'querier input', rql, args |
0 | 607 |
# parse the query and binds variables |
608 |
if eid_key is not None: |
|
609 |
if not isinstance(eid_key, (tuple, list)): |
|
610 |
eid_key = (eid_key,) |
|
611 |
cachekey = [rql] |
|
612 |
for key in eid_key: |
|
613 |
try: |
|
614 |
etype = self._repo.type_from_eid(args[key], session) |
|
615 |
except KeyError: |
|
616 |
raise QueryError('bad cache key %s (no value)' % key) |
|
617 |
except TypeError: |
|
1954 | 618 |
raise QueryError('bad cache key %s (value: %r)' % ( |
619 |
key, args[key])) |
|
0 | 620 |
except UnknownEid: |
621 |
# we want queries such as "Any X WHERE X eid 9999" |
|
622 |
# return an empty result instead of raising UnknownEid |
|
4722
9c13d5db03d9
pylint suggested refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
623 |
return empty_rset(rql, args) |
0 | 624 |
cachekey.append(etype) |
3109
e7e1bb06b716
ensure eid given in arguments will be returned correctly typed (though only if they are in the cachekey for now)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
625 |
# ensure eid is correctly typed in args |
e7e1bb06b716
ensure eid given in arguments will be returned correctly typed (though only if they are in the cachekey for now)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
626 |
args[key] = typed_eid(args[key]) |
0 | 627 |
cachekey = tuple(cachekey) |
628 |
else: |
|
629 |
cachekey = rql |
|
630 |
try: |
|
631 |
rqlst = self._rql_cache[cachekey] |
|
632 |
self.cache_hit += 1 |
|
633 |
except KeyError: |
|
634 |
self.cache_miss += 1 |
|
635 |
rqlst = self.parse(rql) |
|
636 |
try: |
|
637 |
self.solutions(session, rqlst, args) |
|
638 |
except UnknownEid: |
|
4843
5f7363416765
fix hooks control method name + other litle cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
639 |
# we want queries such as "Any X WHERE X eid 9999" return an |
5f7363416765
fix hooks control method name + other litle cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
640 |
# empty result instead of raising UnknownEid |
4722
9c13d5db03d9
pylint suggested refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
641 |
return empty_rset(rql, args, rqlst) |
0 | 642 |
self._rql_cache[cachekey] = rqlst |
643 |
orig_rqlst = rqlst |
|
5072
072ae171aeb0
[cleanup] style fixes, add nodes, 0.2 cents refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4957
diff
changeset
|
644 |
if rqlst.TYPE != 'select': |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4764
diff
changeset
|
645 |
if session.read_security: |
0 | 646 |
check_no_password_selected(rqlst) |
4843
5f7363416765
fix hooks control method name + other litle cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
647 |
# write query, ensure session's mode is 'write' so connections won't |
5f7363416765
fix hooks control method name + other litle cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
648 |
# be released until commit/rollback |
0 | 649 |
session.mode = 'write' |
650 |
cachekey = None |
|
651 |
else: |
|
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4764
diff
changeset
|
652 |
if session.read_security: |
0 | 653 |
for select in rqlst.children: |
654 |
check_no_password_selected(select) |
|
655 |
# on select query, always copy the cached rqlst so we don't have to |
|
656 |
# bother modifying it. This is not necessary on write queries since |
|
657 |
# a new syntax tree is built from them. |
|
658 |
rqlst = rqlst.copy() |
|
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3171
diff
changeset
|
659 |
self._annotate(rqlst) |
0 | 660 |
# make an execution plan |
661 |
plan = self.plan_factory(rqlst, args, session) |
|
662 |
plan.cache_key = cachekey |
|
663 |
self._planner.build_plan(plan) |
|
664 |
# execute the plan |
|
665 |
try: |
|
666 |
results = plan.execute() |
|
667 |
except Unauthorized: |
|
668 |
# XXX this could be done in security's after_add_relation hooks |
|
669 |
# since it's actually realy only needed there (other relations |
|
670 |
# security is done *before* actual changes, and add/update entity |
|
671 |
# security is done after changes but in an operation, and exception |
|
4533 | 672 |
# generated in operation's events properly generate a rollback on |
0 | 673 |
# the session). Even though, this is done here for a better |
674 |
# consistency: getting an Unauthorized exception means the |
|
675 |
# transaction has been rollbacked |
|
4532
85116e75f561
fix auto-rollback on unauthorized error:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
676 |
# |
85116e75f561
fix auto-rollback on unauthorized error:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
677 |
# notes: |
85116e75f561
fix auto-rollback on unauthorized error:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
678 |
# * we should not reset the pool here, since we don't want the |
85116e75f561
fix auto-rollback on unauthorized error:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
679 |
# session to loose its pool during processing |
85116e75f561
fix auto-rollback on unauthorized error:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
680 |
# * don't rollback if we're in the commit process, will be handled |
85116e75f561
fix auto-rollback on unauthorized error:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
681 |
# by the session |
85116e75f561
fix auto-rollback on unauthorized error:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
682 |
if session.commit_state is None: |
85116e75f561
fix auto-rollback on unauthorized error:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
683 |
session.rollback(reset_pool=False) |
0 | 684 |
raise |
685 |
# build a description for the results if necessary |
|
686 |
descr = () |
|
687 |
if build_descr: |
|
688 |
if rqlst.TYPE == 'select': |
|
689 |
# sample selection |
|
690 |
descr = session.build_description(orig_rqlst, args, results) |
|
691 |
elif rqlst.TYPE == 'insert': |
|
692 |
# on insert plan, some entities may have been auto-casted, |
|
693 |
# so compute description manually even if there is only |
|
694 |
# one solution |
|
695 |
basedescr = [None] * len(plan.selected) |
|
696 |
todetermine = zip(xrange(len(plan.selected)), repeat(False)) |
|
697 |
descr = session._build_descr(results, basedescr, todetermine) |
|
698 |
# FIXME: get number of affected entities / relations on non |
|
699 |
# selection queries ? |
|
700 |
# return a result set object |
|
701 |
return ResultSet(results, rql, args, descr, eid_key, orig_rqlst) |
|
702 |
||
703 |
from logging import getLogger |
|
704 |
from cubicweb import set_log_methods |
|
705 |
LOGGER = getLogger('cubicweb.querier') |
|
706 |
set_log_methods(QuerierHelper, LOGGER) |