Skip to content

Commit

Permalink
use requestCertificate to simplify tests and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
emattheis committed Nov 12, 2024
1 parent eb5985f commit 638d8e9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 119 deletions.
51 changes: 19 additions & 32 deletions docs/modules/ROOT/pages/amazon-acm.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ After this, the `amazon-acm` extension has been added to your `pom.xml` as well

== Creating JSON REST service

In this example, we will create an application that allows us to import external certificates into ACM using a RESTful API.
In this example, we will create an application that allows us to request certificates for a particular domain using a RESTful API.
The example application will demonstrate the two programming models supported by the extension.

Let's start with the synchronous implementation in the `org.acme.acm.QuarkusAcmSyncResource` class.
Expand All @@ -90,12 +90,11 @@ Let's start with the synchronous implementation in the `org.acme.acm.QuarkusAcmS
package org.acme.acm;
import jakarta.inject.Inject;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.FormParam;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.services.acm.AcmClient;
@Path("/sync")
Expand All @@ -104,24 +103,17 @@ public class QuarkusAcmSyncResource {
@Inject <1>
AcmClient acm;
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String importCertificate(@FormParam("certificate") String certificate,
@FormParam("certificate_chain") String certificateChain,
@FormParam("private_key") String privateKey) {
return acm.importCertificate(r -> r
.certificate(SdkBytes.fromUtf8String(certificate))
.certificateChain(SdkBytes.fromUtf8String(certificateChain))
.privateKey(SdkBytes.fromUtf8String(privateKey)))
.certificateArn();
@GET
@Produces(MediaType.TEXT_PLAIN)
public String requestCertificate(@QueryParam("domainName") String domainName) {
return acm.requestCertificate(r -> r.domainName(domainName)).certificateArn();
}
}
----

<1> Inject the client provided by the amazon-acm extension

Using the Amazon ACM SDK, we can easily import an external certificate and expose the resulting ARN.
Using the Amazon ACM SDK, we can easily request a certificate for a domain and expose the resulting ARN.

== Configuring ACM clients

Expand Down Expand Up @@ -212,31 +204,26 @@ package org.acme.acm;
import io.smallrye.mutiny.Uni;
import jakarta.inject.Inject;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.FormParam;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.services.acm.AcmAsyncClient;
import software.amazon.awssdk.services.acm.model.ImportCertificateResponse;
import software.amazon.awssdk.services.acm.model.RequestCertificateResponse;
@Path("/async")
public class QuarkusAcmAsyncResource {
@Inject
AcmAsyncClient acm;
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Uni<String> importCertificate(@FormParam("certificate") String certificate,
@FormParam("certificate_chain") String certificateChain,
@FormParam("private_key") String privateKey) {
return Uni.createFrom().completionStage(acm.importCertificate(r -> r
.certificate(SdkBytes.fromUtf8String(certificate))
.certificateChain(SdkBytes.fromUtf8String(certificateChain))
.privateKey(SdkBytes.fromUtf8String(privateKey)))
.thenApply(ImportCertificateResponse::certificateArn));
@GET
@Produces(MediaType.TEXT_PLAIN)
public Uni<String> importCertificate(@QueryParam("domainName") String domainName) {
return Uni.createFrom().completionStage(acm
.requestCertificate(r -> r.domainName(domainName))
.thenApply(RequestCertificateResponse::certificateArn));
}
}
----
Expand Down
Original file line number Diff line number Diff line change
@@ -1,87 +1,26 @@
package io.quarkiverse.it.amazon.acm;

import static jakarta.ws.rs.core.MediaType.TEXT_PLAIN;

import java.util.concurrent.CompletionStage;

import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;

import org.jboss.logging.Logger;

import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.services.acm.AcmAsyncClient;
import software.amazon.awssdk.services.acm.AcmClient;
import software.amazon.awssdk.services.acm.model.CertificateDetail;
import software.amazon.awssdk.services.acm.model.DescribeCertificateResponse;
import software.amazon.awssdk.services.acm.model.ImportCertificateResponse;
import software.amazon.awssdk.services.acm.model.RequestCertificateResponse;

import java.util.concurrent.CompletionStage;

import static jakarta.ws.rs.core.MediaType.TEXT_PLAIN;

@Path("/acm")
public class AcmResource {

private static final Logger LOG = Logger.getLogger(AcmResource.class);

private static final SdkBytes CERTIFICATE = SdkBytes.fromUtf8String("""
-----BEGIN CERTIFICATE-----
MIIB1DCCAXmgAwIBAgIVAJ4BCcAB3Zrfhcv03ZqauYb2daL7MAoGCCqGSM49BAMCMDUxEDAOBgNV
BAMMB1N1YjIgQ0ExFDASBgNVBAoMC1F1YXJraXZlcnNlMQswCQYDVQQGEwJVUzAeFw0yNDExMDgy
MzMwNTFaFw0yNTAyMDgyMzMwNTFaMDsxCzAJBgNVBAYTAlVTMRQwEgYDVQQKDAtRdWFya2l2ZXJz
ZTEWMBQGA1UEAwwNcXVhcmt1cy5sb2NhbDBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABGMRoiHR
aK/1sd4bJPnjTs1gjUbJct/3XIDPT737imjVkdnnfcve6r11k67j2c7DiYXxF9KIxsEHda2YdFrB
e8qjYDBeMAwGA1UdEwEB/wQCMAAwDgYDVR0PAQH/BAQDAgWgMB0GA1UdDgQWBBQqMoBpbK9rf9Ek
76tZ9+Xbxz55yzAfBgNVHSMEGDAWgBShaN3eufhKUHU1DZdyZGGTYm82NjAKBggqhkjOPQQDAgNJ
ADBGAiEAqnQxdXcx6EMkzuYKpQ+73ZTZ6uyJoEBbx8ks+YqtB7oCIQCncKHDwBiMWpjoc2Ro8v3/
XErQqWADBMmNGW0PHrWT8A==
-----END CERTIFICATE-----
""");

private static final SdkBytes CERTIFICATE_CHAIN = SdkBytes.fromUtf8String("""
-----BEGIN CERTIFICATE-----
MIIBzzCCAXW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Af8wDgYDVR0PAQH/BAQDAgGGMB0GA1UdDgQWBBSvH2Ur4yyA3yEHlyip
EQCMccp+qzAfBgNVHSMEGDAWgBQkGPuBipJ3v2x3xW0gYR75aE051zAKBggqhkjOPQQDAgNJADBG
AiEAqQAFjolQzMi+98W2TjqKfbqzRQ9OJOnzhxrlwrR0+dkCIQCn0NMa1G1EsYLTbEYZX8p/EWEA
rXChXCbBQglhbfjMEA==
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
MIIBsDCCAVagAwIBAgIUM9MT9sXZgqxsTsxVCdv/L172UBkwCgYIKoZIzj0EAwIwNTEQMA4GA1UE
AwwHUm9vdCBDQTEUMBIGA1UECgwLUXVhcmtpdmVyc2UxCzAJBgNVBAYTAlVTMCAXDTI0MTEwODIz
MzA0OVoYDzIwNjQxMTA4MjMzMDQ5WjA1MRAwDgYDVQQDDAdSb290IENBMRQwEgYDVQQKDAtRdWFy
a2l2ZXJzZTELMAkGA1UEBhMCVVMwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAATxe12l+aMlorfh
gInjebEkAWLaekHd53gX3VaqkBcQuoje6JVfnveDDAh7nUa5PQHqTuPsX/VrTgL9tyY6YVdko0Iw
QDAPBgNVHRMBAf8EBTADAQH/MA4GA1UdDwEB/wQEAwIBhjAdBgNVHQ4EFgQUJBj7gYqSd79sd8Vt
IGEe+WhNOdcwCgYIKoZIzj0EAwIDSAAwRQIgWNCIdpPN8ixy//bYCRNq0wtQU7A+KZreFeZicPYR
ctgCIQDYqxLA8r2FWUhVZs+IcY2/qjUi6YZKcE2xPMdIHCoOKQ==
-----END CERTIFICATE-----
""");

private static final SdkBytes PRIVATE_KEY = SdkBytes.fromUtf8String("""
-----BEGIN EC PARAMETERS-----
BggqhkjOPQMBBw==
-----END EC PARAMETERS-----
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIJLWR1RfO2Hz8JmRQozr1qMdcKSnjK+qCufDgtbzq9xuoAoGCCqGSM49
AwEHoUQDQgAEYxGiIdFor/Wx3hsk+eNOzWCNRsly3/dcgM9PvfuKaNWR2ed9y97q
vXWTruPZzsOJhfEX0ojGwQd1rZh0WsF7yg==
-----END EC PRIVATE KEY-----
""");
private static final String DOMAIN_NAME = "quarkus.local";

@Inject
AcmClient acmClient;
Expand All @@ -94,17 +33,13 @@ public class AcmResource {
@Produces(TEXT_PLAIN)
public String testSync() {
LOG.info("Testing Sync ACM client");
// Install certificate
// Request certificate
var arn = acmClient
.importCertificate(r -> r
.certificate(CERTIFICATE)
.certificateChain(CERTIFICATE_CHAIN)
.privateKey(PRIVATE_KEY))
.requestCertificate(r -> r.domainName(DOMAIN_NAME))
.certificateArn();
// Get domain name
return acmClient
.describeCertificate(r -> r
.certificateArn(arn))
.describeCertificate(r -> r.certificateArn(arn))
.certificate()
.domainName();
}
Expand All @@ -114,16 +49,11 @@ public String testSync() {
@Produces(TEXT_PLAIN)
public CompletionStage<String> testAsync() {
LOG.info("Testing Async ACM client");
// Install certificate and get domain name
// Request certificate then get domain name
return acmAsyncClient
.importCertificate(r -> r
.certificate(CERTIFICATE)
.certificateChain(CERTIFICATE_CHAIN)
.privateKey(PRIVATE_KEY))
.thenApply(ImportCertificateResponse::certificateArn)
.thenCompose(arn -> acmAsyncClient
.describeCertificate(r -> r
.certificateArn(arn)))
.requestCertificate(r -> r.domainName(DOMAIN_NAME))
.thenApply(RequestCertificateResponse::certificateArn)
.thenCompose(arn -> acmAsyncClient.describeCertificate(r -> r.certificateArn(arn)))
.thenApply(DescribeCertificateResponse::certificate)
.thenApply(CertificateDetail::domainName);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package io.quarkiverse.it.amazon;

import static org.hamcrest.Matchers.is;

import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import org.junit.jupiter.api.Test;

import static org.hamcrest.Matchers.is;

@QuarkusTest
public class AmazonAcmTest {
Expand Down

0 comments on commit 638d8e9

Please sign in to comment.