def _do_command(self):
        self._validate_options()
        # Return code
        imported_certs = []
        for src_cert_file in self.options.certificate_file:
            src_cert_file = os.path.expanduser(src_cert_file)
            if os.path.exists(src_cert_file):
                try:
                    extractor = managerlib.ImportFileExtractor(src_cert_file)

                    # Verify the entitlement data.
                    if extractor.verify_valid_entitlement():
                        extractor.write_to_disk()
                        print(
                            _("Successfully imported certificate {file}").
                            format(file=os.path.basename(src_cert_file)))
                        imported_certs.append(extractor.get_cert())
                    else:
                        log.error(
                            "Error parsing manually imported entitlement "
                            "certificate: {src_cert_file}".format(
                                src_cert_file=src_cert_file))
                        print(
                            _("{file} is not a valid certificate file. Please use a valid certificate."
                              ).format(file=os.path.basename(src_cert_file)))

                except Exception as e:
                    # Should not get here unless something really bad happened.
                    log.exception(e)
                    print(
                        _("An error occurred while importing the certificate. "
                          "Please check log file for more information."))
            else:
                log.error(
                    "Supplied certificate file does not exist: {file}".format(
                        file=src_cert_file))
                print(
                    _("{file}: file not found.").format(
                        file=os.path.basename(src_cert_file)))

        # update branding info for the imported certs, if needed
        if imported_certs:
            # RHELBrandsInstaller will load ent dir by default
            brands_installer = rhelentbranding.RHELBrandsInstaller()
            brands_installer.install()

        self._request_validity_check()

        return_code = 0
        if not imported_certs:
            return_code = 1

        return return_code
    def branding_hook(self):
        """Update branding info based on entitlement cert changes."""

        # RHELBrandsInstaller will use latest ent_dir contents
        brands_installer = rhelentbranding.RHELBrandsInstaller()
        brands_installer.install()
