Search in sources :

Example 6 with Certificate

use of com.amazon.dataprepper.plugins.certificate.model.Certificate in project data-prepper by opensearch-project.

the class FileCertificateProvider method getCertificate.

public Certificate getCertificate() {
    try {
        final Path certFilePath = new File(certificateFilePath).toPath();
        final Path pkFilePath = new File(privateKeyFilePath).toPath();
        final byte[] certFileBytes = Files.readAllBytes(certFilePath);
        final byte[] pkFileBytes = Files.readAllBytes(pkFilePath);
        final String certAsString = new String(certFileBytes);
        final String privateKeyAsString = new String(pkFileBytes);
        return new Certificate(certAsString, privateKeyAsString);
    } catch (final Exception ex) {
        LOG.error("Error encountered while reading the certificate.", ex);
        throw new RuntimeException(ex);
    }
}
Also used : Path(java.nio.file.Path) File(java.io.File) Certificate(com.amazon.dataprepper.plugins.certificate.model.Certificate)

Example 7 with Certificate

use of com.amazon.dataprepper.plugins.certificate.model.Certificate in project data-prepper by opensearch-project.

the class S3CertificateProvider method getCertificate.

public Certificate getCertificate() {
    try {
        final URI certificateFileUri = new URI(certificateFile);
        final URI privateKeyFileUri = new URI(privateKeyFile);
        final String certificate = getObjectWithKey(certificateFileUri.getHost(), certificateFileUri.getPath().substring(1));
        final String privateKey = getObjectWithKey(privateKeyFileUri.getHost(), privateKeyFileUri.getPath().substring(1));
        return new Certificate(certificate, privateKey);
    } catch (URISyntaxException ex) {
        LOG.error("Error encountered while parsing the certificate's Amazon S3 URI.", ex);
        throw new RuntimeException(ex);
    }
}
Also used : URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Certificate(com.amazon.dataprepper.plugins.certificate.model.Certificate)

Example 8 with Certificate

use of com.amazon.dataprepper.plugins.certificate.model.Certificate in project data-prepper by opensearch-project.

the class ACMCertificateProviderTest method getACMCertificateWithEncryptedPrivateKeySuccess.

@Test
public void getACMCertificateWithEncryptedPrivateKeySuccess() throws IOException {
    final Path certFilePath = Path.of("data/certificate/test_cert.crt");
    final Path encryptedKeyFilePath = Path.of("data/certificate/test_encrypted_key.key");
    final Path decryptedKeyFilePath = Path.of("data/certificate/test_decrypted_key.key");
    final String certAsString = Files.readString(certFilePath);
    final String encryptedKeyAsString = Files.readString(encryptedKeyFilePath);
    final String decryptedKeyAsString = Files.readString(decryptedKeyFilePath);
    when(exportCertificateResponse.certificate()).thenReturn(certAsString);
    when(exportCertificateResponse.privateKey()).thenReturn(encryptedKeyAsString);
    when(acmClient.exportCertificate(any(ExportCertificateRequest.class))).thenReturn(exportCertificateResponse);
    final Certificate certificate = acmCertificateProvider.getCertificate();
    assertThat(certificate.getCertificate(), is(certAsString));
    assertThat(certificate.getPrivateKey(), is(decryptedKeyAsString));
}
Also used : Path(java.nio.file.Path) ExportCertificateRequest(software.amazon.awssdk.services.acm.model.ExportCertificateRequest) Certificate(com.amazon.dataprepper.plugins.certificate.model.Certificate) Test(org.junit.jupiter.api.Test)

Example 9 with Certificate

use of com.amazon.dataprepper.plugins.certificate.model.Certificate in project data-prepper by opensearch-project.

the class ACMCertificateProviderTest method getACMCertificateWithUnencryptedPrivateKeySuccess.

@Test
public void getACMCertificateWithUnencryptedPrivateKeySuccess() throws IOException {
    final Path certFilePath = Path.of("data/certificate/test_cert.crt");
    final Path decryptedKeyFilePath = Path.of("data/certificate/test_decrypted_key.key");
    final String certAsString = Files.readString(certFilePath);
    final String decryptedKeyAsString = Files.readString(decryptedKeyFilePath);
    when(exportCertificateResponse.certificate()).thenReturn(certAsString);
    when(exportCertificateResponse.privateKey()).thenReturn(decryptedKeyAsString);
    when(acmClient.exportCertificate(any(ExportCertificateRequest.class))).thenReturn(exportCertificateResponse);
    final Certificate certificate = acmCertificateProvider.getCertificate();
    assertThat(certificate.getCertificate(), is(certAsString));
    assertThat(certificate.getPrivateKey(), is(decryptedKeyAsString));
}
Also used : Path(java.nio.file.Path) ExportCertificateRequest(software.amazon.awssdk.services.acm.model.ExportCertificateRequest) Certificate(com.amazon.dataprepper.plugins.certificate.model.Certificate) Test(org.junit.jupiter.api.Test)

