Merge "Add console auth tokens db api methods"

This commit is contained in:
Jenkins 2016-06-27 18:18:42 +00:00 committed by Gerrit Code Review
commit aa8ffa3f6c
3 changed files with 86 additions and 0 deletions

View File

@ -2050,3 +2050,39 @@ def instance_tag_delete_all(context, instance_uuid):
def instance_tag_exists(context, instance_uuid, tag):
"""Check if specified tag exist on the instance."""
return IMPL.instance_tag_exists(context, instance_uuid, tag)
####################
def console_auth_token_create(context, values):
"""Create a console authorization."""
return IMPL.console_auth_token_create(context, values)
def console_auth_token_get_valid(context, token_hash, instance_uuid):
"""Get a valid console authorization by token_hash and instance_uuid.
The console authorizations expire at the time specified by their
'expires' column. An expired console auth token will not be returned
to the caller - it is treated as if it does not exist.
"""
return IMPL.console_auth_token_get_valid(context,
token_hash,
instance_uuid)
def console_auth_token_destroy_all_by_instance(context, instance_uuid):
"""Delete all console authorizations belonging to the instance."""
return IMPL.console_auth_token_destroy_all_by_instance(context,
instance_uuid)
def console_auth_token_destroy_expired_by_host(context, host):
"""Delete expired console authorizations belonging to the host.
The console authorizations expire at the time specified by their
'expires' column. This function is used to garbage collect expired
tokens associated with the given host.
"""
return IMPL.console_auth_token_destroy_expired_by_host(context, host)

View File

@ -6812,3 +6812,40 @@ def instance_tag_exists(context, instance_uuid, tag):
q = context.session.query(models.Tag).filter_by(
resource_id=instance_uuid, tag=tag)
return context.session.query(q.exists()).scalar()
####################
@pick_context_manager_writer
def console_auth_token_create(context, values):
instance_uuid = values.get('instance_uuid')
_check_instance_exists_in_project(context, instance_uuid)
token_ref = models.ConsoleAuthToken()
token_ref.update(values)
context.session.add(token_ref)
return token_ref
@pick_context_manager_reader
def console_auth_token_get_valid(context, token_hash, instance_uuid):
_check_instance_exists_in_project(context, instance_uuid)
return context.session.query(models.ConsoleAuthToken).\
filter_by(token_hash=token_hash).\
filter_by(instance_uuid=instance_uuid).\
filter(models.ConsoleAuthToken.expires > timeutils.utcnow_ts()).\
first()
@pick_context_manager_writer
def console_auth_token_destroy_all_by_instance(context, instance_uuid):
context.session.query(models.ConsoleAuthToken).\
filter_by(instance_uuid=instance_uuid).delete()
@pick_context_manager_writer
def console_auth_token_destroy_expired_by_host(context, host):
context.session.query(models.ConsoleAuthToken).\
filter_by(host=host).\
filter(models.ConsoleAuthToken.expires <= timeutils.utcnow_ts()).\
delete()

View File

@ -1211,6 +1211,19 @@ def get_hash_str(base_str):
return hashlib.md5(base_str).hexdigest()
def get_sha256_str(base_str):
"""Returns string that represents sha256 hash of base_str (in hex format).
sha1 and md5 are known to be breakable, so sha256 is a better option
when the hash is being used for security purposes. If hashing passwords
or anything else that needs to be retained for a long period a salted
hash is better.
"""
if isinstance(base_str, six.text_type):
base_str = base_str.encode('utf-8')
return hashlib.sha256(base_str).hexdigest()
def filter_and_format_resource_metadata(resource_type, resource_list,
search_filts, metadata_type=None):
"""Get all metadata for a list of resources after filtering.