mirror of
https://github.com/webrecorder/pywb.git
synced 2025-03-24 06:59:52 +01:00
perms: refactor perms config to make interface much clearer
'perms_policy' is a callback which returns a Perms object, which may filter cdx lines from the response
This commit is contained in:
parent
7b5cbaa878
commit
681c2fd8d5
@ -1,6 +1,7 @@
|
|||||||
import urllib
|
import urllib
|
||||||
import urllib2
|
import urllib2
|
||||||
|
|
||||||
|
from pywb.perms.perms_filter import make_perms_cdx_filter
|
||||||
|
|
||||||
#=================================================================
|
#=================================================================
|
||||||
class IndexReader(object):
|
class IndexReader(object):
|
||||||
@ -38,7 +39,7 @@ class IndexReader(object):
|
|||||||
|
|
||||||
def load_cdx(self, wbrequest, params):
|
def load_cdx(self, wbrequest, params):
|
||||||
if self.perms_policy:
|
if self.perms_policy:
|
||||||
perms_op = self.perms_policy.create_perms_filter_op(wbrequest)
|
perms_op = make_perms_cdx_filter(self.perms_policy, wbrequest)
|
||||||
if perms_op:
|
if perms_op:
|
||||||
params['custom_ops'] = [perms_op]
|
params['custom_ops'] = [perms_op]
|
||||||
|
|
||||||
|
@ -2,7 +2,25 @@ from pywb.utils.wbexception import AccessException
|
|||||||
|
|
||||||
|
|
||||||
#=================================================================
|
#=================================================================
|
||||||
def create_filter_op(perms_checker):
|
def make_perms_cdx_filter(perms_policy, wbrequest):
|
||||||
|
"""
|
||||||
|
Called internally to convert a perms_policy and a request
|
||||||
|
to a filter which can be applied on the cdx
|
||||||
|
"""
|
||||||
|
perms_checker = perms_policy(wbrequest)
|
||||||
|
if not perms_checker:
|
||||||
|
return None
|
||||||
|
|
||||||
|
return _create_cdx_perms_filter(perms_checker)
|
||||||
|
|
||||||
|
#=================================================================
|
||||||
|
def _create_cdx_perms_filter(perms_checker):
|
||||||
|
"""
|
||||||
|
Return a function which will filter the cdx given
|
||||||
|
a Perms object.
|
||||||
|
:param perms_checker: a Perms object which implements the
|
||||||
|
allow_url_lookup() and access_check_capture() methods
|
||||||
|
"""
|
||||||
|
|
||||||
def perms_filter_op(cdx_iter, query):
|
def perms_filter_op(cdx_iter, query):
|
||||||
"""
|
"""
|
||||||
@ -25,18 +43,24 @@ def create_filter_op(perms_checker):
|
|||||||
|
|
||||||
|
|
||||||
#================================================================
|
#================================================================
|
||||||
class AllowAllPermsPolicy(object):
|
def allow_all_perms_policy(wbrequest):
|
||||||
def create_perms_filter_op(self, wbrequest):
|
"""
|
||||||
return create_filter_op(self.create_perms_checker(wbrequest))
|
Perms policy which always returns a default Perms object
|
||||||
|
which allows everything.
|
||||||
|
|
||||||
def create_perms_checker(self, wbrequest):
|
The perms object is created per request and may store request
|
||||||
return AllowAllPerms()
|
state, if necessary.
|
||||||
|
|
||||||
|
The same perms object may be called with multiple queries
|
||||||
|
(such as for each cdx line) per request.
|
||||||
|
"""
|
||||||
|
return Perms()
|
||||||
|
|
||||||
|
|
||||||
#=================================================================
|
#=================================================================
|
||||||
class AllowAllPerms(object):
|
class Perms(object):
|
||||||
"""
|
"""
|
||||||
Sample Perm Checker which allows all
|
A base perms checker which allows everything
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def allow_url_lookup(self, key):
|
def allow_url_lookup(self, key):
|
||||||
|
@ -19,7 +19,7 @@ class PermsHandler(WbUrlHandler):
|
|||||||
self.url_canon = url_canon
|
self.url_canon = url_canon
|
||||||
|
|
||||||
def __call__(self, wbrequest):
|
def __call__(self, wbrequest):
|
||||||
perms_checker = self.perms_policy.create_perms_checker(wbrequest)
|
perms_checker = self.perms_policy(wbrequest)
|
||||||
|
|
||||||
if wbrequest.wb_url:
|
if wbrequest.wb_url:
|
||||||
return self.check_single_url(wbrequest, perms_checker)
|
return self.check_single_url(wbrequest, perms_checker)
|
||||||
|
@ -8,7 +8,7 @@ from pywb.core.indexreader import IndexReader
|
|||||||
|
|
||||||
from pytest import raises
|
from pytest import raises
|
||||||
|
|
||||||
from tests.fixture import TestExclusionPermsPolicy, testconfig
|
from tests.fixture import testconfig
|
||||||
|
|
||||||
|
|
||||||
#================================================================
|
#================================================================
|
||||||
|
@ -101,4 +101,4 @@ reporter: !!python/object/new:tests.fixture.PrintReporter []
|
|||||||
#domain_specific_rules: rules.yaml
|
#domain_specific_rules: rules.yaml
|
||||||
|
|
||||||
#perms_checker: !!python/object/new:pywb.cdx.perms.AllowAllPerms []
|
#perms_checker: !!python/object/new:pywb.cdx.perms.AllowAllPerms []
|
||||||
perms_policy: !!python/object/new:tests.fixture.TestExclusionPermsPolicy []
|
perms_policy: !!python/name:tests.fixture.test_exclusion_perms_policy
|
||||||
|
@ -3,7 +3,7 @@ import pytest
|
|||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from pywb.perms.perms_filter import AllowAllPerms, AllowAllPermsPolicy
|
from pywb.perms.perms_filter import Perms
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def testconfig():
|
def testconfig():
|
||||||
@ -27,7 +27,7 @@ class PrintReporter:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
#================================================================
|
#================================================================
|
||||||
class TestExclusionPerms(AllowAllPerms):
|
class TestExclusionPerms(Perms):
|
||||||
"""
|
"""
|
||||||
Perm Checker fixture to block a single url for testing
|
Perm Checker fixture to block a single url for testing
|
||||||
"""
|
"""
|
||||||
@ -46,6 +46,5 @@ class TestExclusionPerms(AllowAllPerms):
|
|||||||
|
|
||||||
|
|
||||||
#================================================================
|
#================================================================
|
||||||
class TestExclusionPermsPolicy(AllowAllPermsPolicy):
|
def test_exclusion_perms_policy(wbrequest):
|
||||||
def create_perms_checker(self, wbrequest):
|
return TestExclusionPerms()
|
||||||
return TestExclusionPerms()
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user