Example 10 with Certificate

use of com.amazon.dataprepper.plugins.certificate.model.Certificate in project data-prepper by opensearch-project.

the class S3CertificateProviderTest method getCertificateValidKeyPathSuccess.

@Test
public void getCertificateValidKeyPathSuccess() {
    final String certificateContent = UUID.randomUUID().toString();
    final String privateKeyContent = UUID.randomUUID().toString();
    final String bucketName = UUID.randomUUID().toString();
    final String certificatePath = UUID.randomUUID().toString();
    final String privateKeyPath = UUID.randomUUID().toString();
    final String s3SslKeyCertChainFile = String.format("s3://%s/%s", bucketName, certificatePath);
    final String s3SslKeyFile = String.format("s3://%s/%s", bucketName, privateKeyPath);
    final InputStream certObjectStream = IOUtils.toInputStream(certificateContent, StandardCharsets.UTF_8);
    final ResponseInputStream certResponseInputStream = new ResponseInputStream<>(GetObjectResponse.builder().build(), AbortableInputStream.create(certObjectStream));
    final InputStream privateKeyObjectStream = IOUtils.toInputStream(privateKeyContent, StandardCharsets.UTF_8);
    final ResponseInputStream<GetObjectResponse> privateKeyResponseInputStream = new ResponseInputStream<>(GetObjectResponse.builder().build(), AbortableInputStream.create(privateKeyObjectStream));
    final GetObjectRequest certRequest = GetObjectRequest.builder().bucket(bucketName).key(certificatePath).build();
    when(s3Client.getObject(certRequest)).thenReturn(certResponseInputStream);
    final GetObjectRequest keyRequest = GetObjectRequest.builder().bucket(bucketName).key(privateKeyPath).build();
    when(s3Client.getObject(keyRequest)).thenReturn(privateKeyResponseInputStream);
    s3CertificateProvider = new S3CertificateProvider(s3Client, s3SslKeyCertChainFile, s3SslKeyFile);
    final Certificate certificate = s3CertificateProvider.getCertificate();
    assertThat(certificate.getCertificate(), is(certificateContent));
    assertThat(certificate.getPrivateKey(), is(privateKeyContent));
}
Also used : GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) AbortableInputStream(software.amazon.awssdk.http.AbortableInputStream) ResponseInputStream(software.amazon.awssdk.core.ResponseInputStream) InputStream(java.io.InputStream) ResponseInputStream(software.amazon.awssdk.core.ResponseInputStream) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) Certificate(com.amazon.dataprepper.plugins.certificate.model.Certificate) Test(org.junit.jupiter.api.Test)

Aggregations

Certificate (com.amazon.dataprepper.plugins.certificate.model.Certificate)11 CertificateProvider (com.amazon.dataprepper.plugins.certificate.CertificateProvider)4 ServerBuilder (com.linecorp.armeria.server.ServerBuilder)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Path (java.nio.file.Path)4 ExecutionException (java.util.concurrent.ExecutionException)4 Test (org.junit.jupiter.api.Test)4 HealthGrpcService (com.amazon.dataprepper.plugins.health.HealthGrpcService)3 GrpcServiceBuilder (com.linecorp.armeria.server.grpc.GrpcServiceBuilder)3 ServerInterceptor (io.grpc.ServerInterceptor)3 ExportCertificateRequest (software.amazon.awssdk.services.acm.model.ExportCertificateRequest)3 OTelProtoCodec (com.amazon.dataprepper.plugins.otel.codec.OTelProtoCodec)1 File (java.io.File)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)1 ResponseInputStream (software.amazon.awssdk.core.ResponseInputStream)1 AbortableInputStream (software.amazon.awssdk.http.AbortableInputStream)1 ExportCertificateResponse (software.amazon.awssdk.services.acm.model.ExportCertificateResponse)1