def create_robot(robot_shortname, parent, description="", unstructured_metadata=None): (username_valid, username_issue) = validate_username(robot_shortname) if not username_valid: raise InvalidRobotException( "The name for the robot '%s' is invalid: %s" % (robot_shortname, username_issue) ) username = format_robot_username(parent.username, robot_shortname) try: User.get(User.username == username) msg = "Existing robot with name: %s" % username logger.info(msg) raise InvalidRobotException(msg) except User.DoesNotExist: pass service = LoginService.get(name="quayrobot") try: with db_transaction(): created = User.create(username=username, email=str(uuid.uuid4()), robot=True) token = random_string_generator(length=64)() RobotAccountToken.create(robot_account=created, token=token, fully_migrated=True) FederatedLogin.create( user=created, service=service, service_ident="robot:%s" % created.id ) RobotAccountMetadata.create( robot_account=created, description=description[0:255], unstructured_json=unstructured_metadata or {}, ) return created, token except Exception as ex: raise DataModelException(ex.message)
def lookup_robot(robot_username): try: robot_username.encode("ascii") except UnicodeEncodeError: raise InvalidRobotException("Could not find robot with specified username") try: return User.get(username=robot_username, robot=True) except User.DoesNotExist: raise InvalidRobotException("Could not find robot with specified username")
def verify_robot(robot_username, password): try: password = remove_unicode(password) except UnicodeEncodeError: msg = ('Could not find robot with username: %s and supplied password.' % robot_username) raise InvalidRobotException(msg) result = parse_robot_username(robot_username) if result is None: raise InvalidRobotException('%s is an invalid robot name' % robot_username) robot = lookup_robot(robot_username) assert robot.robot # Lookup the token for the robot. try: token_data = RobotAccountToken.get(robot_account=robot) if not token_data.token.matches(password): msg = ('Could not find robot with username: %s and supplied password.' % robot_username) raise InvalidRobotException(msg) except RobotAccountToken.DoesNotExist: # TODO(remove-unenc): Remove once migrated. if not ActiveDataMigration.has_flag(ERTMigrationFlags.READ_OLD_FIELDS): raise InvalidRobotException(msg) if password.find('robot:') >= 0: # Just to be sure. raise InvalidRobotException(msg) query = (User .select() .join(FederatedLogin) .join(LoginService) .where(FederatedLogin.service_ident == password, LoginService.name == 'quayrobot', User.username == robot_username)) try: robot = query.get() except User.DoesNotExist: msg = ('Could not find robot with username: %s and supplied password.' % robot_username) raise InvalidRobotException(msg) # Find the owner user and ensure it is not disabled. try: owner = User.get(User.username == result[0]) except User.DoesNotExist: raise InvalidRobotException('Robot %s owner does not exist' % robot_username) if not owner.enabled: raise InvalidRobotException('This user has been disabled. Please contact your administrator.') # Mark that the robot was accessed. _basequery.update_last_accessed(robot) return robot
def delete_robot(robot_username): try: robot = User.get(username=robot_username, robot=True) robot.delete_instance(recursive=True, delete_nullable=True) except User.DoesNotExist: raise InvalidRobotException("Could not find robot with username: %s" % robot_username)
def verify_robot(robot_username, password): try: password.encode("ascii") except UnicodeEncodeError: msg = "Could not find robot with username: %s and supplied password." % robot_username raise InvalidRobotException(msg) result = parse_robot_username(robot_username) if result is None: raise InvalidRobotException("%s is an invalid robot name" % robot_username) robot = lookup_robot(robot_username) assert robot.robot # Lookup the token for the robot. try: token_data = RobotAccountToken.get(robot_account=robot) if not token_data.token.matches(password): msg = "Could not find robot with username: %s and supplied password." % robot_username raise InvalidRobotException(msg) except RobotAccountToken.DoesNotExist: msg = "Could not find robot with username: %s and supplied password." % robot_username raise InvalidRobotException(msg) # Find the owner user and ensure it is not disabled. try: owner = User.get(User.username == result[0]) except User.DoesNotExist: raise InvalidRobotException("Robot %s owner does not exist" % robot_username) if not owner.enabled: raise InvalidRobotException( "This user has been disabled. Please contact your administrator.") # Mark that the robot was accessed. _basequery.update_last_accessed(robot) return robot
def lookup_robot(robot_username): try: return User.get(username=robot_username, robot=True) except User.DoesNotExist: raise InvalidRobotException("Could not find robot with username: %s" % robot_username)