Debian Codegraph @

ansible_collections/community/crypto/tests/integration/targets/get_certificate/files/process_certs.py

raw
1 # Copyright (c) Ansible Project
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 from __future__ import annotations
6
7 from sys import argv
8 from subprocess import Popen, PIPE, STDOUT
9
10 p = Popen(["openssl", "s_client", "-host", argv[1], "-port", "443", "-prexit", "-showcerts"], stdin=PIPE, stdout=PIPE, stderr=STDOUT)
11 stdout = p.communicate(input=b'\n')[0]
12 data = stdout.decode()
13
14 certs = []
15 cert = ""
16 capturing = False
17 for line in data.split('\n'):
18 if line == '-----BEGIN CERTIFICATE-----':
19 capturing = True
20
21 if capturing:
22 cert = "{0}{1}\n".format(cert, line)
23
24 if line == '-----END CERTIFICATE-----':
25 capturing = False
26 certs.append(cert)
27 cert = ""
28
29 with open(argv[2], 'w') as f:
30 for cert in set(certs):
31 f.write(cert)