예제 #1
0
    def test_scan_for_certificates_no_certificates(self):
        with TemporaryDirectory() as temp_dir:
            with open(os.path.join(temp_dir, 'key.pem'), 'w') as f:
                f.write(self.key_data.decode('utf-8'))

            with open(os.path.join(temp_dir, 'random.txt'), 'w') as f:
                f.write('random')

            with self.assertRaisesRegex(CertificateScanFailedException,
                                        'ERROR: No certificates were found'):
                scan_for_certificates(path=temp_dir)
예제 #2
0
    def test_scan_for_certificates_no_chain(self):
        with TemporaryDirectory() as temp_dir:
            with open(os.path.join(temp_dir, 'cert.pem'), 'w') as f:
                f.write(self.cert_data.decode('utf-8'))

            with open(os.path.join(temp_dir, 'random.txt'), 'w') as f:
                f.write('random')

            with self.assertRaisesRegex(
                    CertificateScanFailedException,
                    'ERROR: Only one certificate was found. The chain may be missing.'
            ):
                scan_for_certificates(path=temp_dir)
예제 #3
0
    def test_scan_for_certificates_duplicate_certificate(self):
        with TemporaryDirectory() as temp_dir:
            with open(os.path.join(temp_dir, 'cert.pem'), 'w') as f:
                f.write(self.cert_data.decode('utf-8'))

            with open(os.path.join(temp_dir, 'cert2.pem'), 'w') as f:
                f.write(self.cert_data.decode('utf-8'))

            with open(os.path.join(temp_dir, 'chain.pem'), 'w') as f:
                f.write(self.ca_cert_data.decode('utf-8'))

            with open(os.path.join(temp_dir, 'key.pem'), 'w') as f:
                f.write(self.key_data.decode('utf-8'))

            with open(os.path.join(temp_dir, 'random.txt'), 'w') as f:
                f.write('random')

            with self.assertRaisesRegex(
                    DuplicateCertificateException,
                    'ERROR: Certificate ".+" duplicates ".+"'):
                scan_for_certificates(path=temp_dir)
예제 #4
0
    def test_scan_for_certificates_validation_failed(
            self, mock_parse_certificates, mock_validate_certificates):
        with TemporaryDirectory() as temp_dir:
            with open(os.path.join(temp_dir, 'cert.pem'), 'w') as f:
                f.write(self.cert_data.decode('utf-8'))

            with open(os.path.join(temp_dir, 'chain.pem'), 'w') as f:
                f.write(self.ca_cert_data.decode('utf-8'))

            with open(os.path.join(temp_dir, 'key.pem'), 'w') as f:
                f.write(self.key_data.decode('utf-8'))

            cert = CertificateChain(
                OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM,
                                                self.cert_data),
                os.path.join(temp_dir, 'cert.pem'))
            chain = CertificateChain(
                OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM,
                                                self.ca_cert_data),
                os.path.join(temp_dir, 'chain.pem'))
            key = {
                'key':
                OpenSSL.crypto.load_privatekey(OpenSSL.crypto.FILETYPE_PEM,
                                               self.key_data),
                'path':
                os.path.join(temp_dir, 'key.pem')
            }

            mock_parse_certificates.return_value = {
                'certificate': cert,
                'chain': chain,
                'private_key': key
            }

            with self.assertRaisesRegex(
                    CertificateScanFailedException,
                    'ERROR: Unable to automatically find the correct certificates. Please enter the'
                    ' certificate manually using the command line arguments.'):
                scan_for_certificates(path=temp_dir)
예제 #5
0
    def test_scan_for_certificates_duplicate_key(self):
        with TemporaryDirectory() as temp_dir:
            with open(os.path.join(temp_dir, 'cert.pem'), 'w') as f:
                f.write(self.cert_data.decode('utf-8'))

            with open(os.path.join(temp_dir, 'chain.pem'), 'w') as f:
                f.write(self.ca_cert_data.decode('utf-8'))

            with open(os.path.join(temp_dir, 'key.pem'), 'w') as f:
                f.write(self.key_data.decode('utf-8'))

            with open(os.path.join(temp_dir, 'key2.pem'), 'w') as f:
                f.write(self.key_data.decode('utf-8'))

            with open(os.path.join(temp_dir, 'random.txt'), 'w') as f:
                f.write('random')

            error_message = 'ERROR: Private key "%s" duplicates "%s"' % (
                os.path.join(temp_dir,
                             'key2.pem'), os.path.join(temp_dir, 'key.pem'))

            with self.assertRaisesRegex(DuplicatePrivateKeyException,
                                        error_message):
                scan_for_certificates(path=temp_dir)
예제 #6
0
    def test_scan_for_certificates(self, mock_parse_certificates,
                                   mock_validate_certificates):
        with TemporaryDirectory() as temp_dir:
            os.mkdir(os.path.join(temp_dir, 'directory'))

            with open(os.path.join(temp_dir, 'cert.pem'), 'w') as f:
                f.write(self.cert_data.decode('utf-8'))

            with open(os.path.join(temp_dir, 'chain.pem'), 'w') as f:
                f.write(self.ca_cert_data.decode('utf-8'))

            with open(os.path.join(temp_dir, 'key.pem'), 'w') as f:
                f.write(self.key_data.decode('utf-8'))

            with open(os.path.join(temp_dir, 'random.txt'), 'w') as f:
                f.write('random')

            cert = CertificateChain(
                OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM,
                                                self.cert_data),
                os.path.join(temp_dir, 'cert.pem'))
            chain = CertificateChain(
                OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM,
                                                self.ca_cert_data),
                os.path.join(temp_dir, 'chain.pem'))
            key = {
                'key':
                OpenSSL.crypto.load_privatekey(OpenSSL.crypto.FILETYPE_PEM,
                                               self.key_data),
                'path':
                os.path.join(temp_dir, 'key.pem')
            }

            mock_parse_certificates.return_value = {
                'certificate': cert,
                'chain': chain,
                'private_key': key
            }

            cert_info = scan_for_certificates(path=temp_dir)

            self.assertDictEqual(
                {
                    'certificate': os.path.join(temp_dir, 'cert.pem'),
                    'chain': os.path.join(temp_dir, 'chain.pem'),
                    'private_key': os.path.join(temp_dir, 'key.pem')
                }, cert_info)