2014-03-05 09:46:21 -06:00
|
|
|
#
|
|
|
|
# Authors:
|
|
|
|
# Rob Crittenden <rcritten@redhat.com>
|
|
|
|
#
|
|
|
|
# Copyright (C) 2011 Red Hat
|
|
|
|
# see file 'COPYING' for use and warranty information
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Provide a custom certificate used in the service tests.
|
|
|
|
|
|
|
|
The certificate in cached in a global variable so it only has to be created
|
|
|
|
once per test run.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import tempfile
|
|
|
|
import shutil
|
2015-09-11 06:43:28 -05:00
|
|
|
import six
|
|
|
|
|
2014-03-05 09:46:21 -06:00
|
|
|
from ipalib import api, x509
|
|
|
|
from ipaserver.plugins import rabase
|
|
|
|
from ipapython import ipautil
|
2014-05-29 07:47:17 -05:00
|
|
|
from ipaplatform.paths import paths
|
2014-03-05 09:46:21 -06:00
|
|
|
|
2015-09-11 06:43:28 -05:00
|
|
|
if six.PY3:
|
|
|
|
unicode = str
|
|
|
|
|
2014-03-05 09:46:21 -06:00
|
|
|
|
2015-06-09 03:06:53 -05:00
|
|
|
def get_testcert(subject, principal):
|
2014-03-05 09:46:21 -06:00
|
|
|
"""Get the certificate, creating it if it doesn't exist"""
|
2015-06-09 03:06:53 -05:00
|
|
|
reqdir = tempfile.mkdtemp(prefix="tmp-")
|
|
|
|
try:
|
|
|
|
_testcert = makecert(reqdir, subject,
|
|
|
|
principal)
|
|
|
|
finally:
|
|
|
|
shutil.rmtree(reqdir)
|
2014-03-05 09:46:21 -06:00
|
|
|
return x509.strip_header(_testcert)
|
|
|
|
|
|
|
|
|
|
|
|
def run_certutil(reqdir, args, stdin=None):
|
|
|
|
"""
|
|
|
|
Run an NSS certutil command
|
|
|
|
"""
|
2014-05-29 07:47:17 -05:00
|
|
|
new_args = [paths.CERTUTIL, "-d", reqdir]
|
2014-03-05 09:46:21 -06:00
|
|
|
new_args = new_args + args
|
|
|
|
return ipautil.run(new_args, stdin)
|
|
|
|
|
|
|
|
|
|
|
|
def generate_csr(reqdir, pwname, subject):
|
|
|
|
"""
|
|
|
|
Create a CSR for the given subject.
|
|
|
|
"""
|
|
|
|
req_path = os.path.join(reqdir, 'req')
|
|
|
|
run_certutil(reqdir, ["-R", "-s", subject,
|
|
|
|
"-o", req_path,
|
2014-05-29 07:47:17 -05:00
|
|
|
"-z", paths.GROUP,
|
2014-03-05 09:46:21 -06:00
|
|
|
"-f", pwname,
|
|
|
|
"-a"])
|
|
|
|
with open(req_path, "r") as fp:
|
|
|
|
return fp.read()
|
|
|
|
|
|
|
|
|
2015-06-09 03:06:53 -05:00
|
|
|
def makecert(reqdir, subject, principal):
|
2014-03-05 09:46:21 -06:00
|
|
|
"""
|
2015-06-09 03:06:53 -05:00
|
|
|
Generate a certificate that can be used during unit testing.
|
2014-03-05 09:46:21 -06:00
|
|
|
"""
|
|
|
|
|
2015-06-22 05:58:43 -05:00
|
|
|
ra = rabase.rabase(api)
|
2014-03-05 09:46:21 -06:00
|
|
|
if (not os.path.exists(ra.sec_dir) and
|
|
|
|
api.env.xmlrpc_uri == 'http://localhost:8888/ipa/xml'):
|
|
|
|
raise AssertionError('The self-signed CA is not configured, '
|
|
|
|
'see ipatests/test_xmlrpc/test_cert.py')
|
|
|
|
|
|
|
|
pwname = os.path.join(reqdir, "pwd")
|
|
|
|
|
|
|
|
# Create an empty password file
|
|
|
|
with open(pwname, "w") as fp:
|
|
|
|
fp.write("\n")
|
|
|
|
|
|
|
|
# Generate NSS cert database to store the private key for our CSR
|
|
|
|
run_certutil(reqdir, ["-N", "-f", pwname])
|
|
|
|
|
|
|
|
csr = unicode(generate_csr(reqdir, pwname, str(subject)))
|
|
|
|
|
2015-06-09 03:06:53 -05:00
|
|
|
res = api.Command['cert_request'](csr, principal=principal, add=True)
|
2014-03-05 09:46:21 -06:00
|
|
|
return x509.make_pem(res['result']['certificate'])
|