monkey.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import os, sys
  2. from tempfile import NamedTemporaryFile
  3. from subprocess import Popen
  4. from fuse import FUSE, Operations, LoggingMixIn
  5. try:
  6. from urllib.request import urlopen # Python 3
  7. except ImportError:
  8. from urllib2 import urlopen # Python 2
  9. # domain with server.py running on it for testing
  10. DOMAIN = os.getenv("TRAVIS_DOMAIN", "travis-ci.gethttpsforfree.com")
  11. # generate account and domain keys
  12. def gen_keys():
  13. # good account key
  14. account_key = NamedTemporaryFile()
  15. Popen(["openssl", "genrsa", "-out", account_key.name, "2048"]).wait()
  16. # good domain key
  17. domain_key = NamedTemporaryFile()
  18. domain_csr = NamedTemporaryFile()
  19. Popen(["openssl", "req", "-newkey", "rsa:2048", "-nodes", "-keyout", domain_key.name,
  20. "-subj", "/CN={0}".format(DOMAIN), "-out", domain_csr.name]).wait()
  21. return {
  22. "account_key": account_key,
  23. "domain_key": domain_key,
  24. "domain_csr": domain_csr,
  25. }
  26. # fake a folder structure to catch the key authorization file
  27. FS = {}
  28. class Passthrough(LoggingMixIn, Operations): # pragma: no cover
  29. def getattr(self, path, fh=None):
  30. f = FS.get(path, None)
  31. if f is None:
  32. return super(Passthrough, self).getattr(path, fh=fh)
  33. return f
  34. def write(self, path, buf, offset, fh):
  35. urlopen("http://{0}/.well-known/acme-challenge/?{1}".format(DOMAIN,
  36. os.getenv("TRAVIS_SESSION", "not_set")), buf)
  37. return len(buf)
  38. def create(self, path, mode, fi=None):
  39. FS[path] = {"st_mode": 33204}
  40. return 0
  41. def unlink(self, path):
  42. del(FS[path])
  43. return 0
  44. if __name__ == "__main__": # pragma: no cover
  45. FUSE(Passthrough(), sys.argv[1], nothreads=True, foreground=True)