Skip to content

Commit f2607c0

Browse files
committed
Add CLI support for CSR attributes
Resolves #11 Signed-off-by: Gregers Gram Rygg <gregers.gram.rygg@nordicsemi.no>
1 parent 49658c5 commit f2607c0

3 files changed

Lines changed: 12 additions & 4 deletions

File tree

src/nrfcredstore/cli.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ def parse_args(in_args):
5959
help='Secure tag to store generated key')
6060
generate_parser.add_argument('file', type=argparse.FileType('wb'),
6161
help='File to store CSR in DER format')
62+
generate_parser.add_argument('--attributes', type=str, default='',
63+
help='Comma-separated list of attribute ID and value pairs for the CSR response')
6264

6365
return parser.parse_args(in_args)
6466

@@ -90,7 +92,7 @@ def exec_cmd(args, credstore):
9092
else:
9193
raise RuntimeError('delete failed')
9294
elif args.subcommand=='generate':
93-
credstore.keygen(args.tag, args.file)
95+
credstore.keygen(args.tag, args.file, args.attributes)
9496
print(f'New private key generated in secure tag {args.tag}')
9597
print(f'Wrote CSR in DER format to {args.file.name}')
9698

tests/test_cli.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,20 @@ def test_delete_any_should_fail(self, credstore, offline):
9898
def test_generate_tag(self, mock_file, credstore, offline):
9999
credstore.keygen.return_value = True
100100
main(['fakedev', 'generate', '123', 'foo.der'], credstore)
101-
credstore.keygen.assert_called_with(123, ANY)
101+
credstore.keygen.assert_called_with(123, ANY, ANY)
102102

103103
@patch('builtins.open')
104104
def test_generate_file(self, mock_file, credstore, offline):
105105
credstore.keygen.return_value = True
106106
main(['fakedev', 'generate', '123', 'foo.der'], credstore)
107107
mock_file.assert_called_with('foo.der', 'wb', ANY, ANY, ANY)
108108

109+
@patch('builtins.open')
110+
def test_generate_with_attributes(self, credstore, offline):
111+
credstore.keygen.return_value = True
112+
main(['fakedev', 'generate', '123', 'foo.der', '--attributes', 'CN=foo'], credstore)
113+
credstore.keygen.assert_called_with(123, ANY, 'CN=foo')
114+
109115
def test_no_at_client_exit_code(self, credstore, at_client):
110116
at_client.verify.side_effect = NoATClientException()
111117
with pytest.raises(SystemExit) as e:

tests/test_credstore.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ def test_generate_sends_keygen_cmd(self, cred_store, csr_resp):
125125
self.at_client.at_command.assert_called_with(f'AT%KEYGEN=12345678,2,0')
126126

127127
def test_generate_with_attributes(self, cred_store, csr_resp):
128-
cred_store.keygen(12345678, Mock(), 'O=Nordic Semiconductor,L=Trondheim,C=no')
128+
cred_store.keygen(12345678, Mock(), 'O=Nordic Semiconductor,L=Trondheim,C=no,CN=mydevice')
129129
self.at_client.at_command.assert_called_with(
130-
f'AT%KEYGEN=12345678,2,0,"O=Nordic Semiconductor,L=Trondheim,C=no"')
130+
f'AT%KEYGEN=12345678,2,0,"O=Nordic Semiconductor,L=Trondheim,C=no,CN=mydevice"')
131131

132132
def test_generate_writes_csr_to_stream(self, cred_store, csr_resp):
133133
fake_binary_file = Mock()

0 commit comments

Comments
 (0)