forked from khalahan/nmcontrol
-
Notifications
You must be signed in to change notification settings - Fork 37
Namecoin Core REST support using Python-Requests #58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JeremyRand
wants to merge
8
commits into
namecoin:master
Choose a base branch
from
JeremyRand:rest-requests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cb91319
Implement REST API as backend for NMControl.
domob1812 276c26c
Support REST over TLS.
domob1812 73803b1
Merge branch 'rest' of https://github.com/domob1812/nmcontrol.git int…
JeremyRand 040b612
REST code with python-requests.
JeremyRand 8fb890d
Added newline at end of REST backend file.
JeremyRand 55136b2
Fix REST data backend for systems running old versions of python-requ…
JeremyRand 6a3245a
Split off TLS code from REST data backend to its own module.
JeremyRand d77c60a
REST TLS backend now shows useful error message when using broken PyO…
JeremyRand File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| from common import * | ||
| from utils import * | ||
|
|
||
| import requests | ||
| import urllib | ||
| import urlparse | ||
|
|
||
| # This backend requires non-default modules loaded. | ||
| # If not using TLS, you can do this on Fedora with: | ||
| # yum install python-requests | ||
| # If using TLS (experimental), you can do this on Fedora with: | ||
| # yum install python-requests pyOpenSSL python-ndg_httpsclient python-pyasn1 | ||
|
|
||
| # TODO: Finish testing the TLS code. Note that Bitcoin Core is probably removing TLS support soon, so TLS support is solely for things like Nginx proxies. | ||
|
|
||
| fp_sha256 = "" | ||
|
|
||
| def assert_fingerprint(connection, x509, errnum, errdepth, ok): | ||
| # Accept a cert if verification is forced off, or if it's a non-primary CA cert (the main cert will still be verified), or if the SHA256 matches | ||
| return fp_sha256.lower() == "none" or errdepth > 0 or sanitiseFingerprint(x509.digest("sha256")) == sanitiseFingerprint(fp_sha256) | ||
|
|
||
| class backendData(): | ||
| validURL = False | ||
|
|
||
| def __init__(self, conf): | ||
|
|
||
| global fp_sha256 | ||
|
|
||
| url = urlparse.urlparse(conf) | ||
| if url.scheme == 'http' or url.scheme == 'https': | ||
| self.validURL = True | ||
| self.scheme = url.scheme | ||
| self.tls = (url.scheme == 'https') | ||
| self.host = url.hostname | ||
| self.port = url.port | ||
|
|
||
| # Sessions let us reuse TCP connections, while keeping unique identities on different TCP connections | ||
| self.sessions = {} | ||
|
|
||
| if self.tls: | ||
| try: | ||
| # Set ciphers and enable fingerprint verification via PyOpenSSL | ||
| requests.packages.urllib3.contrib.pyopenssl.DEFAULT_SSL_CIPHER_LIST = "EDH+aRSA+AES256:EECDH+aRSA+AES256:!SSLv3" | ||
| requests.packages.urllib3.contrib.pyopenssl._verify_callback = assert_fingerprint | ||
| requests.packages.urllib3.contrib.pyopenssl.inject_into_urllib3() | ||
| except: | ||
| if app['debug']: | ||
| print "ERROR: Failed to load PyOpenSSL; make sure you have the right packages installed." | ||
| print "On Fedora, run:" | ||
| print "sudo yum install pyOpenSSL python-ndg_httpsclient python-pyasn1" | ||
| print "Other distros/OS's may be similar" | ||
|
|
||
| if app['debug']: | ||
| print "WARNING: You are using the experimental REST over TLS feature. This is probably broken and should not be used in production." | ||
|
|
||
| if url.params == '': | ||
| self.fprs = {} | ||
| else: | ||
| self.fprs = self._parseFprOptions(url.params) | ||
|
|
||
| if "sha256" in self.fprs: | ||
| fp_sha256 = self.fprs["sha256"] | ||
|
|
||
| if self.tls and fp_sha256 == "": | ||
| if app['debug']: | ||
| print "ERROR: REST SHA256 fingerprint missing in plugin-data.conf; REST lookups will fail." | ||
|
|
||
| if "testTlsConfig" in self.fprs: | ||
| testResults = self._queryHttpGet("https://www.ssllabs.com/ssltest/viewMyClient.html", "").text | ||
| print "TLS test result:" | ||
| print testResults | ||
| import os | ||
| os._exit(0) | ||
| elif app['debug']: | ||
| print "ERROR: Unsupported scheme for REST URL:", url.scheme | ||
|
|
||
| def getAllNames(self): | ||
| # The REST API doesn't support enumerating the names. | ||
| if app['debug']: | ||
| print 'ERROR: REST data backend does not support name enumeration; set import.mode=none or switch to a different import.from backend.' | ||
| return (True, None) # TODO: Should this be True rather than False? See the data plugin for usage. | ||
|
|
||
| def getName(self, name, sessionId = ""): | ||
|
|
||
| encoded = urllib.quote_plus(name) | ||
|
|
||
| result = self._queryHttpGet(self.scheme + "://" + self.host + ":" + str(self.port) + "/rest/name/" + encoded + ".json", sessionId) | ||
|
|
||
| try: | ||
| resultJson = result.json() | ||
| except ValueError: | ||
| raise Exception("Error parsing REST response. Make sure that Namecoin Core is running with -rest option.") | ||
|
|
||
| return (None, resultJson) | ||
|
|
||
| def _queryHttpGet(self, url, sessionId): | ||
|
|
||
| # set up a session if we haven't yet for this identity (Tor users will use multiple identities) | ||
| if sessionId not in self.sessions: | ||
| if app['debug']: | ||
| print 'Creating new REST identity = "' + sessionId + '"' | ||
| self.sessions[sessionId] = requests.Session() | ||
|
|
||
| return self.sessions[sessionId].get(url) | ||
|
|
||
| def _parseFprOptions(self, s): | ||
| """ | ||
| Parse the REST URI params string that includes (optionally) | ||
| the TLS certificate fingerprints. | ||
| """ | ||
|
|
||
| pieces = s.split(',') | ||
|
|
||
| res = {} | ||
| for p in pieces: | ||
| parts = p.split('=', 1) | ||
| assert len (parts) <= 2 | ||
| if len (parts) == 2: | ||
| res[parts[0]] = parts[1] | ||
|
|
||
| return res | ||
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| def sanitiseFingerprint(fpr): | ||
| """ | ||
| Sanitise a fingerprint (of a TLS certificate, for instance) for | ||
| comparison. This removes colons, spaces and makes the string | ||
| upper case. | ||
| """ | ||
|
|
||
| #fpr = fpr.translate (None, ': ') | ||
| fpr = fpr.replace (":", "") | ||
| fpr = fpr.replace (" ", "") | ||
| fpr = fpr.upper () | ||
|
|
||
| return fpr |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line did not work for me. I had to remove the
()at the end, sinceresult.jsonis a member variable and not function. With the change, it worked.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@domob1812 with your change I get the following upon calling getIp4 via RPC:
Traceback (most recent call last):
File "/home/jeremy/Downloads/NMControl/nmcontrol/plugin/pluginRpc.py", line 170, in computeData
result = methodRpc(method, _params)
File "/home/jeremy/Downloads/NMControl/nmcontrol/lib/plugin.py", line 203, in _rpc
return func(_args)
File "/home/jeremy/Downloads/NMControl/nmcontrol/plugin/pluginDns.py", line 130, in getIp4
result = self._getRecordForRPC(domain, 'getIp4')
File "/home/jeremy/Downloads/NMControl/nmcontrol/plugin/pluginDns.py", line 125, in _getRecordForRPC
self._resolve(domain, recType, result)
File "/home/jeremy/Downloads/NMControl/nmcontrol/plugin/pluginDns.py", line 87, in _resolve
handler._resolve(domain, recType, result)
File "/home/jeremy/Downloads/NMControl/nmcontrol/plugin/pluginNamespaceDomain.py", line 53, in _resolve
nameData = app['plugins']['data'].getValueProcessed(name)
File "/home/jeremy/Downloads/NMControl/nmcontrol/plugin/pluginData.py", line 133, in getValueProcessed
data = self.getValue(name)
File "/home/jeremy/Downloads/NMControl/nmcontrol/plugin/pluginData.py", line 121, in getValue
data = self.getData(name)
File "/home/jeremy/Downloads/NMControl/nmcontrol/plugin/pluginData.py", line 107, in getData
if 'expired' in data and data['expired']:
TypeError: argument of type 'instancemethod' is not iterable
Without your change it works fine for me. Not sure why you're getting different results. FWIW the example code on the front page of http://www.python-requests.org/en/latest/ agrees with my results.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also do not know. Maybe
jsonchanged from a member variable to a member function with the version of requests? I'm running the one in Debian Wheezy, so already at least two years old.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to https://stackoverflow.com/questions/6386308/http-requests-and-json-parsing-in-python/17517598#17517598 , Wheezy is indeed on an old version of requests which has json as a variable. I will push a workaround shortly. Thanks for pointing this out.