Skip to content

Commit 6f2d92c

Browse files
python-common/cryptotools: use json for structured output
Where possible try to use structured output in JSON for easier parsing and interaction with the parent process. Signed-off-by: John Mulligan <jmulligan@redhat.com>
1 parent 717d0a6 commit 6f2d92c

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

src/python-common/ceph/pybind/mgr/cryptotools.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ def password_hash(args: Namespace) -> None:
2929
else:
3030
salt = salt_password.encode('utf8')
3131

32-
print(bcrypt.hashpw(password.encode('utf8'), salt).decode())
32+
hash_str = bcrypt.hashpw(password.encode('utf8'), salt).decode('utf-8')
33+
json.dump({'hash': hash_str}, sys.stdout)
3334

3435

3536
def create_self_signed_cert(args: Namespace) -> None:
@@ -108,7 +109,8 @@ def verify_cacrt_content(args: Namespace) -> None:
108109
# Certificate still valid, calculate and return days until expiration
109110
with warnings.catch_warnings():
110111
warnings.simplefilter("ignore")
111-
print((end_date - datetime.datetime.utcnow()).days)
112+
days_until_exp = (end_date - datetime.datetime.utcnow()).days
113+
json.dump({'days_until_expiration': int(days_until_exp)}, sys.stdout)
112114

113115

114116
def get_cert_issuer_info(args: Namespace) -> None:
@@ -123,12 +125,11 @@ def get_cert_issuer_info(args: Namespace) -> None:
123125
org_name = c[1].decode()
124126
elif c[0].decode() == 'CN': # common name comp
125127
cn = c[1].decode()
128+
json.dump({'org_name': org_name, 'cn': cn}, sys.stdout)
126129

127-
if args.org_name:
128-
print(org_name)
129130

130-
if args.cn:
131-
print(cn)
131+
def _fail_message(msg: str) -> None:
132+
json.dump({'error': msg}, sys.stdout)
132133

133134

134135
def verify_tls(args: Namespace) -> None:
@@ -142,12 +143,12 @@ def verify_tls(args: Namespace) -> None:
142143
_key = crypto.load_privatekey(crypto.FILETYPE_PEM, key)
143144
_key.check()
144145
except (ValueError, crypto.Error) as e:
145-
print('Invalid private key: %s' % str(e))
146+
_fail_message('Invalid private key: %s' % str(e))
146147
try:
147148
crt_buffer = crt.encode("ascii") if isinstance(crt, str) else crt
148149
_crt = crypto.load_certificate(crypto.FILETYPE_PEM, crt_buffer)
149150
except ValueError as e:
150-
print('Invalid certificate key: %s' % str(e))
151+
_fail_message('Invalid certificate key: %s' % str(e))
151152

152153
try:
153154
context = SSL.Context(SSL.TLSv1_METHOD)
@@ -158,9 +159,9 @@ def verify_tls(args: Namespace) -> None:
158159

159160
context.check_privatekey()
160161
except crypto.Error as e:
161-
print('Private key and certificate do not match up: %s' % str(e))
162+
_fail_message('Private key and certificate do not match up: %s' % str(e))
162163
except SSL.Error as e:
163-
print(f'Invalid cert/key pair: {e}')
164+
_fail_message(f'Invalid cert/key pair: {e}')
164165

165166

166167
if __name__ == "__main__":

0 commit comments

Comments
 (0)