2016-09-23 21:50:09 +00:00
|
|
|
from abc import ABCMeta, abstractmethod
|
2016-08-31 18:31:43 +00:00
|
|
|
from collections import namedtuple
|
|
|
|
|
2016-09-23 21:50:09 +00:00
|
|
|
from six import add_metaclass
|
2016-08-31 18:31:43 +00:00
|
|
|
|
2016-09-23 21:50:09 +00:00
|
|
|
import data.model
|
2016-08-31 18:31:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ServiceKey(namedtuple('ServiceKey', ['name', 'kid', 'service', 'jwk', 'metadata',
|
|
|
|
'created_date', 'expiration_date', 'rotation_duration',
|
|
|
|
'approval'])):
|
|
|
|
"""
|
|
|
|
Service Key represents a public key (JWK) being used by an instance of a particular service to
|
|
|
|
authenticate with other services.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class ServiceKeyException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class ServiceKeyDoesNotExist(ServiceKeyException):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2016-09-23 21:50:09 +00:00
|
|
|
# TODO(jzelinskie): maybe make this interface support superuser API
|
|
|
|
@add_metaclass(ABCMeta)
|
2016-08-31 18:31:43 +00:00
|
|
|
class KeyServerDataInterface(object):
|
|
|
|
"""
|
|
|
|
Interface that represents all data store interactions required by a JWT key service.
|
|
|
|
"""
|
|
|
|
|
2016-09-23 21:50:09 +00:00
|
|
|
@abstractmethod
|
|
|
|
def list_service_keys(self, service):
|
2016-08-31 18:31:43 +00:00
|
|
|
"""
|
|
|
|
Returns a list of service keys or an empty list if the service does not exist.
|
|
|
|
"""
|
2016-09-23 21:50:09 +00:00
|
|
|
pass
|
2016-08-31 18:31:43 +00:00
|
|
|
|
2016-09-23 21:50:09 +00:00
|
|
|
@abstractmethod
|
|
|
|
def get_service_key(self, signer_kid, service=None, alive_only=None, approved_only=None):
|
2016-08-31 18:31:43 +00:00
|
|
|
"""
|
|
|
|
Returns a service kid with the given kid or raises ServiceKeyNotFound.
|
|
|
|
"""
|
2016-09-23 21:50:09 +00:00
|
|
|
pass
|
2016-08-31 18:31:43 +00:00
|
|
|
|
2016-09-23 21:50:09 +00:00
|
|
|
@abstractmethod
|
|
|
|
def create_service_key(self, name, kid, service, jwk, metadata, expiration_date,
|
2016-08-31 18:31:43 +00:00
|
|
|
rotation_duration=None):
|
|
|
|
"""
|
|
|
|
Stores a service key.
|
|
|
|
"""
|
2016-09-23 21:50:09 +00:00
|
|
|
pass
|
2016-08-31 18:31:43 +00:00
|
|
|
|
2016-09-23 21:50:09 +00:00
|
|
|
@abstractmethod
|
|
|
|
def replace_service_key(self, old_kid, kid, jwk, metadata, expiration_date):
|
2016-08-31 18:31:43 +00:00
|
|
|
"""
|
|
|
|
Replaces a service with a new key or raises ServiceKeyNotFound.
|
|
|
|
"""
|
2016-09-23 21:50:09 +00:00
|
|
|
pass
|
2016-08-31 18:31:43 +00:00
|
|
|
|
2016-09-23 21:50:09 +00:00
|
|
|
@abstractmethod
|
|
|
|
def delete_service_key(self, kid):
|
2016-08-31 18:31:43 +00:00
|
|
|
"""
|
|
|
|
Deletes and returns a service key with the given kid or raises ServiceKeyNotFound.
|
|
|
|
"""
|
2016-09-23 21:50:09 +00:00
|
|
|
pass
|
2016-08-31 18:31:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PreOCIModel(KeyServerDataInterface):
|
|
|
|
"""
|
|
|
|
PreOCIModel implements the data model for JWT key service using a database schema before it was
|
|
|
|
changed to support the OCI specification.
|
|
|
|
"""
|
2016-09-23 21:50:09 +00:00
|
|
|
def list_service_keys(self, service):
|
2016-08-31 18:31:43 +00:00
|
|
|
return data.model.service_keys.list_service_keys(service)
|
|
|
|
|
2016-09-23 21:50:09 +00:00
|
|
|
def get_service_key(self, signer_kid, service=None, alive_only=True, approved_only=True):
|
2016-08-31 18:31:43 +00:00
|
|
|
try:
|
|
|
|
key = data.model.service_keys.get_service_key(signer_kid, service, alive_only, approved_only)
|
2016-09-23 21:50:09 +00:00
|
|
|
return _db_key_to_servicekey(key)
|
2016-08-31 18:31:43 +00:00
|
|
|
except data.model.ServiceKeyDoesNotExist:
|
|
|
|
raise ServiceKeyDoesNotExist()
|
|
|
|
|
2016-09-23 21:50:09 +00:00
|
|
|
def create_service_key(self, name, kid, service, jwk, metadata, expiration_date,
|
2016-08-31 18:31:43 +00:00
|
|
|
rotation_duration=None):
|
|
|
|
key = data.model.service_keys.create_service_key(name, kid, service, jwk, metadata,
|
|
|
|
expiration_date, rotation_duration)
|
2016-09-23 21:50:09 +00:00
|
|
|
return _db_key_to_servicekey(key)
|
2016-08-31 18:31:43 +00:00
|
|
|
|
2016-09-23 21:50:09 +00:00
|
|
|
def replace_service_key(self, old_kid, kid, jwk, metadata, expiration_date):
|
2016-08-31 18:31:43 +00:00
|
|
|
try:
|
|
|
|
data.model.service_keys.replace_service_key(old_kid, kid, jwk, metadata, expiration_date)
|
|
|
|
except data.model.ServiceKeyDoesNotExist:
|
|
|
|
raise ServiceKeyDoesNotExist()
|
|
|
|
|
2016-09-23 21:50:09 +00:00
|
|
|
def delete_service_key(self, kid):
|
2016-08-31 18:31:43 +00:00
|
|
|
try:
|
|
|
|
key = data.model.service_keys.delete_service_key(kid)
|
2016-09-23 21:50:09 +00:00
|
|
|
return _db_key_to_servicekey(key)
|
2016-08-31 18:31:43 +00:00
|
|
|
except data.model.ServiceKeyDoesNotExist:
|
|
|
|
raise ServiceKeyDoesNotExist()
|
2016-09-23 21:50:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
pre_oci_model = PreOCIModel()
|
|
|
|
|
|
|
|
|
|
|
|
def _db_key_to_servicekey(key):
|
|
|
|
"""
|
|
|
|
Converts the Pre-OCI database model of a service key into a ServiceKey.
|
|
|
|
"""
|
|
|
|
return ServiceKey(
|
|
|
|
name=key.name,
|
|
|
|
kid=key.kid,
|
|
|
|
service=key.service,
|
|
|
|
jwk=key.jwk,
|
|
|
|
metadata=key.metadata,
|
|
|
|
created_date=key.created_date,
|
|
|
|
expiration_date=key.expiration_date,
|
|
|
|
rotation_duration=key.rotation_duration,
|
|
|
|
approval=key.approval,
|
|
|
|
)
|
|
|
|
|