def connect(self, host: str, port) -> RetVal: '''Creates a connection to the server.''' try: self.__sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Set a short timeout in case the server doesn't respond immediately, # which is the expectation as soon as a client connects. self.__sock.settimeout(10.0) except: return RetVal(NetworkError, "Couldn't create a socket") out_data = RetVal() out_data.set_value('socket', self.__sock) try: self.ip = socket.gethostbyname(host) except socket.gaierror: self.disconnect() return RetVal(ResourceNotFound, "Couldn't locate host %s" % host) try: self.__sock.connect((self.ip, port)) self.port = port status = self.read_msg(pyanselus.rpc_schemas.greeting) if not status.error(): self.version = status['msg']['version'].strip() except Exception as exc: self.disconnect() return RetVal(NetworkError, f"Couldn't connect to host {host}: {exc}") # Set a timeout of 30 minutes self.__sock.settimeout(1800.0) return out_data
def split_address(address): '''Splits an Anselus numeric address into its two parts.''' parts = address.split('/') if len(parts) != 2 or \ not parts[0] or \ not parts[1] or \ not validate_uuid(parts[0]): return RetVal(BadParameterValue, 'Bad workspace address') out = RetVal() out.set_value('wid', parts[0]) out.set_value('domain', parts[1]) return out
def check_password_complexity(indata: str) -> RetVal: '''Checks the requested string as meeting the needed security standards. Returns: RetVal strength: string in [very weak', 'weak', 'medium', 'strong'] ''' if len(indata) < 8: return RetVal(BadParameterValue, 'Passphrase must be at least 8 characters.') \ .set_value('strength', 'very weak') strength_score = 0 strength_strings = [ 'error', 'very weak', 'weak', 'medium', 'strong', 'very strong'] # Anselus *absolutely* permits UTF-8-encoded passwords. This greatly increases the # keyspace try: indata.encode().decode('ascii') except UnicodeDecodeError: strength_score = strength_score + 1 if re.search(r"\d", indata): strength_score = strength_score + 1 if re.search(r"[A-Z]", indata): strength_score = strength_score + 1 if re.search(r"[a-z]", indata): strength_score = strength_score + 1 if re.search(r"[~`!@#$%^&*()_={}/<>,.:;|'[\]\"\\\-\+\?]", indata): strength_score = strength_score + 1 if (len(indata) < 12 and strength_score < 3) or strength_score < 2: # If the passphrase is less than 12 characters, require complexity status = RetVal(BadParameterValue, 'passphrase too weak') status.set_value('strength', strength_strings[strength_score]) return status return RetVal().set_value('strength', strength_strings[strength_score])
def test_hasvalue(): '''Tests hasvalue()''' r = RetVal() assert r.set_value('foo', 'bar'), 'Failed to set RetVal value' assert r.has_value('foo'), 'Failed to find existing RetVal value'