Skip to content

Commit 536590f

Browse files
committed
Merge #16334: test: rpc_users: Also test rpcauth.py with password.
e263a34 test: rpc_users: Make variable names more clear. (Carl Dong) 830dc2d test: rpc_users: Also test rpcauth.py with specified password. (Carl Dong) c73d871 test: rpc_users: Add function for testing auth params. (Carl Dong) 604e2a9 test: rpc_users: Add function for auth'd requests. (Carl Dong) Pull request description: Fixes #14758 First two commits are tidy-ups which I feel are worthwhile as they are very straightforward, cut down the file by 50%, and made the final diff more minimal. Happy to squash after review. ACKs for top commit: laanwj: ACK e263a34 Tree-SHA512: aa75c48570a87060238932d4c68e17234e158077f6195fb4917367e1ecc565e3cd8dd0ae51f9159ddd3d03742739680391bc1246454302db22d4a608c0633e80
2 parents 3453cf2 + e263a34 commit 536590f

File tree

1 file changed

+41
-141
lines changed

1 file changed

+41
-141
lines changed

test/functional/rpc_users.py

Lines changed: 41 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@
2020
import configparser
2121
import sys
2222

23+
def call_with_auth(node, user, password):
24+
url = urllib.parse.urlparse(node.url)
25+
headers = {"Authorization": "Basic " + str_to_b64str('{}:{}'.format(user, password))}
26+
27+
conn = http.client.HTTPConnection(url.hostname, url.port)
28+
conn.connect()
29+
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
30+
resp = conn.getresponse()
31+
conn.close()
32+
return resp
33+
2334

2435
class HTTPBasicsTest(BitcoinTestFramework):
2536
def set_test_params(self):
@@ -28,15 +39,24 @@ def set_test_params(self):
2839
def setup_chain(self):
2940
super().setup_chain()
3041
#Append rpcauth to bitcoin.conf before initialization
42+
self.rtpassword = "cA773lm788buwYe4g4WT+05pKyNruVKjQ25x3n0DQcM="
3143
rpcauth = "rpcauth=rt:93648e835a54c573682c2eb19f882535$7681e9c5b74bdd85e78166031d2058e1069b3ed7ed967c93fc63abba06f31144"
32-
rpcauth2 = "rpcauth=rt2:f8607b1a88861fac29dfccf9b52ff9f$ff36a0c23c8c62b4846112e50fa888416e94c17bfd4c42f88fd8f55ec6a3137e"
33-
rpcuser = "rpcuser=rpcuser💻"
34-
rpcpassword = "rpcpassword=rpcpassword🔑"
3544

36-
self.user = ''.join(SystemRandom().choice(string.ascii_letters + string.digits) for _ in range(10))
45+
self.rpcuser = "rpcuser💻"
46+
self.rpcpassword = "rpcpassword🔑"
47+
3748
config = configparser.ConfigParser()
3849
config.read_file(open(self.options.configfile))
3950
gen_rpcauth = config['environment']['RPCAUTH']
51+
52+
# Generate RPCAUTH with specified password
53+
self.rt2password = "8/F3uMDw4KSEbw96U3CA1C4X05dkHDN2BPFjTgZW4KI="
54+
p = subprocess.Popen([sys.executable, gen_rpcauth, 'rt2', self.rt2password], stdout=subprocess.PIPE, universal_newlines=True)
55+
lines = p.stdout.read().splitlines()
56+
rpcauth2 = lines[1]
57+
58+
# Generate RPCAUTH without specifying password
59+
self.user = ''.join(SystemRandom().choice(string.ascii_letters + string.digits) for _ in range(10))
4060
p = subprocess.Popen([sys.executable, gen_rpcauth, self.user], stdout=subprocess.PIPE, universal_newlines=True)
4161
lines = p.stdout.read().splitlines()
4262
rpcauth3 = lines[1]
@@ -47,160 +67,40 @@ def setup_chain(self):
4767
f.write(rpcauth2+"\n")
4868
f.write(rpcauth3+"\n")
4969
with open(os.path.join(get_datadir_path(self.options.tmpdir, 1), "bitcoin.conf"), 'a', encoding='utf8') as f:
50-
f.write(rpcuser+"\n")
51-
f.write(rpcpassword+"\n")
52-
53-
def run_test(self):
54-
55-
##################################################
56-
# Check correctness of the rpcauth config option #
57-
##################################################
58-
url = urllib.parse.urlparse(self.nodes[0].url)
59-
60-
#Old authpair
61-
authpair = url.username + ':' + url.password
62-
63-
#New authpair generated via share/rpcauth tool
64-
password = "cA773lm788buwYe4g4WT+05pKyNruVKjQ25x3n0DQcM="
65-
66-
#Second authpair with different username
67-
password2 = "8/F3uMDw4KSEbw96U3CA1C4X05dkHDN2BPFjTgZW4KI="
68-
authpairnew = "rt:"+password
70+
f.write("rpcuser={}\n".format(self.rpcuser))
71+
f.write("rpcpassword={}\n".format(self.rpcpassword))
6972

73+
def test_auth(self, node, user, password):
7074
self.log.info('Correct...')
71-
headers = {"Authorization": "Basic " + str_to_b64str(authpair)}
75+
assert_equal(200, call_with_auth(node, user, password).status)
7276

