Skip to content

Commit b7b531c

Browse files
committed
refactor: adjustments for mobileconfig signing
1 parent 3f3fd5b commit b7b531c

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

src/mscp/common_utils/run_command.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
from .logger_instance import logger
99

1010

11-
def run_command(command: str) -> tuple[str | None, str | None]:
11+
def run_command(
12+
command: str, capture_output: bool = True, text: bool = True, check: bool = True
13+
) -> tuple[str | None, str | None]:
1214
"""
1315
Executes a shell command and returns its output or an error message.
1416
result = subprocess.run(args, capture_output=True, text=True, check=True)
@@ -22,12 +24,17 @@ def run_command(command: str) -> tuple[str | None, str | None]:
2224
try:
2325
logger.info("Executing command: {}", command)
2426

25-
result = subprocess.run(args, capture_output=True, text=True, check=True)
27+
result = subprocess.run(
28+
args, capture_output=capture_output, text=text, check=check
29+
)
2630

2731
logger.success("Command executed successfully: {}", command)
28-
logger.debug("Command output: {}", result.stdout.strip())
32+
if text:
33+
logger.debug("Command output: {}", result.stdout.strip())
2934

30-
return result.stdout.strip(), None
35+
return result.stdout.strip(), None
36+
else:
37+
return None, None
3138

3239
except subprocess.CalledProcessError as e:
3340
logger.error(

src/mscp/generate/guidance.py

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,16 @@ def verify_signing_hash(cert_hash: str) -> bool:
4141
bool: If the certificate is valid, returns True.
4242
"""
4343

44-
with tempfile.NamedTemporaryFile(mode="w", delete=True) as in_file:
45-
unsigned_tmp_file_path = in_file.name
46-
in_file.write("temporary file for signing")
44+
with tempfile.NamedTemporaryFile(mode="w", delete=False) as in_file:
45+
unsigned_tmp_file_path = Path(in_file.name)
46+
in_file.write("temporary file for signing\n")
4747
in_file.flush()
4848

49-
cmd: str = f"security cms -SZ {cert_hash} -i {unsigned_tmp_file_path}"
49+
cmd: str = f"security cms -SZ {cert_hash} -i {unsigned_tmp_file_path}"
5050

51-
stdout, error = run_command(cmd)
51+
stdout, error = run_command(cmd, text=False, check=False)
52+
53+
unsigned_tmp_file_path.unlink()
5254

5355
if error:
5456
logger.error(f"Verification failed for hash {cert_hash}. Error: {error}")
@@ -128,24 +130,15 @@ def generate_guidance(args: argparse.Namespace) -> None:
128130

129131
if args.profiles:
130132
logger.info("Generating configuration profiles")
131-
if not signing:
132-
generate_profiles(
133-
build_path,
134-
baseline_name,
135-
baseline,
136-
consolidated=args.consolidated_profile,
137-
granular=args.granular_profiles,
138-
)
139-
else:
140-
generate_profiles(
141-
build_path,
142-
baseline_name,
143-
baseline,
144-
signing,
145-
args.hash,
146-
consolidated=args.consolidated_profile,
147-
granular=args.granular_profiles,
148-
)
133+
generate_profiles(
134+
build_path,
135+
baseline_name,
136+
baseline,
137+
signing,
138+
args.hash,
139+
consolidated=args.consolidated_profile,
140+
granular=args.granular_profiles,
141+
)
149142

150143
if args.ddm:
151144
logger.info("Generating declarative components")
@@ -188,6 +181,8 @@ def generate_guidance(args: argparse.Namespace) -> None:
188181
build_path,
189182
baseline_name,
190183
baseline,
184+
signing,
185+
args.hash,
191186
consolidated=args.consolidated_profile,
192187
granular=args.granular_profiles,
193188
)

0 commit comments

Comments
 (0)