Skip to content

Commit 9f2fdc0

Browse files
authored
Merge pull request #19 from rtpt-romankarwacik/handle_deserialization_errors
Handle deseralization errors
2 parents 10faca4 + 3ff1e51 commit 9f2fdc0

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

pywhisker/pywhisker.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@ def info(self, device_id):
283283
device_id_in_current_values = False
284284
for dn_binary_value in results['raw_attributes']['msDS-KeyCredentialLink']:
285285
keyCredential = KeyCredential.fromDNWithBinary(DNWithBinary.fromRawDNWithBinary(dn_binary_value))
286+
if keyCredential.DeviceId is None:
287+
logger.warning("Failed to parse DeviceId for keyCredential: %s" % (str(dn_binary_value)))
288+
continue
286289
if keyCredential.DeviceId.toFormatD() == device_id:
287290
logger.success("Found device Id")
288291
keyCredential.show()
@@ -319,7 +322,12 @@ def list(self):
319322
logger.info("Listing devices for %s" % self.target_samname)
320323
for dn_binary_value in results['raw_attributes']['msDS-KeyCredentialLink']:
321324
keyCredential = KeyCredential.fromDNWithBinary(DNWithBinary.fromRawDNWithBinary(dn_binary_value))
322-
logger.info("DeviceID: %s | Creation Time (UTC): %s" % (keyCredential.DeviceId.toFormatD(), keyCredential.CreationTime))
325+
if keyCredential.DeviceId is None:
326+
logger.warning("Failed to parse DeviceId for keyCredential: %s" % (str(dn_binary_value)))
327+
logger.warning("DeviceID: %s | Creation Time (UTC): %s" % (keyCredential.DeviceId, keyCredential.CreationTime))
328+
else:
329+
logger.info("DeviceID: %s | Creation Time (UTC): %s" % (keyCredential.DeviceId.toFormatD(), keyCredential.CreationTime))
330+
323331
except IndexError:
324332
logger.info('Attribute msDS-KeyCredentialLink does not exist')
325333
return
@@ -466,6 +474,9 @@ def remove(self, device_id):
466474
device_id_in_current_values = False
467475
for dn_binary_value in results['raw_attributes']['msDS-KeyCredentialLink']:
468476
keyCredential = KeyCredential.fromDNWithBinary(DNWithBinary.fromRawDNWithBinary(dn_binary_value))
477+
if keyCredential.DeviceId is None:
478+
logger.warning("Failed to parse DeviceId for keyCredential: %s" % (str(dn_binary_value)))
479+
continue
469480
if keyCredential.DeviceId.toFormatD() == device_id:
470481
logger.info("Found value to remove")
471482
device_id_in_current_values = True
@@ -553,7 +564,10 @@ def importFromJSON(self, filename):
553564
with open(filename, "r") as f:
554565
data = json.load(f)
555566
for kcjson in data["keyCredentials"]:
556-
keyCredentials.append(KeyCredential.fromDict(kcjson).toDNWithBinary().toString())
567+
if type(kcjson) == dict:
568+
keyCredentials.append(KeyCredential.fromDict(kcjson).toDNWithBinary().toString())
569+
elif type(kcjson) == str:
570+
keyCredentials.append(kcjson)
557571
logger.info("Modifying the msDS-KeyCredentialLink attribute of %s" % self.target_samname)
558572
self.ldap_session.modify(self.target_dn, {'msDS-KeyCredentialLink': [ldap3.MODIFY_REPLACE, keyCredentials]})
559573
if self.ldap_session.result['result'] == 0:
@@ -599,7 +613,11 @@ def exportToJSON(self, filename):
599613
keyCredentialsJSON = {"keyCredentials":[]}
600614
for dn_binary_value in results['raw_attributes']['msDS-KeyCredentialLink']:
601615
keyCredential = KeyCredential.fromDNWithBinary(DNWithBinary.fromRawDNWithBinary(dn_binary_value))
602-
keyCredentialsJSON["keyCredentials"].append(keyCredential.toDict())
616+
try:
617+
keyCredentialsJSON["keyCredentials"].append(keyCredential.toDict())
618+
except Exception as e:
619+
logger.warning(f"Failed to serialize keyCredential, error: %s, saving the raw keyCredential instead, i.e.: %s" % (str(e), dn_binary_value.decode()))
620+
keyCredentialsJSON["keyCredentials"].append(dn_binary_value.decode())
603621
with open(filename, "w") as f:
604622
f.write(json.dumps(keyCredentialsJSON, indent=4))
605623
logger.success("Saved JSON dump at path: %s" % filename)

0 commit comments

Comments
 (0)