Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove auth parameter from postgresql/memory storage backends #2460

Merged
merged 1 commit into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions kinto/core/storage/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ def __init__(self, *args, readonly=False, **kwargs):
self.readonly = readonly
self.flush()

def flush(self, auth=None):
def flush(self):
self._store = tree()
self._cemetery = tree()
self._timestamps = defaultdict(dict)

@synchronized
def resource_timestamp(self, resource_name, parent_id, auth=None):
def resource_timestamp(self, resource_name, parent_id):
ts = self._timestamps[parent_id].get(resource_name)
if ts is not None:
return ts
Expand Down Expand Up @@ -187,7 +187,6 @@ def create(
id_generator=None,
id_field=DEFAULT_ID_FIELD,
modified_field=DEFAULT_MODIFIED_FIELD,
auth=None,
):
id_generator = id_generator or self.id_generator
obj = {**obj}
Expand Down Expand Up @@ -217,7 +216,6 @@ def get(
object_id,
id_field=DEFAULT_ID_FIELD,
modified_field=DEFAULT_MODIFIED_FIELD,
auth=None,
):
objects = self._store[parent_id][resource_name]
if object_id not in objects:
Expand All @@ -234,7 +232,6 @@ def update(
obj,
id_field=DEFAULT_ID_FIELD,
modified_field=DEFAULT_MODIFIED_FIELD,
auth=None,
):
obj = {**obj}
obj[id_field] = object_id
Expand All @@ -256,7 +253,6 @@ def delete(
with_deleted=True,
modified_field=DEFAULT_MODIFIED_FIELD,
deleted_field=DEFAULT_DELETED_FIELD,
auth=None,
last_modified=None,
):
existing = self.get(resource_name, parent_id, object_id)
Expand Down Expand Up @@ -288,7 +284,6 @@ def purge_deleted(
before=None,
id_field=DEFAULT_ID_FIELD,
modified_field=DEFAULT_MODIFIED_FIELD,
auth=None,
):
parent_id_match = re.compile(parent_id.replace("*", ".*"))
by_parent_id = {
Expand Down Expand Up @@ -326,7 +321,6 @@ def list_all(
id_field=DEFAULT_ID_FIELD,
modified_field=DEFAULT_MODIFIED_FIELD,
deleted_field=DEFAULT_DELETED_FIELD,
auth=None,
):
objects = _get_objects_by_parent_id(self._store, parent_id, resource_name)

Expand Down Expand Up @@ -361,7 +355,6 @@ def count_all(
id_field=DEFAULT_ID_FIELD,
modified_field=DEFAULT_MODIFIED_FIELD,
deleted_field=DEFAULT_DELETED_FIELD,
auth=None,
):
objects = _get_objects_by_parent_id(self._store, parent_id, resource_name)
_, count = self.extract_object_set(
Expand All @@ -387,7 +380,6 @@ def delete_all(
with_deleted=True,
modified_field=DEFAULT_MODIFIED_FIELD,
deleted_field=DEFAULT_DELETED_FIELD,
auth=None,
):
objects = _get_objects_by_parent_id(self._store, parent_id, resource_name, with_meta=True)
objects, count = self.extract_object_set(
Expand Down
15 changes: 2 additions & 13 deletions kinto/core/storage/postgresql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def get_installed_version(self):
MAX_FLUSHABLE_SCHEMA_VERSION = 20
return MAX_FLUSHABLE_SCHEMA_VERSION

def flush(self, auth=None):
def flush(self):
"""Delete objects from tables without destroying schema.

This is used in test suites as well as in the flush plugin.
Expand All @@ -192,7 +192,7 @@ def flush(self, auth=None):
conn.execute(query)
logger.debug("Flushed PostgreSQL storage tables")

def resource_timestamp(self, resource_name, parent_id, auth=None):
def resource_timestamp(self, resource_name, parent_id):
query_existing = """
WITH existing_timestamps AS (
-- Timestamp of latest object.
Expand Down Expand Up @@ -256,7 +256,6 @@ def create(
id_generator=None,
id_field=DEFAULT_ID_FIELD,
modified_field=DEFAULT_MODIFIED_FIELD,
auth=None,
):
id_generator = id_generator or self.id_generator
obj = {**obj}
Expand Down Expand Up @@ -328,7 +327,6 @@ def get(
object_id,
id_field=DEFAULT_ID_FIELD,
modified_field=DEFAULT_MODIFIED_FIELD,
auth=None,
):
query = """
SELECT as_epoch(last_modified) AS last_modified, data
Expand Down Expand Up @@ -360,7 +358,6 @@ def update(
obj,
id_field=DEFAULT_ID_FIELD,
modified_field=DEFAULT_MODIFIED_FIELD,
auth=None,
):

# Remove redundancy in data field
Expand Down Expand Up @@ -407,7 +404,6 @@ def delete(
with_deleted=True,
modified_field=DEFAULT_MODIFIED_FIELD,
deleted_field=DEFAULT_DELETED_FIELD,
auth=None,
last_modified=None,
):
if with_deleted:
Expand Down Expand Up @@ -466,7 +462,6 @@ def delete_all(
with_deleted=True,
modified_field=DEFAULT_MODIFIED_FIELD,
deleted_field=DEFAULT_DELETED_FIELD,
auth=None,
):
if with_deleted:
query = """
Expand Down Expand Up @@ -574,7 +569,6 @@ def purge_deleted(
before=None,
id_field=DEFAULT_ID_FIELD,
modified_field=DEFAULT_MODIFIED_FIELD,
auth=None,
):
delete_tombstones = """
DELETE
Expand Down Expand Up @@ -631,7 +625,6 @@ def list_all(
id_field=DEFAULT_ID_FIELD,
modified_field=DEFAULT_MODIFIED_FIELD,
deleted_field=DEFAULT_DELETED_FIELD,
auth=None,
):

query = """
Expand All @@ -658,7 +651,6 @@ def list_all(
id_field=id_field,
modified_field=modified_field,
deleted_field=deleted_field,
auth=auth,
)

if len(rows) == 0:
Expand All @@ -680,7 +672,6 @@ def count_all(
id_field=DEFAULT_ID_FIELD,
modified_field=DEFAULT_MODIFIED_FIELD,
deleted_field=DEFAULT_DELETED_FIELD,
auth=None,
):

query = """
Expand All @@ -699,7 +690,6 @@ def count_all(
id_field=id_field,
modified_field=modified_field,
deleted_field=deleted_field,
auth=auth,
)
return rows[0]["total_count"]

Expand All @@ -716,7 +706,6 @@ def _get_rows(
id_field=DEFAULT_ID_FIELD,
modified_field=DEFAULT_MODIFIED_FIELD,
deleted_field=DEFAULT_DELETED_FIELD,
auth=None,
):

# Unsafe strings escaped by PostgreSQL
Expand Down
20 changes: 9 additions & 11 deletions kinto/core/storage/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ def setUp(self):
self.client_error_patcher = None

self.obj = {"foo": "bar"}
self.storage_kw = {"resource_name": "test", "parent_id": "1234", "auth": "Basic bWF0OjI="}
self.storage_kw = {"resource_name": "test", "parent_id": "1234"}
self.other_parent_id = "5678"
self.other_auth = "Basic bWF0OjE="
self.auth = "Basic bWF0OjE="

def _get_config(self, settings=None):
"""Mock Pyramid config object.
Expand Down Expand Up @@ -76,15 +76,15 @@ def test_backend_error_is_raised_anywhere(self):
for call, kwargs in calls:
kwargs.update(**self.storage_kw)
self.assertRaises(exceptions.BackendError, call, **kwargs)
self.assertRaises(exceptions.BackendError, self.storage.flush, auth=self.other_auth)
self.assertRaises(exceptions.BackendError, self.storage.flush)

def test_initialize_schema_is_idempotent(self):
self.storage.initialize_schema()
self.storage.initialize_schema() # not raising.

def test_ping_returns_false_if_unavailable(self):
request = DummyRequest()
request.headers["Authorization"] = self.storage_kw["auth"]
request.headers["Authorization"] = self.auth
request.registry.settings = {"readonly": "false"}
ping = heartbeat(self.storage)

Expand All @@ -99,7 +99,7 @@ def test_ping_returns_false_if_unavailable(self):

def test_ping_returns_true_when_working(self):
request = DummyRequest()
request.headers["Authorization"] = "Basic bWF0OjI="
request.headers["Authorization"] = self.auth
ping = heartbeat(self.storage)
with mock.patch("kinto.core.storage.random.SystemRandom.random", return_value=0.7):
self.assertTrue(ping(request))
Expand All @@ -108,14 +108,14 @@ def test_ping_returns_true_when_working(self):

def test_ping_returns_true_when_working_in_readonly_mode(self):
request = DummyRequest()
request.headers["Authorization"] = "Basic bWF0OjI="
request.headers["Authorization"] = self.auth
request.registry.settings = {"readonly": "true"}
ping = heartbeat(self.storage)
self.assertTrue(ping(request))

def test_ping_returns_false_if_unavailable_in_readonly_mode(self):
request = DummyRequest()
request.headers["Authorization"] = "Basic bWF0OjI="
request.headers["Authorization"] = self.auth
request.registry.settings = {"readonly": "true"}
ping = heartbeat(self.storage)
with mock.patch.object(
Expand All @@ -135,7 +135,7 @@ def test_ping_logs_error_if_unavailable(self):

def test_ping_leaves_no_tombstone(self):
request = DummyRequest()
request.headers["Authorization"] = "Basic bWF0OjI="
request.headers["Authorization"] = self.auth
ping = heartbeat(self.storage)
with mock.patch("kinto.core.storage.random.SystemRandom.random", return_value=0.7):
ping(request)
Expand Down Expand Up @@ -1623,7 +1623,6 @@ def test_parent_cannot_access_other_parent_object(self):
resource_name=self.storage_kw["resource_name"],
parent_id=self.other_parent_id,
object_id=obj["id"],
auth=self.other_auth,
)

def test_parent_cannot_delete_other_parent_object(self):
Expand All @@ -1634,14 +1633,13 @@ def test_parent_cannot_delete_other_parent_object(self):
resource_name=self.storage_kw["resource_name"],
parent_id=self.other_parent_id,
object_id=obj["id"],
auth=self.other_auth,
)

def test_parent_cannot_update_other_parent_object(self):
obj = self.create_object()

new_object = {"another": "object"}
kw = {**self.storage_kw, "parent_id": self.other_parent_id, "auth": self.other_auth}
kw = {**self.storage_kw, "parent_id": self.other_parent_id}
self.storage.update(object_id=obj["id"], obj=new_object, **kw)

not_updated = self.storage.get(object_id=obj["id"], **self.storage_kw)
Expand Down