예제 #1
0
 def get_hash_dict(cls, accounts, admin_role,
                   object_storage_operator_role=None,
                   object_storage_reseller_admin_role=None):
     hash_dict = {'roles': {}, 'creds': {}, 'networks': {}}
     # Loop over the accounts read from the yaml file
     for account in accounts:
         roles = []
         types = []
         resources = []
         if 'roles' in account:
             roles = account.pop('roles')
         if 'types' in account:
             types = account.pop('types')
         if 'resources' in account:
             resources = account.pop('resources')
         temp_hash = hashlib.md5()
         temp_hash.update(six.text_type(account).encode('utf-8'))
         temp_hash_key = temp_hash.hexdigest()
         hash_dict['creds'][temp_hash_key] = account
         for role in roles:
             hash_dict = cls._append_role(role, temp_hash_key,
                                          hash_dict)
         # If types are set for the account append the matching role
         # subdict with the hash
         for type in types:
             if type == 'admin':
                 hash_dict = cls._append_role(admin_role, temp_hash_key,
                                              hash_dict)
             elif type == 'operator':
                 if object_storage_operator_role:
                     hash_dict = cls._append_role(
                         object_storage_operator_role, temp_hash_key,
                         hash_dict)
                 else:
                     msg = ("Type 'operator' configured, but no "
                            "object_storage_operator_role specified")
                     raise lib_exc.InvalidCredentials(msg)
             elif type == 'reseller_admin':
                 if object_storage_reseller_admin_role:
                     hash_dict = cls._append_role(
                         object_storage_reseller_admin_role,
                         temp_hash_key,
                         hash_dict)
                 else:
                     msg = ("Type 'reseller_admin' configured, but no "
                            "object_storage_reseller_admin_role specified")
                     raise lib_exc.InvalidCredentials(msg)
         # Populate the network subdict
         for resource in resources:
             if resource == 'network':
                 hash_dict['networks'][temp_hash_key] = resources[resource]
             else:
                 LOG.warning('Unknown resource type %s, ignoring this field'
                             % resource)
     return hash_dict
예제 #2
0
 def _get_match_hash_list(self, roles=None):
     hashes = []
     if roles:
         # Loop over all the creds for each role in the subdict and generate
         # a list of cred lists for each role
         for role in roles:
             temp_hashes = self.hash_dict['roles'].get(role, None)
             if not temp_hashes:
                 raise lib_exc.InvalidCredentials(
                     "No credentials with role: %s specified in the "
                     "accounts ""file" % role)
             hashes.append(temp_hashes)
         # Take the list of lists and do a boolean and between each list to
         # find the creds which fall under all the specified roles
         temp_list = set(hashes[0])
         for hash_list in hashes[1:]:
             temp_list = temp_list & set(hash_list)
         hashes = temp_list
     else:
         hashes = self.hash_dict['creds'].keys()
     # NOTE(mtreinish): admin is a special case because of the increased
     # privlege set which could potentially cause issues on tests where that
     # is not expected. So unless the admin role isn't specified do not
     # allocate admin.
     admin_hashes = self.hash_dict['roles'].get(self.admin_role,
                                                None)
     if ((not roles or self.admin_role not in roles) and
             admin_hashes):
         useable_hashes = [x for x in hashes if x not in admin_hashes]
     else:
         useable_hashes = hashes
     return useable_hashes
예제 #3
0
 def is_multi_user(self):
     # Default credentials is not a valid option with locking Account
     if self.use_default_creds:
         raise lib_exc.InvalidCredentials(
             "Account file %s doesn't exist" % self.test_accounts_file)
     else:
         return len(self.hash_dict['creds']) > 1
예제 #4
0
 def _apply_credentials(self, attr):
     for key in attr.keys():
         if key in self.ATTRIBUTES:
             setattr(self, key, attr[key])
         else:
             msg = '%s is not a valid attr for %s' % (key, self.__class__)
             raise exceptions.InvalidCredentials(msg)
