Skip to content

Commit cc5fd1b

Browse files
committed
[IMP] Usability improvements for init_ca
1 parent 329e157 commit cc5fd1b

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

cfssl/cfssl.py

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -159,34 +159,25 @@ def info(self, label, profile=None):
159159
})
160160
return self.call('info', 'POST', data=data)
161161

162-
def init_ca(self, hosts, names, common_name=None, key=None, ca=None):
162+
def init_ca(self, certificate_request, ca=None):
163163
""" It initializes a new certificate authority.
164164
165165
Args:
166-
hosts (:obj:`iter` of :obj:`cfssl.Host`): Subject Alternative Name(s) for the
167-
requested CA certificate.
168-
names (:obj:`iter` of :obj:`cfssl.SubjectInfo`): The Subject Info(s) for the
169-
requested CA certificate.
170-
common_name (:obj:`str`): the common name for the certificate subject in
171-
the requested CA certificate.
172-
key (:obj:`cfssl.ConfigKey`): Cipher and strength to use for certificate.
173-
ca (:obj:`cfssl.ConfigServer`): the CA configuration of the requested CA,
174-
including CA pathlen and CA default expiry.
166+
certificate_request (:obj:`cfssl.CertificateRequest`): The certificate
167+
request to use when creating the CA.
168+
ca (:obj:`cfssl.ConfigServer`, optional): The configuration of the
169+
requested Certificate Authority.
175170
Returns:
176171
(:obj:`dict`) Mapping with two keys:
177172
* private key (:obj:`str`): a PEM-encoded CA private key.
178173
* certificate (:obj:`str`): a PEM-encoded self-signed CA certificate.
179174
"""
180-
key = key or ConfigKey()
175+
csr_api = certificate_request.to_api()
181176
data = self._clean_mapping({
182-
'hosts': [
183-
host.to_api() for host in hosts
184-
],
185-
'names': [
186-
name.to_api() for name in names
187-
],
188-
'CN': common_name,
189-
'key': key and key.to_api() or ConfigKey().to_api(),
177+
'hosts': csr_api['hosts'],
178+
'names': csr_api['names'],
179+
'CN': csr_api['CN'],
180+
'key': csr_api['key'],
190181
'ca': ca and ca.to_api() or None,
191182
})
192183
return self.call('init_ca', 'POST', data=data)

cfssl/tests/test_cfssl.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,21 @@
22
# Copyright 2016 LasLabs Inc.
33
# License MIT (https://opensource.org/licenses/MIT).
44

5+
import logging
56
import mock
67
import unittest
78

8-
from ..cfssl import CFSSL, CFSSLRemoteException, requests
9+
from ..cfssl import (CFSSL,
10+
CFSSLRemoteException,
11+
requests,
12+
)
13+
14+
_logger = logging.getLogger(__name__)
15+
16+
try:
17+
from cfssl import CertificateRequest
18+
except ImportError:
19+
_logger.info('CFSSL Python library not installed.')
920

1021

1122
class TestCFSSL(unittest.TestCase):
@@ -62,16 +73,20 @@ def test_info(self, call):
6273
@mock.patch.object(CFSSL, 'call')
6374
def test_init_ca(self, call):
6475
""" It should call with proper args """
65-
expect = {
76+
csr_vals = {
6677
'hosts': [mock.MagicMock()],
6778
'names': [mock.MagicMock()],
6879
'common_name': 'cn',
6980
'key': mock.MagicMock(),
70-
'ca': mock.MagicMock(),
7181
}
82+
csr = CertificateRequest(**csr_vals)
83+
expect = {'ca': mock.MagicMock(),
84+
'certificate_request': csr}
7285
self.cfssl.init_ca(**expect)
86+
expect.update(csr_vals)
7387
expect['CN'] = 'cn'
7488
del expect['common_name']
89+
del expect['certificate_request']
7590
expect['hosts'][0]= expect['hosts'][0].to_api()
7691
expect['names'][0] = expect['names'][0].to_api()
7792
expect['key'] = expect['key'].to_api()

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
setup_vals = {
1111
'name': 'cfssl',
12-
'version': '0.0.1',
12+
'version': '0.0.2',
1313
'author': 'LasLabs Inc.',
1414
'author_email': '[email protected]',
1515
'description': 'This library will allow you to interact with CFSSL '

0 commit comments

Comments
 (0)