mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(elbv2): round-trip the authenticate-oidc and authenticate-cognito configs (#3664)
* fix(elbv2): round-trip the authenticate-oidc and authenticate-cognito configs Action carried no fields for either authentication action, so CreateListener, CreateRule, ModifyListener and ModifyRule accepted an AuthenticateOidcConfig or an AuthenticateCognitoConfig and kept only Type and Order. DescribeListeners and DescribeRules then echoed an action with no Issuer, no UserPoolArn and no other sub-field, and a client that read its own configuration back saw the listener as changed on every read. Both configs are now parsed on all four write operations, stored on the action and written into the Describe response. SessionCookieName, Scope and SessionTimeout take the defaults their shapes document, since AWS reports the resolved value rather than the caller's gap, and the members each shape marks required are rejected with ValidationError when absent. ClientSecret is stored but never reported. Real AWS omits it from every Describe call, which is why Terraform's provider carries it forward from local state instead of reading it back. * fix(elbv2): require the OIDC client secret on create and keep it on modify Review feedback on #3664. parseAuthenticateOidc accepted a missing ClientSecret on every write and never read UseExistingClientSecret. AuthenticateOidcActionConfig requires the secret when creating and lets a modify omit it only when UseExistingClientSecret is true, so a create without one was accepted where AWS refuses it. The sharper half is the modify path pgermosen described. Both modify operations replace the action list wholesale through this same parser, so the documented way to keep a secret, sending UseExistingClientSecret with no ClientSecret, blanked the secret instead of preserving it. parseActions now knows create from modify. A create without the secret is rejected. A modify may drop it only by asking for the stored one, and applyExistingClientSecrets puts that secret back before the action list replaces the old one. A modify that drops the secret without the flag is rejected. Three tests cover create-without-secret rejection, the modify that preserves and can then preserve again, and the modify that drops it with no flag. Also imports RequestSpecification rather than spelling it inline, per the review nit.
This commit is contained in:
1 parent
7da4ee0b6b
commit
026abe4878
3 files changed
+562
-2
No files matched your search
@@ -470,7 +470,10 @@ public class ElbV2QueryHandler {
|
||||
Integer port = parseIntOrNull(p.getFirst("Port"));
|
||||
String sslPolicy = p.getFirst("SslPolicy");
|
||||
List<String> certs = parseCertificateList(p, "Certificates");
|
||||
List<Action> defaultActions = parseActions(p, "DefaultActions");
|
||||
List<Action> defaultActions = parseActions(p, "DefaultActions", false);
|
||||
applyExistingClientSecrets(defaultActions,
|
||||
service.describeListeners(region, null, List.of(listenerArn)).stream()
|
||||
.findFirst().map(Listener::getDefaultActions).orElse(null));
|
||||
List<String> alpnPolicy = memberList(p, "AlpnPolicy");
|
||||
|
||||
Listener listener = service.modifyListener(region, listenerArn, protocol, port, sslPolicy,
|
||||
@@ -585,7 +588,10 @@ public class ElbV2QueryHandler {
|
||||
private Response handleModifyRule(MultivaluedMap<String, String> p, String region) {
|
||||
String ruleArn = p.getFirst("RuleArn");
|
||||
List<RuleCondition> conditions = parseConditions(p);
|
||||
List<Action> actions = parseActions(p, "Actions");
|
||||
List<Action> actions = parseActions(p, "Actions", false);
|
||||
applyExistingClientSecrets(actions,
|
||||
service.describeRules(region, null, List.of(ruleArn)).stream()
|
||||
.findFirst().map(Rule::getActions).orElse(null));
|
||||
|
||||
Rule rule = service.modifyRule(region, ruleArn,
|
||||
conditions.isEmpty() ? null : conditions,
|
||||
@@ -957,9 +963,58 @@ public class ElbV2QueryHandler {
|
||||
if (a.getFixedResponseMessageBody() != null) xml.elem("MessageBody", a.getFixedResponseMessageBody());
|
||||
xml.end("FixedResponseConfig");
|
||||
}
|
||||
if ("authenticate-oidc".equals(a.getType())) {
|
||||
xml.start("AuthenticateOidcConfig");
|
||||
xml.elem("Issuer", safe(a.getOidcIssuer()));
|
||||
xml.elem("AuthorizationEndpoint", safe(a.getOidcAuthorizationEndpoint()));
|
||||
xml.elem("TokenEndpoint", safe(a.getOidcTokenEndpoint()));
|
||||
xml.elem("UserInfoEndpoint", safe(a.getOidcUserInfoEndpoint()));
|
||||
xml.elem("ClientId", safe(a.getOidcClientId()));
|
||||
// ClientSecret is deliberately absent. Real AWS never returns it, so a client that
|
||||
// read it back here would compare it against a value AWS would not have sent.
|
||||
if (a.getOidcSessionCookieName() != null) {
|
||||
xml.elem("SessionCookieName", a.getOidcSessionCookieName());
|
||||
}
|
||||
if (a.getOidcScope() != null) xml.elem("Scope", a.getOidcScope());
|
||||
if (a.getOidcSessionTimeout() != null) {
|
||||
xml.elem("SessionTimeout", String.valueOf(a.getOidcSessionTimeout()));
|
||||
}
|
||||
if (a.getOidcOnUnauthenticatedRequest() != null) {
|
||||
xml.elem("OnUnauthenticatedRequest", a.getOidcOnUnauthenticatedRequest());
|
||||
}
|
||||
xml.raw(authExtraParamsXml(a.getOidcAuthenticationRequestExtraParams()));
|
||||
xml.end("AuthenticateOidcConfig");
|
||||
}
|
||||
if ("authenticate-cognito".equals(a.getType())) {
|
||||
xml.start("AuthenticateCognitoConfig");
|
||||
xml.elem("UserPoolArn", safe(a.getCognitoUserPoolArn()));
|
||||
xml.elem("UserPoolClientId", safe(a.getCognitoUserPoolClientId()));
|
||||
xml.elem("UserPoolDomain", safe(a.getCognitoUserPoolDomain()));
|
||||
if (a.getCognitoSessionCookieName() != null) {
|
||||
xml.elem("SessionCookieName", a.getCognitoSessionCookieName());
|
||||
}
|
||||
if (a.getCognitoScope() != null) xml.elem("Scope", a.getCognitoScope());
|
||||
if (a.getCognitoSessionTimeout() != null) {
|
||||
xml.elem("SessionTimeout", String.valueOf(a.getCognitoSessionTimeout()));
|
||||
}
|
||||
if (a.getCognitoOnUnauthenticatedRequest() != null) {
|
||||
xml.elem("OnUnauthenticatedRequest", a.getCognitoOnUnauthenticatedRequest());
|
||||
}
|
||||
xml.raw(authExtraParamsXml(a.getCognitoAuthenticationRequestExtraParams()));
|
||||
xml.end("AuthenticateCognitoConfig");
|
||||
}
|
||||
return xml.build();
|
||||
}
|
||||
|
||||
private String authExtraParamsXml(Map<String, String> params) {
|
||||
if (params == null || params.isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
XmlBuilder xml = new XmlBuilder().start("AuthenticationRequestExtraParams");
|
||||
params.forEach((k, v) -> xml.start("entry").elem("key", k).elem("value", safe(v)).end("entry"));
|
||||
return xml.end("AuthenticationRequestExtraParams").build();
|
||||
}
|
||||
|
||||
private String conditionXml(RuleCondition c) {
|
||||
XmlBuilder xml = new XmlBuilder();
|
||||
xml.elem("Field", safe(c.getField()));
|
||||
@@ -1059,6 +1114,16 @@ public class ElbV2QueryHandler {
|
||||
}
|
||||
|
||||
private List<Action> parseActions(MultivaluedMap<String, String> p, String prefix) {
|
||||
return parseActions(p, prefix, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param creating true for CreateListener and CreateRule, false for the two modify operations.
|
||||
* AuthenticateOidcActionConfig.ClientSecret is required on a create and may be omitted on a
|
||||
* modify only when UseExistingClientSecret is true, so the two paths cannot share one rule.
|
||||
*/
|
||||
private List<Action> parseActions(MultivaluedMap<String, String> p, String prefix,
|
||||
boolean creating) {
|
||||
List<Action> result = new ArrayList<>();
|
||||
int i = 1;
|
||||
while (true) {
|
||||
@@ -1105,6 +1170,10 @@ public class ElbV2QueryHandler {
|
||||
a.setFixedResponseContentType(p.getFirst(prefix + ".member." + i + ".FixedResponseConfig.ContentType"));
|
||||
a.setFixedResponseMessageBody(p.getFirst(prefix + ".member." + i + ".FixedResponseConfig.MessageBody"));
|
||||
}
|
||||
case "authenticate-oidc" -> parseAuthenticateOidc(p,
|
||||
prefix + ".member." + i + ".AuthenticateOidcConfig.", a, creating);
|
||||
case "authenticate-cognito" -> parseAuthenticateCognito(p,
|
||||
prefix + ".member." + i + ".AuthenticateCognitoConfig.", a);
|
||||
}
|
||||
result.add(a);
|
||||
i++;
|
||||
@@ -1112,6 +1181,111 @@ public class ElbV2QueryHandler {
|
||||
return result;
|
||||
}
|
||||
|
||||
// AuthenticateOidcActionConfig and AuthenticateCognitoActionConfig document these three
|
||||
// defaults, and AWS reports the resolved values on Describe rather than the caller's gaps.
|
||||
private static final String DEFAULT_AUTH_SESSION_COOKIE = "AWSELBAuthSessionCookie";
|
||||
private static final String DEFAULT_AUTH_SCOPE = "openid";
|
||||
private static final long DEFAULT_AUTH_SESSION_TIMEOUT = 604800L;
|
||||
|
||||
private void parseAuthenticateOidc(MultivaluedMap<String, String> p, String base, Action a,
|
||||
boolean creating) {
|
||||
a.setOidcIssuer(requiredAuthMember(p, base, "Issuer"));
|
||||
a.setOidcAuthorizationEndpoint(requiredAuthMember(p, base, "AuthorizationEndpoint"));
|
||||
a.setOidcTokenEndpoint(requiredAuthMember(p, base, "TokenEndpoint"));
|
||||
a.setOidcUserInfoEndpoint(requiredAuthMember(p, base, "UserInfoEndpoint"));
|
||||
a.setOidcClientId(requiredAuthMember(p, base, "ClientId"));
|
||||
String clientSecret = p.getFirst(base + "ClientSecret");
|
||||
boolean useExisting = Boolean.parseBoolean(p.getFirst(base + "UseExistingClientSecret"));
|
||||
if (clientSecret == null || clientSecret.isBlank()) {
|
||||
// Required on a create. On a modify it may be dropped only by asking for the stored
|
||||
// one, and modifyExistingClientSecrets below is what puts that secret back.
|
||||
if (creating || !useExisting) {
|
||||
throw new AwsException("ValidationError", creating
|
||||
? "ClientSecret is required."
|
||||
: "ClientSecret is required unless UseExistingClientSecret is true.", 400);
|
||||
}
|
||||
}
|
||||
a.setOidcClientSecret(clientSecret);
|
||||
a.setOidcUseExistingClientSecret(useExisting);
|
||||
a.setOidcSessionCookieName(orDefault(p.getFirst(base + "SessionCookieName"),
|
||||
DEFAULT_AUTH_SESSION_COOKIE));
|
||||
a.setOidcScope(orDefault(p.getFirst(base + "Scope"), DEFAULT_AUTH_SCOPE));
|
||||
a.setOidcSessionTimeout(parseSessionTimeout(p.getFirst(base + "SessionTimeout")));
|
||||
a.setOidcOnUnauthenticatedRequest(p.getFirst(base + "OnUnauthenticatedRequest"));
|
||||
a.setOidcAuthenticationRequestExtraParams(parseAuthExtraParams(p, base));
|
||||
}
|
||||
|
||||
private void parseAuthenticateCognito(MultivaluedMap<String, String> p, String base, Action a) {
|
||||
a.setCognitoUserPoolArn(requiredAuthMember(p, base, "UserPoolArn"));
|
||||
a.setCognitoUserPoolClientId(requiredAuthMember(p, base, "UserPoolClientId"));
|
||||
a.setCognitoUserPoolDomain(requiredAuthMember(p, base, "UserPoolDomain"));
|
||||
a.setCognitoSessionCookieName(orDefault(p.getFirst(base + "SessionCookieName"),
|
||||
DEFAULT_AUTH_SESSION_COOKIE));
|
||||
a.setCognitoScope(orDefault(p.getFirst(base + "Scope"), DEFAULT_AUTH_SCOPE));
|
||||
a.setCognitoSessionTimeout(parseSessionTimeout(p.getFirst(base + "SessionTimeout")));
|
||||
a.setCognitoOnUnauthenticatedRequest(p.getFirst(base + "OnUnauthenticatedRequest"));
|
||||
a.setCognitoAuthenticationRequestExtraParams(parseAuthExtraParams(p, base));
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts the stored client secret back on any authenticate-oidc action that asked to keep it.
|
||||
* Both modify operations replace the action list wholesale, so without this the documented
|
||||
* UseExistingClientSecret flow would blank the secret it was written to preserve. A rule or a
|
||||
* listener carries at most one authentication action, so the stored one is unambiguous.
|
||||
*/
|
||||
private void applyExistingClientSecrets(List<Action> parsed, List<Action> existing) {
|
||||
String stored = existing == null ? null : existing.stream()
|
||||
.filter(a -> "authenticate-oidc".equals(a.getType()))
|
||||
.map(Action::getOidcClientSecret)
|
||||
.filter(Objects::nonNull)
|
||||
.findFirst().orElse(null);
|
||||
for (Action a : parsed) {
|
||||
if ("authenticate-oidc".equals(a.getType()) && a.isOidcUseExistingClientSecret()
|
||||
&& (a.getOidcClientSecret() == null || a.getOidcClientSecret().isBlank())) {
|
||||
if (stored == null) {
|
||||
throw new AwsException("ValidationError",
|
||||
"UseExistingClientSecret is true but no client secret is stored.", 400);
|
||||
}
|
||||
a.setOidcClientSecret(stored);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String requiredAuthMember(MultivaluedMap<String, String> p, String base, String name) {
|
||||
String value = p.getFirst(base + name);
|
||||
if (value == null || value.isBlank()) {
|
||||
throw new AwsException("ValidationError", name + " is required.", 400);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
private static String orDefault(String value, String fallback) {
|
||||
return value != null && !value.isBlank() ? value : fallback;
|
||||
}
|
||||
|
||||
private static Long parseSessionTimeout(String raw) {
|
||||
if (raw == null || raw.isBlank()) {
|
||||
return DEFAULT_AUTH_SESSION_TIMEOUT;
|
||||
}
|
||||
return Long.parseLong(raw);
|
||||
}
|
||||
|
||||
// AuthenticationRequestExtraParams is a map on the wire, so it arrives as
|
||||
// <base>.AuthenticationRequestExtraParams.entry.N.key / .value.
|
||||
private Map<String, String> parseAuthExtraParams(MultivaluedMap<String, String> p, String base) {
|
||||
Map<String, String> params = new LinkedHashMap<>();
|
||||
int n = 1;
|
||||
while (true) {
|
||||
String key = p.getFirst(base + "AuthenticationRequestExtraParams.entry." + n + ".key");
|
||||
if (key == null) {
|
||||
break;
|
||||
}
|
||||
params.put(key, safe(p.getFirst(base + "AuthenticationRequestExtraParams.entry." + n + ".value")));
|
||||
n++;
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
private List<RuleCondition> parseConditions(MultivaluedMap<String, String> p) {
|
||||
List<RuleCondition> result = new ArrayList<>();
|
||||
int i = 1;
|
||||
|
||||
@@ -4,7 +4,9 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
@@ -34,6 +36,36 @@ public class Action {
|
||||
private String fixedResponseContentType;
|
||||
private String fixedResponseMessageBody;
|
||||
|
||||
// authenticate-oidc
|
||||
private String oidcIssuer;
|
||||
private String oidcAuthorizationEndpoint;
|
||||
private String oidcTokenEndpoint;
|
||||
private String oidcUserInfoEndpoint;
|
||||
private String oidcClientId;
|
||||
/**
|
||||
* Stored so a modify can keep it, never reported. Describe* on real AWS omits the secret,
|
||||
* which is why Terraform's provider carries it forward from local state instead of reading
|
||||
* it back.
|
||||
*/
|
||||
private String oidcClientSecret;
|
||||
/** Set by a modify that asked to keep the stored secret. Never reported. */
|
||||
private boolean oidcUseExistingClientSecret;
|
||||
private String oidcSessionCookieName;
|
||||
private String oidcScope;
|
||||
private Long oidcSessionTimeout;
|
||||
private String oidcOnUnauthenticatedRequest;
|
||||
private Map<String, String> oidcAuthenticationRequestExtraParams = new LinkedHashMap<>();
|
||||
|
||||
// authenticate-cognito
|
||||
private String cognitoUserPoolArn;
|
||||
private String cognitoUserPoolClientId;
|
||||
private String cognitoUserPoolDomain;
|
||||
private String cognitoSessionCookieName;
|
||||
private String cognitoScope;
|
||||
private Long cognitoSessionTimeout;
|
||||
private String cognitoOnUnauthenticatedRequest;
|
||||
private Map<String, String> cognitoAuthenticationRequestExtraParams = new LinkedHashMap<>();
|
||||
|
||||
public Action() {}
|
||||
|
||||
public String getType() { return type; }
|
||||
@@ -81,6 +113,96 @@ public class Action {
|
||||
public String getFixedResponseMessageBody() { return fixedResponseMessageBody; }
|
||||
public void setFixedResponseMessageBody(String fixedResponseMessageBody) { this.fixedResponseMessageBody = fixedResponseMessageBody; }
|
||||
|
||||
public String getOidcIssuer() { return oidcIssuer; }
|
||||
public void setOidcIssuer(String oidcIssuer) { this.oidcIssuer = oidcIssuer; }
|
||||
|
||||
public String getOidcAuthorizationEndpoint() { return oidcAuthorizationEndpoint; }
|
||||
public void setOidcAuthorizationEndpoint(String oidcAuthorizationEndpoint) {
|
||||
this.oidcAuthorizationEndpoint = oidcAuthorizationEndpoint;
|
||||
}
|
||||
|
||||
public String getOidcTokenEndpoint() { return oidcTokenEndpoint; }
|
||||
public void setOidcTokenEndpoint(String oidcTokenEndpoint) { this.oidcTokenEndpoint = oidcTokenEndpoint; }
|
||||
|
||||
public String getOidcUserInfoEndpoint() { return oidcUserInfoEndpoint; }
|
||||
public void setOidcUserInfoEndpoint(String oidcUserInfoEndpoint) {
|
||||
this.oidcUserInfoEndpoint = oidcUserInfoEndpoint;
|
||||
}
|
||||
|
||||
public String getOidcClientId() { return oidcClientId; }
|
||||
public void setOidcClientId(String oidcClientId) { this.oidcClientId = oidcClientId; }
|
||||
|
||||
public String getOidcClientSecret() { return oidcClientSecret; }
|
||||
public void setOidcClientSecret(String oidcClientSecret) { this.oidcClientSecret = oidcClientSecret; }
|
||||
|
||||
public boolean isOidcUseExistingClientSecret() { return oidcUseExistingClientSecret; }
|
||||
public void setOidcUseExistingClientSecret(boolean oidcUseExistingClientSecret) {
|
||||
this.oidcUseExistingClientSecret = oidcUseExistingClientSecret;
|
||||
}
|
||||
|
||||
public String getOidcSessionCookieName() { return oidcSessionCookieName; }
|
||||
public void setOidcSessionCookieName(String oidcSessionCookieName) {
|
||||
this.oidcSessionCookieName = oidcSessionCookieName;
|
||||
}
|
||||
|
||||
public String getOidcScope() { return oidcScope; }
|
||||
public void setOidcScope(String oidcScope) { this.oidcScope = oidcScope; }
|
||||
|
||||
public Long getOidcSessionTimeout() { return oidcSessionTimeout; }
|
||||
public void setOidcSessionTimeout(Long oidcSessionTimeout) { this.oidcSessionTimeout = oidcSessionTimeout; }
|
||||
|
||||
public String getOidcOnUnauthenticatedRequest() { return oidcOnUnauthenticatedRequest; }
|
||||
public void setOidcOnUnauthenticatedRequest(String oidcOnUnauthenticatedRequest) {
|
||||
this.oidcOnUnauthenticatedRequest = oidcOnUnauthenticatedRequest;
|
||||
}
|
||||
|
||||
public Map<String, String> getOidcAuthenticationRequestExtraParams() {
|
||||
return oidcAuthenticationRequestExtraParams;
|
||||
}
|
||||
|
||||
public void setOidcAuthenticationRequestExtraParams(Map<String, String> params) {
|
||||
this.oidcAuthenticationRequestExtraParams = params;
|
||||
}
|
||||
|
||||
public String getCognitoUserPoolArn() { return cognitoUserPoolArn; }
|
||||
public void setCognitoUserPoolArn(String cognitoUserPoolArn) { this.cognitoUserPoolArn = cognitoUserPoolArn; }
|
||||
|
||||
public String getCognitoUserPoolClientId() { return cognitoUserPoolClientId; }
|
||||
public void setCognitoUserPoolClientId(String cognitoUserPoolClientId) {
|
||||
this.cognitoUserPoolClientId = cognitoUserPoolClientId;
|
||||
}
|
||||
|
||||
public String getCognitoUserPoolDomain() { return cognitoUserPoolDomain; }
|
||||
public void setCognitoUserPoolDomain(String cognitoUserPoolDomain) {
|
||||
this.cognitoUserPoolDomain = cognitoUserPoolDomain;
|
||||
}
|
||||
|
||||
public String getCognitoSessionCookieName() { return cognitoSessionCookieName; }
|
||||
public void setCognitoSessionCookieName(String cognitoSessionCookieName) {
|
||||
this.cognitoSessionCookieName = cognitoSessionCookieName;
|
||||
}
|
||||
|
||||
public String getCognitoScope() { return cognitoScope; }
|
||||
public void setCognitoScope(String cognitoScope) { this.cognitoScope = cognitoScope; }
|
||||
|
||||
public Long getCognitoSessionTimeout() { return cognitoSessionTimeout; }
|
||||
public void setCognitoSessionTimeout(Long cognitoSessionTimeout) {
|
||||
this.cognitoSessionTimeout = cognitoSessionTimeout;
|
||||
}
|
||||
|
||||
public String getCognitoOnUnauthenticatedRequest() { return cognitoOnUnauthenticatedRequest; }
|
||||
public void setCognitoOnUnauthenticatedRequest(String cognitoOnUnauthenticatedRequest) {
|
||||
this.cognitoOnUnauthenticatedRequest = cognitoOnUnauthenticatedRequest;
|
||||
}
|
||||
|
||||
public Map<String, String> getCognitoAuthenticationRequestExtraParams() {
|
||||
return cognitoAuthenticationRequestExtraParams;
|
||||
}
|
||||
|
||||
public void setCognitoAuthenticationRequestExtraParams(Map<String, String> params) {
|
||||
this.cognitoAuthenticationRequestExtraParams = params;
|
||||
}
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public static class TargetGroupTuple {
|
||||
|
||||
+264
@@ -0,0 +1,264 @@
|
||||
package io.github.hectorvent.floci.services.elbv2;
|
||||
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2Service;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.path.xml.XmlPath;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
|
||||
/**
|
||||
* DescribeListeners and DescribeRules have to echo back the authenticate-oidc and
|
||||
* authenticate-cognito configurations a listener or rule was created with. Without them a client
|
||||
* that reads its own configuration back sees every sub-field missing and treats the listener as
|
||||
* changed on every plan.
|
||||
*
|
||||
* <p>ClientSecret is the exception. Real AWS never returns it, so neither does floci.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class ElbV2AuthenticateActionIntegrationTest {
|
||||
|
||||
private static final String AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260427/us-east-1/elasticloadbalancing/aws4_request";
|
||||
|
||||
private static String lbArn;
|
||||
private static String tgArn;
|
||||
private static String listenerArn;
|
||||
|
||||
private static String xml(RequestSpecification spec) {
|
||||
return spec.header("Authorization", AUTH).when().post("/").then().statusCode(200)
|
||||
.extract().asString();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void seedLoadBalancerAndTargetGroup() {
|
||||
lbArn = XmlPath.from(xml(given()
|
||||
.formParam("Action", "CreateLoadBalancer")
|
||||
.formParam("Name", "auth-action-lb")
|
||||
.formParam("Type", "application")
|
||||
.formParam("Subnets.member.1", Ec2Service.defaultSubnetId("us-east-1", "a"))
|
||||
.formParam("Subnets.member.2", Ec2Service.defaultSubnetId("us-east-1", "b"))))
|
||||
.getString("CreateLoadBalancerResponse.CreateLoadBalancerResult"
|
||||
+ ".LoadBalancers.member.LoadBalancerArn");
|
||||
|
||||
tgArn = XmlPath.from(xml(given()
|
||||
.formParam("Action", "CreateTargetGroup")
|
||||
.formParam("Name", "auth-action-tg")
|
||||
.formParam("Protocol", "HTTP")
|
||||
.formParam("Port", "80")
|
||||
.formParam("VpcId", Ec2Service.defaultVpcId("us-east-1"))))
|
||||
.getString("CreateTargetGroupResponse.CreateTargetGroupResult"
|
||||
+ ".TargetGroups.member.TargetGroupArn");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void createListener_withAuthenticateOidc_roundTripsEverySubFieldExceptTheSecret() {
|
||||
String base = "DefaultActions.member.1.AuthenticateOidcConfig.";
|
||||
listenerArn = XmlPath.from(xml(given()
|
||||
.formParam("Action", "CreateListener")
|
||||
.formParam("LoadBalancerArn", lbArn)
|
||||
.formParam("Protocol", "HTTPS")
|
||||
.formParam("Port", "443")
|
||||
.formParam("DefaultActions.member.1.Type", "authenticate-oidc")
|
||||
.formParam("DefaultActions.member.1.Order", "1")
|
||||
.formParam(base + "Issuer", "https://idp.example.com")
|
||||
.formParam(base + "AuthorizationEndpoint", "https://idp.example.com/authorize")
|
||||
.formParam(base + "TokenEndpoint", "https://idp.example.com/token")
|
||||
.formParam(base + "UserInfoEndpoint", "https://idp.example.com/userinfo")
|
||||
.formParam(base + "ClientId", "client-abc")
|
||||
.formParam(base + "ClientSecret", "shhh")
|
||||
.formParam(base + "OnUnauthenticatedRequest", "authenticate")
|
||||
.formParam(base + "AuthenticationRequestExtraParams.entry.1.key", "prompt")
|
||||
.formParam(base + "AuthenticationRequestExtraParams.entry.1.value", "login")
|
||||
.formParam("DefaultActions.member.2.Type", "forward")
|
||||
.formParam("DefaultActions.member.2.Order", "2")
|
||||
.formParam("DefaultActions.member.2.TargetGroupArn", tgArn)))
|
||||
.getString("CreateListenerResponse.CreateListenerResult"
|
||||
+ ".Listeners.member.ListenerArn");
|
||||
|
||||
String raw = xml(given()
|
||||
.formParam("Action", "DescribeListeners")
|
||||
.formParam("ListenerArns.member.1", listenerArn));
|
||||
XmlPath described = XmlPath.from(raw);
|
||||
|
||||
String action = "DescribeListenersResponse.DescribeListenersResult.Listeners.member"
|
||||
+ ".DefaultActions.member[0].AuthenticateOidcConfig.";
|
||||
assertEquals("https://idp.example.com", described.getString(action + "Issuer"));
|
||||
assertEquals("https://idp.example.com/authorize",
|
||||
described.getString(action + "AuthorizationEndpoint"));
|
||||
assertEquals("https://idp.example.com/token", described.getString(action + "TokenEndpoint"));
|
||||
assertEquals("https://idp.example.com/userinfo",
|
||||
described.getString(action + "UserInfoEndpoint"));
|
||||
assertEquals("client-abc", described.getString(action + "ClientId"));
|
||||
assertEquals("authenticate", described.getString(action + "OnUnauthenticatedRequest"));
|
||||
assertEquals("prompt",
|
||||
described.getString(action + "AuthenticationRequestExtraParams.entry.key"));
|
||||
assertEquals("login",
|
||||
described.getString(action + "AuthenticationRequestExtraParams.entry.value"));
|
||||
|
||||
// The model documents all three defaults, and AWS reports the resolved value.
|
||||
assertEquals("AWSELBAuthSessionCookie", described.getString(action + "SessionCookieName"));
|
||||
assertEquals("openid", described.getString(action + "Scope"));
|
||||
assertEquals("604800", described.getString(action + "SessionTimeout"));
|
||||
|
||||
assertFalse(raw.contains("ClientSecret"),
|
||||
"real AWS never returns ClientSecret from a Describe call");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void createRule_withAuthenticateCognito_roundTripsItsConfig() {
|
||||
String base = "Actions.member.1.AuthenticateCognitoConfig.";
|
||||
given()
|
||||
.formParam("Action", "CreateRule")
|
||||
.formParam("ListenerArn", listenerArn)
|
||||
.formParam("Priority", "10")
|
||||
.formParam("Conditions.member.1.Field", "path-pattern")
|
||||
.formParam("Conditions.member.1.Values.member.1", "/private/*")
|
||||
.formParam("Actions.member.1.Type", "authenticate-cognito")
|
||||
.formParam("Actions.member.1.Order", "1")
|
||||
.formParam(base + "UserPoolArn",
|
||||
"arn:aws:cognito-idp:us-east-1:000000000000:userpool/us-east-1_abc")
|
||||
.formParam(base + "UserPoolClientId", "pool-client")
|
||||
.formParam(base + "UserPoolDomain", "auth.example.com")
|
||||
.formParam(base + "Scope", "openid profile")
|
||||
.formParam(base + "SessionTimeout", "3600")
|
||||
.formParam("Actions.member.2.Type", "forward")
|
||||
.formParam("Actions.member.2.Order", "2")
|
||||
.formParam("Actions.member.2.TargetGroupArn", tgArn)
|
||||
.header("Authorization", AUTH)
|
||||
.when().post("/").then().statusCode(200);
|
||||
|
||||
XmlPath described = XmlPath.from(xml(given()
|
||||
.formParam("Action", "DescribeRules")
|
||||
.formParam("ListenerArn", listenerArn)));
|
||||
|
||||
String action = "DescribeRulesResponse.DescribeRulesResult.Rules.member"
|
||||
+ ".findAll { it.Priority.text() == '10' }.Actions.member[0]"
|
||||
+ ".AuthenticateCognitoConfig.";
|
||||
assertEquals("arn:aws:cognito-idp:us-east-1:000000000000:userpool/us-east-1_abc",
|
||||
described.getString(action + "UserPoolArn"));
|
||||
assertEquals("pool-client", described.getString(action + "UserPoolClientId"));
|
||||
assertEquals("auth.example.com", described.getString(action + "UserPoolDomain"));
|
||||
assertEquals("openid profile", described.getString(action + "Scope"));
|
||||
assertEquals("3600", described.getString(action + "SessionTimeout"));
|
||||
assertEquals("AWSELBAuthSessionCookie", described.getString(action + "SessionCookieName"));
|
||||
}
|
||||
|
||||
// ClientSecret is required on a create and may be omitted on a modify only by asking for the
|
||||
// stored one. Both modify operations replace the action list wholesale, so without carrying the
|
||||
// stored secret forward the documented keep-the-secret flow would blank it instead.
|
||||
@Test
|
||||
@Order(4)
|
||||
void createListener_withoutClientSecret_isRejected() {
|
||||
String base = "DefaultActions.member.1.AuthenticateOidcConfig.";
|
||||
given()
|
||||
.formParam("Action", "CreateListener")
|
||||
.formParam("LoadBalancerArn", lbArn)
|
||||
.formParam("Protocol", "HTTPS")
|
||||
.formParam("Port", "9443")
|
||||
.formParam("DefaultActions.member.1.Type", "authenticate-oidc")
|
||||
.formParam(base + "Issuer", "https://idp.example.com")
|
||||
.formParam(base + "AuthorizationEndpoint", "https://idp.example.com/authorize")
|
||||
.formParam(base + "TokenEndpoint", "https://idp.example.com/token")
|
||||
.formParam(base + "UserInfoEndpoint", "https://idp.example.com/userinfo")
|
||||
.formParam(base + "ClientId", "client-abc")
|
||||
.header("Authorization", AUTH)
|
||||
.when().post("/")
|
||||
.then().statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
|
||||
.body("ErrorResponse.Error.Message", equalTo("ClientSecret is required."));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
void modifyListener_withUseExistingClientSecret_keepsTheStoredSecret() {
|
||||
String base = "DefaultActions.member.1.AuthenticateOidcConfig.";
|
||||
xml(given()
|
||||
.formParam("Action", "ModifyListener")
|
||||
.formParam("ListenerArn", listenerArn)
|
||||
.formParam("DefaultActions.member.1.Type", "authenticate-oidc")
|
||||
.formParam("DefaultActions.member.1.Order", "1")
|
||||
.formParam(base + "Issuer", "https://idp.example.com")
|
||||
.formParam(base + "AuthorizationEndpoint", "https://idp.example.com/authorize")
|
||||
.formParam(base + "TokenEndpoint", "https://idp.example.com/token")
|
||||
.formParam(base + "UserInfoEndpoint", "https://idp.example.com/userinfo")
|
||||
.formParam(base + "ClientId", "client-abc")
|
||||
.formParam(base + "UseExistingClientSecret", "true")
|
||||
.formParam(base + "Scope", "openid email")
|
||||
.formParam("DefaultActions.member.2.Type", "forward")
|
||||
.formParam("DefaultActions.member.2.Order", "2")
|
||||
.formParam("DefaultActions.member.2.TargetGroupArn", tgArn));
|
||||
|
||||
// The modify took effect, and the secret the create supplied is still the stored one.
|
||||
XmlPath described = XmlPath.from(xml(given()
|
||||
.formParam("Action", "DescribeListeners")
|
||||
.formParam("ListenerArns.member.1", listenerArn)));
|
||||
String action = "DescribeListenersResponse.DescribeListenersResult.Listeners.member"
|
||||
+ ".DefaultActions.member[0].AuthenticateOidcConfig.";
|
||||
assertEquals("openid email", described.getString(action + "Scope"));
|
||||
|
||||
// A later modify that keeps the secret again still works, which it cannot do once the
|
||||
// stored secret has been blanked.
|
||||
given()
|
||||
.formParam("Action", "ModifyListener")
|
||||
.formParam("ListenerArn", listenerArn)
|
||||
.formParam("DefaultActions.member.1.Type", "authenticate-oidc")
|
||||
.formParam(base + "Issuer", "https://idp.example.com")
|
||||
.formParam(base + "AuthorizationEndpoint", "https://idp.example.com/authorize")
|
||||
.formParam(base + "TokenEndpoint", "https://idp.example.com/token")
|
||||
.formParam(base + "UserInfoEndpoint", "https://idp.example.com/userinfo")
|
||||
.formParam(base + "ClientId", "client-abc")
|
||||
.formParam(base + "UseExistingClientSecret", "true")
|
||||
.header("Authorization", AUTH)
|
||||
.when().post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void modifyListener_withoutSecretAndWithoutTheFlag_isRejected() {
|
||||
String base = "DefaultActions.member.1.AuthenticateOidcConfig.";
|
||||
given()
|
||||
.formParam("Action", "ModifyListener")
|
||||
.formParam("ListenerArn", listenerArn)
|
||||
.formParam("DefaultActions.member.1.Type", "authenticate-oidc")
|
||||
.formParam(base + "Issuer", "https://idp.example.com")
|
||||
.formParam(base + "AuthorizationEndpoint", "https://idp.example.com/authorize")
|
||||
.formParam(base + "TokenEndpoint", "https://idp.example.com/token")
|
||||
.formParam(base + "UserInfoEndpoint", "https://idp.example.com/userinfo")
|
||||
.formParam(base + "ClientId", "client-abc")
|
||||
.header("Authorization", AUTH)
|
||||
.when().post("/")
|
||||
.then().statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
|
||||
.body("ErrorResponse.Error.Message",
|
||||
equalTo("ClientSecret is required unless UseExistingClientSecret is true."));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void createListener_missingRequiredOidcMember_isRejected() {
|
||||
given()
|
||||
.formParam("Action", "CreateListener")
|
||||
.formParam("LoadBalancerArn", lbArn)
|
||||
.formParam("Protocol", "HTTPS")
|
||||
.formParam("Port", "8443")
|
||||
.formParam("DefaultActions.member.1.Type", "authenticate-oidc")
|
||||
.formParam("DefaultActions.member.1.AuthenticateOidcConfig.Issuer",
|
||||
"https://idp.example.com")
|
||||
.header("Authorization", AUTH)
|
||||
.when().post("/")
|
||||
.then().statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("ValidationError"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user