2016-03-19 20:58:06 +01:00
|
|
|
#!/usr/bin/env python3
|
2018-07-27 00:36:45 +02:00
|
|
|
# Copyright (c) 2015-2018 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.
|
2017-01-18 00:34:40 +01:00
|
|
|
"""Test multiple RPC users."""
|
2015-11-11 16:49:32 +01:00
|
|
|
|
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
2018-01-02 14:57:27 +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-14 13:01:31 +02:00
|
|
|
import os
|
2016-03-19 20:58:06 +01:00
|
|
|
import http.client
|
|
|
|
import urllib.parse
|
2018-04-18 23:39:24 +02:00
|
|
|
import subprocess
|
|
|
|
from random import SystemRandom
|
|
|
|
import string
|
|
|
|
import configparser
|
2018-08-03 22:30:15 +02:00
|
|
|
import sys
|
2015-11-11 16:49:32 +01:00
|
|
|
|
2018-01-02 14:57:27 +01:00
|
|
|
|
|
|
|
class HTTPBasicsTest(BitcoinTestFramework):
|
2017-06-10 00:21:21 +02:00
|
|
|
def set_test_params(self):
|
2017-06-06 20:43:24 +02:00
|
|
|
self.num_nodes = 2
|
2015-11-11 16:49:32 +01:00
|
|
|
|
|
|
|
def setup_chain(self):
|
2016-05-14 13:01:31 +02:00
|
|
|
super().setup_chain()
|
2019-08-30 17:28:43 +02:00
|
|
|
#Append rpcauth to lbrycrd.conf before initialization
|
2015-11-11 16:49:32 +01:00
|
|
|
rpcauth = "rpcauth=rt:93648e835a54c573682c2eb19f882535$7681e9c5b74bdd85e78166031d2058e1069b3ed7ed967c93fc63abba06f31144"
|
|
|
|
rpcauth2 = "rpcauth=rt2:f8607b1a88861fac29dfccf9b52ff9f$ff36a0c23c8c62b4846112e50fa888416e94c17bfd4c42f88fd8f55ec6a3137e"
|
2017-06-06 20:43:24 +02:00
|
|
|
rpcuser = "rpcuser=rpcuser💻"
|
|
|
|
rpcpassword = "rpcpassword=rpcpassword🔑"
|
2018-04-18 23:39:24 +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-03 22:30:15 +02:00
|
|
|
p = subprocess.Popen([sys.executable, gen_rpcauth, self.user], stdout=subprocess.PIPE, universal_newlines=True)
|
2018-04-18 23:39:24 +02:00
|
|
|
lines = p.stdout.read().splitlines()
|
|
|
|
rpcauth3 = lines[1]
|
|
|
|
self.password = lines[3]
|
|
|
|
|
2019-08-30 17:28:43 +02:00
|
|
|
with open(os.path.join(get_datadir_path(self.options.tmpdir, 0), "lbrycrd.conf"), 'a', encoding='utf8') as f:
|
2015-11-11 16:49:32 +01:00
|
|
|
f.write(rpcauth+"\n")
|
|
|
|
f.write(rpcauth2+"\n")
|
2018-04-18 23:39:24 +02:00
|
|
|
f.write(rpcauth3+"\n")
|
2019-08-30 17:28:43 +02:00
|
|
|
with open(os.path.join(get_datadir_path(self.options.tmpdir, 1), "lbrycrd.conf"), 'a', encoding='utf8') as f:
|
2017-06-06 20:43:24 +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-03-19 20:58:06 +01:00
|
|
|
url = urllib.parse.urlparse(self.nodes[0].url)
|
2015-11-11 16:49:32 +01:00
|
|
|
|
|
|
|
#Old authpair
|
|
|
|
authpair = url.username + ':' + url.password
|
|
|
|
|
2018-07-14 18:42:39 +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-18 23:39:24 +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-03-19 20:58:06 +01: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-06 20:43:24 +02:00
|
|
|
assert_equal(resp.status, 200)
|
2015-11-11 16:49:32 +01:00
|
|
|
conn.close()
|
2018-01-02 14:57:27 +01:00
|
|
|
|
2015-11-11 16:49:32 +01:00
|
|
|
#Use new authpair to confirm both work
|
2018-04-18 23:39:24 +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-03-19 20:58:06 +01: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-06 20:43:24 +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-18 23:39:24 +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-03-19 20:58:06 +01: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-06 20:43:24 +02:00
|
|
|
assert_equal(resp.status, 401)
|
2015-11-11 16:49:32 +01:00
|
|
|
conn.close()
|
|
|
|
|
|
|
|
#Wrong password for rt
|
2018-04-18 23:39:24 +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-03-19 20:58:06 +01: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-06 20:43:24 +02:00
|
|
|
assert_equal(resp.status, 401)
|
2015-11-11 16:49:32 +01:00
|
|
|
conn.close()
|
|
|
|
|
|
|
|
#Correct for rt2
|
2018-04-18 23:39:24 +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-03-19 20:58:06 +01: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-06 20:43:24 +02:00
|
|
|
assert_equal(resp.status, 200)
|
2015-11-11 16:49:32 +01:00
|
|
|
conn.close()
|
|
|
|
|
|
|
|
#Wrong password for rt2
|
2018-04-18 23:39:24 +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-03-19 20:58:06 +01: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-06 20:43:24 +02:00
|
|
|
assert_equal(resp.status, 401)
|
|
|
|
conn.close()
|
|
|
|
|
2018-04-18 23:39:24 +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-06 20:43:24 +02:00
|
|
|
###############################################################
|
|
|
|
# Check correctness of the rpcuser/rpcpassword config options #
|
|
|
|
###############################################################
|
|
|
|
url = urllib.parse.urlparse(self.nodes[1].url)
|
|
|
|
|
|
|
|
# rpcuser and rpcpassword authpair
|
2018-04-18 23:39:24 +02:00
|
|
|
self.log.info('Correct...')
|
2017-06-06 20:43:24 +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-18 23:39:24 +02:00
|
|
|
self.log.info('Wrong...')
|
2017-06-06 20:43:24 +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 ()
|