73-
conn = http.client.HTTPConnection(url.hostname, url.port)
74-
conn.connect()
75-
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
76-
resp = conn.getresponse()
77-
assert_equal(resp.status, 200)
78-
conn.close()
79-
80-
#Use new authpair to confirm both work
81-
self.log.info('Correct...')
82-
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
83-
84-
conn = http.client.HTTPConnection(url.hostname, url.port)
85-
conn.connect()
86-
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
87-
resp = conn.getresponse()
88-
assert_equal(resp.status, 200)
89-
conn.close()
90-
91-
#Wrong login name with rt's password
9277
self.log.info('Wrong...')
93-
authpairnew = "rtwrong:"+password
94-
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
95-
96-
conn = http.client.HTTPConnection(url.hostname, url.port)
97-
conn.connect()
98-
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
99-
resp = conn.getresponse()
100-
assert_equal(resp.status, 401)
101-
conn.close()
78+
assert_equal(401, call_with_auth(node, user, password+'wrong').status)
10279

103-
#Wrong password for rt
10480
self.log.info('Wrong...')
105-
authpairnew = "rt:"+password+"wrong"
106-
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
81+
assert_equal(401, call_with_auth(node, user+'wrong', password).status)
10782

108-
conn = http.client.HTTPConnection(url.hostname, url.port)
109-
conn.connect()
110-
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
111-
resp = conn.getresponse()
112-
assert_equal(resp.status, 401)
113-
conn.close()
114-
115-
#Correct for rt2
116-
self.log.info('Correct...')
117-
authpairnew = "rt2:"+password2
118-
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
119-
120-
conn = http.client.HTTPConnection(url.hostname, url.port)
121-
conn.connect()
122-
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
123-
resp = conn.getresponse()
124-
assert_equal(resp.status, 200)
125-
conn.close()
126-
127-
#Wrong password for rt2
12883
self.log.info('Wrong...')
129-
authpairnew = "rt2:"+password2+"wrong"
130-
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
131-
132-
conn = http.client.HTTPConnection(url.hostname, url.port)
133-
conn.connect()
134-
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
135-
resp = conn.getresponse()
136-
assert_equal(resp.status, 401)
137-
conn.close()
84+
assert_equal(401, call_with_auth(node, user+'wrong', password+'wrong').status)
13885

139-
#Correct for randomly generated user
140-
self.log.info('Correct...')
141-
authpairnew = self.user+":"+self.password
142-
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
143-
144-
conn = http.client.HTTPConnection(url.hostname, url.port)
145-
conn.connect()
146-
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
147-
resp = conn.getresponse()
148-
assert_equal(resp.status, 200)
149-
conn.close()
86+
def run_test(self):
15087

151-
#Wrong password for randomly generated user
152-
self.log.info('Wrong...')
153-
authpairnew = self.user+":"+self.password+"Wrong"
154-
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
88+
##################################################
89+
# Check correctness of the rpcauth config option #
90+
##################################################
91+
url = urllib.parse.urlparse(self.nodes[0].url)
15592

156-
conn = http.client.HTTPConnection(url.hostname, url.port)
157-
conn.connect()
158-
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
159-
resp = conn.getresponse()
160-
assert_equal(resp.status, 401)
161-
conn.close()
93+
self.test_auth(self.nodes[0], url.username, url.password)
94+
self.test_auth(self.nodes[0], 'rt', self.rtpassword)
95+
self.test_auth(self.nodes[0], 'rt2', self.rt2password)
96+
self.test_auth(self.nodes[0], self.user, self.password)
16297

16398
###############################################################
16499
# Check correctness of the rpcuser/rpcpassword config options #
165100
###############################################################
166101
url = urllib.parse.urlparse(self.nodes[1].url)
167102

168-
# rpcuser and rpcpassword authpair
169-
self.log.info('Correct...')
170-
rpcuserauthpair = "rpcuser💻:rpcpassword🔑"
171-
172-
headers = {"Authorization": "Basic " + str_to_b64str(rpcuserauthpair)}
173-
174-
conn = http.client.HTTPConnection(url.hostname, url.port)
175-
conn.connect()
176-
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
177-
resp = conn.getresponse()
178-
assert_equal(resp.status, 200)
179-
conn.close()
180-
181-
#Wrong login name with rpcuser's password
182-
rpcuserauthpair = "rpcuserwrong:rpcpassword"
183-
headers = {"Authorization": "Basic " + str_to_b64str(rpcuserauthpair)}
184-
185-
conn = http.client.HTTPConnection(url.hostname, url.port)
186-
conn.connect()
187-
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
188-
resp = conn.getresponse()
189-
assert_equal(resp.status, 401)
190-
conn.close()
191-
192-
#Wrong password for rpcuser
193-
self.log.info('Wrong...')
194-
rpcuserauthpair = "rpcuser:rpcpasswordwrong"
195-
headers = {"Authorization": "Basic " + str_to_b64str(rpcuserauthpair)}
196-
197-
conn = http.client.HTTPConnection(url.hostname, url.port)
198-
conn.connect()
199-
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
200-
resp = conn.getresponse()
201-
assert_equal(resp.status, 401)
202-
conn.close()
203-
103+
self.test_auth(self.nodes[1], self.rpcuser, self.rpcpassword)
204104

205105
if __name__ == '__main__':
206106
HTTPBasicsTest ().main ()

0 commit comments

Comments
 (0)