def register_user(self, user_json): """ register a new user in the persistence checks if the user already exists and then adds to persistence Parameters: user_json [json]: json with node information Return: Success -> 'OK' [str] : returns 'OK' as response Failure -> [str] : returns appropriate failure response """ self.logger.debug(f"Entered register_user with {user_json}") # create user object new_user = User(user_json) # run validations new_user.validate_mandatory_fields() new_user.validate_field_values() # valid inputs - exception would have been raised in case of missing / # invalid info # now, set other fields as required # Password should not be saved as plain text in database. # Encrypting the password before saving it to database self.logger.info("Registering a new user") user_pwd = user_json["pwd"] new_user.set('pwd', sha512_crypt.encrypt(user_json['pwd'])) new_user.set('loginStatus', False) new_user.set('activationStatus', True) new_user.set('nodes', []) self.logger.info("adding user to the database") self.persistence_manager.insert("users", new_user.data, False) self.logger.info("user successfully added to the persistence") try: self.ldapmanager.add(user_json["uid"], user_pwd) except Exception as e: self.logger.error("Unable to add user") print("unable to add user to ldap server : ", e) return XprResponse("failure", None, str(e)) # NFS User directory changes self.logger.info("Setting up NFS for the user") nfs_manager = NFSUserManager(config=self.config) nfs_manager.setup_user_folder(user=user_json['uid']) self.logger.info("NFS set up") return XprResponse("success", None, None)
def create_project(self, project_json: dict) -> object: """ creates a new project in the persistence and bitbucket Creates a new project and repo on bitbucket. Then setup necessary nfs mount. Then adds the project json to persistence. Parameters: project_json: json with project information Return: returns xprresponse object """ # checks if the project_json has the complete info required self.logger.info("checking the provided project information") # Updating next linux uid project_json[self.LINUX_UID_KEY] = self.get_next_linux_uid() new_project = Project(project_json) new_project.project_info_check(self.persistence_manager) new_project.complete_project_info() self.logger.debug(f"Updated project info is: {new_project.data}") self.logger.info("calling setup_project to complete the setup") setup = setup_project(new_project.data) setup_code = setup['status'] self.logger.info(f"setup_project status code is {setup_code}") if setup_code != 200: self.logger.error("Project setup failed") return XprResponse('failure', setup_code, {"message": "Project setup failed"}) self.logger.info("project setup completed") self.logger.info( "Adding the project with complete info to persistence") self.persistence_manager.insert("projects", setup['project_json'], False) self.logger.info("project successfully added to persistence") # allocate required environments self.logger.info("Allocating project environments") env_manager = EnvManager() env_manager.allocate_env(project_json["name"], project_json["environments"]) self.logger.info("Allocated project environments") new_project.filter_display_fields() return XprResponse("success", None, new_project.data)
def modify_user(self, filter_json, changes_json): """ modify_user updates the user info in the persistence checks if user is available and then updates the info as per changes_json Parameters: filter_json: filter to find user changes_json: json with user changes info Return: Success -> 'OK' [str] : returns OK if provision_node succeeds Failure -> [str] : returns appropriate failure response """ self.logger.debug( f"Modifying user information of {filter_json} to {changes_json}") # checks if the user is present in database self.logger.info("Checking if the user is present in the database") users = self.persistence_manager.find("users", filter_json) if not users or len(users) == 0: self.logger.error(f"user {filter_json} not found in the database") raise UserNotFoundException() # checks if the user password is also present in changes_json temp_user = User(changes_json) temp_user.validate_field_values() temp_user.validate_modifiable_fields() self.logger.info("updating the user information") self.persistence_manager.update("users", filter_json, changes_json) return XprResponse('success', '', {})
def deactivate_user(self, uid): """ Deletes an user and his info from the persistence Deletes the user from database Parameters: uid [str] : uid of the user Return: returns appropriate output """ uid_json = {"uid": uid} # deletes the user from persistence del_users = self.persistence_manager.find("users", uid_json) if del_users and len(del_users) != 0: self.logger.info(f"deactivating the user {uid_json['uid']}") if 'activationStatus' in del_users[0] and \ del_users[0]['activationStatus']: self.persistence_manager.update("users", uid_json, {"activationStatus": False}) self.logger.info( f"user {uid_json['uid']} successfully deactivated") return XprResponse('success', '', {}) else: raise DeactivatedUserException else: raise UserNotFoundException()
def logout(self, token): """ Authentication method for user logout Args: token: access token Returns: Deletion status (True/False) """ self.logger.info('entering logout method') status = self.delete_token(token) self.logger.debug( 'exiting logout method with status {}'.format(status)) return XprResponse("success", None, {})
def login(self, credentials): """ Authentication method for user login. Args: credentials: object containing uid and pwd Returns: dictionary object consisting of access token """ try: # validate data authentication_context = AuthenticationContext(credentials) uid = authentication_context.get('uid') self.logger.debug('entering login method ' 'with uid {}'.format(uid)) check = self.validate_credentials(authentication_context) if check == error_codes.already_logged_in: self.logger.debug('Relogin request from {}.'.format(uid)) relogin = True else: relogin = False self.logger.debug('Providing new token to {}.'.format(uid)) token_info = self.generate_token() self.save_token({"uid": uid}, token_info) credentials = { 'access_token': token_info.token, 'relogin': relogin } response = XprResponse('success', None, credentials) self.logger.debug('Login successful. Exiting.') return response except AuthenticationFailedException as e: self.logger.error('Authentication failed for user {}'.format(uid)) return XprResponse("failure", e.error_code, {"message": "Authentication failed"})
def deactivate_node(self, node_id): """ Deactivates a node in persistence Deletes all the installed packages of the node on server then deactivates the node in database Parameters: node_id [str] : name of the node Return: returns appropriate output """ self.logger.info(f"request received for deactivating node {node_id}") node_id_json = {"address": node_id} self.logger.info("checking persistence if node is present or not") nodes = self.persistence_manager.find("nodes", node_id_json) if not len(nodes): raise NodeNotFoundException("Node not found for deactivation") if 'activationStatus' in nodes[0] and not nodes[0]['activationStatus']: self.logger.error("This node is already deactivated") raise NodeDeactivatedException() new_node = Node(nodes[0]) # deletes all the packages installed on the node self.logger.info("deleting all packages on the node") node_deprovision = 1 if new_node.data["provisionStatus"]: # deprovision shall be called only on provisioned nodes node_deprovision = new_node.deprovision_node() if node_deprovision == 1: self.logger.info("deleted all of the packages on node") # deletes the node entry from the database self.logger.info('deactivating node from persistence') deactivate_json = { "activationStatus": False, "provisionStatus": False } self.persistence_manager.update("nodes", node_id_json, deactivate_json) return XprResponse('success', '', {}) else: self.logger.error('Node deletion failed: kubernetes error') raise ProvisionKubernetesException("Deactivation Failed")
def modify_project(self, changes_json: dict): """ Modifies a project in persistence and on bitbucket Parameters: changes_json: project information that needs to be modified Return: returns a xprresponse object """ if 'name' not in changes_json: raise IncompleteProjectInfoException( "Project name needs to be provided for modify_project") uid_json = {'name': changes_json['name']} self.logger.info("checking if the project is already present") projects = self.persistence_manager.find("projects", uid_json) if len(projects) == 0: self.logger.error("cannot modify a project which doesn't exist.") raise NoProjectException("Cannot Modify unregistered project") self.logger.info("calling modify_info_check to validate the info") new_project = Project(projects[0]) new_project.modify_info_check(changes_json, self.persistence_manager) self.logger.info("modify_project_locally has been called") modify_status = modify_project_locally(projects[0], changes_json) if modify_status != 200: self.logger.error("modify_project_locally failed") XprResponse('failure', modify_status, {"message": "Modify project failed"}) self.logger.info( "project info is being modified before updating @persistence") update_json = new_project.modify_project_info(changes_json) self.persistence_manager.update("projects", uid_json, update_json) self.logger.info("Project modified successfully") # allocate required environments self.logger.info("Allocating project environments") env_manager = EnvManager() env_manager.allocate_env(changes_json["name"], changes_json["environments"]) self.logger.info("Allocated project environments")