2009-04-22 13:35:27 -05:00
|
|
|
# Authors: Rob Crittenden <rcritten@redhat.com>
|
|
|
|
#
|
|
|
|
# Copyright (C) 2009 Red Hat
|
|
|
|
# see file 'COPYING' for use and warranty information
|
|
|
|
#
|
2010-12-09 06:59:11 -06:00
|
|
|
# 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.
|
2009-04-22 13:35:27 -05:00
|
|
|
#
|
|
|
|
# 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
|
2010-12-09 06:59:11 -06:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-04-22 13:35:27 -05:00
|
|
|
#
|
|
|
|
|
2015-08-13 01:32:54 -05:00
|
|
|
import collections
|
2018-05-29 03:52:05 -05:00
|
|
|
import gzip
|
2018-05-30 03:56:46 -05:00
|
|
|
import io
|
2017-05-24 09:35:07 -05:00
|
|
|
import logging
|
2018-09-27 00:47:07 -05:00
|
|
|
from urllib.parse import urlencode
|
2009-04-22 13:35:27 -05:00
|
|
|
import xml.dom.minidom
|
2018-05-29 03:52:05 -05:00
|
|
|
import zlib
|
2012-08-23 11:38:45 -05:00
|
|
|
|
2015-09-11 06:43:28 -05:00
|
|
|
import six
|
2012-07-04 07:52:47 -05:00
|
|
|
|
2017-02-14 02:58:44 -06:00
|
|
|
# pylint: disable=ipa-forbidden-import
|
2012-07-04 07:52:47 -05:00
|
|
|
from ipalib import api, errors
|
2016-12-20 03:23:47 -06:00
|
|
|
from ipalib.util import create_https_connection
|
2014-03-18 10:23:30 -05:00
|
|
|
from ipalib.errors import NetworkError
|
2012-07-04 07:52:47 -05:00
|
|
|
from ipalib.text import _
|
2017-02-14 02:58:44 -06:00
|
|
|
# pylint: enable=ipa-forbidden-import
|
2016-12-20 03:23:47 -06:00
|
|
|
from ipapython import ipautil
|
2012-08-23 11:38:45 -05:00
|
|
|
|
2015-09-14 07:03:58 -05:00
|
|
|
# Python 3 rename. The package is available in "six.moves.http_client", but
|
|
|
|
# pylint cannot handle classes from that alias
|
|
|
|
try:
|
|
|
|
import httplib
|
|
|
|
except ImportError:
|
2016-08-24 06:37:30 -05:00
|
|
|
# pylint: disable=import-error
|
2015-09-14 07:03:58 -05:00
|
|
|
import http.client as httplib
|
|
|
|
|
2015-09-11 06:43:28 -05:00
|
|
|
if six.PY3:
|
|
|
|
unicode = str
|
|
|
|
|
2017-05-24 09:35:07 -05:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2015-08-13 01:32:54 -05:00
|
|
|
Profile = collections.namedtuple('Profile', ['profile_id', 'description', 'store_issued'])
|
|
|
|
|
2015-05-11 20:17:48 -05:00
|
|
|
INCLUDED_PROFILES = {
|
2015-08-13 01:32:54 -05:00
|
|
|
Profile(u'caIPAserviceCert', u'Standard profile for network services', True),
|
|
|
|
Profile(u'IECUserRoles', u'User profile that includes IECUserRoles extension from request', True),
|
2016-07-26 10:19:01 -05:00
|
|
|
Profile(u'KDCs_PKINIT_Certs',
|
|
|
|
u'Profile for PKINIT support by KDCs',
|
|
|
|
False),
|
2020-10-06 14:17:15 -05:00
|
|
|
Profile(u'acmeIPAServerCert',
|
|
|
|
u'ACME IPA service certificate profile',
|
2020-03-25 00:41:56 -05:00
|
|
|
False),
|
2015-05-11 20:17:48 -05:00
|
|
|
}
|
|
|
|
|
2015-05-08 01:23:24 -05:00
|
|
|
DEFAULT_PROFILE = u'caIPAserviceCert'
|
2016-07-26 10:19:01 -05:00
|
|
|
KDC_PROFILE = u'KDCs_PKINIT_Certs'
|
2015-05-08 01:23:24 -05:00
|
|
|
|
2009-04-22 13:35:27 -05:00
|
|
|
|
2018-05-30 03:56:46 -05:00
|
|
|
if six.PY3:
|
|
|
|
gzip_decompress = gzip.decompress # pylint: disable=no-member
|
|
|
|
else:
|
|
|
|
# note: gzip.decompress available in Python >= 3.2
|
|
|
|
def gzip_decompress(data):
|
|
|
|
with gzip.GzipFile(fileobj=io.BytesIO(data)) as f:
|
|
|
|
return f.read()
|
|
|
|
|
|
|
|
|
2012-09-25 08:57:03 -05:00
|
|
|
def error_from_xml(doc, message_template):
|
|
|
|
try:
|
|
|
|
item_node = doc.getElementsByTagName("Error")
|
|
|
|
reason = item_node[0].childNodes[0].data
|
|
|
|
return errors.RemoteRetrieveError(reason=reason)
|
2015-07-30 09:49:29 -05:00
|
|
|
except Exception as e:
|
2012-09-25 08:57:03 -05:00
|
|
|
return errors.RemoteRetrieveError(reason=message_template % e)
|
|
|
|
|
|
|
|
|
2015-11-09 11:28:47 -06:00
|
|
|
def get_ca_certchain(ca_host=None):
|
2009-04-22 13:35:27 -05:00
|
|
|
"""
|
|
|
|
Retrieve the CA Certificate chain from the configured Dogtag server.
|
|
|
|
"""
|
2009-07-10 15:18:16 -05:00
|
|
|
if ca_host is None:
|
|
|
|
ca_host = api.env.ca_host
|
2009-04-22 13:35:27 -05:00
|
|
|
chain = None
|
2014-03-18 10:23:30 -05:00
|
|
|
conn = httplib.HTTPConnection(
|
|
|
|
ca_host,
|
2015-11-09 11:28:47 -06:00
|
|
|
api.env.ca_install_port or 8080)
|
2009-04-22 13:35:27 -05:00
|
|
|
conn.request("GET", "/ca/ee/ca/getCertChain")
|
|
|
|
res = conn.getresponse()
|
2010-11-22 09:27:34 -06:00
|
|
|
doc = None
|
2009-04-22 13:35:27 -05:00
|
|
|
if res.status == 200:
|
|
|
|
data = res.read()
|
|
|
|
conn.close()
|
2009-05-21 16:34:00 -05:00
|
|
|
try:
|
|
|
|
doc = xml.dom.minidom.parseString(data)
|
|
|
|
try:
|
|
|
|
item_node = doc.getElementsByTagName("ChainBase64")
|
|
|
|
chain = item_node[0].childNodes[0].data
|
|
|
|
except IndexError:
|
2012-09-25 08:57:03 -05:00
|
|
|
raise error_from_xml(
|
|
|
|
doc, _("Retrieving CA cert chain failed: %s"))
|
2009-05-21 16:34:00 -05:00
|
|
|
finally:
|
2010-11-22 09:27:34 -06:00
|
|
|
if doc:
|
|
|
|
doc.unlink()
|
|
|
|
else:
|
2012-07-04 07:52:47 -05:00
|
|
|
raise errors.RemoteRetrieveError(
|
|
|
|
reason=_("request failed with HTTP status %d") % res.status)
|
2009-04-22 13:35:27 -05:00
|
|
|
|
|
|
|
return chain
|
2010-02-02 21:52:11 -06:00
|
|
|
|
2012-09-25 08:57:03 -05:00
|
|
|
|
2014-11-18 12:49:15 -06:00
|
|
|
def _parse_ca_status(body):
|
|
|
|
doc = xml.dom.minidom.parseString(body)
|
|
|
|
try:
|
|
|
|
item_node = doc.getElementsByTagName("XMLResponse")[0]
|
|
|
|
item_node = item_node.getElementsByTagName("Status")[0]
|
|
|
|
return item_node.childNodes[0].data
|
|
|
|
except IndexError:
|
|
|
|
raise error_from_xml(doc, _("Retrieving CA status failed: %s"))
|
|
|
|
|
|
|
|
|
2016-01-20 01:35:15 -06:00
|
|
|
def ca_status(ca_host=None):
|
2012-09-25 08:57:03 -05:00
|
|
|
"""Return the status of the CA, and the httpd proxy in front of it
|
|
|
|
|
|
|
|
The returned status can be:
|
|
|
|
- running
|
|
|
|
- starting
|
|
|
|
- Service Temporarily Unavailable
|
|
|
|
"""
|
|
|
|
if ca_host is None:
|
|
|
|
ca_host = api.env.ca_host
|
2016-09-26 07:08:17 -05:00
|
|
|
status, _headers, body = http_request(
|
2017-05-02 12:52:13 -05:00
|
|
|
ca_host, 8080, '/ca/admin/ca/getStatus',
|
|
|
|
# timeout: CA sometimes forgot to answer, we have to try again
|
|
|
|
timeout=api.env.http_timeout)
|
2012-09-25 08:57:03 -05:00
|
|
|
if status == 503:
|
|
|
|
# Service temporarily unavailable
|
2016-01-05 21:50:42 -06:00
|
|
|
return status
|
2012-09-25 08:57:03 -05:00
|
|
|
elif status != 200:
|
|
|
|
raise errors.RemoteRetrieveError(
|
2016-01-05 21:50:42 -06:00
|
|
|
reason=_("Retrieving CA status failed with status %d") % status)
|
2014-11-18 12:49:15 -06:00
|
|
|
return _parse_ca_status(body)
|
2012-09-25 08:57:03 -05:00
|
|
|
|
|
|
|
|
2020-10-14 12:20:16 -05:00
|
|
|
def acme_status(ca_host=None):
|
|
|
|
"""Return the status of ACME
|
|
|
|
|
|
|
|
Returns a boolean.
|
|
|
|
|
|
|
|
If the proxy is not working or the CA is not running then this could
|
|
|
|
return a false negative.
|
|
|
|
"""
|
|
|
|
if ca_host is None:
|
|
|
|
ca_host = api.env.ca_host
|
|
|
|
status, _headers, _body = https_request(
|
|
|
|
ca_host, 443,
|
|
|
|
url='/acme/directory',
|
|
|
|
cafile=api.env.tls_ca_cert,
|
|
|
|
client_certfile=None,
|
|
|
|
client_keyfile=None,
|
|
|
|
method='GET',
|
|
|
|
timeout=api.env.http_timeout)
|
|
|
|
if status == 200:
|
|
|
|
return True
|
|
|
|
elif status == 503:
|
|
|
|
# This is what it should return when disabled
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
# Unexpected status code, log and return False
|
|
|
|
logger.error('ACME status request returned %d', status)
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2017-01-13 02:08:42 -06:00
|
|
|
def https_request(
|
|
|
|
host, port, url, cafile, client_certfile, client_keyfile,
|
|
|
|
method='POST', headers=None, body=None, **kw):
|
2010-02-02 21:52:11 -06:00
|
|
|
"""
|
2015-04-30 03:55:29 -05:00
|
|
|
:param method: HTTP request method (defalut: 'POST')
|
2012-09-25 08:57:03 -05:00
|
|
|
:param url: The path (not complete URL!) to post to.
|
2015-04-30 03:55:29 -05:00
|
|
|
:param body: The request body (encodes kw if None)
|
2010-02-02 21:52:11 -06:00
|
|
|
:param kw: Keyword arguments to encode into POST body.
|
2016-01-05 21:50:42 -06:00
|
|
|
:return: (http_status, http_headers, http_body)
|
|
|
|
as (integer, dict, str)
|
2010-02-02 21:52:11 -06:00
|
|
|
|
|
|
|
Perform a client authenticated HTTPS request
|
|
|
|
"""
|
2012-09-25 08:57:03 -05:00
|
|
|
|
|
|
|
def connection_factory(host, port):
|
2016-12-20 03:23:47 -06:00
|
|
|
return create_https_connection(
|
|
|
|
host, port,
|
|
|
|
cafile=cafile,
|
|
|
|
client_certfile=client_certfile,
|
2017-01-13 02:08:42 -06:00
|
|
|
client_keyfile=client_keyfile,
|
2016-12-20 03:23:47 -06:00
|
|
|
tls_version_min=api.env.tls_version_min,
|
|
|
|
tls_version_max=api.env.tls_version_max)
|
2012-09-25 08:57:03 -05:00
|
|
|
|
2015-04-30 03:55:29 -05:00
|
|
|
if body is None:
|
|
|
|
body = urlencode(kw)
|
2012-09-25 08:57:03 -05:00
|
|
|
return _httplib_request(
|
2015-04-30 03:55:29 -05:00
|
|
|
'https', host, port, url, connection_factory, body,
|
|
|
|
method=method, headers=headers)
|
2012-09-25 08:57:03 -05:00
|
|
|
|
|
|
|
|
2017-05-02 12:24:16 -05:00
|
|
|
def http_request(host, port, url, timeout=None, **kw):
|
2012-09-25 08:57:03 -05:00
|
|
|
"""
|
|
|
|
:param url: The path (not complete URL!) to post to.
|
2017-05-02 12:24:16 -05:00
|
|
|
:param timeout: Timeout in seconds for waiting for reply.
|
2012-09-25 08:57:03 -05:00
|
|
|
:param kw: Keyword arguments to encode into POST body.
|
2016-01-05 21:50:42 -06:00
|
|
|
:return: (http_status, http_headers, http_body)
|
|
|
|
as (integer, dict, str)
|
2012-09-25 08:57:03 -05:00
|
|
|
|
|
|
|
Perform an HTTP request.
|
|
|
|
"""
|
|
|
|
body = urlencode(kw)
|
2017-05-02 12:24:16 -05:00
|
|
|
if timeout is None:
|
|
|
|
conn_opt = {}
|
|
|
|
else:
|
|
|
|
conn_opt = {"timeout": timeout}
|
|
|
|
|
2012-09-25 08:57:03 -05:00
|
|
|
return _httplib_request(
|
2017-05-02 12:24:16 -05:00
|
|
|
'http', host, port, url, httplib.HTTPConnection, body,
|
|
|
|
connection_options=conn_opt)
|
2012-09-25 08:57:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
def _httplib_request(
|
2015-04-30 03:55:29 -05:00
|
|
|
protocol, host, port, path, connection_factory, request_body,
|
2017-05-02 12:24:16 -05:00
|
|
|
method='POST', headers=None, connection_options=None):
|
2012-09-25 08:57:03 -05:00
|
|
|
"""
|
|
|
|
:param request_body: Request body
|
|
|
|
:param connection_factory: Connection class to use. Will be called
|
|
|
|
with the host and port arguments.
|
2015-04-30 03:55:29 -05:00
|
|
|
:param method: HTTP request method (default: 'POST')
|
2017-05-02 12:24:16 -05:00
|
|
|
:param connection_options: a dictionary that will be passed to
|
|
|
|
connection_factory as keyword arguments.
|
2012-09-25 08:57:03 -05:00
|
|
|
|
|
|
|
Perform a HTTP(s) request.
|
|
|
|
"""
|
2017-05-02 12:24:16 -05:00
|
|
|
if connection_options is None:
|
|
|
|
connection_options = {}
|
|
|
|
|
2017-01-10 11:21:13 -06:00
|
|
|
uri = u'%s://%s%s' % (protocol, ipautil.format_netloc(host, port), path)
|
2017-05-24 09:35:07 -05:00
|
|
|
logger.debug('request %s %s', method, uri)
|
|
|
|
logger.debug('request body %r', request_body)
|
2015-04-30 03:55:29 -05:00
|
|
|
|
|
|
|
headers = headers or {}
|
|
|
|
if (
|
|
|
|
method == 'POST'
|
Use Python3-compatible dict method names
Python 2 has keys()/values()/items(), which return lists,
iterkeys()/itervalues()/iteritems(), which return iterators,
and viewkeys()/viewvalues()/viewitems() which return views.
Python 3 has only keys()/values()/items(), which return views.
To get iterators, one can use iter() or a for loop/comprehension;
for lists there's the list() constructor.
When iterating through the entire dict, without modifying the dict,
the difference between Python 2's items() and iteritems() is
negligible, especially on small dicts (the main overhead is
extra memory, not CPU time). In the interest of simpler code,
this patch changes many instances of iteritems() to items(),
iterkeys() to keys() etc.
In other cases, helpers like six.itervalues are used.
Reviewed-By: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2015-08-11 06:51:14 -05:00
|
|
|
and 'content-type' not in (str(k).lower() for k in headers)
|
2015-04-30 03:55:29 -05:00
|
|
|
):
|
|
|
|
headers['content-type'] = 'application/x-www-form-urlencoded'
|
|
|
|
|
2012-09-25 08:57:03 -05:00
|
|
|
try:
|
2017-05-02 12:24:16 -05:00
|
|
|
conn = connection_factory(host, port, **connection_options)
|
2019-03-18 18:07:39 -05:00
|
|
|
conn.request(method, path, body=request_body, headers=headers)
|
2010-02-02 21:52:11 -06:00
|
|
|
res = conn.getresponse()
|
|
|
|
|
|
|
|
http_status = res.status
|
2017-01-10 11:24:16 -06:00
|
|
|
http_headers = res.msg
|
2010-02-02 21:52:11 -06:00
|
|
|
http_body = res.read()
|
|
|
|
conn.close()
|
2015-07-30 09:49:29 -05:00
|
|
|
except Exception as e:
|
2017-05-24 09:35:07 -05:00
|
|
|
logger.debug("httplib request failed:", exc_info=True)
|
2010-02-02 21:52:11 -06:00
|
|
|
raise NetworkError(uri=uri, error=str(e))
|
|
|
|
|
2018-05-29 03:52:05 -05:00
|
|
|
encoding = res.getheader('Content-Encoding')
|
|
|
|
if encoding == 'gzip':
|
2018-05-30 03:56:46 -05:00
|
|
|
http_body = gzip_decompress(http_body)
|
2018-05-29 03:52:05 -05:00
|
|
|
elif encoding == 'deflate':
|
|
|
|
http_body = zlib.decompress(http_body)
|
|
|
|
|
2017-05-24 09:35:07 -05:00
|
|
|
logger.debug('response status %d', http_status)
|
|
|
|
logger.debug('response headers %s', http_headers)
|
2018-05-29 03:52:05 -05:00
|
|
|
logger.debug('response body (decoded): %r', http_body)
|
2010-02-02 21:52:11 -06:00
|
|
|
|
2016-01-05 21:50:42 -06:00
|
|
|
return http_status, http_headers, http_body
|