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

Fix first order dangling affirmation delete #3682

Merged
merged 15 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions octopoes/octopoes/core/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,15 @@ def save_origin(
logger.debug("Affirmation source %s already deleted", origin.source)
return

if (
origin.origin_type == OriginType.AFFIRMATION
and [origin.source] == origin.result
originalsouth marked this conversation as resolved.
Show resolved Hide resolved
and not self.origin_repository.list_origins(valid_time=valid_time, result=origin.source)
originalsouth marked this conversation as resolved.
Show resolved Hide resolved
):
logger.debug("Affirmation source %s seems dangling, deleting", origin.source)
self.ooi_repository.delete(origin.source, valid_time)
return

for ooi in oois:
self.ooi_repository.save(ooi, valid_time=valid_time, end_valid_time=end_valid_time)
self.origin_repository.save(origin, valid_time=valid_time)
Expand Down
75 changes: 75 additions & 0 deletions octopoes/tests/integration/test_ooi_deletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from octopoes.models.ooi.dns.records import NXDOMAIN, DNSARecord
from octopoes.models.ooi.dns.zone import Hostname
from octopoes.models.ooi.findings import Finding, KATFindingType
from octopoes.models.ooi.monitoring import Application
from octopoes.models.ooi.network import IPAddressV4, Network
from octopoes.models.ooi.software import Software, SoftwareInstance
from octopoes.models.origin import Origin, OriginType
Expand Down Expand Up @@ -511,3 +512,77 @@ def test_delecration_ooi_delete(octopoes_api_connector: OctopoesAPIConnector, va
octopoes_api_connector.delete(network.reference, valid_time)
time.sleep(1)
assert octopoes_api_connector.list_objects({Hostname}, valid_time).count == 1


def test_dangling_affirmantion_delete(
originalsouth marked this conversation as resolved.
Show resolved Hide resolved
xtdb_octopoes_service: OctopoesService, event_manager: Mock, valid_time: datetime
):
app = Application(name="Acme")

xtdb_octopoes_service.ooi_repository.save(app, valid_time)
time.sleep(1)

declaration = Origin(
origin_type=OriginType.DECLARATION,
method="",
source=app.reference,
result=[app.reference],
task_id=uuid.uuid4(),
)

xtdb_octopoes_service.save_origin(declaration, [app], valid_time)
time.sleep(1)
event_manager.complete_process_events(xtdb_octopoes_service)
time.sleep(1)

assert xtdb_octopoes_service.list_ooi({Application}, valid_time).count == 1
assert (
len(
xtdb_octopoes_service.origin_repository.list_origins(
origin_type=OriginType.DECLARATION, valid_time=valid_time
)
)
== 1
)

xtdb_octopoes_service.origin_repository.delete(declaration, valid_time)
time.sleep(1)
event_manager.complete_process_events(xtdb_octopoes_service)
time.sleep(1)

assert xtdb_octopoes_service.list_ooi({Application}, valid_time).count == 0
assert (
len(
xtdb_octopoes_service.origin_repository.list_origins(
origin_type=OriginType.DECLARATION, valid_time=valid_time
)
)
== 0
)

xtdb_octopoes_service.ooi_repository.save(app, valid_time)
xtdb_octopoes_service.commit()
time.sleep(1)

affirmation = Origin(
origin_type=OriginType.AFFIRMATION,
method="",
source=app.reference,
result=[app.reference],
task_id=uuid.uuid4(),
)

xtdb_octopoes_service.save_origin(affirmation, [app], valid_time)
time.sleep(1)
event_manager.complete_process_events(xtdb_octopoes_service)
time.sleep(1)

assert xtdb_octopoes_service.list_ooi({Application}, valid_time).count == 0
assert (
len(
xtdb_octopoes_service.origin_repository.list_origins(
origin_type=OriginType.AFFIRMATION, valid_time=valid_time
)
)
== 0
)