Esempio n. 3
0
class ImportSubDialog(object):
    """
    Dialog to manually import an entitlement certificate for this machine.
    Generally used for disconnected (unregistered) systems.
    """
    def __init__(self):
        self._parent = None

        self.dialog = gtk.FileChooserDialog(_("Import Certificates"),
                None, gtk.FILE_CHOOSER_ACTION_OPEN,
                (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                 _("Import"), gtk.RESPONSE_OK))
        self.dialog.set_default_response(gtk.RESPONSE_OK)
        self.dialog.set_modal(True)

        self.dialog.set_local_only(True)
        self.dialog.set_select_multiple(True)
        self.dialog.set_icon_name('subscription-manager')

        afilter = gtk.FileFilter()
        afilter.set_name(_("Certificates"))
        afilter.add_pattern("*.pem")
        self.dialog.add_filter(afilter)

        afilter = gtk.FileFilter()
        afilter.set_name(_("All files"))
        afilter.add_pattern("*")
        self.dialog.add_filter(afilter)

        self.dialog.connect("response", self._on_dialog_response)
        self.dialog.connect("delete-event", self._delete_event)

    def _on_dialog_response(self, dialog, response_id):
        if response_id == gtk.RESPONSE_CANCEL:
            return self._cancel_button_clicked()
        elif response_id == gtk.RESPONSE_OK:
            return self._import_button_clicked()
        # other response is on dialog destroy, we don't act on that.

    def _cancel_button_clicked(self):
        self.dialog.hide()
        return True

    def show(self):
        self.dialog.present()

    def _delete_event(self, event, data=None):
        return self._cancel_button_clicked()

    def _import_button_clicked(self):
        src_cert_files = self.dialog.get_filenames()

        invalid_certs = []
        error_certs = []
        good_certs = []
        imported_certs = []
        non_cert_files = []

        for cert_file in src_cert_files:
            if not os.path.exists(cert_file):
                non_cert_files.append(cert_file)
            else:
                # Check to see if we have a key included in the cert file
                try:
                    extractor = ImportFileExtractor(cert_file)

                    #Verify the entitlement data.
                    if not extractor.verify_valid_entitlement():
                        log.error("Invalid X509 entitlement certificate.")
                        log.error("Error parsing manually imported entitlement "
                            "certificate: %s" % cert_file)
                        invalid_certs.append(cert_file)
                    else:
                        extractor.write_to_disk()
                        good_certs.append(cert_file)
                        imported_certs.append(extractor.get_cert())
                except Exception, e:
                    # Should not get here unless something really bad happened.
                    log.exception(e)
                    error_certs.append(cert_file)

        if imported_certs:
            brands_installer = rhelentbranding.RHELBrandsInstaller()
            brands_installer.install()

        if len(error_certs) > 0 \
            or len(invalid_certs) > 0 \
            or len(non_cert_files) > 0:

            msg = ""
            if len(non_cert_files) > 0:
                msg += _("The following certificate files did not exist:")
                msg += "\n" + "\n".join(non_cert_files)
            if len(invalid_certs) > 0:
                msg += _("The following files are not valid certificates and were not imported:")
                msg += "\n" + "\n".join(invalid_certs)
            if len(error_certs) > 0:
                if len(invalid_certs) > 0:
                    msg += "\n\n"
                msg += _("An error occurred while importing the following certificates. Please check the log file for more information.")
                msg += "\n" + "\n".join(error_certs)
            if len(good_certs) > 0:
                msg += "\n\n"
                msg += _("The following certificates were successfully imported:")
                msg += "\n" + "\n".join(good_certs)
            show_error_window(msg, parent=self._parent)
        else:
            #if we get to this point, the import was successful
            messageWindow.InfoDialog(_("Certificate import was successful."),
                    parent=self._parent)
        self.dialog.hide()
        self.dialog.unselect_all()
    def _import_button_clicked(self):
        src_cert_files = self.dialog.get_filenames()

        invalid_certs = []
        error_certs = []
        good_certs = []
        imported_certs = []
        non_cert_files = []

        for cert_file in src_cert_files:
            if not os.path.exists(cert_file):
                non_cert_files.append(cert_file)
            else:
                # Check to see if we have a key included in the cert file
                try:
                    extractor = ImportFileExtractor(cert_file)

                    #Verify the entitlement data.
                    if not extractor.verify_valid_entitlement():
                        log.error("Invalid X509 entitlement certificate.")
                        log.error(
                            "Error parsing manually imported entitlement "
                            "certificate: %s" % cert_file)
                        invalid_certs.append(cert_file)
                    else:
                        extractor.write_to_disk()
                        good_certs.append(cert_file)
                        imported_certs.append(extractor.get_cert())
                except Exception as e:
                    # Should not get here unless something really bad happened.
                    log.exception(e)
                    error_certs.append(cert_file)

        if imported_certs:
            brands_installer = rhelentbranding.RHELBrandsInstaller()
            brands_installer.install()

        if len(error_certs) > 0 \
            or len(invalid_certs) > 0 \
            or len(non_cert_files) > 0:

            msg = ""
            if len(non_cert_files) > 0:
                msg += _("The following certificate files did not exist:")
                msg += "\n" + "\n".join(non_cert_files)
            if len(invalid_certs) > 0:
                msg += _(
                    "The following files are not valid certificates and were not imported:"
                )
                msg += "\n" + "\n".join(invalid_certs)
            if len(error_certs) > 0:
                if len(invalid_certs) > 0:
                    msg += "\n\n"
                msg += _(
                    "An error occurred while importing the following certificates. Please check the log file for more information."
                )
                msg += "\n" + "\n".join(error_certs)
            if len(good_certs) > 0:
                msg += "\n\n"
                msg += _(
                    "The following certificates were successfully imported:")
                msg += "\n" + "\n".join(good_certs)
            show_error_window(msg, parent=self._parent)
        else:
            #if we get to this point, the import was successful
            messageWindow.InfoDialog(_("Certificate import was successful."),
                                     parent=self._parent)
        self.dialog.hide()
        self.dialog.unselect_all()