From ec63e495fcd3079698bec2c01808a59f7255c926 Mon Sep 17 00:00:00 2001 From: Evan Cordell Date: Wed, 12 Apr 2017 15:47:24 -0400 Subject: [PATCH] Add repo purge callbacks and register TUF metadata deletion as one --- app.py | 2 ++ data/model/__init__.py | 4 ++++ data/model/repository.py | 4 ++++ endpoints/api/repository.py | 3 --- util/tufmetadata/test/test_tufmetadata.py | 14 +++++++++++++- 5 files changed, 23 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index 587432e47..0bc3da2d7 100644 --- a/app.py +++ b/app.py @@ -210,6 +210,8 @@ database.configure(app.config) model.config.app_config = app.config model.config.store = storage model.config.register_image_cleanup_callback(secscan_api.cleanup_layers) +model.config.register_repo_cleanup_callback(tuf_metadata_api.delete_metadata) + @login_manager.user_loader def load_user(user_uuid): diff --git a/data/model/__init__.py b/data/model/__init__.py index 41a5bbf80..9078f2880 100644 --- a/data/model/__init__.py +++ b/data/model/__init__.py @@ -111,9 +111,13 @@ class Config(object): self.app_config = None self.store = None self.image_cleanup_callbacks = [] + self.repo_cleanup_callbacks = [] def register_image_cleanup_callback(self, callback): self.image_cleanup_callbacks.append(callback) + + def register_repo_cleanup_callback(self, callback): + self.repo_cleanup_callbacks.append(callback) config = Config() diff --git a/data/model/repository.py b/data/model/repository.py index ddaaa2e4b..8ca6bba76 100644 --- a/data/model/repository.py +++ b/data/model/repository.py @@ -105,6 +105,10 @@ def purge_repository(namespace_name, repository_name): return False fetched.delete_instance(recursive=True, delete_nullable=False) + + # Run callbacks + for callback in config.repo_cleanup_callbacks: + callback(namespace_name, repository_name) return True diff --git a/endpoints/api/repository.py b/endpoints/api/repository.py index 88b929480..33f7f3eb6 100644 --- a/endpoints/api/repository.py +++ b/endpoints/api/repository.py @@ -420,9 +420,6 @@ class Repository(RepositoryParamResource): # Remove any builds from the queue. dockerfile_build_queue.delete_namespaced_items(namespace, repository) - if features.SIGNING: - tuf_metadata_api.delete_metadata(namespace, repository) - log_action('delete_repo', namespace, {'repo': repository, 'namespace': namespace}) return '', 204 diff --git a/util/tufmetadata/test/test_tufmetadata.py b/util/tufmetadata/test/test_tufmetadata.py index 78046df86..d41fcf6fd 100644 --- a/util/tufmetadata/test/test_tufmetadata.py +++ b/util/tufmetadata/test/test_tufmetadata.py @@ -1,11 +1,14 @@ import pytest import requests -from mock import mock +from mock import mock,patch from flask import Flask from test import testconfig +from test.fixtures import init_db_path from util.tufmetadata import api +from data import model + valid_response = { 'signed' : { @@ -121,3 +124,12 @@ def test_delete_metadata_exception(response_code, exception): tuf_api = api.TUFMetadataAPI(app, app.config, client=client) response = tuf_api.delete_metadata('quay', 'quay') assert response == False + + +def test_purge_repo(init_db_path): + app = Flask(__name__) + app.config.from_object(testconfig.TestConfig()) + app.config["DB_URI"] = init_db_path + with patch('app.tuf_metadata_api') as mock_tuf: + model.repository.purge_repository("ns", "repo") + assert mock_tuf.delete_metadata.called_with("ns", "repo") \ No newline at end of file