Change servicekeyworker to use a data interface
This commit is contained in:
parent
0afc222214
commit
932db23a5c
4 changed files with 53 additions and 8 deletions
27
workers/servicekeyworker/models_interface.py
Normal file
27
workers/servicekeyworker/models_interface.py
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
from abc import ABCMeta, abstractmethod
|
||||||
|
from six import add_metaclass
|
||||||
|
|
||||||
|
@add_metaclass(ABCMeta)
|
||||||
|
class ServiceKeyWorkerDataInterface(object):
|
||||||
|
"""
|
||||||
|
Interface that represents all data store interactions required by the service key worker.
|
||||||
|
"""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def set_key_expiration(self, key_id, expiration_date):
|
||||||
|
""" Sets the expiration date of the service key with the given key ID to that given. """
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def create_service_key_for_testing(self, expiration):
|
||||||
|
""" Creates a service key for testing with the given expiration. Returns the KID for
|
||||||
|
key.
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def get_service_key_expiration(self, key_id):
|
||||||
|
""" Returns the expiration date for the key with the given ID. If the key doesn't exist or
|
||||||
|
does not have an expiration, returns None.
|
||||||
|
"""
|
||||||
|
pass
|
19
workers/servicekeyworker/models_pre_oci.py
Normal file
19
workers/servicekeyworker/models_pre_oci.py
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
from data import model
|
||||||
|
from workers.servicekeyworker.models_interface import ServiceKeyWorkerDataInterface
|
||||||
|
|
||||||
|
class PreOCIModel(ServiceKeyWorkerDataInterface):
|
||||||
|
def set_key_expiration(self, kid, expiration_date):
|
||||||
|
model.service_keys.set_key_expiration(kid, expiration_date)
|
||||||
|
|
||||||
|
def create_service_key_for_testing(self, expiration):
|
||||||
|
key = model.service_keys.create_service_key('test', 'somekid', 'quay', '', {}, expiration)
|
||||||
|
return key.kid
|
||||||
|
|
||||||
|
def get_service_key_expiration(self, kid):
|
||||||
|
try:
|
||||||
|
key = model.service_keys.get_service_key(kid, approved_only=False)
|
||||||
|
return key.expiration_date
|
||||||
|
except model.ServiceKeyDoesNotExist:
|
||||||
|
return None
|
||||||
|
|
||||||
|
pre_oci_model = PreOCIModel()
|
|
@ -2,7 +2,7 @@ import logging
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
from app import app, instance_keys
|
from app import app, instance_keys
|
||||||
from data.model.service_keys import set_key_expiration
|
from workers.servicekeyworker.models_pre_oci import pre_oci_model as model
|
||||||
from workers.worker import Worker
|
from workers.worker import Worker
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
@ -19,7 +19,7 @@ class ServiceKeyWorker(Worker):
|
||||||
"""
|
"""
|
||||||
expiration = timedelta(minutes=instance_keys.service_key_expiration)
|
expiration = timedelta(minutes=instance_keys.service_key_expiration)
|
||||||
logger.debug('Starting refresh of automatic service keys')
|
logger.debug('Starting refresh of automatic service keys')
|
||||||
set_key_expiration(instance_keys.local_key_id, datetime.now() + expiration)
|
model.set_key_expiration(instance_keys.local_key_id, datetime.now() + expiration)
|
||||||
logger.debug('Finished refresh of automatic service keys')
|
logger.debug('Finished refresh of automatic service keys')
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
@ -6,18 +6,17 @@ from workers.servicekeyworker.servicekeyworker import ServiceKeyWorker
|
||||||
from util.morecollections import AttrDict
|
from util.morecollections import AttrDict
|
||||||
|
|
||||||
from test.fixtures import *
|
from test.fixtures import *
|
||||||
|
from workers.servicekeyworker.models_pre_oci import pre_oci_model as model
|
||||||
|
|
||||||
def test_refresh_service_key(initialized_db):
|
def test_refresh_service_key(initialized_db):
|
||||||
# Create a service key for testing.
|
# Create a service key for testing.
|
||||||
test_key = model.service_keys.create_service_key('test', 'somekid', 'quay', '', {},
|
original_expiration = datetime.now() + timedelta(minutes=10)
|
||||||
datetime.now() + timedelta(minutes=10))
|
test_key_kid = model.create_service_key_for_testing(original_expiration)
|
||||||
|
|
||||||
instance_keys = AttrDict(dict(local_key_id=test_key.kid, service_key_expiration=10))
|
|
||||||
|
|
||||||
|
instance_keys = AttrDict(dict(local_key_id=test_key_kid, service_key_expiration=10))
|
||||||
with patch('workers.servicekeyworker.servicekeyworker.instance_keys', instance_keys):
|
with patch('workers.servicekeyworker.servicekeyworker.instance_keys', instance_keys):
|
||||||
worker = ServiceKeyWorker()
|
worker = ServiceKeyWorker()
|
||||||
worker._refresh_service_key()
|
worker._refresh_service_key()
|
||||||
|
|
||||||
# Ensure the key's expiration was changed.
|
# Ensure the key's expiration was changed.
|
||||||
updated_key = model.service_keys.get_service_key(test_key.kid, approved_only=False)
|
assert model.get_service_key_expiration(test_key_kid) > original_expiration
|
||||||
assert updated_key.expiration_date > test_key.expiration_date
|
|
||||||
|
|
Reference in a new issue