2016-05-06 11:23:48 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright (c) 2015-2016 The Bitcoin Core developers
|
2015-11-11 16:49:32 +01:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2019-01-07 10:55:35 +01:00
|
|
|
"""Test multiple RPC users."""
|
2015-11-11 16:49:32 +01:00
|
|
|
|
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
2018-03-22 11:04:37 +01:00
|
|
|
from test_framework.util import (
|
|
|
|
assert_equal,
|
|
|
|
get_datadir_path,
|
|
|
|
str_to_b64str,
|
|
|
|
)
|
2015-11-11 16:49:32 +01:00
|
|
|
|
2016-05-20 15:16:51 +02:00
|
|
|
import os
|
2016-05-06 11:23:48 +02:00
|
|
|
import http.client
|
|
|
|
import urllib.parse
|
2018-04-24 13:51:09 +02:00
|
|
|
import subprocess
|
|
|
|
from random import SystemRandom
|
|
|
|
import string
|
|
|
|
import configparser
|
2018-08-11 13:01:29 +02:00
|
|
|
import sys
|
2015-11-11 16:49:32 +01:00
|
|
|
|
2018-03-22 11:04:37 +01:00
|
|
|
|
|
|
|
class HTTPBasicsTest(BitcoinTestFramework):
|
2017-09-01 18:47:13 +02:00
|
|
|
def set_test_params(self):
|
2017-06-21 14:26:10 +02:00
|
|
|
self.num_nodes = 2
|
2019-12-09 19:52:38 +01:00
|
|
|
self.supports_cli = False
|
2015-11-11 16:49:32 +01:00
|
|
|
|
|
|
|
def setup_chain(self):
|
2016-05-20 15:16:51 +02:00
|
|
|
super().setup_chain()
|
2016-03-06 16:14:39 +01:00
|
|
|
#Append rpcauth to dash.conf before initialization
|
2015-11-11 16:49:32 +01:00
|
|
|
rpcauth = "rpcauth=rt:93648e835a54c573682c2eb19f882535$7681e9c5b74bdd85e78166031d2058e1069b3ed7ed967c93fc63abba06f31144"
|
|
|
|
rpcauth2 = "rpcauth=rt2:f8607b1a88861fac29dfccf9b52ff9f$ff36a0c23c8c62b4846112e50fa888416e94c17bfd4c42f88fd8f55ec6a3137e"
|
2017-06-21 14:26:10 +02:00
|
|
|
rpcuser = "rpcuser=rpcuser💻"
|
|
|
|
rpcpassword = "rpcpassword=rpcpassword🔑"
|
2018-04-24 13:51:09 +02:00
|
|
|
|
|
|
|
self.user = ''.join(SystemRandom().choice(string.ascii_letters + string.digits) for _ in range(10))
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config.read_file(open(self.options.configfile))
|
|
|
|
gen_rpcauth = config['environment']['RPCAUTH']
|
2018-08-11 13:01:29 +02:00
|
|
|
p = subprocess.Popen([sys.executable, gen_rpcauth, self.user], stdout=subprocess.PIPE, universal_newlines=True)
|
2018-04-24 13:51:09 +02:00
|
|
|
lines = p.stdout.read().splitlines()
|
|
|
|
rpcauth3 = lines[1]
|
|
|
|
self.password = lines[3]
|
|
|
|
|
2018-03-22 11:04:37 +01:00
|
|
|
with open(os.path.join(get_datadir_path(self.options.tmpdir, 0), "dash.conf"), 'a', encoding='utf8') as f:
|
2015-11-11 16:49:32 +01:00
|
|
|
f.write(rpcauth+"\n")
|
|
|
|
f.write(rpcauth2+"\n")
|
2018-04-24 13:51:09 +02:00
|
|
|
f.write(rpcauth3+"\n")
|
2018-03-22 11:04:37 +01:00
|
|
|
with open(os.path.join(get_datadir_path(self.options.tmpdir, 1), "dash.conf"), 'a', encoding='utf8') as f:
|
2017-06-21 14:26:10 +02:00
|
|
|
f.write(rpcuser+"\n")
|
|
|
|
f.write(rpcpassword+"\n")
|
2015-11-11 16:49:32 +01:00
|
|
|
|
|
|
|
def run_test(self):
|
|
|
|
|
|
|
|
##################################################
|
|
|
|
# Check correctness of the rpcauth config option #
|
|
|
|
##################################################
|
2016-05-06 11:23:48 +02:00
|
|
|
url = urllib.parse.urlparse(self.nodes[0].url)
|
2015-11-11 16:49:32 +01:00
|
|
|
|
|
|
|
#Old authpair
|
|
|
|
authpair = url.username + ':' + url.password
|
|
|
|
|
2020-07-19 08:15:38 +02:00
|
|
|
#New authpair generated via share/rpcauth tool
|
2015-11-11 16:49:32 +01:00
|
|
|
password = "cA773lm788buwYe4g4WT+05pKyNruVKjQ25x3n0DQcM="
|
|
|
|
|
|
|
|
#Second authpair with different username
|
|
|
|
password2 = "8/F3uMDw4KSEbw96U3CA1C4X05dkHDN2BPFjTgZW4KI="
|
|
|
|
authpairnew = "rt:"+password
|
|
|
|
|
2018-04-24 13:51:09 +02:00
|
|
|
self.log.info('Correct...')
|
2016-04-10 16:54:28 +02:00
|
|
|
headers = {"Authorization": "Basic " + str_to_b64str(authpair)}
|
2015-11-11 16:49:32 +01:00
|
|
|
|
2016-05-06 11:23:48 +02:00
|
|
|
conn = http.client.HTTPConnection(url.hostname, url.port)
|
2015-11-11 16:49:32 +01:00
|
|
|
conn.connect()
|
|
|
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
|
|
|
resp = conn.getresponse()
|
2017-06-21 14:26:10 +02:00
|
|
|
assert_equal(resp.status, 200)
|
2015-11-11 16:49:32 +01:00
|
|
|
conn.close()
|
Merge #12987: tests/tools: Enable additional Python flake8 rules for automatic linting via Travis
643aad17fa Enable additional flake8 rules (practicalswift)
f020aca297 Minor Python cleanups to make flake8 pass with the new rules enabled (practicalswift)
Pull request description:
Enabled rules:
```
* E242: tab after ','
* E266: too many leading '#' for block comment
* E401: multiple imports on one line
* E402: module level import not at top of file
* E701: multiple statements on one line (colon)
* E901: SyntaxError: invalid syntax
* E902: TokenError: EOF in multi-line string
* F821: undefined name 'Foo'
* W293: blank line contains whitespace
* W606: 'async' and 'await' are reserved keywords starting with Python 3.7
```
Note to reviewers:
* In general we don't allow whitespace cleanups to existing code, but in order to allow for enabling Travis checking for these rules a few smaller whitespace cleanups had to made as part of this PR.
* Use [this `?w=1` link](https://github.com/bitcoin/bitcoin/pull/12987/files?w=1) to show a diff without whitespace changes.
Before this commit:
```
$ flake8 -qq --statistics --ignore=B,C,E,F,I,N,W --select=E112,E113,E115,E116,E125,E131,E133,E223,E224,E242,E266,E271,E272,E273,E274,E275,E304,E306,E401,E402,E502,E701,E702,E703,E714,E721,E741,E742,E743,F401,E901,E902,F402,F404,F406,F407,F601,F602,F621,F622,F631,F701,F702,F703,F704,F705,F706,F707,F811,F812,F821,F822,F823,F831,F841,W292,W293,W504,W601,W602,W603,W604,W605,W606 .
5 E266 too many leading '#' for block comment
4 E401 multiple imports on one line
6 E402 module level import not at top of file
5 E701 multiple statements on one line (colon)
1 F812 list comprehension redefines 'n' from line 159
4 F821 undefined name 'ConnectionRefusedError'
28 W293 blank line contains whitespace
```
After this commit:
```
$ flake8 -qq --statistics --ignore=B,C,E,F,I,N,W --select=E112,E113,E115,E116,E125,E131,E133,E223,E224,E242,E266,E271,E272,E273,E274,E275,E304,E306,E401,E402,E502,E701,E702,E703,E714,E721,E741,E742,E743,F401,E901,E902,F402,F404,F406,F407,F601,F602,F621,F622,F631,F701,F702,F703,F704,F705,F706,F707,F811,F812,F821,F822,F823,F831,F841,W292,W293,W504,W601,W602,W603,W604,W605,W606 .
$
```
Tree-SHA512: fc7d5e752298a50d4248afc620ee2c173135b4ca008e48e02913ac968e5a24a5fd5396926047ec62f1d580d537434ccae01f249bb2f3338fa59dc630bf97ca7a
Signed-off-by: pasta <pasta@dashboost.org>
2018-04-16 17:49:49 +02:00
|
|
|
|
2015-11-11 16:49:32 +01:00
|
|
|
#Use new authpair to confirm both work
|
2018-04-24 13:51:09 +02:00
|
|
|
self.log.info('Correct...')
|
2016-04-10 16:54:28 +02:00
|
|
|
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
|
2015-11-11 16:49:32 +01:00
|
|
|
|
2016-05-06 11:23:48 +02:00
|
|
|
conn = http.client.HTTPConnection(url.hostname, url.port)
|
2015-11-11 16:49:32 +01:00
|
|
|
conn.connect()
|
|
|
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
|
|
|
resp = conn.getresponse()
|
2017-06-21 14:26:10 +02:00
|
|
|
assert_equal(resp.status, 200)
|
2015-11-11 16:49:32 +01:00
|
|
|
conn.close()
|
|
|
|
|
|
|
|
#Wrong login name with rt's password
|
2018-04-24 13:51:09 +02:00
|
|
|
self.log.info('Wrong...')
|
2015-11-11 16:49:32 +01:00
|
|
|
authpairnew = "rtwrong:"+password
|
2016-04-10 16:54:28 +02:00
|
|
|
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
|
2015-11-11 16:49:32 +01:00
|
|
|
|
2016-05-06 11:23:48 +02:00
|
|
|
conn = http.client.HTTPConnection(url.hostname, url.port)
|
2015-11-11 16:49:32 +01:00
|
|
|
conn.connect()
|
|
|
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
|
|
|
resp = conn.getresponse()
|
2017-06-21 14:26:10 +02:00
|
|
|
assert_equal(resp.status, 401)
|
2015-11-11 16:49:32 +01:00
|
|
|
conn.close()
|
|
|
|
|
|
|
|
#Wrong password for rt
|
2018-04-24 13:51:09 +02:00
|
|
|
self.log.info('Wrong...')
|
2015-11-11 16:49:32 +01:00
|
|
|
authpairnew = "rt:"+password+"wrong"
|
2016-04-10 16:54:28 +02:00
|
|
|
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
|
2015-11-11 16:49:32 +01:00
|
|
|
|
2016-05-06 11:23:48 +02:00
|
|
|
conn = http.client.HTTPConnection(url.hostname, url.port)
|
2015-11-11 16:49:32 +01:00
|
|
|
conn.connect()
|
|
|
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
|
|
|
resp = conn.getresponse()
|
2017-06-21 14:26:10 +02:00
|
|
|
assert_equal(resp.status, 401)
|
2015-11-11 16:49:32 +01:00
|
|
|
conn.close()
|
|
|
|
|
|
|
|
#Correct for rt2
|
2018-04-24 13:51:09 +02:00
|
|
|
self.log.info('Correct...')
|
2015-11-11 16:49:32 +01:00
|
|
|
authpairnew = "rt2:"+password2
|
2016-04-10 16:54:28 +02:00
|
|
|
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
|
2015-11-11 16:49:32 +01:00
|
|
|
|
2016-05-06 11:23:48 +02:00
|
|
|
conn = http.client.HTTPConnection(url.hostname, url.port)
|
2015-11-11 16:49:32 +01:00
|
|
|
conn.connect()
|
|
|
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
|
|
|
resp = conn.getresponse()
|
2017-06-21 14:26:10 +02:00
|
|
|
assert_equal(resp.status, 200)
|
2015-11-11 16:49:32 +01:00
|
|
|
conn.close()
|
|
|
|
|
|
|
|
#Wrong password for rt2
|
2018-04-24 13:51:09 +02:00
|
|
|
self.log.info('Wrong...')
|
2015-11-11 16:49:32 +01:00
|
|
|
authpairnew = "rt2:"+password2+"wrong"
|
2016-04-10 16:54:28 +02:00
|
|
|
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
|
2015-11-11 16:49:32 +01:00
|
|
|
|
2016-05-06 11:23:48 +02:00
|
|
|
conn = http.client.HTTPConnection(url.hostname, url.port)
|
2015-11-11 16:49:32 +01:00
|
|
|
conn.connect()
|
|
|
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
|
|
|
resp = conn.getresponse()
|
2017-06-21 14:26:10 +02:00
|
|
|
assert_equal(resp.status, 401)
|
|
|
|
conn.close()
|
|
|
|
|
2018-04-24 13:51:09 +02:00
|
|
|
#Correct for randomly generated user
|
|
|
|
self.log.info('Correct...')
|
|
|
|
authpairnew = self.user+":"+self.password
|
|
|
|
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
|
|
|
|
|
|
|
|
conn = http.client.HTTPConnection(url.hostname, url.port)
|
|
|
|
conn.connect()
|
|
|
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
|
|
|
resp = conn.getresponse()
|
|
|
|
assert_equal(resp.status, 200)
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
#Wrong password for randomly generated user
|
|
|
|
self.log.info('Wrong...')
|
|
|
|
authpairnew = self.user+":"+self.password+"Wrong"
|
|
|
|
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
|
|
|
|
|
|
|
|
conn = http.client.HTTPConnection(url.hostname, url.port)
|
|
|
|
conn.connect()
|
|
|
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
|
|
|
resp = conn.getresponse()
|
|
|
|
assert_equal(resp.status, 401)
|
|
|
|
conn.close()
|
|
|
|
|
2017-06-21 14:26:10 +02:00
|
|
|
###############################################################
|
|
|
|
# Check correctness of the rpcuser/rpcpassword config options #
|
|
|
|
###############################################################
|
|
|
|
url = urllib.parse.urlparse(self.nodes[1].url)
|
|
|
|
|
|
|
|
# rpcuser and rpcpassword authpair
|
2018-04-24 13:51:09 +02:00
|
|
|
self.log.info('Correct...')
|
2017-06-21 14:26:10 +02:00
|
|
|
rpcuserauthpair = "rpcuser💻:rpcpassword🔑"
|
|
|
|
|
|
|
|
headers = {"Authorization": "Basic " + str_to_b64str(rpcuserauthpair)}
|
|
|
|
|
|
|
|
conn = http.client.HTTPConnection(url.hostname, url.port)
|
|
|
|
conn.connect()
|
|
|
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
|
|
|
resp = conn.getresponse()
|
|
|
|
assert_equal(resp.status, 200)
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
#Wrong login name with rpcuser's password
|
|
|
|
rpcuserauthpair = "rpcuserwrong:rpcpassword"
|
|
|
|
headers = {"Authorization": "Basic " + str_to_b64str(rpcuserauthpair)}
|
|
|
|
|
|
|
|
conn = http.client.HTTPConnection(url.hostname, url.port)
|
|
|
|
conn.connect()
|
|
|
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
|
|
|
resp = conn.getresponse()
|
|
|
|
assert_equal(resp.status, 401)
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
#Wrong password for rpcuser
|
2018-04-24 13:51:09 +02:00
|
|
|
self.log.info('Wrong...')
|
2017-06-21 14:26:10 +02:00
|
|
|
rpcuserauthpair = "rpcuser:rpcpasswordwrong"
|
|
|
|
headers = {"Authorization": "Basic " + str_to_b64str(rpcuserauthpair)}
|
|
|
|
|
|
|
|
conn = http.client.HTTPConnection(url.hostname, url.port)
|
|
|
|
conn.connect()
|
|
|
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
|
|
|
resp = conn.getresponse()
|
|
|
|
assert_equal(resp.status, 401)
|
2015-11-11 16:49:32 +01:00
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
HTTPBasicsTest ().main ()
|