diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SnsTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SnsTest.java index 18e5e56447..38dc26f951 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SnsTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SnsTest.java @@ -38,6 +38,7 @@ import software.amazon.awssdk.services.sns.model.SetSubscriptionAttributesReques import software.amazon.awssdk.services.sns.model.SubscribeRequest; import software.amazon.awssdk.services.sns.model.SubscribeResponse; import software.amazon.awssdk.services.sns.model.UnsubscribeRequest; +import software.amazon.awssdk.services.sns.model.VerificationException; import software.amazon.awssdk.services.sns.model.VerifySmsSandboxPhoneNumberRequest; import software.amazon.awssdk.services.sqs.SqsClient; import software.amazon.awssdk.services.sqs.model.*; @@ -671,8 +672,15 @@ class SnsTest { try { assertThat(sandboxStatus(phoneNumber)).isEqualTo(SMSSandboxPhoneNumberVerificationStatus.PENDING); + String code = textedCode(phoneNumber); + String wrong = String.format("%06d", (Integer.parseInt(code) + 1) % 1_000_000); + assertThatThrownBy(() -> sns.verifySMSSandboxPhoneNumber(VerifySmsSandboxPhoneNumberRequest.builder() + .phoneNumber(phoneNumber).oneTimePassword(wrong).build())) + .isInstanceOfSatisfying(VerificationException.class, + e -> assertThat(e.status()).isEqualTo("Pending")); + sns.verifySMSSandboxPhoneNumber(VerifySmsSandboxPhoneNumberRequest.builder() - .phoneNumber(phoneNumber).oneTimePassword(textedCode(phoneNumber)).build()); + .phoneNumber(phoneNumber).oneTimePassword(code).build()); assertThat(sandboxStatus(phoneNumber)).isEqualTo(SMSSandboxPhoneNumberVerificationStatus.VERIFIED); } finally { diff --git a/docs/services/sns.md b/docs/services/sns.md index fa2401c2ee..9e6eba1858 100644 --- a/docs/services/sns.md +++ b/docs/services/sns.md @@ -75,6 +75,11 @@ aws sns verify-sms-sandbox-phone-number --phone-number +15555550100 --one-time-p --endpoint-url $AWS_ENDPOINT_URL ``` +A wrong code fails with `VerificationException` and the number stays `Pending`. AWS requires the +error's `Status` member but documents no values for it, so Floci picks one: the number's verification +status, `Pending`. `Pending` and `Verified` are the only statuses SNS publishes for a sandbox number, +and the developer guide reads a number left `Pending` as an unsuccessful verification. + AWS opts a number out when its owner replies STOP. Nothing reaches Floci that way, so no number is ever opted out: `CheckIfPhoneNumberIsOptedOut` answers `false`, `ListPhoneNumbersOptedOut` is empty, and `OptInPhoneNumber` only checks that the number is in E.164 format. diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryResponse.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryResponse.java index d678395a6b..e0fedcf385 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryResponse.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryResponse.java @@ -3,6 +3,7 @@ package io.github.hectorvent.floci.core.common; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; +import java.util.Map; import java.util.UUID; /** @@ -113,12 +114,32 @@ public final class AwsQueryResponse { * @param type the error type, typically "Sender" for 4xx or "Receiver" for 5xx */ public static Response error(String code, String message, String xmlns, int status, String type) { - String xml = new XmlBuilder() + return error(code, message, xmlns, status, type, Map.of()); + } + + /** + * Builds a Query-protocol XML error response for an error shape that declares members beyond its + * message, such as SNS {@code VerificationException}'s {@code Status}. Each member is an element of + * {@code } after {@code }, where the SDKs read it. + * + * @param members the error's other members by name, in the order they are written + */ + public static Response error(String code, String message, String xmlns, int status, + Map members) { + String type = status >= 500 ? "Receiver" : "Sender"; + return error(code, message, xmlns, status, type, members); + } + + private static Response error(String code, String message, String xmlns, int status, String type, + Map members) { + XmlBuilder error = new XmlBuilder() .start("ErrorResponse", xmlns) .start("Error") .elem("Type", type) .elem("Code", code) - .elem("Message", message) + .elem("Message", message); + members.forEach(error::elem); + String xml = error .end("Error") .raw(responseMetadata()) .end("ErrorResponse") diff --git a/src/main/java/io/github/hectorvent/floci/services/sns/SnsQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/sns/SnsQueryHandler.java index 8ca3bb7d71..a42e680356 100644 --- a/src/main/java/io/github/hectorvent/floci/services/sns/SnsQueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/sns/SnsQueryHandler.java @@ -18,6 +18,7 @@ import java.util.Base64; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.TreeMap; /** * Query-protocol handler for SNS actions. @@ -99,7 +100,7 @@ public class SnsQueryHandler { snsService.deleteTopic(topicArn, region); return Response.ok(AwsQueryResponse.envelopeNoResult("DeleteTopic", AwsNamespaces.SNS)).build(); } catch (AwsException e) { - return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); + return xmlErrorResponse(e); } } @@ -129,7 +130,7 @@ public class SnsQueryHandler { xml.end("Attributes"); return Response.ok(AwsQueryResponse.envelope("GetTopicAttributes", AwsNamespaces.SNS, xml.build())).build(); } catch (AwsException e) { - return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); + return xmlErrorResponse(e); } } @@ -141,7 +142,7 @@ public class SnsQueryHandler { snsService.setTopicAttributes(topicArn, attributeName, attributeValue, region); return Response.ok(AwsQueryResponse.envelopeNoResult("SetTopicAttributes", AwsNamespaces.SNS)).build(); } catch (AwsException e) { - return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); + return xmlErrorResponse(e); } } @@ -151,7 +152,7 @@ public class SnsQueryHandler { memberList(params, "AWSAccountId"), memberList(params, "ActionName"), region); return Response.ok(AwsQueryResponse.envelopeNoResult("AddPermission", AwsNamespaces.SNS)).build(); } catch (AwsException e) { - return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); + return xmlErrorResponse(e); } } @@ -160,7 +161,7 @@ public class SnsQueryHandler { snsService.removePermission(getParam(params, "TopicArn"), getParam(params, "Label"), region); return Response.ok(AwsQueryResponse.envelopeNoResult("RemovePermission", AwsNamespaces.SNS)).build(); } catch (AwsException e) { - return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); + return xmlErrorResponse(e); } } @@ -206,7 +207,7 @@ public class SnsQueryHandler { return Response.ok(AwsQueryResponse.envelopeEmptyResult("CreateSMSSandboxPhoneNumber", AwsNamespaces.SNS)).build(); } catch (AwsException e) { - return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); + return xmlErrorResponse(e); } } @@ -217,7 +218,7 @@ public class SnsQueryHandler { return Response.ok(AwsQueryResponse.envelopeEmptyResult("VerifySMSSandboxPhoneNumber", AwsNamespaces.SNS)).build(); } catch (AwsException e) { - return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); + return xmlErrorResponse(e); } } @@ -227,7 +228,7 @@ public class SnsQueryHandler { return Response.ok(AwsQueryResponse.envelopeEmptyResult("DeleteSMSSandboxPhoneNumber", AwsNamespaces.SNS)).build(); } catch (AwsException e) { - return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); + return xmlErrorResponse(e); } } @@ -250,7 +251,7 @@ public class SnsQueryHandler { return Response.ok(AwsQueryResponse.envelope("ListSMSSandboxPhoneNumbers", AwsNamespaces.SNS, xml.build())).build(); } catch (AwsException e) { - return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); + return xmlErrorResponse(e); } } @@ -263,7 +264,7 @@ public class SnsQueryHandler { return Response.ok(AwsQueryResponse.envelope("CheckIfPhoneNumberIsOptedOut", AwsNamespaces.SNS, result)).build(); } catch (AwsException e) { - return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); + return xmlErrorResponse(e); } } @@ -283,7 +284,7 @@ public class SnsQueryHandler { return Response.ok(AwsQueryResponse.envelopeEmptyResult("OptInPhoneNumber", AwsNamespaces.SNS)) .build(); } catch (AwsException e) { - return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); + return xmlErrorResponse(e); } } @@ -298,7 +299,7 @@ public class SnsQueryHandler { String result = new XmlBuilder().elem("SubscriptionArn", sub.getSubscriptionArn()).build(); return Response.ok(AwsQueryResponse.envelope("Subscribe", AwsNamespaces.SNS, result)).build(); } catch (AwsException e) { - return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); + return xmlErrorResponse(e); } } @@ -308,7 +309,7 @@ public class SnsQueryHandler { snsService.unsubscribe(subscriptionArn, region); return Response.ok(AwsQueryResponse.envelopeNoResult("Unsubscribe", AwsNamespaces.SNS)).build(); } catch (AwsException e) { - return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); + return xmlErrorResponse(e); } } @@ -323,7 +324,7 @@ public class SnsQueryHandler { List subs = snsService.listSubscriptionsByTopic(topicArn, region); return buildSubscriptionListResponse("ListSubscriptionsByTopic", subs); } catch (AwsException e) { - return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); + return xmlErrorResponse(e); } } @@ -356,7 +357,7 @@ public class SnsQueryHandler { try { attributes = parseMessageAttributes(params, "MessageAttributes.entry."); } catch (AwsException e) { - return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); + return xmlErrorResponse(e); } try { @@ -366,7 +367,7 @@ public class SnsQueryHandler { String result = new XmlBuilder().elem("MessageId", messageId).build(); return Response.ok(AwsQueryResponse.envelope("Publish", AwsNamespaces.SNS, result)).build(); } catch (AwsException e) { - return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); + return xmlErrorResponse(e); } } @@ -379,7 +380,7 @@ public class SnsQueryHandler { String result = new XmlBuilder().elem("PlatformApplicationArn", app.getArn()).build(); return Response.ok(AwsQueryResponse.envelope("CreatePlatformApplication", AwsNamespaces.SNS, result)).build(); } catch (AwsException e) { - return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); + return xmlErrorResponse(e); } } @@ -400,7 +401,7 @@ public class SnsQueryHandler { xml.end("Attributes"); return Response.ok(AwsQueryResponse.envelope("GetPlatformApplicationAttributes", AwsNamespaces.SNS, xml.build())).build(); } catch (AwsException e) { - return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); + return xmlErrorResponse(e); } } @@ -411,7 +412,7 @@ public class SnsQueryHandler { snsService.setPlatformApplicationAttributes(arn, attrs, region); return Response.ok(AwsQueryResponse.envelopeNoResult("SetPlatformApplicationAttributes", AwsNamespaces.SNS)).build(); } catch (AwsException e) { - return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); + return xmlErrorResponse(e); } } @@ -439,7 +440,7 @@ public class SnsQueryHandler { String result = new XmlBuilder().elem("EndpointArn", endpoint.getArn()).build(); return Response.ok(AwsQueryResponse.envelope("CreatePlatformEndpoint", AwsNamespaces.SNS, result)).build(); } catch (AwsException e) { - return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); + return xmlErrorResponse(e); } } @@ -460,7 +461,7 @@ public class SnsQueryHandler { xml.end("Attributes"); return Response.ok(AwsQueryResponse.envelope("GetEndpointAttributes", AwsNamespaces.SNS, xml.build())).build(); } catch (AwsException e) { - return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); + return xmlErrorResponse(e); } } @@ -471,7 +472,7 @@ public class SnsQueryHandler { snsService.setEndpointAttributes(arn, attrs, region); return Response.ok(AwsQueryResponse.envelopeNoResult("SetEndpointAttributes", AwsNamespaces.SNS)).build(); } catch (AwsException e) { - return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); + return xmlErrorResponse(e); } } @@ -490,7 +491,7 @@ public class SnsQueryHandler { xml.end("Endpoints"); return Response.ok(AwsQueryResponse.envelope("ListEndpointsByPlatformApplication", AwsNamespaces.SNS, xml.build())).build(); } catch (AwsException e) { - return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); + return xmlErrorResponse(e); } } @@ -538,7 +539,7 @@ public class SnsQueryHandler { xml.end("Failed"); return Response.ok(AwsQueryResponse.envelope("PublishBatch", AwsNamespaces.SNS, xml.build())).build(); } catch (AwsException e) { - return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); + return xmlErrorResponse(e); } } @@ -553,7 +554,7 @@ public class SnsQueryHandler { xml.end("Attributes"); return Response.ok(AwsQueryResponse.envelope("GetSubscriptionAttributes", AwsNamespaces.SNS, xml.build())).build(); } catch (AwsException e) { - return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); + return xmlErrorResponse(e); } } @@ -565,7 +566,7 @@ public class SnsQueryHandler { snsService.setSubscriptionAttribute(subscriptionArn, attributeName, attributeValue, region); return Response.ok(AwsQueryResponse.envelopeNoResult("SetSubscriptionAttributes", AwsNamespaces.SNS)).build(); } catch (AwsException e) { - return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); + return xmlErrorResponse(e); } } @@ -577,7 +578,7 @@ public class SnsQueryHandler { String result = new XmlBuilder().elem("SubscriptionArn", subscriptionArn).build(); return Response.ok(AwsQueryResponse.envelope("ConfirmSubscription", AwsNamespaces.SNS, result)).build(); } catch (AwsException e) { - return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); + return xmlErrorResponse(e); } } @@ -588,7 +589,7 @@ public class SnsQueryHandler { snsService.tagResource(resourceArn, tags, region); return Response.ok(AwsQueryResponse.envelopeNoResult("TagResource", AwsNamespaces.SNS)).build(); } catch (AwsException e) { - return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); + return xmlErrorResponse(e); } } @@ -604,7 +605,7 @@ public class SnsQueryHandler { snsService.untagResource(resourceArn, tagKeys, region); return Response.ok(AwsQueryResponse.envelopeNoResult("UntagResource", AwsNamespaces.SNS)).build(); } catch (AwsException e) { - return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); + return xmlErrorResponse(e); } } @@ -623,7 +624,7 @@ public class SnsQueryHandler { xml.end("Tags"); return Response.ok(AwsQueryResponse.envelope("ListTagsForResource", AwsNamespaces.SNS, xml.build())).build(); } catch (AwsException e) { - return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); + return xmlErrorResponse(e); } } @@ -695,4 +696,14 @@ public class SnsQueryHandler { Response xmlErrorResponse(String code, String message, int status) { return AwsQueryResponse.error(code, message, AwsNamespaces.SNS, status); } + + /** The error {@code e} raises, with the members its shape declares beyond the message. */ + Response xmlErrorResponse(AwsException e) { + Map members = new TreeMap<>(); + if (e.getExtendedData() != null) { + e.getExtendedData().forEach((name, value) -> members.put(name, String.valueOf(value))); + } + return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.SNS, e.getHttpStatus(), + members); + } } diff --git a/src/main/java/io/github/hectorvent/floci/services/sns/SnsService.java b/src/main/java/io/github/hectorvent/floci/services/sns/SnsService.java index edb4add9e3..c55570891f 100644 --- a/src/main/java/io/github/hectorvent/floci/services/sns/SnsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/sns/SnsService.java @@ -475,8 +475,12 @@ public class SnsService implements Resettable, ResourceProvider { return; } if (!oneTimePassword.equals(number.getOneTimePassword())) { + // Status is a required member of the error and AWS documents no values for it, so it carries + // the number's own verification status, which a wrong password leaves Pending: the only + // statuses SNS publishes for a sandbox number are Pending and Verified. throw new AwsException("VerificationException", - "The one-time password does not match the one sent to " + phoneNumber + ".", 400); + "The one-time password does not match the one sent to " + phoneNumber + ".", 400, + Map.of("Status", number.getStatus())); } number.setStatus(SmsSandboxPhoneNumber.VERIFIED); number.setOneTimePassword(null); diff --git a/src/test/java/io/github/hectorvent/floci/core/common/AwsQueryResponseTest.java b/src/test/java/io/github/hectorvent/floci/core/common/AwsQueryResponseTest.java new file mode 100644 index 0000000000..5dfee75d91 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/core/common/AwsQueryResponseTest.java @@ -0,0 +1,38 @@ +package io.github.hectorvent.floci.core.common; + +import io.restassured.path.xml.XmlPath; +import jakarta.ws.rs.core.Response; +import org.junit.jupiter.api.Test; + +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class AwsQueryResponseTest { + + @Test + void anErrorCarriesTheMembersItsShapeDeclaresBesideTheMessage() { + Response response = AwsQueryResponse.error("VerificationException", "Wrong .", + AwsNamespaces.SNS, 400, Map.of("Status", "Pending")); + + XmlPath error = XmlPath.from((String) response.getEntity()).setRoot("ErrorResponse.Error"); + assertEquals(400, response.getStatus()); + assertEquals("Sender", error.getString("Type")); + assertEquals("VerificationException", error.getString("Code")); + assertEquals("Wrong .", error.getString("Message")); + assertEquals("Pending", error.getString("Status")); + } + + @Test + void anErrorWithNoOtherMembersIsTheSameAsBefore() { + Response withNone = AwsQueryResponse.error("NotFound", "Topic does not exist", AwsNamespaces.SNS, 404, + Map.of()); + Response plain = AwsQueryResponse.error("NotFound", "Topic does not exist", AwsNamespaces.SNS, 404); + + assertEquals(withoutRequestId(plain), withoutRequestId(withNone)); + } + + private static String withoutRequestId(Response response) { + return ((String) response.getEntity()).replaceAll("[^<]*", ""); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/sns/SnsSmsSandboxIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/sns/SnsSmsSandboxIntegrationTest.java index db51af8ec5..69069b564f 100644 --- a/src/test/java/io/github/hectorvent/floci/services/sns/SnsSmsSandboxIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/sns/SnsSmsSandboxIntegrationTest.java @@ -62,10 +62,10 @@ class SnsSmsSandboxIntegrationTest { assertEquals("Pending", statusOf(phoneNumber)); String code = codeTextedTo(phoneNumber); - String wrong = String.format("%06d", (Integer.parseInt(code) + 1) % 1_000_000); - query("VerifySMSSandboxPhoneNumber", Map.of("PhoneNumber", phoneNumber, "OneTimePassword", wrong)) + query("VerifySMSSandboxPhoneNumber", Map.of("PhoneNumber", phoneNumber, "OneTimePassword", anotherCode(code))) .statusCode(400) - .body("ErrorResponse.Error.Code", equalTo("VerificationException")); + .body("ErrorResponse.Error.Code", equalTo("VerificationException")) + .body("ErrorResponse.Error.Status", equalTo("Pending")); assertEquals("Pending", statusOf(phoneNumber)); query("VerifySMSSandboxPhoneNumber", Map.of("PhoneNumber", phoneNumber, "OneTimePassword", code)) @@ -130,8 +130,15 @@ class SnsSmsSandboxIntegrationTest { .formatted(phoneNumber)) .statusCode(200); + String code = codeTextedTo(phoneNumber); json("VerifySMSSandboxPhoneNumber", "{\"PhoneNumber\": \"%s\", \"OneTimePassword\": \"%s\"}" - .formatted(phoneNumber, codeTextedTo(phoneNumber))) + .formatted(phoneNumber, anotherCode(code))) + .statusCode(400) + .body("__type", equalTo("VerificationException")) + .body("Status", equalTo("Pending")); + + json("VerifySMSSandboxPhoneNumber", "{\"PhoneNumber\": \"%s\", \"OneTimePassword\": \"%s\"}" + .formatted(phoneNumber, code)) .statusCode(200); assertEquals("Verified", statusOf(phoneNumber)); @@ -217,6 +224,11 @@ class SnsSmsSandboxIntegrationTest { return byNumber; } + /** A well-formed one-time password that is not {@code code}. */ + private static String anotherCode(String code) { + return String.format("%06d", (Integer.parseInt(code) + 1) % 1_000_000); + } + private static ValidatableResponse query(String action, Map params) { RequestSpecification spec = given() .contentType("application/x-www-form-urlencoded")