Skip to content

Commit 801ed7c

Browse files
committed
Fix linting errors in autoscale code
- Add missing getpass import in salt/crypt.py - Remove duplicate PublicKey class definition - Add pylint disable comments for intentional super().__init__() overrides - Remove unused shutil import from salt/channel/server.py - Add explicit utf-8 encoding to pathlib.Path().write_text() calls - Add pylint disable comments for intentional broad exception handlers - Fix test imports to use tests.support.mock instead of unittest.mock - Remove unused hashlib import from integration test All linting now passes with 10/10 rating.
1 parent eba1b43 commit 801ed7c

4 files changed

Lines changed: 12 additions & 26 deletions

File tree

salt/channel/server.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import os
1313
import pathlib
1414
import random
15-
import shutil
1615
import string
1716
import time
1817

@@ -1318,7 +1317,7 @@ async def handle_pool_publish(self, payload, _):
13181317

13191318
try:
13201319
notify_data = salt.payload.loads(data["payload"])
1321-
except Exception as e:
1320+
except Exception as e: # pylint: disable=broad-except
13221321
log.error("Failed to load join-notify payload: %s", e)
13231322
return
13241323

@@ -1364,7 +1363,7 @@ async def handle_pool_publish(self, payload, _):
13641363
sender_id,
13651364
)
13661365
return
1367-
except Exception as e:
1366+
except Exception as e: # pylint: disable=broad-except
13681367
log.error("Error verifying join-notify signature: %s", e)
13691368
return
13701369

@@ -1395,7 +1394,7 @@ async def handle_pool_publish(self, payload, _):
13951394

13961395
try:
13971396
payload = salt.payload.loads(data["payload"])
1398-
except Exception as e:
1397+
except Exception as e: # pylint: disable=broad-except
13991398
log.error("Failed to load join-reply payload: %s", e)
14001399
return
14011400

@@ -1427,7 +1426,7 @@ async def handle_pool_publish(self, payload, _):
14271426
data["peer_id"],
14281427
)
14291428
return
1430-
except Exception as e:
1429+
except Exception as e: # pylint: disable=broad-except
14311430
log.error("Error verifying join-reply signature: %s", e)
14321431
return
14331432

@@ -1467,7 +1466,7 @@ async def handle_pool_publish(self, payload, _):
14671466
# Load and validate it's a valid private key
14681467
cluster_key_obj = salt.crypt.PrivateKeyString(cluster_key_pem)
14691468

1470-
except Exception as e:
1469+
except Exception as e: # pylint: disable=broad-except
14711470
log.error("Error decrypting/validating cluster key: %s", e)
14721471
return
14731472

@@ -1488,7 +1487,7 @@ async def handle_pool_publish(self, payload, _):
14881487
log.error("Invalid peer_id in join-reply %s: %s", peer, e)
14891488
continue
14901489
log.info("Installing peer key: %s", peer)
1491-
pathlib.Path(peer_pub_path).write_text(payload["peers"][peer])
1490+
pathlib.Path(peer_pub_path).write_text(payload["peers"][peer], encoding="utf-8")
14921491

14931492
# Process minion keys from verified payload
14941493
allowed_kinds = [
@@ -1538,7 +1537,7 @@ async def handle_pool_publish(self, payload, _):
15381537
log.error("Invalid minion_id in join-reply %s: %s", minion, e)
15391538
continue
15401539
log.info("Installing minion key: %s/%s", kind, minion)
1541-
pathlib.Path(minion_pub_path).write_text(payload["minions"][kind][minion])
1540+
pathlib.Path(minion_pub_path).write_text(payload["minions"][kind][minion], encoding="utf-8")
15421541

15431542
# Signal completion
15441543
event = self._discover_event

salt/crypt.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import base64
99
import binascii
1010
import copy
11+
import getpass
1112
import hashlib
1213
import hmac
1314
import logging
@@ -383,20 +384,8 @@ def public_key(self):
383384
return self.key.public_key()
384385

385386

386-
class PublicKey(BaseKey):
387-
def __init__(self, key_bytes):
388-
log.debug("Loading public key")
389-
try:
390-
self.key = serialization.load_pem_public_key(key_bytes)
391-
except ValueError:
392-
raise InvalidKeyError("Encountered bad RSA public key")
393-
except cryptography.exceptions.UnsupportedAlgorithm:
394-
raise InvalidKeyError("Unsupported key algorithm")
395-
396-
397387
class PrivateKeyString(PrivateKey):
398-
399-
def __init__(self, data, password=None):
388+
def __init__(self, data, password=None): # pylint: disable=super-init-not-called
400389
self.key = serialization.load_pem_private_key(
401390
data.encode(),
402391
password=password,
@@ -455,7 +444,7 @@ def decrypt(self, data):
455444

456445

457446
class PublicKeyString(PublicKey):
458-
def __init__(self, data):
447+
def __init__(self, data): # pylint: disable=super-init-not-called
459448
try:
460449
self.key = serialization.load_pem_public_key(data.encode())
461450
except ValueError as exc:

tests/pytests/integration/cluster/test_autoscale_security.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ def test_autoscale_rejects_missing_cluster_secret(
370370
# If it did start, check that join was rejected
371371
logs = factory.get_log_contents()
372372
assert "cluster_secret" in logs.lower()
373-
except Exception:
373+
except Exception: # pylint: disable=broad-except
374374
# Expected - configuration validation should catch this
375375
pass
376376

@@ -499,8 +499,6 @@ def test_autoscale_rejects_join_without_cluster_pub_signature(
499499
Security: cluster_pub_signature_required defaults to True (secure by default).
500500
Without cluster_pub_signature configured, join should be rejected to prevent TOFU attacks.
501501
"""
502-
import hashlib
503-
504502
# Create joining master WITHOUT cluster_pub_signature (default: required=True)
505503
config_defaults, config_overrides = {
506504
"master_port": autoscale_bootstrap_master.config["ret_port"] + 1,

tests/pytests/unit/channel/test_server_autoscale.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
import pathlib
1212
import random
1313
import string
14-
from unittest.mock import MagicMock, Mock, patch, call
1514

1615
import pytest
1716

1817
import salt.channel.server
1918
import salt.crypt
2019
import salt.master
20+
from tests.support.mock import MagicMock, patch
2121
import salt.payload
2222
import salt.utils.event
2323
from salt.exceptions import SaltValidationError

0 commit comments

Comments
 (0)