We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Chain Certificate is not exporting
Hi Team,

While extracting .p12 formatted file (which was exported from Lemur) we cannot find the Chain file on the same. Could you please let us know if we need to perform any additional step to download the .p12 along with the chain. 

Thanks,
Akash John


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `lemur/plugins/lemur_openssl/plugin.py`
Content:
```
1 """
2 .. module: lemur.plugins.lemur_openssl.plugin
3     :platform: Unix
4     :copyright: (c) 2015 by Netflix Inc., see AUTHORS for more
5     :license: Apache, see LICENSE for more details.
6 
7 .. moduleauthor:: Kevin Glisson <kglisson@netflix.com>
8 """
9 import subprocess
10 
11 from flask import current_app
12 
13 from lemur.utils import mktempfile, mktemppath
14 from lemur.plugins.bases import ExportPlugin
15 from lemur.plugins import lemur_openssl as openssl
16 from lemur.common.utils import get_psuedo_random_string
17 
18 
19 def run_process(command):
20     """
21     Runs a given command with pOpen and wraps some
22     error handling around it.
23     :param command:
24     :return:
25     """
26     p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
27     current_app.logger.debug(command)
28     stdout, stderr = p.communicate()
29 
30     if p.returncode != 0:
31         current_app.logger.debug(" ".join(command))
32         current_app.logger.error(stderr)
33         raise Exception(stderr)
34 
35 
36 def create_pkcs12(cert, p12_tmp, key, alias, passphrase):
37     """
38     Creates a pkcs12 formated file.
39     :param cert:
40     :param jks_tmp:
41     :param key:
42     :param alias:
43     :param passphrase:
44     """
45     with mktempfile() as key_tmp:
46         with open(key_tmp, 'w') as f:
47             f.write(key)
48 
49         # Create PKCS12 keystore from private key and public certificate
50         with mktempfile() as cert_tmp:
51             with open(cert_tmp, 'w') as f:
52                 f.write(cert)
53 
54             run_process([
55                 "openssl",
56                 "pkcs12",
57                 "-export",
58                 "-name", alias,
59                 "-in", cert_tmp,
60                 "-inkey", key_tmp,
61                 "-out", p12_tmp,
62                 "-password", "pass:{}".format(passphrase)
63             ])
64 
65 
66 class OpenSSLExportPlugin(ExportPlugin):
67     title = 'OpenSSL'
68     slug = 'openssl-export'
69     description = 'Is a loose interface to openssl and support various formats'
70     version = openssl.VERSION
71 
72     author = 'Kevin Glisson'
73     author_url = 'https://github.com/netflix/lemur'
74 
75     options = [
76         {
77             'name': 'type',
78             'type': 'select',
79             'required': True,
80             'available': ['PKCS12 (.p12)'],
81             'helpMessage': 'Choose the format you wish to export',
82         },
83         {
84             'name': 'passphrase',
85             'type': 'str',
86             'required': False,
87             'helpMessage': 'If no passphrase is given one will be generated for you, we highly recommend this. Minimum length is 8.',
88             'validation': ''
89         },
90         {
91             'name': 'alias',
92             'type': 'str',
93             'required': False,
94             'helpMessage': 'Enter the alias you wish to use for the keystore.',
95         }
96     ]
97 
98     def export(self, body, chain, key, options, **kwargs):
99         """
100         Generates a Java Keystore or Truststore
101 
102         :param key:
103         :param chain:
104         :param body:
105         :param options:
106         :param kwargs:
107         """
108         if self.get_option('passphrase', options):
109             passphrase = self.get_option('passphrase', options)
110         else:
111             passphrase = get_psuedo_random_string()
112 
113         if self.get_option('alias', options):
114             alias = self.get_option('alias', options)
115         else:
116             alias = "blah"
117 
118         type = self.get_option('type', options)
119 
120         with mktemppath() as output_tmp:
121             if type == 'PKCS12 (.p12)':
122                 create_pkcs12(body, output_tmp, key, alias, passphrase)
123                 extension = "p12"
124             else:
125                 raise Exception("Unable to export, unsupported type: {0}".format(type))
126 
127             with open(output_tmp, 'rb') as f:
128                 raw = f.read()
129 
130         return extension, passphrase, raw
131
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/lemur/plugins/lemur_openssl/plugin.py b/lemur/plugins/lemur_openssl/plugin.py
--- a/lemur/plugins/lemur_openssl/plugin.py
+++ b/lemur/plugins/lemur_openssl/plugin.py
@@ -33,11 +33,12 @@
         raise Exception(stderr)
 
 
-def create_pkcs12(cert, p12_tmp, key, alias, passphrase):
+def create_pkcs12(cert, chain, p12_tmp, key, alias, passphrase):
     """
     Creates a pkcs12 formated file.
     :param cert:
-    :param jks_tmp:
+    :param chain:
+    :param p12_tmp:
     :param key:
     :param alias:
     :param passphrase:
@@ -49,7 +50,7 @@
         # Create PKCS12 keystore from private key and public certificate
         with mktempfile() as cert_tmp:
             with open(cert_tmp, 'w') as f:
-                f.write(cert)
+                f.writelines([cert + "\n", chain + "\n"])
 
             run_process([
                 "openssl",
@@ -119,7 +120,7 @@
 
         with mktemppath() as output_tmp:
             if type == 'PKCS12 (.p12)':
-                create_pkcs12(body, output_tmp, key, alias, passphrase)
+                create_pkcs12(body, chain, output_tmp, key, alias, passphrase)
                 extension = "p12"
             else:
                 raise Exception("Unable to export, unsupported type: {0}".format(type))

{"golden_diff": "diff --git a/lemur/plugins/lemur_openssl/plugin.py b/lemur/plugins/lemur_openssl/plugin.py\n--- a/lemur/plugins/lemur_openssl/plugin.py\n+++ b/lemur/plugins/lemur_openssl/plugin.py\n@@ -33,11 +33,12 @@\n         raise Exception(stderr)\n \n \n-def create_pkcs12(cert, p12_tmp, key, alias, passphrase):\n+def create_pkcs12(cert, chain, p12_tmp, key, alias, passphrase):\n     \"\"\"\n     Creates a pkcs12 formated file.\n     :param cert:\n-    :param jks_tmp:\n+    :param chain:\n+    :param p12_tmp:\n     :param key:\n     :param alias:\n     :param passphrase:\n@@ -49,7 +50,7 @@\n         # Create PKCS12 keystore from private key and public certificate\n         with mktempfile() as cert_tmp:\n             with open(cert_tmp, 'w') as f:\n-                f.write(cert)\n+                f.writelines([cert + \"\\n\", chain + \"\\n\"])\n \n             run_process([\n                 \"openssl\",\n@@ -119,7 +120,7 @@\n \n         with mktemppath() as output_tmp:\n             if type == 'PKCS12 (.p12)':\n-                create_pkcs12(body, output_tmp, key, alias, passphrase)\n+                create_pkcs12(body, chain, output_tmp, key, alias, passphrase)\n                 extension = \"p12\"\n             else:\n                 raise Exception(\"Unable to export, unsupported type: {0}\".format(type))\n", "issue": "Chain Certificate is not exporting\nHi Team,\n\nWhile extracting .p12 formatted file (which was exported from Lemur) we cannot find the Chain file on the same. Could you please let us know if we need to perform any additional step to download the .p12 along with the chain. \n\nThanks,\nAkash John\n\n", "before_files": [{"content": "\"\"\"\n.. module: lemur.plugins.lemur_openssl.plugin\n    :platform: Unix\n    :copyright: (c) 2015 by Netflix Inc., see AUTHORS for more\n    :license: Apache, see LICENSE for more details.\n\n.. moduleauthor:: Kevin Glisson <kglisson@netflix.com>\n\"\"\"\nimport subprocess\n\nfrom flask import current_app\n\nfrom lemur.utils import mktempfile, mktemppath\nfrom lemur.plugins.bases import ExportPlugin\nfrom lemur.plugins import lemur_openssl as openssl\nfrom lemur.common.utils import get_psuedo_random_string\n\n\ndef run_process(command):\n    \"\"\"\n    Runs a given command with pOpen and wraps some\n    error handling around it.\n    :param command:\n    :return:\n    \"\"\"\n    p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n    current_app.logger.debug(command)\n    stdout, stderr = p.communicate()\n\n    if p.returncode != 0:\n        current_app.logger.debug(\" \".join(command))\n        current_app.logger.error(stderr)\n        raise Exception(stderr)\n\n\ndef create_pkcs12(cert, p12_tmp, key, alias, passphrase):\n    \"\"\"\n    Creates a pkcs12 formated file.\n    :param cert:\n    :param jks_tmp:\n    :param key:\n    :param alias:\n    :param passphrase:\n    \"\"\"\n    with mktempfile() as key_tmp:\n        with open(key_tmp, 'w') as f:\n            f.write(key)\n\n        # Create PKCS12 keystore from private key and public certificate\n        with mktempfile() as cert_tmp:\n            with open(cert_tmp, 'w') as f:\n                f.write(cert)\n\n            run_process([\n                \"openssl\",\n                \"pkcs12\",\n                \"-export\",\n                \"-name\", alias,\n                \"-in\", cert_tmp,\n                \"-inkey\", key_tmp,\n                \"-out\", p12_tmp,\n                \"-password\", \"pass:{}\".format(passphrase)\n            ])\n\n\nclass OpenSSLExportPlugin(ExportPlugin):\n    title = 'OpenSSL'\n    slug = 'openssl-export'\n    description = 'Is a loose interface to openssl and support various formats'\n    version = openssl.VERSION\n\n    author = 'Kevin Glisson'\n    author_url = 'https://github.com/netflix/lemur'\n\n    options = [\n        {\n            'name': 'type',\n            'type': 'select',\n            'required': True,\n            'available': ['PKCS12 (.p12)'],\n            'helpMessage': 'Choose the format you wish to export',\n        },\n        {\n            'name': 'passphrase',\n            'type': 'str',\n            'required': False,\n            'helpMessage': 'If no passphrase is given one will be generated for you, we highly recommend this. Minimum length is 8.',\n            'validation': ''\n        },\n        {\n            'name': 'alias',\n            'type': 'str',\n            'required': False,\n            'helpMessage': 'Enter the alias you wish to use for the keystore.',\n        }\n    ]\n\n    def export(self, body, chain, key, options, **kwargs):\n        \"\"\"\n        Generates a Java Keystore or Truststore\n\n        :param key:\n        :param chain:\n        :param body:\n        :param options:\n        :param kwargs:\n        \"\"\"\n        if self.get_option('passphrase', options):\n            passphrase = self.get_option('passphrase', options)\n        else:\n            passphrase = get_psuedo_random_string()\n\n        if self.get_option('alias', options):\n            alias = self.get_option('alias', options)\n        else:\n            alias = \"blah\"\n\n        type = self.get_option('type', options)\n\n        with mktemppath() as output_tmp:\n            if type == 'PKCS12 (.p12)':\n                create_pkcs12(body, output_tmp, key, alias, passphrase)\n                extension = \"p12\"\n            else:\n                raise Exception(\"Unable to export, unsupported type: {0}\".format(type))\n\n            with open(output_tmp, 'rb') as f:\n                raw = f.read()\n\n        return extension, passphrase, raw\n", "path": "lemur/plugins/lemur_openssl/plugin.py"}], "after_files": [{"content": "\"\"\"\n.. module: lemur.plugins.lemur_openssl.plugin\n    :platform: Unix\n    :copyright: (c) 2015 by Netflix Inc., see AUTHORS for more\n    :license: Apache, see LICENSE for more details.\n\n.. moduleauthor:: Kevin Glisson <kglisson@netflix.com>\n\"\"\"\nimport subprocess\n\nfrom flask import current_app\n\nfrom lemur.utils import mktempfile, mktemppath\nfrom lemur.plugins.bases import ExportPlugin\nfrom lemur.plugins import lemur_openssl as openssl\nfrom lemur.common.utils import get_psuedo_random_string\n\n\ndef run_process(command):\n    \"\"\"\n    Runs a given command with pOpen and wraps some\n    error handling around it.\n    :param command:\n    :return:\n    \"\"\"\n    p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n    current_app.logger.debug(command)\n    stdout, stderr = p.communicate()\n\n    if p.returncode != 0:\n        current_app.logger.debug(\" \".join(command))\n        current_app.logger.error(stderr)\n        raise Exception(stderr)\n\n\ndef create_pkcs12(cert, chain, p12_tmp, key, alias, passphrase):\n    \"\"\"\n    Creates a pkcs12 formated file.\n    :param cert:\n    :param chain:\n    :param p12_tmp:\n    :param key:\n    :param alias:\n    :param passphrase:\n    \"\"\"\n    with mktempfile() as key_tmp:\n        with open(key_tmp, 'w') as f:\n            f.write(key)\n\n        # Create PKCS12 keystore from private key and public certificate\n        with mktempfile() as cert_tmp:\n            with open(cert_tmp, 'w') as f:\n                f.writelines([cert + \"\\n\", chain + \"\\n\"])\n\n            run_process([\n                \"openssl\",\n                \"pkcs12\",\n                \"-export\",\n                \"-name\", alias,\n                \"-in\", cert_tmp,\n                \"-inkey\", key_tmp,\n                \"-out\", p12_tmp,\n                \"-password\", \"pass:{}\".format(passphrase)\n            ])\n\n\nclass OpenSSLExportPlugin(ExportPlugin):\n    title = 'OpenSSL'\n    slug = 'openssl-export'\n    description = 'Is a loose interface to openssl and support various formats'\n    version = openssl.VERSION\n\n    author = 'Kevin Glisson'\n    author_url = 'https://github.com/netflix/lemur'\n\n    options = [\n        {\n            'name': 'type',\n            'type': 'select',\n            'required': True,\n            'available': ['PKCS12 (.p12)'],\n            'helpMessage': 'Choose the format you wish to export',\n        },\n        {\n            'name': 'passphrase',\n            'type': 'str',\n            'required': False,\n            'helpMessage': 'If no passphrase is given one will be generated for you, we highly recommend this. Minimum length is 8.',\n            'validation': ''\n        },\n        {\n            'name': 'alias',\n            'type': 'str',\n            'required': False,\n            'helpMessage': 'Enter the alias you wish to use for the keystore.',\n        }\n    ]\n\n    def export(self, body, chain, key, options, **kwargs):\n        \"\"\"\n        Generates a Java Keystore or Truststore\n\n        :param key:\n        :param chain:\n        :param body:\n        :param options:\n        :param kwargs:\n        \"\"\"\n        if self.get_option('passphrase', options):\n            passphrase = self.get_option('passphrase', options)\n        else:\n            passphrase = get_psuedo_random_string()\n\n        if self.get_option('alias', options):\n            alias = self.get_option('alias', options)\n        else:\n            alias = \"blah\"\n\n        type = self.get_option('type', options)\n\n        with mktemppath() as output_tmp:\n            if type == 'PKCS12 (.p12)':\n                create_pkcs12(body, chain, output_tmp, key, alias, passphrase)\n                extension = \"p12\"\n            else:\n                raise Exception(\"Unable to export, unsupported type: {0}\".format(type))\n\n            with open(output_tmp, 'rb') as f:\n                raw = f.read()\n\n        return extension, passphrase, raw\n", "path": "lemur/plugins/lemur_openssl/plugin.py"}]}