r46980 - experiment with appdirs

hawkowl-TA+aISz0psMTMxyoc4vAAJOcrHinNvQL0E9HWUfgJXw@public.gmane.org Thu, 10 Mar 2016 23:27:00 -0700 (MST)
Newsgroups gmane.comp.python.twisted.commits
Message-ID <[email protected]>
Author: hawkowl
Date: Thu Mar 10 23:26:56 2016
New Revision: 46980

Added:
   branches/manhole-hardcodedkey-8229/twisted/python/_appdirs.py
Modified:
   branches/manhole-hardcodedkey-8229/twisted/conch/manhole_tap.py
   branches/manhole-hardcodedkey-8229/twisted/conch/ssh/keys.py
   branches/manhole-hardcodedkey-8229/twisted/python/dist3.py

Log:
experiment with appdirs

Modified: branches/manhole-hardcodedkey-8229/twisted/conch/manhole_tap.py
==============================================================================
--- branches/manhole-hardcodedkey-8229/twisted/conch/manhole_tap.py	(original)
+++ branches/manhole-hardcodedkey-8229/twisted/conch/manhole_tap.py	Thu Mar 10 23:26:56 2016
@@ -12,7 +12,7 @@
 from twisted.internet import protocol
 from twisted.application import service, strports
 from twisted.cred import portal, checkers
-from twisted.python import usage
+from twisted.python import usage, filepath
 
 from twisted.conch import manhole, manhole_ssh, telnet
 from twisted.conch.insults import insults
@@ -67,9 +67,9 @@
           "connections")],
         ["passwd", "p", "/etc/passwd",
          "name of a passwd(5)-format username/password file"],
-        ["sshKeyDir", None, None,
+        ["sshKeyDir", None, "<USER DATA DIR>",
          "Directory where the autogenerated SSH key is kept."],
-        ["sshKeyName", None, None,
+        ["sshKeyName", None, "server.key",
          "Filename of the autogenerated SSH key."],
         ["sshKeySize", None, 4096,
          "Size of the automatically generated SSH key."],
@@ -108,10 +108,9 @@
 
         "passwd": Name of a passwd(5)-format username/password file.
 
-        "sshKeyDir": The folder that the SSH server key will be kept in. If
-                     None, it is in the user's data dir.
+        "sshKeyDir": The folder that the SSH server key will be kept in.
 
-        "sshKeyName": The filename of the key. If None, it is "server.pem".
+        "sshKeyName": The filename of the key.
 
         "sshKeySize": The size of the key, in bits. Default is 4096.
 
@@ -147,8 +146,15 @@
         sshPortal = portal.Portal(sshRealm, [checker])
         sshFactory = manhole_ssh.ConchFactory(sshPortal)
 
-        sshKey = keys._getPersistentRSAKey(options['sshKeyDir'],
-                                           options['sshKeyName'],
+        if options['sshKeyDir'] != "<USER DATA DIR>":
+            keyDir = options['sshKeyDir']
+        else:
+            from twisted.python._appdirs import getDataDirectory
+            keyDir = getDataDirectory()
+
+        keyLocation = filepath.FilePath(keyDir).child(options['sshKeyName'])
+
+        sshKey = keys._getPersistentRSAKey(keyLocation,
                                            int(options['sshKeySize']))
         sshFactory.publicKeys["ssh-rsa"] = sshKey
         sshFactory.privateKeys["ssh-rsa"] = sshKey

Modified: branches/manhole-hardcodedkey-8229/twisted/conch/ssh/keys.py
==============================================================================
--- branches/manhole-hardcodedkey-8229/twisted/conch/ssh/keys.py	(original)
+++ branches/manhole-hardcodedkey-8229/twisted/conch/ssh/keys.py	Thu Mar 10 23:26:56 2016
@@ -1231,44 +1231,26 @@
 
 
 
-def _getPersistentRSAKey(directory=None, filename=None, keySize=4096,
-                         _appdirs=None):
+def _getPersistentRSAKey(location, keySize=4096):
     """
     This function returns a persistent L{Key}.
 
-    The key is loaded from a PEM file in C{directory}, named C{filename}. If it
-    does not exist, a key with the key size of C{keySize} is generated and
-    saved.
+    The key is loaded from a PEM file in C{location}. If it does not exist, a
+    key with the key size of C{keySize} is generated and saved.
 
-    @param directory: The directory the key is stored in.
-    @type directory: L{str} or L{bytes}
-
-    @param filename: The filename of the key file.
-    @type filename: L{str} or L{bytes}
+    @param location: Where the key is stored.
+    @type location: L{twisted.python.filepath.FilePath)
 
     @param keySize: The size of the key, if it needs to be generated.
     @type keySize: L{int}
 
-    @param _appdirs: Private mocking function for testing, do not use.
-
     @returns: A persistent key.
     @rtype: L{Key}
     """
-    if _appdirs is None:
-        import appdirs as _appdirs
-
-    if filename is None:
-        filename = "server.pem"
-
-    if directory is None:
-        directory = _appdirs.user_data_dir("Twisted", "Conch")
-
-    configDir = filepath.FilePath(directory)
-    configDir.makedirs(ignoreExistingDirectory=True)
-    pemFile = configDir.child(filename)
+    location.parent().makedirs(ignoreExistingDirectory=True)
 
     # If it doesn't exist, we want to generate a new key and save it
-    if not pemFile.exists():
+    if not location.exists():
         privateKey = rsa.generate_private_key(
             public_exponent=65537,
             key_size=keySize,
@@ -1281,13 +1263,13 @@
             encryption_algorithm=serialization.NoEncryption()
         )
 
-        pemFile.setContent(pem)
+        location.setContent(pem)
 
     # By this point (save any hilarious race conditions) we should have a
     # working PEM file. Load it!
-    # (Future archelogical readers: I chose not to short circuit above, because
-    # then there's two exit paths to this code!)
-    with pemFile.open("rb") as keyFile:
+    # (Future archaelogical readers: I chose not to short circuit above,
+    # because then there's two exit paths to this code!)
+    with location.open("rb") as keyFile:
         privateKey = serialization.load_pem_private_key(
             keyFile.read(),
             password=None,

Modified: branches/manhole-hardcodedkey-8229/twisted/python/dist3.py
==============================================================================
--- branches/manhole-hardcodedkey-8229/twisted/python/dist3.py	(original)
+++ branches/manhole-hardcodedkey-8229/twisted/python/dist3.py	Thu Mar 10 23:26:56 2016
@@ -158,6 +158,7 @@
     "twisted.protocols.test.__init__",
     "twisted.protocols.tls",
     "twisted.python.__init__",
+    "twisted.python._appdirs",
     "twisted.python._tzhelper",
     "twisted.python._url",
     "twisted.python.compat",