Skip to content

Commit 708cbb1

Browse files
committed
Merge #14756: Improve rpcauth.py by using argparse and getpass modules
d6cde00 rpcauth: Improve by using argparse and getpass modules (João Barbosa) Pull request description: This PR improves argument handling in `rpcauth.py` script by using `argparse` module. Specifying `-` as password makes it prompt securely with `getpass` module which prevents leaking passwords to bash history. Tree-SHA512: 489d66c95f66b5618cb75fd8f07ea5647281226ab9e32b03051eb43f758b9334ac19b7c82c2ed4f8c7ffbb0bee949b3d389e1564ec7a6e372f2864233bc7cb88
2 parents e77a225 + d6cde00 commit 708cbb1

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

share/rpcauth/README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ RPC Tools
33

44
### [RPCAuth](/share/rpcauth) ###
55

6-
Create login credentials for a JSON-RPC user.
6+
```
7+
usage: rpcauth.py [-h] username [password]
78
8-
Usage:
9+
Create login credentials for a JSON-RPC user
910
10-
./rpcauth.py <username>
11+
positional arguments:
12+
username the username for authentication
13+
password leave empty to generate a random password or specify "-" to
14+
prompt for password
1115
12-
in which case the script will generate a password. To specify a custom password do:
13-
14-
./rpcauth.py <username> <password>
16+
optional arguments:
17+
-h, --help show this help message and exit
18+
```

share/rpcauth/rpcauth.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,44 @@
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

6-
import sys
7-
import os
8-
import base64
6+
from argparse import ArgumentParser
7+
from base64 import urlsafe_b64encode
98
from binascii import hexlify
9+
from getpass import getpass
10+
from os import urandom
11+
1012
import hmac
1113

1214
def generate_salt(size):
1315
"""Create size byte hex salt"""
14-
return hexlify(os.urandom(size)).decode()
16+
return hexlify(urandom(size)).decode()
1517

1618
def generate_password():
1719
"""Create 32 byte b64 password"""
18-
return base64.urlsafe_b64encode(os.urandom(32)).decode('utf-8')
20+
return urlsafe_b64encode(urandom(32)).decode('utf-8')
1921

2022
def password_to_hmac(salt, password):
2123
m = hmac.new(bytearray(salt, 'utf-8'), bytearray(password, 'utf-8'), 'SHA256')
2224
return m.hexdigest()
2325

2426
def main():
25-
if len(sys.argv) < 2:
26-
sys.stderr.write('Please include username (and an optional password, will generate one if not provided) as an argument.\n')
27-
sys.exit(0)
27+
parser = ArgumentParser(description='Create login credentials for a JSON-RPC user')
28+
parser.add_argument('username', help='the username for authentication')
29+
parser.add_argument('password', help='leave empty to generate a random password or specify "-" to prompt for password', nargs='?')
30+
args = parser.parse_args()
2831

29-
username = sys.argv[1]
32+
if not args.password:
33+
args.password = generate_password()
34+
elif args.password == '-':
35+
args.password = getpass()
3036

3137
# Create 16 byte hex salt
3238
salt = generate_salt(16)
33-
if len(sys.argv) > 2:
34-
password = sys.argv[2]
35-
else:
36-
password = generate_password()
37-
password_hmac = password_to_hmac(salt, password)
39+
password_hmac = password_to_hmac(salt, args.password)
3840

3941
print('String to be appended to bitcoin.conf:')
40-
print('rpcauth={0}:{1}${2}'.format(username, salt, password_hmac))
41-
print('Your password:\n{0}'.format(password))
42+
print('rpcauth={0}:{1}${2}'.format(args.username, salt, password_hmac))
43+
print('Your password:\n{0}'.format(args.password))
4244

4345
if __name__ == '__main__':
4446
main()

0 commit comments

Comments
 (0)