2016-03-23 22:16:03 +00:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
|
|
|
from app import app
|
|
|
|
from data.database import db_for_update, User, ServiceKey, ServiceKeyApproval
|
|
|
|
from data.model import ServiceKeyDoesNotExist, ServiceKeyAlreadyApproved, db_transaction
|
2016-03-29 19:58:14 +00:00
|
|
|
from data.model.notification import create_notification, delete_all_notifications_by_path_prefix
|
2016-03-23 22:16:03 +00:00
|
|
|
|
2016-03-28 23:00:00 +00:00
|
|
|
# TODO ACTION_LOGS for keys
|
2016-03-23 22:16:03 +00:00
|
|
|
UNAPPROVED_TTL = timedelta(seconds=app.config['UNAPPROVED_SERVICE_KEY_TTL_SEC'])
|
2016-03-16 19:49:25 +00:00
|
|
|
|
|
|
|
|
2016-03-28 23:00:00 +00:00
|
|
|
def _expired_keys_clause(service):
|
|
|
|
return ((ServiceKey.service == service) &
|
|
|
|
(ServiceKey.expiration_date <= datetime.utcnow()))
|
2016-03-23 22:16:03 +00:00
|
|
|
|
|
|
|
|
2016-03-28 23:00:00 +00:00
|
|
|
def _stale_unapproved_keys_clause(service):
|
|
|
|
return ((ServiceKey.service >> service) &
|
|
|
|
(ServiceKey.approval >> None) &
|
|
|
|
(ServiceKey.created_date + UNAPPROVED_TTL) >= datetime.utcnow())
|
2016-03-16 19:49:25 +00:00
|
|
|
|
|
|
|
|
2016-03-28 23:00:00 +00:00
|
|
|
def _gc_expired(service):
|
|
|
|
ServiceKey.delete().where(_expired_keys_clause(service) |
|
|
|
|
_stale_unapproved_keys_clause(service)).execute()
|
2016-03-23 22:16:03 +00:00
|
|
|
|
2016-03-25 22:44:11 +00:00
|
|
|
|
2016-03-29 21:16:51 +00:00
|
|
|
def _notify_superusers(key):
|
2016-03-29 19:58:14 +00:00
|
|
|
notification_metadata = {
|
2016-03-29 21:16:51 +00:00
|
|
|
'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,
|
2016-03-29 19:58:14 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 22:44:11 +00:00
|
|
|
superusers = User.select().where(User.username << app.config['SUPER_USERS'])
|
|
|
|
for superuser in superusers:
|
|
|
|
# TODO(jzelinskie): create notification type in the database migration
|
|
|
|
# I already put it in initdb
|
2016-03-29 19:58:14 +00:00
|
|
|
create_notification('service_key_submitted', superuser, metadata=notification_metadata,
|
2016-03-29 21:16:51 +00:00
|
|
|
lookup_path='/service_key_approval/{0}'.format(key.kid))
|
|
|
|
|
2016-03-25 22:44:11 +00:00
|
|
|
|
2016-03-29 21:16:51 +00:00
|
|
|
def create_service_key(name, kid, service, jwk, metadata, expiration_date):
|
|
|
|
key = ServiceKey.create(name=name, kid=kid, service=service, jwk=jwk, metadata=metadata,
|
|
|
|
expiration_date=expiration_date)
|
|
|
|
|
|
|
|
_notify_superusers(key)
|
2016-03-16 19:49:25 +00:00
|
|
|
_gc_expired(service)
|
|
|
|
|
2016-03-28 23:00:00 +00:00
|
|
|
|
2016-03-29 21:16:51 +00:00
|
|
|
def replace_service_key(old_kid, kid, jwk, metadata, expiration_date):
|
2016-03-28 23:00:00 +00:00
|
|
|
try:
|
|
|
|
with db_transaction():
|
2016-03-29 21:16:51 +00:00
|
|
|
key = db_for_update(ServiceKey.select().where(ServiceKey.kid == old_kid)).get()
|
2016-03-28 23:00:00 +00:00
|
|
|
metadata = key.metadata.update(metadata)
|
|
|
|
ServiceKey.create(name=key.name, kid=kid, service=key.service, jwk=jwk,
|
|
|
|
metadata=metadata, expiration_date=expiration_date, approval=key.approval)
|
|
|
|
key.delete_instance()
|
|
|
|
except ServiceKey.DoesNotExist:
|
|
|
|
raise ServiceKeyDoesNotExist
|
|
|
|
|
2016-03-29 21:16:51 +00:00
|
|
|
_notify_superusers(key)
|
|
|
|
delete_all_notifications_by_path_prefix('/service_key_approval/{0}'.format(old_kid))
|
2016-03-28 23:00:00 +00:00
|
|
|
_gc_expired(key.service)
|
|
|
|
|
|
|
|
|
|
|
|
def update_service_key(name, kid, metadata, expiration_date):
|
2016-03-16 19:49:25 +00:00
|
|
|
try:
|
2016-03-22 15:44:54 +00:00
|
|
|
with db_transaction():
|
|
|
|
key = db_for_update(ServiceKey.select().where(ServiceKey.kid == kid)).get()
|
2016-03-23 22:16:03 +00:00
|
|
|
key.name = name
|
|
|
|
key.metadata.update(metadata)
|
2016-03-22 15:44:54 +00:00
|
|
|
key.expiration_date = expiration_date
|
|
|
|
key.save()
|
2016-03-16 19:49:25 +00:00
|
|
|
except ServiceKey.DoesNotExist:
|
2016-03-25 22:44:11 +00:00
|
|
|
raise ServiceKeyDoesNotExist
|
|
|
|
|
2016-03-28 23:00:00 +00:00
|
|
|
_gc_expired(key.service)
|
2016-03-28 18:52:20 +00:00
|
|
|
|
|
|
|
|
2016-03-16 19:49:25 +00:00
|
|
|
def delete_service_key(service, kid):
|
|
|
|
try:
|
|
|
|
ServiceKey.delete().where(ServiceKey.service == service,
|
|
|
|
ServiceKey.kid == kid).execute()
|
|
|
|
except ServiceKey.DoesNotExist:
|
|
|
|
raise ServiceKeyDoesNotExist()
|
2016-03-22 15:44:54 +00:00
|
|
|
|
2016-03-29 21:16:51 +00:00
|
|
|
delete_all_notifications_by_path_prefix('/service_key_approval/{0}'.format(kid))
|
2016-03-28 23:00:00 +00:00
|
|
|
_gc_expired(service)
|
|
|
|
|
2016-03-22 15:44:54 +00:00
|
|
|
|
2016-03-25 22:44:11 +00:00
|
|
|
def approve_service_key(kid, approver, approval_type):
|
2016-03-22 15:44:54 +00:00
|
|
|
try:
|
|
|
|
with db_transaction():
|
2016-03-25 22:44:11 +00:00
|
|
|
key = db_for_update(ServiceKey.select().where(ServiceKey.kid == kid)).get()
|
2016-03-23 22:16:03 +00:00
|
|
|
if key.approval is not None:
|
|
|
|
raise ServiceKeyAlreadyApproved
|
|
|
|
|
|
|
|
approval = ServiceKeyApproval.create(approver=approver, approval_type=approval_type)
|
2016-03-22 15:44:54 +00:00
|
|
|
key.approval = approval
|
|
|
|
key.save()
|
|
|
|
except ServiceKey.DoesNotExist:
|
|
|
|
raise ServiceKeyDoesNotExist
|
2016-03-28 23:00:00 +00:00
|
|
|
|
2016-03-29 19:58:14 +00:00
|
|
|
delete_all_notifications_by_path_prefix('/service_key_approval/{0}'.format(kid))
|
|
|
|
_gc_expired(key.service)
|
|
|
|
|
2016-03-28 23:00:00 +00:00
|
|
|
|
|
|
|
def _get_service_keys_query(kid=None, service=None, approved_only=False):
|
|
|
|
query = ServiceKey.select()
|
|
|
|
|
|
|
|
if approved_only:
|
|
|
|
query = query.where(~(ServiceKey.approval >> None))
|
|
|
|
|
|
|
|
if service is not None:
|
|
|
|
query = query.where(ServiceKey.service == service)
|
|
|
|
query = query.where(~(_expired_keys_clause(service)) &
|
|
|
|
~(_stale_unapproved_keys_clause(service)))
|
|
|
|
|
|
|
|
if kid is not None:
|
|
|
|
query.where(ServiceKey.kid == kid)
|
|
|
|
|
|
|
|
return query
|
|
|
|
|
|
|
|
|
|
|
|
def get_keys():
|
|
|
|
return list(_get_service_keys_query())
|
|
|
|
|
|
|
|
|
|
|
|
def get_service_keys(service):
|
|
|
|
return list(_get_service_keys_query(service=service, approved_only=True))
|
|
|
|
|
|
|
|
|
|
|
|
def get_service_key(kid):
|
|
|
|
return _get_service_keys_query(kid=kid).get()
|