Skip to content

Commit dcaeba6

Browse files
fixed issues Copilot found
1 parent 4432f84 commit dcaeba6

2 files changed

Lines changed: 31 additions & 32 deletions

File tree

src/nrfcloud_utils/nrf91_gather_self_signed_certs.py

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from nrfcloud_utils.cli_helpers import (
1515
setup_logging,
1616
parser_add_comms_args,
17+
user_request_open_mode,
1718
CMD_TERM_DICT, CMD_TYPE_AUTO, CMD_TYPE_AT, CMD_TYPE_AT_SHELL,
1819
)
1920
from nrfcloud_utils.device_credentials_installer import parse_mfw_ver
@@ -152,25 +153,6 @@ def check_if_device_exists_in_csv(csv_filename, dev_id, delete_duplicates):
152153
return duplicate_rows, row_count
153154

154155

155-
def user_request_open_mode(filename, append):
156-
mode = "a" if append else "w"
157-
exists = os.path.isfile(filename)
158-
if not append and exists:
159-
answer = " "
160-
while answer not in "yan":
161-
answer = input(
162-
f"--- File {filename} exists; overwrite, append, or quit (y,a,n)? "
163-
)
164-
if answer == "n":
165-
logger.info("File will not be overwritten")
166-
return None
167-
mode = "w" if answer == "y" else "a"
168-
elif not exists and append:
169-
mode = "w"
170-
logger.warning("Append specified but file does not exist...")
171-
return mode
172-
173-
174156
def save_csv(csv_filename, append, replace, dev_id, attestation):
175157
mode = user_request_open_mode(csv_filename, append)
176158
if mode is None:
@@ -239,19 +221,22 @@ def main(in_args):
239221
if not cred_if.go_offline():
240222
error_exit("Failed to switch modem to offline mode")
241223

242-
if args.clear_sectag:
243-
logger.info(f"Clearing existing credentials in sectag {args.sectag}...")
244-
cred_if.delete_credential(args.sectag, 1)
245-
cred_if.delete_credential(args.sectag, 2)
246-
247-
logger.info(f"Generating self-signed certificate (sectag {args.sectag})...")
248-
attestation = gen_self_signed_cert(cred_if, args.sectag)
249-
if not attestation:
250-
error_exit("Failed to generate self-signed certificate, use --clear-sectag if the slot is already occupied")
251-
252-
logger.info("Returning modem to online mode...")
253-
if not cred_if.at_command("AT+CFUN=1", wait_for_result=True):
254-
logger.warning("Failed to return modem to online mode")
224+
try:
225+
if args.clear_sectag:
226+
logger.info(f"Clearing existing credentials in sectag {args.sectag}...")
227+
cred_if.delete_credential(args.sectag, 1)
228+
cred_if.delete_credential(args.sectag, 2)
229+
230+
logger.info(f"Generating self-signed certificate (sectag {args.sectag})...")
231+
attestation = gen_self_signed_cert(cred_if, args.sectag)
232+
if not attestation:
233+
error_exit("Failed to generate self-signed certificate, use --clear-sectag if the slot is already occupied")
234+
finally:
235+
# Always try to return the modem to online mode, even if keygen failed,
236+
# so the user isn't left with a modem stuck in CFUN=4.
237+
logger.info("Returning modem to online mode...")
238+
if not cred_if.at_command("AT+CFUN=1", wait_for_result=True):
239+
logger.warning("Failed to return modem to online mode")
255240

256241
print(f"{dev_id},{attestation}")
257242

tests/test_nrf91_gather_self_signed_certs.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,20 @@ def test_brings_modem_back_online(self):
318318
calls = [c.args[0] for c in ati.at_command.call_args_list]
319319
assert "AT+CFUN=1" in calls
320320

321+
def test_modem_returned_online_even_when_keygen_fails(self):
322+
patches = _base_main_patches()
323+
patches["gen_self_signed_cert"] = Mock(return_value=None)
324+
with patch.multiple(MODULE, **patches):
325+
ati = patches["ATCommandInterface"].return_value
326+
ati.at_command.return_value = True
327+
ati.go_offline.return_value = True
328+
with pytest.raises(SystemExit):
329+
dut.main("--port /dev/ttyACM0 --cmd-type at".split())
330+
# Even though keygen failed and the script exited, AT+CFUN=1
331+
# must still have been issued so the modem isn't stuck offline.
332+
calls = [c.args[0] for c in ati.at_command.call_args_list]
333+
assert "AT+CFUN=1" in calls
334+
321335
def test_default_does_not_clear_sectag(self):
322336
patches = _base_main_patches()
323337
with patch.multiple(MODULE, **patches):

0 commit comments

Comments
 (0)