Skip to content

Commit

Permalink
Serialize a DisclosureOrigin to/from dict for task (pypi#17276)
Browse files Browse the repository at this point in the history
  • Loading branch information
di authored Dec 12, 2024
1 parent 87e5366 commit d7b266f
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 14 deletions.
25 changes: 25 additions & 0 deletions tests/unit/integration/secrets/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest

from warehouse.integrations.secrets import utils


@pytest.fixture
def someorigin():
return utils.DisclosureOrigin(
name="SomeOrigin",
key_id_header="SOME_KEY_ID_HEADER",
signature_header="SOME_SIGNATURE_HEADER",
verification_url="https://some.verification.url",
)
7 changes: 3 additions & 4 deletions tests/unit/integration/secrets/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,23 @@
from warehouse.integrations.secrets import tasks, utils


def test_analyze_disclosure_task(monkeypatch):
def test_analyze_disclosure_task(monkeypatch, someorigin):
analyze_disclosure = pretend.call_recorder(lambda *a, **k: None)
monkeypatch.setattr(utils, "analyze_disclosure", analyze_disclosure)

request = pretend.stub()
disclosure_record = pretend.stub()
origin = pretend.stub()

tasks.analyze_disclosure_task(
request=request,
disclosure_record=disclosure_record,
origin=origin,
origin=someorigin.to_dict(),
)

assert analyze_disclosure.calls == [
pretend.call(
request=request,
disclosure_record=disclosure_record,
origin=origin,
origin=someorigin,
)
]
35 changes: 26 additions & 9 deletions tests/unit/integration/secrets/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,31 @@
from warehouse.integrations.secrets import tasks, utils


@pytest.fixture
def someorigin():
return utils.DisclosureOrigin(
name="SomeOrigin",
def test_disclosure_origin_serialization(someorigin):
assert (
someorigin.to_dict()
== utils.DisclosureOrigin.from_dict(someorigin.to_dict()).to_dict()
== {
"name": "SomeOrigin",
"key_id_header": "SOME_KEY_ID_HEADER",
"signature_header": "SOME_SIGNATURE_HEADER",
"verification_url": "https://some.verification.url",
"api_token": None,
}
)


def test_disclosure_origin_equivalence(someorigin):
assert someorigin == someorigin
someotherorigin = utils.DisclosureOrigin(
name="SomeOtherOrigin",
key_id_header="SOME_KEY_ID_HEADER",
signature_header="SOME_SIGNATURE_HEADER",
verification_url="https://some.verification.url",
api_token=None,
)
assert someorigin != someotherorigin
assert someorigin != "wu-tang"


def test_token_leak_matcher_extract():
Expand Down Expand Up @@ -726,7 +743,7 @@ def test_analyze_disclosures_wrong_type(metrics, someorigin):
assert exc.value.reason == "format"


def test_analyze_disclosures_raise(metrics, monkeypatch):
def test_analyze_disclosures_raise(metrics, monkeypatch, someorigin):
task = pretend.stub(delay=pretend.call_recorder(lambda *a, **k: None))
request = pretend.stub(task=lambda x: task)

Expand All @@ -735,12 +752,12 @@ def test_analyze_disclosures_raise(metrics, monkeypatch):
utils.analyze_disclosures(
request=request,
disclosure_records=[1, 2, 3],
origin="yay",
origin=someorigin,
metrics=metrics,
)

assert task.delay.calls == [
pretend.call(disclosure_record=1, origin="yay"),
pretend.call(disclosure_record=2, origin="yay"),
pretend.call(disclosure_record=3, origin="yay"),
pretend.call(disclosure_record=1, origin=someorigin.to_dict()),
pretend.call(disclosure_record=2, origin=someorigin.to_dict()),
pretend.call(disclosure_record=3, origin=someorigin.to_dict()),
]
1 change: 1 addition & 0 deletions warehouse/integrations/secrets/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

@tasks.task(ignore_result=True, acks_late=True)
def analyze_disclosure_task(request, disclosure_record, origin):
origin = utils.DisclosureOrigin.from_dict(origin)
utils.analyze_disclosure(
request=request,
disclosure_record=disclosure_record,
Expand Down
27 changes: 26 additions & 1 deletion warehouse/integrations/secrets/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,31 @@ def headers(self):
"""Set of all headers that must be present"""
return {self.key_id_header, self.signature_header}

def to_dict(self):
return {
"name": self.name,
"key_id_header": self.key_id_header,
"signature_header": self.signature_header,
"verification_url": self.verification_url,
"api_token": self.api_token,
}

@classmethod
def from_dict(cls, data):
return cls(**data)

def __eq__(self, other):
if not isinstance(other, DisclosureOrigin):
return False

return (
self.name == other.name
and self.key_id_header == other.key_id_header
and self.signature_header == other.signature_header
and self.verification_url == other.verification_url
and self.api_token == other.api_token
)


class ExtractionFailedError(Exception):
pass
Expand Down Expand Up @@ -314,5 +339,5 @@ def analyze_disclosures(request, disclosure_records, origin, metrics):

for disclosure_record in disclosure_records:
request.task(tasks.analyze_disclosure_task).delay(
disclosure_record=disclosure_record, origin=origin
disclosure_record=disclosure_record, origin=origin.to_dict()
)

0 comments on commit d7b266f

Please sign in to comment.