Skip to content

Commit 59dfc28

Browse files
committed
Replace broad exception handlers with specific exception types
Instead of catching generic Exception, now catch specific exceptions: - SaltDeserializationError for payload deserialization failures - OSError, InvalidKeyError for public key loading failures - OSError, InvalidKeyError, ValueError, UnicodeDecodeError for cluster key decryption/validation This provides better error specificity while still handling all expected failure modes.
1 parent b935059 commit 59dfc28

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

salt/channel/server.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@
2828
import salt.utils.platform
2929
import salt.utils.stringutils
3030
import salt.utils.verify
31-
from salt.exceptions import SaltDeserializationError, SaltValidationError, UnsupportedAlgorithm
31+
from salt.exceptions import (
32+
InvalidKeyError,
33+
SaltDeserializationError,
34+
SaltValidationError,
35+
UnsupportedAlgorithm,
36+
)
3237
from salt.utils.cache import CacheCli
3338

3439
log = logging.getLogger(__name__)
@@ -1317,8 +1322,8 @@ async def handle_pool_publish(self, payload, _):
13171322

13181323
try:
13191324
notify_data = salt.payload.loads(data["payload"])
1320-
except Exception as e: # pylint: disable=broad-except
1321-
log.error("Failed to load join-notify payload: %s", e)
1325+
except SaltDeserializationError as e:
1326+
log.error("Failed to deserialize join-notify payload: %s", e)
13221327
return
13231328

13241329
sender_id = notify_data.get("peer_id")
@@ -1363,8 +1368,8 @@ async def handle_pool_publish(self, payload, _):
13631368
sender_id,
13641369
)
13651370
return
1366-
except Exception as e: # pylint: disable=broad-except
1367-
log.error("Error verifying join-notify signature: %s", e)
1371+
except (OSError, InvalidKeyError) as e:
1372+
log.error("Error loading sender public key for signature verification: %s", e)
13681373
return
13691374

13701375
# Signature verified - now we can trust the notification
@@ -1394,8 +1399,8 @@ async def handle_pool_publish(self, payload, _):
13941399

13951400
try:
13961401
payload = salt.payload.loads(data["payload"])
1397-
except Exception as e: # pylint: disable=broad-except
1398-
log.error("Failed to load join-reply payload: %s", e)
1402+
except SaltDeserializationError as e:
1403+
log.error("Failed to deserialize join-reply payload: %s", e)
13991404
return
14001405

14011406
# Verify the peer_id matches who we're expecting (bootstrap peer)
@@ -1426,8 +1431,8 @@ async def handle_pool_publish(self, payload, _):
14261431
data["peer_id"],
14271432
)
14281433
return
1429-
except Exception as e: # pylint: disable=broad-except
1430-
log.error("Error verifying join-reply signature: %s", e)
1434+
except (OSError, InvalidKeyError) as e:
1435+
log.error("Error loading bootstrap public key for signature verification: %s", e)
14311436
return
14321437

14331438
# Verify the return token matches what we sent
@@ -1466,7 +1471,7 @@ async def handle_pool_publish(self, payload, _):
14661471
# Load and validate it's a valid private key
14671472
cluster_key_obj = salt.crypt.PrivateKeyString(cluster_key_pem)
14681473

1469-
except Exception as e: # pylint: disable=broad-except
1474+
except (OSError, InvalidKeyError, ValueError, UnicodeDecodeError) as e:
14701475
log.error("Error decrypting/validating cluster key: %s", e)
14711476
return
14721477

0 commit comments

Comments
 (0)