Skip to content

Commit 12662e2

Browse files
authored
avoid throwing exception on decrypt failure (#2128)
* avoid throwing exception on decrypt failure * add additional comments and error handling
1 parent 1169f53 commit 12662e2

1 file changed

Lines changed: 7 additions & 10 deletions

File tree

Utils/HandlerUtil.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -201,24 +201,21 @@ def _parse_config(self, ctxt):
201201
cms_cmd = 'openssl cms -inform DER -decrypt -recip {0} -inkey {1}'.format(cert,pkey)
202202
smime_cmd = 'openssl smime -inform DER -decrypt -recip {0} -inkey {1}'.format(cert,pkey)
203203

204-
protected_settings_str = None
204+
protected_settings_str = ''
205205
for decrypt_cmd in [cms_cmd, smime_cmd]:
206206
try:
207-
session = subprocess.Popen([decrypt_cmd], shell=True,
208-
stdin=subprocess.PIPE,
209-
stderr=subprocess.STDOUT,
210-
stdout=subprocess.PIPE)
211-
output = session.communicate(unencodedSettings)
212-
# success only if return code is 0 and we have output
213-
if session.returncode == 0 and output[0]:
214-
protected_settings_str = output[0]
207+
# waagent.RunSendStdin returns a tuple (return code, stdout)
208+
output = waagent.RunSendStdin(decrypt_cmd, unencodedSettings)
209+
if output and output[0] == 0 and output[1]:
210+
protected_settings_str = output[1]
215211
if decrypt_cmd == cms_cmd:
216212
self.log('Decrypted protectedSettings using openssl cms.')
217213
else:
218214
self.log('Decrypted protectedSettings using openssl smime fallback.')
219215
break
220216
else:
221-
self.log('Attempt to decrypt protectedSettings with "{0}" failed (rc={1}).'.format(decrypt_cmd, session.returncode))
217+
rc = output[0] if output else 'N/A'
218+
self.log('Attempt to decrypt protectedSettings with "{0}" failed (rc={1}).'.format(decrypt_cmd, rc))
222219
except OSError:
223220
pass
224221

0 commit comments

Comments
 (0)