mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(elbv2): reconcile security settings on listener protocol changes (#7598)
* fix(elbv2): reconcile security settings on listener protocol changes Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(elbv2): assign unique protocol transition test order Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(elbv2): align protocol transition CI fixtures Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> --------- Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>
This commit is contained in:
1 parent
cbb2db82d1
commit
56eedbece5
8 files changed
+353
-31
No files matched your search
+6
-1
@@ -21,6 +21,8 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
@DisplayName("ELBv2 default listener certificates")
|
||||
class ElbV2DefaultCertificateTest {
|
||||
|
||||
private static final String SSL_POLICY = "ELBSecurityPolicy-2016-08";
|
||||
|
||||
@Test
|
||||
@DisplayName("HTTPS needs a default before listener creation or protocol replacement")
|
||||
void missingHttpsDefaultDoesNotPublishOrChangeAListener() {
|
||||
@@ -44,14 +46,17 @@ class ElbV2DefaultCertificateTest {
|
||||
assertThat(certificates(elb, https)).containsExactly(certificate);
|
||||
String http = createListener(elb, lbArn, "HTTP", 8080, null);
|
||||
InvalidConfigurationRequestException modification = assertThrows(InvalidConfigurationRequestException.class,
|
||||
() -> elb.modifyListener(b -> b.listenerArn(http).protocol("HTTPS").port(8443)));
|
||||
() -> elb.modifyListener(b -> b.listenerArn(http).protocol("HTTPS").port(8443)
|
||||
.sslPolicy(SSL_POLICY)));
|
||||
assertThat(modification.statusCode()).isEqualTo(400);
|
||||
assertThat(modification.getMessage()).contains("default certificate");
|
||||
Listener unchanged = elb.describeListeners(b -> b.listenerArns(http)).listeners().get(0);
|
||||
assertThat(unchanged.protocolAsString()).isEqualTo("HTTP");
|
||||
assertThat(unchanged.port()).isEqualTo(8080);
|
||||
assertThat(unchanged.certificates()).isEmpty();
|
||||
assertThat(unchanged.hasCertificates()).isFalse();
|
||||
elb.modifyListener(b -> b.listenerArn(http).protocol("HTTPS").port(8443)
|
||||
.sslPolicy(SSL_POLICY)
|
||||
.certificates(Certificate.builder().certificateArn(certificate).build()));
|
||||
assertThat(certificates(elb, http)).containsExactly(certificate);
|
||||
} finally {
|
||||
|
||||
@@ -50,12 +50,17 @@ HTTPS listeners require a default certificate. Supply one when creating a listen
|
||||
Listener responses omit `Certificates` when the listener has none. Configured certificates
|
||||
remain present in create, describe and modify responses.
|
||||
|
||||
Creating an HTTPS listener without `SslPolicy` selects `ELBSecurityPolicy-2016-08`,
|
||||
Creating an HTTPS or TLS listener without `SslPolicy` selects `ELBSecurityPolicy-2016-08`,
|
||||
matching AWS's default for API, CLI and CloudFormation creation. An explicit policy
|
||||
is preserved, and omitting the policy on subsequent modifications retains its current
|
||||
value. These policies remain present in create, describe and modify responses;
|
||||
listeners with no policy, such as HTTP listeners, omit `SslPolicy`.
|
||||
|
||||
Changing HTTPS to HTTP or TLS to TCP removes the SSL policy and certificate configuration.
|
||||
Changing HTTP to HTTPS or TCP to TLS requires an explicit SSL policy and default certificate;
|
||||
missing either rejects the modification without changing the listener. Changing other
|
||||
properties without changing protocol retains the existing security settings.
|
||||
|
||||
Gateway Load Balancer listeners require omitted `Port` and `Protocol` fields on both create and modify requests. Their portless control-plane configuration does not provide GENEVE packet forwarding.
|
||||
|
||||
| Action | Description |
|
||||
|
||||
@@ -45,7 +45,7 @@ import java.util.stream.Collectors;
|
||||
public class ElbV2Service implements ResourceProvider, ServerCertificateReferenceProvider, VpcDependencyProvider {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(ElbV2Service.class);
|
||||
private static final String DEFAULT_HTTPS_SSL_POLICY = "ELBSecurityPolicy-2016-08";
|
||||
private static final String DEFAULT_SSL_POLICY = "ELBSecurityPolicy-2016-08";
|
||||
/** {@code LoadBalancerTypeEnum}, in the model's order. */
|
||||
private static final List<String> LOAD_BALANCER_TYPES = List.of("application", "network", "gateway");
|
||||
|
||||
@@ -665,8 +665,8 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc
|
||||
listener.setLoadBalancerArn(lbArn);
|
||||
listener.setPort(port);
|
||||
listener.setProtocol(protocol != null ? protocol : ("gateway".equals(lbType) ? "GENEVE" : "HTTP"));
|
||||
listener.setSslPolicy(sslPolicy == null && "HTTPS".equalsIgnoreCase(listener.getProtocol())
|
||||
? DEFAULT_HTTPS_SSL_POLICY : sslPolicy);
|
||||
listener.setSslPolicy(sslPolicy == null && servesCertificates(listener.getProtocol())
|
||||
? DEFAULT_SSL_POLICY : sslPolicy);
|
||||
listener.setCertificates(certificates != null ? new ArrayList<>(certificates) : new ArrayList<>());
|
||||
listener.setDefaultActions(defaultActions != null ? new ArrayList<>(defaultActions) : new ArrayList<>());
|
||||
listener.setAlpnPolicy(alpnPolicy != null ? new ArrayList<>(alpnPolicy) : new ArrayList<>());
|
||||
@@ -811,9 +811,11 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc
|
||||
ListenerState initialState = listenerState(initial, initialLb);
|
||||
requireAvailableListenerPort(listeners.getOrDefault(region, Map.of()), initial.getLoadBalancerArn(),
|
||||
listenerArn, port != null ? port : initialState.port());
|
||||
if (validateExistingListener(region, initialState, initialLb, () ->
|
||||
requireListenerCertificates(region, protocol != null ? protocol : initialState.protocol(),
|
||||
certificates != null ? certificates : initialState.certificates()))) {
|
||||
if (validateExistingListener(region, initialState, initialLb, () -> {
|
||||
requireListenerProtocolTransition(initialState.protocol(), protocol, sslPolicy, certificates);
|
||||
requireListenerCertificates(region, protocol != null ? protocol : initialState.protocol(),
|
||||
certificates != null ? certificates : initialState.certificates());
|
||||
})) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -822,9 +824,11 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc
|
||||
LoadBalancer lb = requireLoadBalancer(region, listener.getLoadBalancerArn());
|
||||
ListenerState expected = listenerState(listener, lb);
|
||||
Listener prospective = prospectiveListener(expected, protocol, port, certificates);
|
||||
boolean clearSecuritySettings = clearsSecuritySettings(expected.protocol(), protocol);
|
||||
requireAvailableListenerPort(listeners.getOrDefault(region, Map.of()), listener.getLoadBalancerArn(),
|
||||
listenerArn, prospective.getPort());
|
||||
if (!validateExistingListener(region, expected, lb, () -> {
|
||||
requireListenerProtocolTransition(expected.protocol(), protocol, sslPolicy, certificates);
|
||||
requireListenerCertificates(region, prospective.getProtocol(), prospective.getCertificates());
|
||||
requireCompatibleListenerProtocol(listenerArn, prospective.getPort(), prospective.getProtocol());
|
||||
})) {
|
||||
@@ -844,11 +848,13 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc
|
||||
if (protocol != null) {
|
||||
listener.setProtocol(protocol);
|
||||
}
|
||||
if (sslPolicy != null) {
|
||||
if (clearSecuritySettings) {
|
||||
listener.setSslPolicy(null);
|
||||
} else if (sslPolicy != null) {
|
||||
listener.setSslPolicy(sslPolicy);
|
||||
}
|
||||
if (certificates != null) {
|
||||
listener.setCertificates(new ArrayList<>(certificates));
|
||||
if (certificates != null || clearSecuritySettings) {
|
||||
listener.setCertificates(new ArrayList<>(prospective.getCertificates()));
|
||||
}
|
||||
if (alpnPolicy != null) {
|
||||
listener.setAlpnPolicy(new ArrayList<>(alpnPolicy));
|
||||
@@ -1270,10 +1276,34 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc
|
||||
prospective.setLoadBalancerArn(expected.listener().getLoadBalancerArn());
|
||||
prospective.setPort(port != null ? port : expected.port());
|
||||
prospective.setProtocol(protocol != null ? protocol : expected.protocol());
|
||||
prospective.setCertificates(new ArrayList<>(certificates != null ? certificates : expected.certificates()));
|
||||
prospective.setCertificates(clearsSecuritySettings(expected.protocol(), protocol) ? new ArrayList<>()
|
||||
: new ArrayList<>(certificates != null ? certificates : expected.certificates()));
|
||||
return prospective;
|
||||
}
|
||||
|
||||
private static boolean clearsSecuritySettings(String currentProtocol, String newProtocol) {
|
||||
return ("HTTPS".equalsIgnoreCase(currentProtocol) && "HTTP".equalsIgnoreCase(newProtocol))
|
||||
|| ("TLS".equalsIgnoreCase(currentProtocol) && "TCP".equalsIgnoreCase(newProtocol));
|
||||
}
|
||||
|
||||
private static void requireListenerProtocolTransition(String currentProtocol, String newProtocol,
|
||||
String sslPolicy, List<String> certificates) {
|
||||
boolean requiresSecuritySettings =
|
||||
("HTTP".equalsIgnoreCase(currentProtocol) && "HTTPS".equalsIgnoreCase(newProtocol))
|
||||
|| ("TCP".equalsIgnoreCase(currentProtocol) && "TLS".equalsIgnoreCase(newProtocol));
|
||||
if (!requiresSecuritySettings) {
|
||||
return;
|
||||
}
|
||||
if (certificates == null || certificates.isEmpty()) {
|
||||
throw new AwsException("InvalidConfigurationRequest",
|
||||
"A default certificate is required when changing the listener protocol to " + newProtocol + ".", 400);
|
||||
}
|
||||
if (sslPolicy == null) {
|
||||
throw new AwsException("InvalidConfigurationRequest",
|
||||
"An SSL policy is required when changing the listener protocol to " + newProtocol + ".", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private void requireCanonicalLoadBalancer(String region, LoadBalancer lb) {
|
||||
if (loadBalancers.getOrDefault(region, Map.of()).get(lb.getLoadBalancerArn()) != lb) {
|
||||
throw new AwsException("LoadBalancerNotFound", "One or more load balancers not found.", 400);
|
||||
|
||||
+7
-4
@@ -41,6 +41,7 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@@ -142,7 +143,8 @@ class ElbV2CertificatePublicationServiceTest {
|
||||
null, 9090, null, null, null, null));
|
||||
assertEquals("HTTP", moved.getProtocol());
|
||||
assertEquals(9090, moved.getPort());
|
||||
assertEquals(List.of(HELD_CERTIFICATE), moved.getCertificates());
|
||||
assertTrue(moved.getCertificates().isEmpty());
|
||||
assertNull(moved.getSslPolicy());
|
||||
assertFalse(first.get());
|
||||
}
|
||||
|
||||
@@ -239,19 +241,20 @@ class ElbV2CertificatePublicationServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void modifyListenerProtocolAloneTakesTheGuardForTheCertificatesItPutsIntoService() {
|
||||
void modifyListenerProtocolTakesTheGuardForTheCertificatesItPutsIntoService() {
|
||||
String lbArn = service.createLoadBalancer(
|
||||
REGION, "sample-lb", "internal", "application", "ipv4",
|
||||
ALB_SUBNETS, List.of("sg-a"), Map.of()).getLoadBalancerArn();
|
||||
String tgArn = createTargetGroup("sample-tg");
|
||||
String listenerArn = service.createListener(
|
||||
REGION, lbArn, "HTTP", 80, null, List.of(HELD_CERTIFICATE),
|
||||
REGION, lbArn, "HTTP", 80, null, List.of(),
|
||||
List.of(forwardAction(tgArn)), List.of(), Map.of()).getListenerArn();
|
||||
clearInvocations(dataPlane);
|
||||
service.certificateResolver = deletedOnTheSecondLook();
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () -> service.modifyListener(
|
||||
REGION, listenerArn, "HTTPS", null, null, null, null, null));
|
||||
REGION, listenerArn, "HTTPS", null, "ELBSecurityPolicy-2016-08",
|
||||
List.of(HELD_CERTIFICATE), null, null));
|
||||
|
||||
assertEquals("CertificateNotFound", e.getErrorCode());
|
||||
assertEquals("HTTP", service.describeListeners(REGION, lbArn, List.of(listenerArn))
|
||||
|
||||
+53
-1
@@ -418,6 +418,7 @@ class ElbV2HttpsDataPlaneIntegrationTest {
|
||||
() -> assertPlainResponse(loadBalancer.dnsName(), HTTPS_PORT, "original"));
|
||||
given().header("Authorization", AUTH).formParam("Action", "ModifyListener")
|
||||
.formParam("ListenerArn", listener).formParam("Protocol", "HTTPS")
|
||||
.formParam("SslPolicy", "ELBSecurityPolicy-2016-08")
|
||||
.formParam("Certificates.member.1.CertificateArn", certificate.getArn())
|
||||
.when().post("/").then().statusCode(200);
|
||||
HttpsResult retried = awaitHttps(loadBalancer.dnsName(), certificate);
|
||||
@@ -539,6 +540,7 @@ class ElbV2HttpsDataPlaneIntegrationTest {
|
||||
awaitListenerPortClosed();
|
||||
given().header("Authorization", AUTH).formParam("Action", "ModifyListener")
|
||||
.formParam("ListenerArn", firstArn).formParam("Protocol", "HTTPS").formParam("Port", HTTPS_PORT)
|
||||
.formParam("SslPolicy", "ELBSecurityPolicy-2016-08")
|
||||
.formParam("Certificates.member.1.CertificateArn", certificate.getArn())
|
||||
.when().post("/").then().statusCode(200);
|
||||
assertEquals("original", awaitHttps(first.dnsName(), certificate).body());
|
||||
@@ -742,7 +744,8 @@ class ElbV2HttpsDataPlaneIntegrationTest {
|
||||
}
|
||||
|
||||
private static void awaitPlainResponse(String host, int port, String body) {
|
||||
await().atMost(Duration.ofSeconds(10)).pollInterval(Duration.ofMillis(100)).untilAsserted(() ->
|
||||
await().ignoreException(ConnectException.class)
|
||||
.atMost(Duration.ofSeconds(10)).pollInterval(Duration.ofMillis(100)).untilAsserted(() ->
|
||||
assertPlainResponse(host, port, body));
|
||||
}
|
||||
|
||||
@@ -854,6 +857,7 @@ class ElbV2HttpsDataPlaneIntegrationTest {
|
||||
Response rejected = given().header("Authorization", AUTH)
|
||||
.formParam("Action", "ModifyListener").formParam("ListenerArn", firstListener)
|
||||
.formParam("Protocol", "HTTPS")
|
||||
.formParam("SslPolicy", "ELBSecurityPolicy-2016-08")
|
||||
.formParam("Certificates.member.1.CertificateArn", certificate.getArn())
|
||||
.when().post("/");
|
||||
assertTrue(rejected.statusCode() >= 400);
|
||||
@@ -872,6 +876,54 @@ class ElbV2HttpsDataPlaneIntegrationTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(11)
|
||||
void protocolChangesPublishSecuritySettingsTogetherWithTheRuntime() throws Exception {
|
||||
LoadBalancerRef first = createLoadBalancer("protocol-first");
|
||||
LoadBalancerRef second = createLoadBalancer("protocol-second");
|
||||
Certificate certificate = requestCertificate(first.dnsName());
|
||||
String firstListener = null;
|
||||
String secondListener = null;
|
||||
try {
|
||||
firstListener = createFixedResponseListener(first.arn(), "HTTPS", HTTPS_PORT, certificate.getArn(), "first");
|
||||
secondListener = createFixedResponseListener(second.arn(), "HTTPS", HTTPS_PORT, certificate.getArn(), "second");
|
||||
given().header("Authorization", AUTH).formParam("Action", "ModifyListener")
|
||||
.formParam("ListenerArn", firstListener).formParam("Protocol", "HTTP")
|
||||
.when().post("/").then().statusCode(400);
|
||||
describeHttpsListener(firstListener, certificate.getArn());
|
||||
assertEquals("first", awaitHttps(first.dnsName(), certificate).body());
|
||||
|
||||
deleteListener(secondListener);
|
||||
secondListener = null;
|
||||
given().header("Authorization", AUTH).formParam("Action", "ModifyListener")
|
||||
.formParam("ListenerArn", firstListener).formParam("Protocol", "HTTP")
|
||||
.when().post("/").then().statusCode(200);
|
||||
awaitPlainResponse(first.dnsName(), HTTPS_PORT, "first");
|
||||
assertAcmConsumers(certificate.getArn(), List.of());
|
||||
|
||||
given().header("Authorization", AUTH).formParam("Action", "ModifyListener")
|
||||
.formParam("ListenerArn", firstListener).formParam("Protocol", "HTTPS")
|
||||
.formParam("Certificates.member.1.CertificateArn", certificate.getArn())
|
||||
.when().post("/").then().statusCode(400);
|
||||
assertPlainResponse(first.dnsName(), HTTPS_PORT, "first");
|
||||
given().header("Authorization", AUTH).formParam("Action", "ModifyListener")
|
||||
.formParam("ListenerArn", firstListener).formParam("Protocol", "HTTPS")
|
||||
.formParam("SslPolicy", "ELBSecurityPolicy-TLS13-1-2-2021-06")
|
||||
.formParam("Certificates.member.1.CertificateArn", certificate.getArn())
|
||||
.when().post("/").then().statusCode(200);
|
||||
describeHttpsListener(firstListener, certificate.getArn());
|
||||
HttpsResult restored = awaitHttps(first.dnsName(), certificate);
|
||||
assertEquals("first", restored.body());
|
||||
assertPeerCertificate(restored.session(), certificate, first.dnsName());
|
||||
} finally {
|
||||
deleteListener(firstListener);
|
||||
deleteListener(secondListener);
|
||||
deleteLoadBalancer(first.arn());
|
||||
deleteLoadBalancer(second.arn());
|
||||
acmService.deleteCertificate(certificate.getArn(), REGION);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void rejectedDefaultCertificateRemovalPreservesTlsListener() throws Exception {
|
||||
|
||||
+9
-6
@@ -235,6 +235,7 @@ class ElbV2ListenerCertificateIntegrationTest {
|
||||
() -> assertTrue(listenerCertificates(listener).isEmpty()));
|
||||
given().header("Authorization", ELB_AUTH).formParam("Action", "ModifyListener")
|
||||
.formParam("ListenerArn", listener).formParam("Protocol", "HTTPS").formParam("Port", "444")
|
||||
.formParam("SslPolicy", "ELBSecurityPolicy-2016-08")
|
||||
.formParam("Certificates.member.1.CertificateArn", certificate)
|
||||
.when().post("/").then().statusCode(200);
|
||||
assertEquals("HTTPS", listenerField(listener, "Protocol"));
|
||||
@@ -345,16 +346,15 @@ class ElbV2ListenerCertificateIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void turningAnHttpListenerIntoAnHttpsOneChecksTheCertificateItAlreadyHeld() {
|
||||
// An HTTP listener may carry a certificate nothing resolves, because the check does not
|
||||
// cover its protocol. Serving it is what the check covers, and the request that starts
|
||||
// serving it names no certificate of its own, so the one on file is the one to resolve.
|
||||
String listenerArn = listenerServing("HTTP", MISSING_IAM_CERTIFICATE);
|
||||
void turningAnHttpListenerIntoAnHttpsOneChecksTheSuppliedCertificate() {
|
||||
String listenerArn = listenerServing("HTTP", null);
|
||||
|
||||
given().header("Authorization", ELB_AUTH)
|
||||
.formParam("Action", "ModifyListener")
|
||||
.formParam("ListenerArn", listenerArn)
|
||||
.formParam("Protocol", "HTTPS")
|
||||
.formParam("SslPolicy", "ELBSecurityPolicy-2016-08")
|
||||
.formParam("Certificates.member.1.CertificateArn", MISSING_IAM_CERTIFICATE)
|
||||
.when().post("/").then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("CertificateNotFound"));
|
||||
@@ -364,12 +364,15 @@ class ElbV2ListenerCertificateIntegrationTest {
|
||||
|
||||
@Test
|
||||
void turningAnHttpListenerIntoAnHttpsOneIsAllowedWhenTheCertificateExists() {
|
||||
String listenerArn = listenerServing("HTTP", uploadCertificate());
|
||||
String certificate = uploadCertificate();
|
||||
String listenerArn = listenerServing("HTTP", null);
|
||||
|
||||
given().header("Authorization", ELB_AUTH)
|
||||
.formParam("Action", "ModifyListener")
|
||||
.formParam("ListenerArn", listenerArn)
|
||||
.formParam("Protocol", "HTTPS")
|
||||
.formParam("SslPolicy", "ELBSecurityPolicy-2016-08")
|
||||
.formParam("Certificates.member.1.CertificateArn", certificate)
|
||||
.when().post("/").then().statusCode(200);
|
||||
|
||||
assertEquals("HTTPS", listenerField(listenerArn, "Protocol"));
|
||||
|
||||
+195
@@ -0,0 +1,195 @@
|
||||
package io.github.hectorvent.floci.services.elbv2;
|
||||
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2Service;
|
||||
import io.github.hectorvent.floci.testing.AwsTestClients;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.acm.AcmClient;
|
||||
import software.amazon.awssdk.services.elasticloadbalancingv2.ElasticLoadBalancingV2Client;
|
||||
import software.amazon.awssdk.services.elasticloadbalancingv2.model.Certificate;
|
||||
import software.amazon.awssdk.services.elasticloadbalancingv2.model.ElasticLoadBalancingV2Exception;
|
||||
import software.amazon.awssdk.services.elasticloadbalancingv2.model.Listener;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.github.hectorvent.floci.testing.QueryRequests.request;
|
||||
import static org.junit.jupiter.api.Assertions.assertAll;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
class ElbV2ListenerProtocolIntegrationTest {
|
||||
|
||||
private static final String ACCOUNT = "000000000000";
|
||||
private static final Region REGION = Region.US_EAST_1;
|
||||
private static final String SSL_POLICY = "ELBSecurityPolicy-TLS13-1-2-2021-06";
|
||||
private ElasticLoadBalancingV2Client elb;
|
||||
private AcmClient acm;
|
||||
private String loadBalancer;
|
||||
private String targetGroup;
|
||||
private String certificate;
|
||||
|
||||
@BeforeEach
|
||||
void createClients() {
|
||||
elb = AwsTestClients.create(ElasticLoadBalancingV2Client.builder(), ACCOUNT, REGION);
|
||||
acm = AwsTestClients.create(AcmClient.builder(), ACCOUNT, REGION);
|
||||
certificate = acm.requestCertificate(request -> request.domainName("listener-protocol.example.test")
|
||||
.validationMethod("DNS")).certificateArn();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void removeResources() {
|
||||
try (ElasticLoadBalancingV2Client ownedElb = elb; AcmClient ownedAcm = acm) {
|
||||
assertAll("Remove listeners before their certificate",
|
||||
() -> {
|
||||
if (loadBalancer != null) {
|
||||
ownedElb.deleteLoadBalancer(request -> request.loadBalancerArn(loadBalancer));
|
||||
}
|
||||
},
|
||||
() -> {
|
||||
if (targetGroup != null) {
|
||||
ownedElb.deleteTargetGroup(request -> request.targetGroupArn(targetGroup));
|
||||
}
|
||||
},
|
||||
() -> {
|
||||
if (certificate != null) {
|
||||
ownedAcm.deleteCertificate(request -> request.certificateArn(certificate));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void tlsCreationDefaultsSslPolicyAndRetainsExplicitReplacement() {
|
||||
createLoadBalancer("network");
|
||||
Listener created = createListener("TLS", null, certificate);
|
||||
Listener described = describe(created.listenerArn());
|
||||
Listener modified = elb.modifyListener(request -> request.listenerArn(created.listenerArn()).port(8443))
|
||||
.listeners().getFirst();
|
||||
assertAll("TLS creation uses the documented API default",
|
||||
() -> assertEquals("ELBSecurityPolicy-2016-08", created.sslPolicy()),
|
||||
() -> assertEquals("ELBSecurityPolicy-2016-08", described.sslPolicy()),
|
||||
() -> assertEquals("ELBSecurityPolicy-2016-08", modified.sslPolicy()),
|
||||
() -> assertTrue(describeXml(created.listenerArn())
|
||||
.contains("<SslPolicy>ELBSecurityPolicy-2016-08</SslPolicy>")));
|
||||
|
||||
Listener replaced = elb.modifyListener(request -> request.listenerArn(created.listenerArn())
|
||||
.sslPolicy(SSL_POLICY)).listeners().getFirst();
|
||||
Listener retained = elb.modifyListener(request -> request.listenerArn(created.listenerArn()).port(9443))
|
||||
.listeners().getFirst();
|
||||
assertEquals(SSL_POLICY, replaced.sslPolicy());
|
||||
assertEquals(SSL_POLICY, retained.sslPolicy());
|
||||
assertEquals(List.of(certificate), retained.certificates().stream().map(Certificate::certificateArn).toList());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"application, HTTPS, HTTP", "network, TLS, TCP"})
|
||||
void changingToPlainProtocolDropsSecuritySettingsAndCertificateOwnership(
|
||||
String type, String encrypted, String plain) {
|
||||
createLoadBalancer(type);
|
||||
Listener created = createListener(encrypted, SSL_POLICY, certificate);
|
||||
Listener modified = elb.modifyListener(request -> request.listenerArn(created.listenerArn()).protocol(plain))
|
||||
.listeners().getFirst();
|
||||
Listener described = describe(created.listenerArn());
|
||||
String xml = describeXml(created.listenerArn());
|
||||
|
||||
assertAll("The plain listener has no TLS configuration",
|
||||
() -> assertEquals(plain, modified.protocolAsString()),
|
||||
() -> assertNull(modified.sslPolicy()),
|
||||
() -> assertFalse(modified.hasCertificates()),
|
||||
() -> assertNull(described.sslPolicy()),
|
||||
() -> assertFalse(described.hasCertificates()),
|
||||
() -> assertFalse(xml.contains("<SslPolicy"), xml),
|
||||
() -> assertFalse(xml.contains("<Certificates"), xml),
|
||||
() -> assertFalse(elb.describeListenerCertificates(request -> request.listenerArn(created.listenerArn()))
|
||||
.hasCertificates()),
|
||||
() -> assertTrue(acm.describeCertificate(request -> request.certificateArn(certificate))
|
||||
.certificate().inUseBy().isEmpty()));
|
||||
acm.deleteCertificate(request -> request.certificateArn(certificate));
|
||||
certificate = null;
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"application, HTTP, HTTPS", "network, TCP, TLS"})
|
||||
void changingToEncryptedProtocolRequiresBothSettingsBeforeMutation(
|
||||
String type, String plain, String encrypted) {
|
||||
createLoadBalancer(type);
|
||||
Listener created = createListener(plain, null, null);
|
||||
String arn = created.listenerArn();
|
||||
ElasticLoadBalancingV2Exception missingCertificate = assertThrows(ElasticLoadBalancingV2Exception.class,
|
||||
() -> elb.modifyListener(request -> request.listenerArn(arn).protocol(encrypted).sslPolicy(SSL_POLICY)));
|
||||
assertEquals("InvalidConfigurationRequest", missingCertificate.awsErrorDetails().errorCode());
|
||||
assertTrue(missingCertificate.getMessage().contains("certificate"));
|
||||
ElasticLoadBalancingV2Exception missingPolicy = assertThrows(ElasticLoadBalancingV2Exception.class,
|
||||
() -> elb.modifyListener(request -> request.listenerArn(arn).protocol(encrypted)
|
||||
.certificates(Certificate.builder().certificateArn(certificate).build())));
|
||||
assertEquals("InvalidConfigurationRequest", missingPolicy.awsErrorDetails().errorCode());
|
||||
assertTrue(missingPolicy.getMessage().contains("policy"));
|
||||
|
||||
Listener unchanged = describe(arn);
|
||||
assertAll("Refused changes preserve the plain listener",
|
||||
() -> assertEquals(plain, unchanged.protocolAsString()),
|
||||
() -> assertNull(unchanged.sslPolicy()),
|
||||
() -> assertFalse(unchanged.hasCertificates()),
|
||||
() -> assertTrue(acm.describeCertificate(request -> request.certificateArn(certificate))
|
||||
.certificate().inUseBy().isEmpty()));
|
||||
Listener upgraded = elb.modifyListener(request -> request.listenerArn(arn).protocol(encrypted)
|
||||
.sslPolicy(SSL_POLICY).certificates(Certificate.builder().certificateArn(certificate).build()))
|
||||
.listeners().getFirst();
|
||||
Listener retained = elb.modifyListener(request -> request.listenerArn(arn).port(8443))
|
||||
.listeners().getFirst();
|
||||
assertEquals(encrypted, upgraded.protocolAsString());
|
||||
assertEquals(SSL_POLICY, upgraded.sslPolicy());
|
||||
assertEquals(SSL_POLICY, retained.sslPolicy());
|
||||
assertEquals(List.of(certificate), retained.certificates().stream().map(Certificate::certificateArn).toList());
|
||||
|
||||
elb.modifyListener(request -> request.listenerArn(arn).protocol(plain));
|
||||
ElasticLoadBalancingV2Exception missingSettings = assertThrows(ElasticLoadBalancingV2Exception.class,
|
||||
() -> elb.modifyListener(request -> request.listenerArn(arn).protocol(encrypted)));
|
||||
assertEquals("InvalidConfigurationRequest", missingSettings.awsErrorDetails().errorCode());
|
||||
assertEquals(plain, describe(arn).protocolAsString());
|
||||
}
|
||||
|
||||
private void createLoadBalancer(String type) {
|
||||
loadBalancer = elb.createLoadBalancer(request -> request.name(uniqueName("protocol-lb")).type(type)
|
||||
.subnets(Ec2Service.defaultSubnetId(REGION.id(), "a"),
|
||||
Ec2Service.defaultSubnetId(REGION.id(), "b")))
|
||||
.loadBalancers().getFirst().loadBalancerArn();
|
||||
targetGroup = elb.createTargetGroup(request -> request.name(uniqueName("protocol-tg"))
|
||||
.targetType("ip").protocol(type.equals("network") ? "TCP" : "HTTP").port(80)
|
||||
.vpcId(Ec2Service.defaultVpcId(REGION.id()))).targetGroups().getFirst().targetGroupArn();
|
||||
}
|
||||
|
||||
private Listener createListener(String protocol, String sslPolicy, String certificateArn) {
|
||||
return elb.createListener(request -> {
|
||||
request.loadBalancerArn(loadBalancer).protocol(protocol).port(443).sslPolicy(sslPolicy)
|
||||
.defaultActions(action -> action.type("forward").targetGroupArn(targetGroup));
|
||||
if (certificateArn != null) {
|
||||
request.certificates(Certificate.builder().certificateArn(certificateArn).build());
|
||||
}
|
||||
}).listeners().getFirst();
|
||||
}
|
||||
|
||||
private Listener describe(String arn) {
|
||||
return elb.describeListeners(request -> request.listenerArns(arn)).listeners().getFirst();
|
||||
}
|
||||
|
||||
private static String describeXml(String arn) {
|
||||
return request(ACCOUNT, "elasticloadbalancing", "DescribeListeners")
|
||||
.formParam("Version", "2015-12-01").formParam("ListenerArns.member.1", arn)
|
||||
.post("/").then().statusCode(200).extract().asString();
|
||||
}
|
||||
|
||||
private static String uniqueName(String prefix) {
|
||||
return prefix + "-" + UUID.randomUUID().toString().substring(0, 12);
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,6 @@ import org.junit.jupiter.api.io.TempDir;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.NullAndEmptySource;
|
||||
import org.junit.jupiter.params.provider.NullSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
@@ -396,7 +395,7 @@ class ElbV2ServiceTest {
|
||||
assertEquals("HTTP", listener.getProtocol());
|
||||
assertEquals(9080, listener.getPort());
|
||||
assertEquals(List.of(), listener.getCertificates());
|
||||
service.modifyListener(REGION, listener.getListenerArn(), "HTTPS", 9443, null,
|
||||
service.modifyListener(REGION, listener.getListenerArn(), "HTTPS", 9443, "ELBSecurityPolicy-2016-08",
|
||||
List.of(HELD_CERTIFICATE), null, null);
|
||||
assertEquals("HTTPS", listener.getProtocol());
|
||||
assertEquals(List.of(HELD_CERTIFICATE), listener.getCertificates());
|
||||
@@ -600,18 +599,18 @@ class ElbV2ServiceTest {
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@NullSource
|
||||
@ValueSource(strings = "ELBSecurityPolicy-TLS13-1-2-2021-06")
|
||||
void initializeStorageRestoresHttpsListenerCertificateAndSslPolicy(String sslPolicy) {
|
||||
@CsvSource({"HTTPS,", "HTTPS, ELBSecurityPolicy-TLS13-1-2-2021-06",
|
||||
"TLS,", "TLS, ELBSecurityPolicy-TLS13-1-2-2021-06"})
|
||||
void initializeStorageRestoresEncryptedListenerCertificateAndSslPolicy(String protocol, String sslPolicy) {
|
||||
SharedStorageFactory storageFactory = new SharedStorageFactory();
|
||||
ElbV2Service first = serviceWithStorage(
|
||||
storageFactory, mock(ElbV2DataPlane.class), mock(ElbV2HealthChecker.class));
|
||||
String lbArn = first.createLoadBalancer(
|
||||
REGION, "persisted-https-lb", "internal", "application", "ipv4",
|
||||
REGION, "persisted-https-lb", "internal", protocol.equals("TLS") ? "network" : "application", "ipv4",
|
||||
ALB_SUBNETS, List.of("sg-a"), Map.of()).getLoadBalancerArn();
|
||||
String certificateArn = "arn:aws:acm:us-west-2:000000000000:certificate/persisted";
|
||||
String listenerArn = first.createListener(
|
||||
REGION, lbArn, "HTTPS", 9443, sslPolicy,
|
||||
REGION, lbArn, protocol, 9443, sslPolicy,
|
||||
List.of(certificateArn), List.of(), List.of(), Map.of()).getListenerArn();
|
||||
|
||||
ElbV2DataPlane reloadedDataPlane = mock(ElbV2DataPlane.class);
|
||||
@@ -621,13 +620,43 @@ class ElbV2ServiceTest {
|
||||
ArgumentCaptor<Listener> listenerCaptor = ArgumentCaptor.captor();
|
||||
verify(reloadedDataPlane).publishListener(listenerCaptor.capture(), eq(REGION), any(), any(), any(), any());
|
||||
assertEquals(listenerArn, listenerCaptor.getValue().getListenerArn());
|
||||
assertEquals("HTTPS", listenerCaptor.getValue().getProtocol());
|
||||
assertEquals(protocol, listenerCaptor.getValue().getProtocol());
|
||||
assertEquals(List.of(certificateArn), listenerCaptor.getValue().getCertificates());
|
||||
Listener described = reloaded.describeListeners(REGION, lbArn, List.of(listenerArn)).getFirst();
|
||||
assertEquals(sslPolicy == null ? "ELBSecurityPolicy-2016-08" : sslPolicy,
|
||||
described.getSslPolicy());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"HTTPS, HTTP, application", "TLS, TCP, network"})
|
||||
void storageReloadPreservesRemovedAndReplacedListenerSecuritySettings(
|
||||
String encrypted, String plain, String type) {
|
||||
SharedStorageFactory storageFactory = new SharedStorageFactory();
|
||||
ElbV2Service first = serviceWithStorage(
|
||||
storageFactory, mock(ElbV2DataPlane.class), mock(ElbV2HealthChecker.class));
|
||||
String lb = first.createLoadBalancer(REGION, "protocol-reload", "internal", type, "ipv4",
|
||||
ALB_SUBNETS, List.of(), Map.of()).getLoadBalancerArn();
|
||||
String arn = first.createListener(REGION, lb, encrypted, 9443, null,
|
||||
List.of(HELD_CERTIFICATE), List.of(), List.of(), Map.of()).getListenerArn();
|
||||
first.modifyListener(REGION, arn, plain, null, null, null, null, null);
|
||||
|
||||
ElbV2Service plainReload = serviceWithStorage(
|
||||
storageFactory, mock(ElbV2DataPlane.class), mock(ElbV2HealthChecker.class));
|
||||
Listener plainListener = plainReload.describeListeners(REGION, lb, List.of(arn)).getFirst();
|
||||
assertEquals(plain, plainListener.getProtocol());
|
||||
assertNull(plainListener.getSslPolicy());
|
||||
assertTrue(plainListener.getCertificates().isEmpty());
|
||||
plainReload.modifyListener(REGION, arn, encrypted, null, "ELBSecurityPolicy-2016-08",
|
||||
List.of(OTHER_CERTIFICATE), null, null);
|
||||
|
||||
ElbV2Service encryptedReload = serviceWithStorage(
|
||||
storageFactory, mock(ElbV2DataPlane.class), mock(ElbV2HealthChecker.class));
|
||||
Listener encryptedListener = encryptedReload.describeListeners(REGION, lb, List.of(arn)).getFirst();
|
||||
assertEquals(encrypted, encryptedListener.getProtocol());
|
||||
assertEquals("ELBSecurityPolicy-2016-08", encryptedListener.getSslPolicy());
|
||||
assertEquals(List.of(OTHER_CERTIFICATE), encryptedListener.getCertificates());
|
||||
}
|
||||
|
||||
@Test
|
||||
void modifyListenerDefaultActionsRecompilesRulesWithoutRestartingListener() {
|
||||
String lbArn = service.createLoadBalancer(
|
||||
|
||||
Reference in new issue
Block a user