def getAuthorizedKeys(self, username): userKeys = [] keyFile = self.baseDir.child(username + b".key") keyDir = self.baseDir.child(username) print(keyFile, keyDir) if keyFile.isfile(): for key in readAuthorizedKeyFile(keyFile.open(), self.parseKey): yield key if keyDir.isdir(): for f in keyDir.globChildren("*.key"): for key in readAuthorizedKeyFile(f.open(), self.parseKey): yield key
def test_ignoresLeadingWhitespaceAndEmptyLines(self): """ L{checkers.readAuthorizedKeyFile} ignores leading whitespace in lines, as well as empty lines """ fileobj = StringIO(u""" # ignore not ignored """) result = checkers.readAuthorizedKeyFile(fileobj, parseKey=lambda x: x) self.assertEqual(['not ignored'], list(result))
def test_ignoresComments(self): """ L{checkers.readAuthorizedKeyFile} does not attempt to turn comments into keys """ fileobj = StringIO(u'# this comment is ignored\n' u'this is not\n' u'# this is again\n' u'and this is not') result = checkers.readAuthorizedKeyFile(fileobj, lambda x: x) self.assertEqual(['this is not', 'and this is not'], list(result))
def test_ignoresComments(self): """ L{checkers.readAuthorizedKeyFile} does not attempt to turn comments into keys """ fileobj = BytesIO(b"# this comment is ignored\n" b"this is not\n" b"# this is again\n" b"and this is not") result = checkers.readAuthorizedKeyFile(fileobj, lambda x: x) self.assertEqual([b"this is not", b"and this is not"], list(result))
def test_ignoresUnparsableKeys(self): """ L{checkers.readAuthorizedKeyFile} does not raise an exception when a key fails to parse (raises a L{twisted.conch.ssh.keys.BadKeyError}), but rather just keeps going """ def failOnSome(line): if line.startswith('f'): raise keys.BadKeyError('failed to parse') return line fileobj = StringIO(u'failed key\ngood key') result = checkers.readAuthorizedKeyFile(fileobj, parseKey=failOnSome) self.assertEqual(['good key'], list(result))