Blame roles/ipaclient/library/ipaclient_fix_ca.py

Packit 8cb997
# -*- coding: utf-8 -*-
Packit 8cb997
Packit 8cb997
# Authors:
Packit 8cb997
#   Thomas Woerner <twoerner@redhat.com>
Packit 8cb997
#
Packit 8cb997
# Based on ipa-client-install code
Packit 8cb997
#
Packit 8cb997
# Copyright (C) 2017  Red Hat
Packit 8cb997
# see file 'COPYING' for use and warranty information
Packit 8cb997
#
Packit 8cb997
# This program is free software; you can redistribute it and/or modify
Packit 8cb997
# it under the terms of the GNU General Public License as published by
Packit 8cb997
# the Free Software Foundation, either version 3 of the License, or
Packit 8cb997
# (at your option) any later version.
Packit 8cb997
#
Packit 8cb997
# This program is distributed in the hope that it will be useful,
Packit 8cb997
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 8cb997
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 8cb997
# GNU General Public License for more details.
Packit 8cb997
#
Packit 8cb997
# You should have received a copy of the GNU General Public License
Packit 8cb997
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit 8cb997
Packit 8cb997
ANSIBLE_METADATA = {'metadata_version': '1.0',
Packit 8cb997
                    'status': ['preview'],
Packit 8cb997
                    'supported_by': 'community'}
Packit 8cb997
Packit 8cb997
DOCUMENTATION = '''
Packit 8cb997
---
Packit 8cb997
module: ipaclient_fix_ca
Packit 8cb997
short description: Fix IPA ca certificate
Packit 8cb997
description:
Packit 8cb997
Repair Fix IPA ca certificate
Packit 8cb997
options:
Packit 8cb997
  servers:
Packit 8cb997
    description: Fully qualified name of IPA servers to enroll to
Packit 8cb997
    required: no
Packit 8cb997
  realm:
Packit 8cb997
    description: Kerberos realm name of the IPA deployment
Packit 8cb997
    required: no
Packit 8cb997
  basedn:
Packit 8cb997
    description: The basedn of the IPA server (of the form dc=example,dc=com)
Packit 8cb997
    required: no
Packit 8cb997
  allow_repair:
Packit 8cb997
    description:
Packit 8cb997
      Allow repair of already joined hosts. Contrary to ipaclient_force_join
Packit 8cb997
      the host entry will not be changed on the server
Packit 8cb997
    required: no
Packit 8cb997
author:
Packit 8cb997
    - Thomas Woerner
Packit 8cb997
'''
Packit 8cb997
Packit 8cb997
EXAMPLES = '''
Packit 8cb997
- name: Fix IPA ca certificate
Packit 8cb997
  ipaclient_fix_ca:
Packit 8cb997
    servers: ["server1.example.com","server2.example.com"]
Packit 8cb997
    realm: EXAMPLE.COM
Packit 8cb997
    basedn: dc=example,dc=com
Packit 8cb997
    allow_repair: yes
Packit 8cb997
'''
Packit 8cb997
Packit 8cb997
RETURN = '''
Packit 8cb997
'''
Packit 8cb997
Packit 8cb997
import os
Packit 8cb997
Packit 8cb997
from ansible.module_utils.basic import AnsibleModule
Packit 8cb997
from ansible.module_utils.ansible_ipa_client import (
Packit Service 0f71a7
    setup_logging,
Packit 8cb997
    SECURE_PATH, paths, sysrestore, options, NUM_VERSION, get_ca_cert,
Packit 8cb997
    get_ca_certs, errors
Packit 8cb997
)
Packit 8cb997
Packit 8cb997
Packit 8cb997
def main():
Packit 8cb997
    module = AnsibleModule(
Packit 8cb997
        argument_spec=dict(
Packit 8cb997
            servers=dict(required=True, type='list'),
Packit 8cb997
            realm=dict(required=True),
Packit 8cb997
            basedn=dict(required=True),
Packit 8cb997
            allow_repair=dict(required=True, type='bool'),
Packit 8cb997
        ),
Packit 8cb997
    )
Packit 8cb997
Packit 8cb997
    module._ansible_debug = True
Packit Service 0f71a7
    setup_logging()
Packit Service 0f71a7
Packit 8cb997
    servers = module.params.get('servers')
Packit 8cb997
    realm = module.params.get('realm')
Packit 8cb997
    basedn = module.params.get('basedn')
Packit 8cb997
    allow_repair = module.params.get('allow_repair')
Packit 8cb997
Packit 8cb997
    env = {'PATH': SECURE_PATH}
Packit 8cb997
    fstore = sysrestore.FileStore(paths.IPA_CLIENT_SYSRESTORE)
Packit 8cb997
    os.environ['KRB5CCNAME'] = paths.IPA_DNS_CCACHE
Packit 8cb997
Packit 8cb997
    options.ca_cert_file = None
Packit 8cb997
    options.principal = None
Packit 8cb997
    options.force = False
Packit 8cb997
    options.password = None
Packit 8cb997
Packit 8cb997
    changed = False
Packit 8cb997
    if not os.path.exists(paths.IPA_CA_CRT):
Packit 8cb997
        if not allow_repair:
Packit 8cb997
            module.fail_json(
Packit 8cb997
                msg="%s missing, enable allow_repair to fix it." %
Packit 8cb997
                paths.IPA_CA_CRT)
Packit 8cb997
Packit 8cb997
        # Repair missing ca.crt file
Packit 8cb997
        try:
Packit 8cb997
            os.environ['KRB5_CONFIG'] = env['KRB5_CONFIG'] = "/etc/krb5.conf"
Packit 8cb997
            env['KRB5CCNAME'] = os.environ['KRB5CCNAME']
Packit 8cb997
            if NUM_VERSION < 40100:
Packit 8cb997
                get_ca_cert(fstore, options, servers[0], basedn)
Packit 8cb997
            else:
Packit 8cb997
                get_ca_certs(fstore, options, servers[0], basedn, realm)
Packit 8cb997
            changed = True
Packit 8cb997
            del os.environ['KRB5_CONFIG']
Packit 8cb997
        except errors.FileError as e:
Packit 8cb997
            module.fail_json(msg='%s' % e)
Packit 8cb997
        except Exception as e:
Packit 8cb997
            module.fail_json(msg="Cannot obtain CA certificate\n%s" % e)
Packit 8cb997
Packit 8cb997
    module.exit_json(changed=changed)
Packit 8cb997
Packit 8cb997
Packit 8cb997
if __name__ == '__main__':
Packit 8cb997
    main()