1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
# Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
from functionaltests.cli.v1.behaviors import base_behaviors
class ACLBehaviors(base_behaviors.BaseBehaviors):
_args_map_list = {'users': ['--user', '-u'],
'operation_type': ['--operation-type', '-o']
}
def __init__(self):
super(ACLBehaviors, self).__init__()
self.LOG = logging.getLogger(type(self).__name__)
self.acl_entity_set = set()
def _add_ref_arg(self, argv, entity_ref):
argv.extend([entity_ref])
return argv
def _add_per_acl_args(self, argv, users=[], project_access=None,
operation_type=None, use_short_arg=False):
index = 1 if use_short_arg else 0
if users is not None:
if users:
for user in users:
argv.extend([self._args_map_list['users'][index], user])
else: # empty list case
argv.extend([self._args_map_list['users'][index]])
if project_access is not None:
if project_access:
argv.extend(['--project-access'])
else:
argv.extend(['--no-project-access'])
if operation_type and operation_type != 'read':
argv.extend([self._args_map_list['operation_type'][index],
operation_type])
return argv
def acl_delete(self, entity_ref):
"""Delete a secret or container acl
:param entity_ref: Reference to secret or container entity
:return: If error returns stderr string otherwise returns None.
"""
argv = ['acl', 'delete']
self.add_auth_and_endpoint(argv)
self._add_ref_arg(argv, entity_ref)
_, stderr = self.issue_barbican_command(argv)
self.acl_entity_set.discard(entity_ref)
if stderr:
return stderr
def acl_get(self, entity_ref):
"""Get a 'read' ACL setting for a secret or a container.
:param entity_ref: Reference to secret or container entity
:return: dict of 'read' operation ACL settings if found otherwise empty
dict in case of error.
"""
argv = ['acl', 'get']
self.add_auth_and_endpoint(argv)
self._add_ref_arg(argv, entity_ref)
stdout, stderr = self.issue_barbican_command(argv)
if '4xx Client error: Not Found' in stderr:
return {}
acl_list = self._prettytable_to_list(stdout)
return acl_list[0] # return first ACL which is for 'read' op type
def acl_submit(self, entity_ref, users=None,
project_access=None, use_short_arg=False,
operation_type='read'):
"""Submits a secret or container ACL
:param entity_ref: Reference to secret or container entity
:param users: List of users for ACL
:param project_access: Flag to pass for project access behavior
:param use_short_arg: Flag to indicate if use short arguments in cli.
Default is False
:param operation_type: ACL operation type. Default is 'read' as
Barbican currently supports only that type of operation.
:return: dict of 'read' operation ACL settings if found otherwise empty
dict in case of error.
"""
argv = ['acl', 'submit']
self.add_auth_and_endpoint(argv)
self._add_per_acl_args(argv, users=users,
project_access=project_access,
use_short_arg=use_short_arg,
operation_type=operation_type)
self._add_ref_arg(argv, entity_ref)
stdout, stderr = self.issue_barbican_command(argv)
if '4xx Client error: Not Found' in stderr:
return {}
acl_list = self._prettytable_to_list(stdout)
self.acl_entity_set.add(entity_ref)
return acl_list[0] # return first ACL which is for 'read' op type
def acl_add(self, entity_ref, users=None,
project_access=None, use_short_arg=False,
operation_type='read'):
"""Add to a secret or container ACL
:param entity_ref: Reference to secret or container entity
:param users: List of users to be added in ACL
:param project_access: Flag to pass for project access behavior
:param use_short_arg: Flag to indicate if use short arguments in cli.
Default is False
:param operation_type: ACL operation type. Default is 'read' as
Barbican currently supports only that type of operation.
:return: dict of 'read' operation ACL settings if found otherwise empty
dict in case of error.
"""
argv = ['acl', 'user', 'add']
self.add_auth_and_endpoint(argv)
self._add_per_acl_args(argv, users=users,
project_access=project_access,
use_short_arg=use_short_arg,
operation_type=operation_type)
self._add_ref_arg(argv, entity_ref)
stdout, stderr = self.issue_barbican_command(argv)
if '4xx Client error: Not Found' in stderr:
return {}
self.acl_entity_set.add(entity_ref)
acl_list = self._prettytable_to_list(stdout)
return acl_list
def acl_remove(self, entity_ref, users=None,
project_access=None, use_short_arg=False,
operation_type='read'):
"""Remove users from a secret or container ACL
:param entity_ref: Reference to secret or container entity
:param users: List of users to be removed from ACL
:param project_access: Flag to pass for project access behavior
:param use_short_arg: Flag to indicate if use short arguments in cli.
Default is False
:param operation_type: ACL operation type. Default is 'read' as
Barbican currently supports only that type of operation.
:return: dict of 'read' operation ACL settings if found otherwise empty
dict in case of error.
"""
argv = ['acl', 'user', 'remove']
self.add_auth_and_endpoint(argv)
self._add_per_acl_args(argv, users=users,
project_access=project_access,
use_short_arg=use_short_arg,
operation_type=operation_type)
self._add_ref_arg(argv, entity_ref)
stdout, stderr = self.issue_barbican_command(argv)
if '4xx Client error: Not Found' in stderr:
return {}
acl_list = self._prettytable_to_list(stdout)
return acl_list
def delete_all_created_acls(self):
"""Delete all ACLs that we created"""
entities_to_delete = [entry for entry in self.acl_entity_set]
for entity_ref in entities_to_delete:
self.acl_delete(entity_ref)
|