susmng.py reads secret from file instead from cli

This commit is contained in:
Akbar Rahman 2022-05-26 02:32:56 +01:00
parent 1b5746096e
commit d4d1c8e956
Signed by: alvierahman90
GPG Key ID: 20609519444A1269

View File

@ -4,6 +4,8 @@ import sys
import requests import requests
import hmac import hmac
import hashlib import hashlib
import pathlib
import os
def get_args(): def get_args():
@ -15,13 +17,21 @@ def get_args():
parser.add_argument('command') parser.add_argument('command')
parser.add_argument('shortlink') parser.add_argument('shortlink')
parser.add_argument('value') parser.add_argument('value')
parser.add_argument('--secret', default="secret") parser.add_argument('--secret-file', type=pathlib.Path, default=pathlib.Path(os.path.expanduser('~/.susmng_secret')))
parser.add_argument('--http', action='store_true') parser.add_argument('--http', action='store_true')
return parser.parse_args() return parser.parse_args()
def main(args): def main(args):
""" Entry point for script """ """ Entry point for script """
if not args.secret_file.exists():
print(f"secret file does not exist at: {args.secret_file}")
return
with open(args.secret_file) as fp:
secret = fp.read().strip()
r = requests.post(f"{'http' if args.http else 'https'}://{args.susserver}", r = requests.post(f"{'http' if args.http else 'https'}://{args.susserver}",
data = { data = {
'Command': args.command, 'Command': args.command,
@ -30,7 +40,7 @@ def main(args):
}, },
headers = { headers = {
'Signature': 'SUS-SIGNATURE-' + hmac.new( 'Signature': 'SUS-SIGNATURE-' + hmac.new(
args.secret.encode("UTF-8"), secret.encode("UTF-8"),
(args.command+":"+args.shortlink+":"+args.value).encode("UTF-8"), (args.command+":"+args.shortlink+":"+args.value).encode("UTF-8"),
hashlib.sha256 hashlib.sha256
).hexdigest() ).hexdigest()