def authenticate_user(username, password): authenticated = False try: user_exist = retrieve_object(username, UserCollections.Users) if user_exist: if user_exist[UserKeys.Enabled] == CommonKeys.YES: original_encrypted_password = ( user_exist[UserKeys.Password].encode('utf-8')) password_verified = (Crypto().verify_bcrypt_hash( password, original_encrypted_password)) if password_verified: authenticated = True except Exception as e: logger.exception(e) return (authenticated)
def authenticate_user(username, password): authenticated = False try: user_exist = retrieve_object(username, UserCollections.Users) if user_exist: if user_exist[UserKeys.Enabled] == CommonKeys.YES: original_encrypted_password = ( user_exist[UserKeys.Password].encode('utf-8') ) password_verified = ( Crypto().verify_bcrypt_hash( password, original_encrypted_password ) ) if password_verified: authenticated = True except Exception as e: logger.exception(e) return(authenticated)
def add_user_to_groups( username, customer_name, group_ids, user_name=None, uri=None, method=None ): """Add a user into a vFense group Args: username (str): Name of the user already in vFense. customer_name (str): The customer this user is part of. group_ids (list): List of group ids. Kwargs: user_name (str): The name of the user who called this function. uri (str): The uri that was used to call this function. method (str): The HTTP methos that was used to call this function. Basic Usage: >>> from vFense.group.groups import add_user_to_groups >>> username = '******' >>> customer_name = 'default' >>> group_ids = ['0834e656-27a5-4b13-ba56-635797d0d1fc'] >>> add_user_to_groups(username, customer_name, group_ids) Returns: Returns the results in a dictionary { 'uri': None, 'rv_status_code': 1010, 'http_method': None, 'http_status': 200, 'message': "None - groups per user [u'ee54820c-cb4e-46a1-9d11-73afe8c4c4e3'] was created", 'data': { 'group_name': u'FooLah', 'user_name': 'alien', 'group_id': '0834e656-27a5-4b13-ba56-635797d0d1fc', 'customer_name': 'default' } } """ status = add_user_to_groups.func_name + ' - ' groups_are_valid = validate_group_ids(group_ids, customer_name) user_exist = retrieve_object(username, UserCollections.Users) customer_exist = retrieve_object(customer_name, CustomerCollections.Customers) results = None generated_ids = [] users_group_exist = [] generic_status_code = 0 vfense_status_code = 0 if groups_are_valid[0] and user_exist and customer_exist: data_list = [] for group_id in group_ids: group_exist = get_group(group_id) user_in_group = ( user_exist_in_group(username, group_id) ) if not user_in_group: data_to_add = ( { GroupsPerUserKeys.CustomerName: customer_name, GroupsPerUserKeys.UserName: username, GroupsPerUserKeys.GroupName: group_exist[GroupKeys.GroupName], GroupsPerUserKeys.GroupId: group_id } ) data_list.append(data_to_add) else: users_group_exist.append(group_id) if len(data_list) == len(group_ids): status_code, object_count, error, generated_ids = ( insert_group_per_user(data_list) ) if status_code == DbCodes.Inserted: msg = 'user %s add to groups' % (username) generic_status_code = GenericCodes.ObjectCreated vfense_status_code = GroupCodes.GroupCreated else: msg = ( 'user %s is already in groups %s' % ( username, ' and '.join(users_group_exist) ) ) status_code = DbCodes.Skipped generic_status_code = GenericCodes.ObjectExists vfense_status_code = GroupFailureCodes.GroupExistForUser elif not groups_are_valid[0]: msg = ( 'Group Ids are invalid: %s' % ( ' and '.join(groups_are_valid[2]) ) ) status_code = DbCodes.Errors generic_status_code = GenericCodes.InvalidId vfense_status_code = GroupFailureCodes.InvalidGroupId elif not user_exist: msg = 'User name is invalid: %s' % (username) status_code = DbCodes.Errors generic_status_code = GenericCodes.InvalidId vfense_status_code = UserFailureCodes.InvalidUserName elif not customer_exist: msg = 'Customer name is invalid: %s' % (customer_name) status_code = DbCodes.Errors generic_status_code = GenericCodes.InvalidId vfense_status_code = CustomerFailureCodes.CustomerDoesNotExist results = { ApiResultKeys.DB_STATUS_CODE: status_code, ApiResultKeys.GENERIC_STATUS_CODE: generic_status_code, ApiResultKeys.VFENSE_STATUS_CODE: vfense_status_code, ApiResultKeys.GENERATED_IDS: generated_ids, ApiResultKeys.MESSAGE: status + msg, ApiResultKeys.DATA: [], ApiResultKeys.USERNAME: user_name, ApiResultKeys.URI: uri, ApiResultKeys.HTTP_METHOD: method } return(results)
def create_customer(customer, username=None, init=None, user_name=None, uri=None, method=None): """Create a new customer inside of vFense Args: customer (Customer): A customer instance filled out with the appropriate fields. Kwargs: username (str): Name of the user that you are adding to this customer. Default=None If init is set to True, then it can stay as None else, then a valid user must be passed init (boolean): Create the customer, without adding a user into it. Default=False user_name (str): The name of the user who called this function. uri (str): The uri that was used to call this function. method (str): The HTTP methos that was used to call this function. Basic Usage: >>> from vFense.core.customer import Customer >>> from vFense.core.customer.customers import create_customer >>> customer = Customer('NewCustomer', package_download_url='https://10.0.0.16/packages/') >>> username = '******' >>> create_customer(customer, api_user) Return: Dictionary of the status of the operation. { 'uri': None, 'rv_status_code': 1010, 'http_method': None, 'http_status': 200, 'message': 'api_user - customer vFense was created', 'data': { 'cpu_throttle': 'normal', 'package_download_url_base': 'https: //10.0.0.21/packages/', 'server_queue_ttl': 10, 'agent_queue_ttl': 10, 'net_throttle': 0, 'customer_name': 'vFense' } } """ customer_exist = get_customer(customer.name) status = create_customer.func_name + ' - ' msg = '' status_code = 0 generic_status_code = 0 vfense_status_code = 0 generated_ids = [] invalid_fields = customer.get_invalid_fields() if invalid_fields: # TODO: Inform about more than just the first invalid field invalid_field = invalid_fields[0] status_code = DbCodes.Errors generic_status_code = GenericCodes.InvalidId if invalid_field.get(CustomerKeys.CustomerName): msg = ('customer name is not within the 36 character range ' 'or contains unsupported characters :%s' % (invalid_field.get(CustomerKeys.CustomerName))) vfense_status_code = CustomerFailureCodes.InvalidCustomerName elif invalid_field.get(CustomerKeys.NetThrottle): msg = ('network throttle was not given a valid integer :%s' % (invalid_field.get(CustomerKeys.NetThrottle))) vfense_status_code = CustomerFailureCodes.InvalidNetworkThrottle elif invalid_field.get(CustomerKeys.CpuThrottle): msg = ('cpu throttle was not given a valid value :%s' % (invalid_field.get(CustomerKeys.CpuThrottle))) vfense_status_code = CustomerFailureCodes.InvalidCpuThrottle elif invalid_field.get(CustomerKeys.ServerQueueTTL): msg = ('server queue ttl was not given a valid value :%s' % (invalid_field.get(CustomerKeys.ServerQueueTTL))) vfense_status_code = CustomerFailureCodes.InvalidOperationTTL elif invalid_field.get(CustomerKeys.AgentQueueTTL): msg = ('agent queue ttl was not given a valid value :%s' % (invalid_field.get(CustomerKeys.AgentQueueTTL))) vfense_status_code = CustomerFailureCodes.InvalidOperationTTL # TODO: handle invalid base package url string #elif invalid_field.get(CustomerKeys.PackageUrl): elif not customer_exist: # Fill in any empty fields customer.fill_in_defaults() if not customer.package_download_url: customer.package_download_url = (get_customer( DefaultCustomers.DEFAULT, [CustomerKeys.PackageUrl]).get(CustomerKeys.PackageUrl)) object_status, _, _, generated_ids = (insert_customer( customer.to_dict())) if object_status == DbCodes.Inserted: generated_ids.append(customer.name) msg = 'customer %s created - ' % (customer.name) generic_status_code = GenericCodes.ObjectCreated vfense_status_code = CustomerCodes.CustomerCreated if object_status == DbCodes.Inserted and not init and username: add_user_to_customers(username, [customer.name], user_name, uri, method) if username != DefaultUsers.ADMIN: add_user_to_customers(DefaultUsers.ADMIN, [customer.name], user_name, uri, method) #The admin user should be part of every group elif object_status == DbCodes.Inserted and username != DefaultUsers.ADMIN: admin_exist = (retrieve_object(DefaultUsers.ADMIN, UserCollections.Users)) if admin_exist: add_user_to_customers(DefaultUsers.ADMIN, [customer.name], user_name, uri, method) elif customer_exist: status_code = DbCodes.Unchanged msg = 'customer name %s already exists' % (customer.name) generic_status_code = GenericCodes.ObjectExists vfense_status_code = CustomerFailureCodes.CustomerExists results = { ApiResultKeys.DB_STATUS_CODE: status_code, ApiResultKeys.GENERIC_STATUS_CODE: generic_status_code, ApiResultKeys.VFENSE_STATUS_CODE: vfense_status_code, ApiResultKeys.MESSAGE: status + msg, ApiResultKeys.DATA: [customer.to_dict()], ApiResultKeys.USERNAME: user_name, ApiResultKeys.URI: uri, ApiResultKeys.HTTP_METHOD: method } return results
def add_user_to_customers(username, customer_names, user_name=None, uri=None, method=None): """Add a multiple customers to a user Args: username (str): Name of the user already in vFense. customer_names (list): List of customer names, this user will be added to. Kwargs: user_name (str): The name of the user who called this function. uri (str): The uri that was used to call this function. method (str): The HTTP methos that was used to call this function. Basic Usage: >>> from vFense.core.customer.customer import add_user_to_customers >>> username = '******' >>> customer_names = ['default', 'TopPatch', 'vFense'] >>> add_user_to_customers(username, customer_names) Returns: Dictionary of the status of the operation. { 'uri': None, 'rv_status_code': 1017, 'http_method': None, 'http_status': 200, 'message': "None - add_user_to_customers - customer names existed 'default', 'TopPatch', 'vFense' unchanged", 'data': [] } """ if isinstance(customer_names, str): customer_names = customer_names.split(',') customers_are_valid = validate_customer_names(customer_names) results = None user_exist = retrieve_object(username, UserCollections.Users) data_list = [] status = add_user_to_customers.func_name + ' - ' msg = '' status_code = 0 generic_status_code = 0 vfense_status_code = 0 generated_ids = [] if customers_are_valid[0]: data_list = [] for customer_name in customer_names: if not users_exists_in_customer(username, customer_name): data_to_add = ({ CustomerPerUserKeys.CustomerName: customer_name, CustomerPerUserKeys.UserName: username, }) data_list.append(data_to_add) if user_exist and data_list: status_code, object_count, error, generated_ids = ( insert_user_per_customer(data_list)) if status_code == DbCodes.Inserted: msg = ('user %s added to %s' % (username, ' and '.join(customer_names))) generic_status_code = GenericCodes.ObjectCreated vfense_status_code = CustomerCodes.CustomersAddedToUser elif user_exist and not data_list: status_code = DbCodes.Unchanged msg = 'customer names existed for user %s' % (username) generic_status_code = GenericCodes.ObjectExists vfense_status_code = CustomerFailureCodes.UsersExistForCustomer elif not user_exist: status_code = DbCodes.Errors msg = 'User name is invalid: %s' % (username) generic_status_code = GenericCodes.InvalidId vfense_status_code = UserFailureCodes.UserNameDoesNotExist elif not customers_are_valid[0]: status_code = DbCodes.Errors msg = ('Customer names are invalid: %s' % (' and '.join(customers_are_valid[2]))) generic_status_code = GenericCodes.InvalidId vfense_status_code = CustomerFailureCodes.InvalidCustomerName results = { ApiResultKeys.DB_STATUS_CODE: status_code, ApiResultKeys.GENERIC_STATUS_CODE: generic_status_code, ApiResultKeys.VFENSE_STATUS_CODE: vfense_status_code, ApiResultKeys.GENERATED_IDS: generated_ids, ApiResultKeys.MESSAGE: status + msg, ApiResultKeys.DATA: [], ApiResultKeys.USERNAME: user_name, ApiResultKeys.URI: uri, ApiResultKeys.HTTP_METHOD: method } return (results)
def add_user_to_groups(username, customer_name, group_ids, user_name=None, uri=None, method=None): """Add a user into a vFense group Args: username (str): Name of the user already in vFense. customer_name (str): The customer this user is part of. group_ids (list): List of group ids. Kwargs: user_name (str): The name of the user who called this function. uri (str): The uri that was used to call this function. method (str): The HTTP methos that was used to call this function. Basic Usage: >>> from vFense.group.groups import add_user_to_groups >>> username = '******' >>> customer_name = 'default' >>> group_ids = ['0834e656-27a5-4b13-ba56-635797d0d1fc'] >>> add_user_to_groups(username, customer_name, group_ids) Returns: Returns the results in a dictionary { 'uri': None, 'rv_status_code': 1010, 'http_method': None, 'http_status': 200, 'message': "None - groups per user [u'ee54820c-cb4e-46a1-9d11-73afe8c4c4e3'] was created", 'data': { 'group_name': u'FooLah', 'user_name': 'alien', 'group_id': '0834e656-27a5-4b13-ba56-635797d0d1fc', 'customer_name': 'default' } } """ status = add_user_to_groups.func_name + ' - ' groups_are_valid = validate_group_ids(group_ids, customer_name) user_exist = retrieve_object(username, UserCollections.Users) customer_exist = retrieve_object(customer_name, CustomerCollections.Customers) results = None generated_ids = [] users_group_exist = [] generic_status_code = 0 vfense_status_code = 0 if groups_are_valid[0] and user_exist and customer_exist: data_list = [] for group_id in group_ids: group_exist = get_group(group_id) user_in_group = (user_exist_in_group(username, group_id)) if not user_in_group: data_to_add = ({ GroupsPerUserKeys.CustomerName: customer_name, GroupsPerUserKeys.UserName: username, GroupsPerUserKeys.GroupName: group_exist[GroupKeys.GroupName], GroupsPerUserKeys.GroupId: group_id }) data_list.append(data_to_add) else: users_group_exist.append(group_id) if len(data_list) == len(group_ids): status_code, object_count, error, generated_ids = ( insert_group_per_user(data_list)) if status_code == DbCodes.Inserted: msg = 'user %s add to groups' % (username) generic_status_code = GenericCodes.ObjectCreated vfense_status_code = GroupCodes.GroupCreated else: msg = ('user %s is already in groups %s' % (username, ' and '.join(users_group_exist))) status_code = DbCodes.Skipped generic_status_code = GenericCodes.ObjectExists vfense_status_code = GroupFailureCodes.GroupExistForUser elif not groups_are_valid[0]: msg = ('Group Ids are invalid: %s' % (' and '.join(groups_are_valid[2]))) status_code = DbCodes.Errors generic_status_code = GenericCodes.InvalidId vfense_status_code = GroupFailureCodes.InvalidGroupId elif not user_exist: msg = 'User name is invalid: %s' % (username) status_code = DbCodes.Errors generic_status_code = GenericCodes.InvalidId vfense_status_code = UserFailureCodes.InvalidUserName elif not customer_exist: msg = 'Customer name is invalid: %s' % (customer_name) status_code = DbCodes.Errors generic_status_code = GenericCodes.InvalidId vfense_status_code = CustomerFailureCodes.CustomerDoesNotExist results = { ApiResultKeys.DB_STATUS_CODE: status_code, ApiResultKeys.GENERIC_STATUS_CODE: generic_status_code, ApiResultKeys.VFENSE_STATUS_CODE: vfense_status_code, ApiResultKeys.GENERATED_IDS: generated_ids, ApiResultKeys.MESSAGE: status + msg, ApiResultKeys.DATA: [], ApiResultKeys.USERNAME: user_name, ApiResultKeys.URI: uri, ApiResultKeys.HTTP_METHOD: method } return (results)
def create_customer( customer, username=None, init=None, user_name=None, uri=None, method=None ): """Create a new customer inside of vFense Args: customer (Customer): A customer instance filled out with the appropriate fields. Kwargs: username (str): Name of the user that you are adding to this customer. Default=None If init is set to True, then it can stay as None else, then a valid user must be passed init (boolean): Create the customer, without adding a user into it. Default=False user_name (str): The name of the user who called this function. uri (str): The uri that was used to call this function. method (str): The HTTP methos that was used to call this function. Basic Usage: >>> from vFense.core.customer import Customer >>> from vFense.core.customer.customers import create_customer >>> customer = Customer('NewCustomer', package_download_url='https://10.0.0.16/packages/') >>> username = '******' >>> create_customer(customer, api_user) Return: Dictionary of the status of the operation. { 'uri': None, 'rv_status_code': 1010, 'http_method': None, 'http_status': 200, 'message': 'api_user - customer vFense was created', 'data': { 'cpu_throttle': 'normal', 'package_download_url_base': 'https: //10.0.0.21/packages/', 'server_queue_ttl': 10, 'agent_queue_ttl': 10, 'net_throttle': 0, 'customer_name': 'vFense' } } """ customer_exist = get_customer(customer.name) status = create_customer.func_name + ' - ' msg = '' status_code = 0 generic_status_code = 0 vfense_status_code = 0 generated_ids = [] invalid_fields = customer.get_invalid_fields() if invalid_fields: # TODO: Inform about more than just the first invalid field invalid_field = invalid_fields[0] status_code = DbCodes.Errors generic_status_code = GenericCodes.InvalidId if invalid_field.get(CustomerKeys.CustomerName): msg = ( 'customer name is not within the 36 character range ' 'or contains unsupported characters :%s' % (invalid_field.get(CustomerKeys.CustomerName)) ) vfense_status_code = CustomerFailureCodes.InvalidCustomerName elif invalid_field.get(CustomerKeys.NetThrottle): msg = ( 'network throttle was not given a valid integer :%s' % (invalid_field.get(CustomerKeys.NetThrottle)) ) vfense_status_code = CustomerFailureCodes.InvalidNetworkThrottle elif invalid_field.get(CustomerKeys.CpuThrottle): msg = ( 'cpu throttle was not given a valid value :%s' % (invalid_field.get(CustomerKeys.CpuThrottle)) ) vfense_status_code = CustomerFailureCodes.InvalidCpuThrottle elif invalid_field.get(CustomerKeys.ServerQueueTTL): msg = ( 'server queue ttl was not given a valid value :%s' % (invalid_field.get(CustomerKeys.ServerQueueTTL)) ) vfense_status_code = CustomerFailureCodes.InvalidOperationTTL elif invalid_field.get(CustomerKeys.AgentQueueTTL): msg = ( 'agent queue ttl was not given a valid value :%s' % (invalid_field.get(CustomerKeys.AgentQueueTTL)) ) vfense_status_code = CustomerFailureCodes.InvalidOperationTTL # TODO: handle invalid base package url string #elif invalid_field.get(CustomerKeys.PackageUrl): elif not customer_exist: # Fill in any empty fields customer.fill_in_defaults() if not customer.package_download_url: customer.package_download_url = ( get_customer( DefaultCustomers.DEFAULT, [CustomerKeys.PackageUrl] ).get(CustomerKeys.PackageUrl) ) object_status, _, _, generated_ids = ( insert_customer(customer.to_dict()) ) if object_status == DbCodes.Inserted: generated_ids.append(customer.name) msg = 'customer %s created - ' % (customer.name) generic_status_code = GenericCodes.ObjectCreated vfense_status_code = CustomerCodes.CustomerCreated if object_status == DbCodes.Inserted and not init and username: add_user_to_customers( username, [customer.name], user_name, uri, method ) if username != DefaultUsers.ADMIN: add_user_to_customers( DefaultUsers.ADMIN, [customer.name], user_name, uri, method ) #The admin user should be part of every group elif object_status == DbCodes.Inserted and username != DefaultUsers.ADMIN: admin_exist = ( retrieve_object( DefaultUsers.ADMIN, UserCollections.Users ) ) if admin_exist: add_user_to_customers( DefaultUsers.ADMIN, [customer.name], user_name, uri, method ) elif customer_exist: status_code = DbCodes.Unchanged msg = 'customer name %s already exists' % (customer.name) generic_status_code = GenericCodes.ObjectExists vfense_status_code = CustomerFailureCodes.CustomerExists results = { ApiResultKeys.DB_STATUS_CODE: status_code, ApiResultKeys.GENERIC_STATUS_CODE: generic_status_code, ApiResultKeys.VFENSE_STATUS_CODE: vfense_status_code, ApiResultKeys.MESSAGE: status + msg, ApiResultKeys.DATA: [customer.to_dict()], ApiResultKeys.USERNAME: user_name, ApiResultKeys.URI: uri, ApiResultKeys.HTTP_METHOD: method } return results
def add_user_to_customers( username, customer_names, user_name=None, uri=None, method=None ): """Add a multiple customers to a user Args: username (str): Name of the user already in vFense. customer_names (list): List of customer names, this user will be added to. Kwargs: user_name (str): The name of the user who called this function. uri (str): The uri that was used to call this function. method (str): The HTTP methos that was used to call this function. Basic Usage: >>> from vFense.core.customer.customer import add_user_to_customers >>> username = '******' >>> customer_names = ['default', 'TopPatch', 'vFense'] >>> add_user_to_customers(username, customer_names) Returns: Dictionary of the status of the operation. { 'uri': None, 'rv_status_code': 1017, 'http_method': None, 'http_status': 200, 'message': "None - add_user_to_customers - customer names existed 'default', 'TopPatch', 'vFense' unchanged", 'data': [] } """ if isinstance(customer_names, str): customer_names = customer_names.split(',') customers_are_valid = validate_customer_names(customer_names) results = None user_exist = retrieve_object(username, UserCollections.Users) data_list = [] status = add_user_to_customers.func_name + ' - ' msg = '' status_code = 0 generic_status_code = 0 vfense_status_code = 0 generated_ids = [] if customers_are_valid[0]: data_list = [] for customer_name in customer_names: if not users_exists_in_customer(username, customer_name): data_to_add = ( { CustomerPerUserKeys.CustomerName: customer_name, CustomerPerUserKeys.UserName: username, } ) data_list.append(data_to_add) if user_exist and data_list: status_code, object_count, error, generated_ids = ( insert_user_per_customer(data_list) ) if status_code == DbCodes.Inserted: msg = ( 'user %s added to %s' % ( username, ' and '.join(customer_names) ) ) generic_status_code = GenericCodes.ObjectCreated vfense_status_code = CustomerCodes.CustomersAddedToUser elif user_exist and not data_list: status_code = DbCodes.Unchanged msg = 'customer names existed for user %s' % (username) generic_status_code = GenericCodes.ObjectExists vfense_status_code = CustomerFailureCodes.UsersExistForCustomer elif not user_exist: status_code = DbCodes.Errors msg = 'User name is invalid: %s' % (username) generic_status_code = GenericCodes.InvalidId vfense_status_code = UserFailureCodes.UserNameDoesNotExist elif not customers_are_valid[0]: status_code = DbCodes.Errors msg = ( 'Customer names are invalid: %s' % ( ' and '.join(customers_are_valid[2]) ) ) generic_status_code = GenericCodes.InvalidId vfense_status_code = CustomerFailureCodes.InvalidCustomerName results = { ApiResultKeys.DB_STATUS_CODE: status_code, ApiResultKeys.GENERIC_STATUS_CODE: generic_status_code, ApiResultKeys.VFENSE_STATUS_CODE: vfense_status_code, ApiResultKeys.GENERATED_IDS: generated_ids, ApiResultKeys.MESSAGE: status + msg, ApiResultKeys.DATA: [], ApiResultKeys.USERNAME: user_name, ApiResultKeys.URI: uri, ApiResultKeys.HTTP_METHOD: method } return(results)