<commit_before>"""
.. module: lemur.plugins.lemur_openssl.plugin
    :platform: Unix
    :copyright: (c) 2015 by Netflix Inc., see AUTHORS for more
    :license: Apache, see LICENSE for more details.

.. moduleauthor:: <NAME> <<EMAIL>>
"""
from io import open
import subprocess

from flask import current_app

from lemur.utils import mktempfile, mktemppath
from lemur.plugins.bases import ExportPlugin
from lemur.plugins import lemur_openssl as openssl
from lemur.common.utils import get_psuedo_random_string


def run_process(command):
    """
    Runs a given command with pOpen and wraps some
    error handling around it.
    :param command:
    :return:
    """
    p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    current_app.logger.debug(command)
    stdout, stderr = p.communicate()

    if p.returncode != 0:
        current_app.logger.debug(" ".join(command))
        current_app.logger.error(stderr)
        raise Exception(stderr)


def create_pkcs12(cert, chain, p12_tmp, key, alias, passphrase):
    """
    Creates a pkcs12 formated file.
    :param cert:
    :param chain:
    :param p12_tmp:
    :param key:
    :param alias:
    :param passphrase:
    """
    if isinstance(cert, bytes):
        cert = cert.decode('utf-8')

    if isinstance(chain, bytes):
        chain = chain.decode('utf-8')

    if isinstance(key, bytes):
        key = key.decode('utf-8')

    with mktempfile() as key_tmp:
        with open(key_tmp, 'w') as f:
            f.write(key)

        # Create PKCS12 keystore from private key and public certificate
        with mktempfile() as cert_tmp:
            with open(cert_tmp, 'w') as f:
                f.writelines([cert, chain])

            run_process([
                "openssl",
                "pkcs12",
                "-export",
                "-name", alias,
                "-in", cert_tmp,
                "-inkey", key_tmp,
                "-out", p12_tmp,
                "-password", "<PASSWORD>:{}".format(passphrase)
            ])


class OpenSSLExportPlugin(ExportPlugin):
    title = 'OpenSSL'
    slug = 'openssl-export'
    description = 'Is a loose interface to openssl and support various formats'<commit_msg>Ensuring that the temporary certificate is created correctly (#400)<commit_after>"""
.. module: lemur.plugins.lemur_openssl.plugin
    :platform: Unix
    :copyright: (c) 2015 by Netflix Inc., see AUTHORS for more
    :license: Apache, see LICENSE for more details.

.. moduleauthor:: <NAME> <<EMAIL>>
"""
from io import open
import subprocess

from flask import current_app

from lemur.utils import mktempfile, mktemppath
from lemur.plugins.bases import ExportPlugin
from lemur.plugins import lemur_openssl as openssl
from lemur.common.utils import get_psuedo_random_string


def run_process(command):
    """
    Runs a given command with pOpen and wraps some
    error handling around it.
    :param command:
    :return:
    """
    p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    current_app.logger.debug(command)
    stdout, stderr = p.communicate()

    if p.returncode != 0:
        current_app.logger.debug(" ".join(command))
        current_app.logger.error(stderr)
        raise Exception(stderr)


def create_pkcs12(cert, chain, p12_tmp, key, alias, passphrase):
    """
    Creates a pkcs12 formated file.
    :param cert:
    :param chain:
    :param p12_tmp:
    :param key:
    :param alias:
    :param passphrase:
    """
    if isinstance(cert, bytes):
        cert = cert.decode('utf-8')

    if isinstance(chain, bytes):
        chain = chain.decode('utf-8')

    if isinstance(key, bytes):
        key = key.decode('utf-8')

    with mktempfile() as key_tmp:
        with open(key_tmp, 'w') as f:
            f.write(key)

        # Create PKCS12 keystore from private key and public certificate
        with mktempfile() as cert_tmp:
            with open(cert_tmp, 'w') as f:
                f.writelines([cert.strip() + "\n", chain.strip() + "\n"])

            run_process([
                "openssl",
                "pkcs12",
                "-export",
                "-name", alias,
                "-in", cert_tmp,
                "-inkey", key_tmp,
                "-out", p12_tmp,
                "-password", "<PASSWORD>(passphrase)
            ])


class OpenSSLExportPlugin(ExportPlugin):
    title = 'OpenSSL'
    slug = 'openssl-export'
    description = 'Is a loose interface to openssl and support various formats'
{'fraction_non_alphanumeric': 0.11153077093979116, 'fraction_numerical': 0.010442123972450567, 'mean_word_length': 2.447166921898928, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3503098', 'n_tokens_mistral': 1434, 'n_tokens_neox': 1331, 'n_words': 385}