@Test public void getExportedInternalTlsCertificate() throws Exception { byte[] certFileData = internalTlsCertificateService.exportInternalTlsCertificate(); assertTrue(certFileData.length > 100); InputStream exportedTarStream = getClass().getClassLoader() .getResourceAsStream("exported-example-certs.tar.gz"); Map<String, byte[]> exportedExampleFiles = extractTarGZ(IOUtils.toByteArray(exportedTarStream)); Map<String, byte[]> filesFromService = extractTarGZ( internalTlsCertificateService.exportInternalTlsCertificate()); assertEquals(exportedExampleFiles.size(), filesFromService.size()); for (String fileName : exportedExampleFiles.keySet()) { assertTrue(filesFromService.containsKey(fileName)); assertTrue(Arrays.equals(exportedExampleFiles.get(fileName), filesFromService.get(fileName))); } }
public byte[] exportInternalTlsCertificate() { X509Certificate certificate = internalTlsCertificateRepository.getInternalTlsCertificate(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try ( GzipCompressorOutputStream gzipCompressorOutputStream = new GzipCompressorOutputStream(byteArrayOutputStream); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(gzipCompressorOutputStream); TarArchiveOutputStream tarOutputStream = new TarArchiveOutputStream(bufferedOutputStream) ) { ByteArrayOutputStream pemStream = new ByteArrayOutputStream(); CryptoUtils.writeCertificatePem(certificate.getEncoded(), pemStream); writeFileToArchive(tarOutputStream, pemStream.toByteArray(), CERT_PEM_FILENAME); writeFileToArchive(tarOutputStream, certificate.getEncoded(), CERT_CER_FILENAME); } catch (IOException | CertificateEncodingException e) { log.error("writing certificate file failed", e); throw new RuntimeException(e); } return byteArrayOutputStream.toByteArray(); }
InternalTlsCertificateService { public byte[] exportInternalTlsCertificate() { X509Certificate certificate = internalTlsCertificateRepository.getInternalTlsCertificate(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try ( GzipCompressorOutputStream gzipCompressorOutputStream = new GzipCompressorOutputStream(byteArrayOutputStream); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(gzipCompressorOutputStream); TarArchiveOutputStream tarOutputStream = new TarArchiveOutputStream(bufferedOutputStream) ) { ByteArrayOutputStream pemStream = new ByteArrayOutputStream(); CryptoUtils.writeCertificatePem(certificate.getEncoded(), pemStream); writeFileToArchive(tarOutputStream, pemStream.toByteArray(), CERT_PEM_FILENAME); writeFileToArchive(tarOutputStream, certificate.getEncoded(), CERT_CER_FILENAME); } catch (IOException | CertificateEncodingException e) { log.error("writing certificate file failed", e); throw new RuntimeException(e); } return byteArrayOutputStream.toByteArray(); } }
InternalTlsCertificateService { public byte[] exportInternalTlsCertificate() { X509Certificate certificate = internalTlsCertificateRepository.getInternalTlsCertificate(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try ( GzipCompressorOutputStream gzipCompressorOutputStream = new GzipCompressorOutputStream(byteArrayOutputStream); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(gzipCompressorOutputStream); TarArchiveOutputStream tarOutputStream = new TarArchiveOutputStream(bufferedOutputStream) ) { ByteArrayOutputStream pemStream = new ByteArrayOutputStream(); CryptoUtils.writeCertificatePem(certificate.getEncoded(), pemStream); writeFileToArchive(tarOutputStream, pemStream.toByteArray(), CERT_PEM_FILENAME); writeFileToArchive(tarOutputStream, certificate.getEncoded(), CERT_CER_FILENAME); } catch (IOException | CertificateEncodingException e) { log.error("writing certificate file failed", e); throw new RuntimeException(e); } return byteArrayOutputStream.toByteArray(); } @Autowired  InternalTlsCertificateService(InternalTlsCertificateRepository internalTlsCertificateRepository,
            ExternalProcessRunner externalProcessRunner,
            @Value("${script.generate-certificate.path}") String generateCertScriptPath,
            @Value("${script.generate-certificate.args}") String generateCertScriptArgs,
            AuditDataHelper auditDataHelper); }
InternalTlsCertificateService { public byte[] exportInternalTlsCertificate() { X509Certificate certificate = internalTlsCertificateRepository.getInternalTlsCertificate(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try ( GzipCompressorOutputStream gzipCompressorOutputStream = new GzipCompressorOutputStream(byteArrayOutputStream); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(gzipCompressorOutputStream); TarArchiveOutputStream tarOutputStream = new TarArchiveOutputStream(bufferedOutputStream) ) { ByteArrayOutputStream pemStream = new ByteArrayOutputStream(); CryptoUtils.writeCertificatePem(certificate.getEncoded(), pemStream); writeFileToArchive(tarOutputStream, pemStream.toByteArray(), CERT_PEM_FILENAME); writeFileToArchive(tarOutputStream, certificate.getEncoded(), CERT_CER_FILENAME); } catch (IOException | CertificateEncodingException e) { log.error("writing certificate file failed", e); throw new RuntimeException(e); } return byteArrayOutputStream.toByteArray(); } @Autowired  InternalTlsCertificateService(InternalTlsCertificateRepository internalTlsCertificateRepository,
            ExternalProcessRunner externalProcessRunner,
            @Value("${script.generate-certificate.path}") String generateCertScriptPath,
            @Value("${script.generate-certificate.args}") String generateCertScriptArgs,
            AuditDataHelper auditDataHelper); X509Certificate getInternalTlsCertificate(); byte[] exportInternalTlsCertificate(); void generateInternalTlsKeyAndCertificate(); X509Certificate importInternalTlsCertificate(byte[] certificateBytes); }
InternalTlsCertificateService { public byte[] exportInternalTlsCertificate() { X509Certificate certificate = internalTlsCertificateRepository.getInternalTlsCertificate(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try ( GzipCompressorOutputStream gzipCompressorOutputStream = new GzipCompressorOutputStream(byteArrayOutputStream); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(gzipCompressorOutputStream); TarArchiveOutputStream tarOutputStream = new TarArchiveOutputStream(bufferedOutputStream) ) { ByteArrayOutputStream pemStream = new ByteArrayOutputStream(); CryptoUtils.writeCertificatePem(certificate.getEncoded(), pemStream); writeFileToArchive(tarOutputStream, pemStream.toByteArray(), CERT_PEM_FILENAME); writeFileToArchive(tarOutputStream, certificate.getEncoded(), CERT_CER_FILENAME); } catch (IOException | CertificateEncodingException e) { log.error("writing certificate file failed", e); throw new RuntimeException(e); } return byteArrayOutputStream.toByteArray(); } @Autowired  InternalTlsCertificateService(InternalTlsCertificateRepository internalTlsCertificateRepository,
            ExternalProcessRunner externalProcessRunner,
            @Value("${script.generate-certificate.path}") String generateCertScriptPath,
            @Value("${script.generate-certificate.args}") String generateCertScriptArgs,
            AuditDataHelper auditDataHelper); X509Certificate getInternalTlsCertificate(); byte[] exportInternalTlsCertificate(); void generateInternalTlsKeyAndCertificate(); X509Certificate importInternalTlsCertificate(byte[] certificateBytes); static final String KEY_CERT_GENERATION_FAILED; static final String IMPORT_INTERNAL_CERT_FAILED; }