예제 #5
0
    def _get_auth_credentials(self, auth_version, **credentials):
        """Retrieves auth credentials based on passed in creds and version

        :param auth_version: auth version ('v2' or 'v3')
        :param credentials: credentials dict to validate against
        :returns: credentials object
        """

        if credentials is None:
            raise exceptions.InvalidCredentials(
                'Credentials must be specified')
        if auth_version == 'v3':
            return auth.KeystoneV3Credentials(**credentials)
        elif auth_version == 'v2':
            return auth.KeystoneV2Credentials(**credentials)
        else:
            raise exceptions.InvalidCredentials('Specify identity version')
예제 #6
0
 def _get_creds(self, roles=None):
     if self.use_default_creds:
         raise lib_exc.InvalidCredentials(
             "Account file %s doesn't exist" % self.test_accounts_file)
     useable_hashes = self._get_match_hash_list(roles)
     free_hash = self._get_free_hash(useable_hashes)
     clean_creds = self._sanitize_creds(
         self.hash_dict['creds'][free_hash])
     LOG.info('%s allocated creds:\n%s' % (self.name, clean_creds))
     return self._wrap_creds_with_network(free_hash)
예제 #7
0
 def __init__(self, identity_client, domain_name):
     super(V3CredsClient, self).__init__(identity_client)
     try:
         # Domain names must be unique, in any case a list is returned,
         # selecting the first (and only) element
         self.creds_domain = self.identity_client.list_domains(
             params={'name': domain_name})['domains'][0]
     except lib_exc.NotFound:
         # TODO(andrea) we could probably create the domain on the fly
         msg = "Requested domain %s could not be found" % domain_name
         raise lib_exc.InvalidCredentials(msg)
예제 #8
0
 def _get_free_hash(self, hashes):
     # Cast as a list because in some edge cases a set will be passed in
     hashes = list(hashes)
     if not os.path.isdir(self.accounts_dir):
         os.mkdir(self.accounts_dir)
         # Create File from first hash (since none are in use)
         self._create_hash_file(hashes[0])
         return hashes[0]
     names = []
     for _hash in hashes:
         res = self._create_hash_file(_hash)
         if res:
             return _hash
         else:
             path = os.path.join(os.path.join(self.accounts_dir,
                                              _hash))
             with open(path, 'r') as fd:
                 names.append(fd.read())
     msg = ('Insufficient number of users provided. %s have allocated all '
            'the credentials for this allocation request' % ','.join(names))
     raise lib_exc.InvalidCredentials(msg)
예제 #9
0
    def get_auth_provider(self, **credentials):
        """Validates credentials and returns auth provider

        Auth provider will contain required security context to pass to magnum

        :param credentials: credentials dict to validate against
        :returns: auth provider object
        """

        auth_version = config.Config.auth_version
        creds = self._get_auth_credentials(auth_version, **credentials)
        if auth_version == 'v3':
            auth_provider = auth.KeystoneV3AuthProvider(
                creds, config.Config.auth_url)
        elif auth_version == 'v2':
            auth_provider = auth.KeystoneV2AuthProvider(
                creds, config.Config.auth_url)
        else:
            raise exceptions.InvalidCredentials('Specify identity version')

        auth_provider.fill_credentials()
        return auth_provider
예제 #10
0
    def __init__(self, credentials):
        """Auth provider __init__

        :param credentials: credentials for authentication
        """
        if self.check_credentials(credentials):
            self.credentials = credentials
        else:
            if isinstance(credentials, Credentials):
                password = credentials.get('password')
                message = "Credentials are: " + str(credentials)
                if password is None:
                    message += " Password is not defined."
                else:
                    message += " Password is defined."
                raise exceptions.InvalidCredentials(message)
            else:
                raise TypeError("credentials object is of type %s, which is"
                                " not a valid Credentials object type." %
                                credentials.__class__.__name__)
        self.cache = None
        self.alt_auth_data = None
        self.alt_part = None