feat(marketplace-metering): emulate AWS Marketplace Metering API (#3344)

This commit is contained in:
Franklin G. Mendoza authored and GitHub committed 2026-09-11 10:06:04 -04:00
1 parent f9f90cfc98
commit efcbc4d993
12 files changed
+652 -3

No files matched your search

@@ -0,0 +1,59 @@
package io.github.hectorvent.floci.services.marketplace;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.matchesPattern;
import static org.hamcrest.Matchers.notNullValue;
@QuarkusTest
class MarketplaceMeteringIntegrationTest {
@BeforeAll static void configure() { RestAssuredJsonUtils.configureAwsContentTypes(); }
@Test
void resolveCustomerIsStableForRegistrationToken() {
String body = "{\"RegistrationToken\":\"local-registration-token\"}";
var first = rpc("ResolveCustomer", body).statusCode(200)
.body("CustomerAWSAccountId", notNullValue()).body("LicenseArn", notNullValue())
.extract().response();
rpc("ResolveCustomer", body).statusCode(200)
.body("CustomerAWSAccountId", equalTo(first.path("CustomerAWSAccountId")))
.body("LicenseArn", equalTo(first.path("LicenseArn")));
}
@Test
void meterUsageIsIdempotentWithinHourForSameQuantity() {
long timestamp = System.currentTimeMillis() / 1000L;
String body = "{\"ProductCode\":\"prod-local\",\"Timestamp\":" + timestamp
+ ",\"UsageDimension\":\"requests\",\"UsageQuantity\":3}";
String recordId = rpc("MeterUsage", body).statusCode(200)
.body("MeteringRecordId", notNullValue()).extract().path("MeteringRecordId");
rpc("MeterUsage", body).statusCode(200).body("MeteringRecordId", equalTo(recordId));
}
@Test
void batchMeterUsageReportsDuplicateRecordForChangedQuantity() {
long timestamp = System.currentTimeMillis() / 1000L;
String base = "{\"ProductCode\":\"prod-local\",\"UsageRecords\":[{\"Timestamp\":" + timestamp
+ ",\"CustomerAWSAccountId\":\"123456789012\",\"Dimension\":\"users\",\"Quantity\":";
rpc("BatchMeterUsage", base + "1}]}").statusCode(200).body("Results[0].Status", equalTo("Success"));
rpc("BatchMeterUsage", base + "2}]}").statusCode(200).body("Results[0].Status", equalTo("DuplicateRecord"));
}
@Test
void registerUsageReturnsSignature() {
rpc("RegisterUsage", "{\"ProductCode\":\"prod-local\",\"PublicKeyVersion\":1,\"Nonce\":\"instance-1\"}")
.statusCode(200).body("Signature", matchesPattern("^[^.]+\\.[^.]+\\.[^.]+$"));
}
private static io.restassured.response.ValidatableResponse rpc(String action, String body) {
return given().contentType("application/x-amz-json-1.1").header("Authorization", auth())
.header("X-Amz-Target", "AWSMPMeteringService." + action).body(body).post("/").then();
}
private static String auth() { return "AWS4-HMAC-SHA256 Credential=000000000000/20260908/us-east-1/aws-marketplace/aws4_request"; }
}
@@ -0,0 +1,85 @@
package io.github.hectorvent.floci.services.marketplace;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
class MarketplaceMeteringServiceTest {
private final ObjectMapper mapper = new ObjectMapper();
private MarketplaceMeteringService service;
@BeforeEach
void setUp() {
service = new MarketplaceMeteringService(mapper, null,
AccountAwareStorageBackend.inMemory("000000000000"),
AccountAwareStorageBackend.inMemory("000000000000"),
AccountAwareStorageBackend.inMemory("000000000000"),
AccountAwareStorageBackend.inMemory("000000000000"));
}
@Test
void registerUsageReturnsPs256Jwt() throws Exception {
String jwt = service.handle("RegisterUsage", mapper.readTree(
"{\"ProductCode\":\"prod-local\",\"PublicKeyVersion\":1}"), "us-east-1")
.path("Signature").asText();
String header = new String(Base64.getUrlDecoder().decode(jwt.split("\\.")[0]), StandardCharsets.UTF_8);
assertEquals("PS256", mapper.readTree(header).path("alg").asText());
assertEquals(256, Base64.getUrlDecoder().decode(jwt.split("\\.")[2]).length);
}
@Test
void meteringStateIsRegionScoped() throws Exception {
long timestamp = System.currentTimeMillis() / 1000L;
JsonNode request = mapper.readTree("{\"ProductCode\":\"prod-local\",\"Timestamp\":" + timestamp
+ ",\"UsageDimension\":\"requests\",\"UsageQuantity\":1}");
String east = service.handle("MeterUsage", request, "us-east-1").path("MeteringRecordId").asText();
String west = service.handle("MeterUsage", request, "us-west-2").path("MeteringRecordId").asText();
assertNotEquals(east, west);
}
@Test
void genericValidationUsesValidationException() throws Exception {
long timestamp = System.currentTimeMillis() / 1000L;
AwsException quantity = assertThrows(AwsException.class, () -> service.handle("MeterUsage", mapper.readTree(
"{\"ProductCode\":\"prod-local\",\"Timestamp\":" + timestamp
+ ",\"UsageDimension\":\"requests\",\"UsageQuantity\":1.5}"), "us-east-1"));
assertEquals("ValidationException", quantity.getErrorCode());
AwsException records = assertThrows(AwsException.class, () -> service.handle("BatchMeterUsage", mapper.readTree(
"{\"ProductCode\":\"prod-local\",\"UsageRecords\":\"not-an-array\"}"), "us-east-1"));
assertEquals("ValidationException", records.getErrorCode());
AwsException clientToken = assertThrows(AwsException.class, () -> service.handle("MeterUsage", mapper.readTree(
"{\"ProductCode\":\"prod-local\",\"Timestamp\":" + timestamp
+ ",\"UsageDimension\":\"requests\",\"UsageQuantity\":1,\"ClientToken\":\""
+ "x".repeat(65) + "\"}"), "us-east-1"));
assertEquals("ValidationException", clientToken.getErrorCode());
AwsException nonce = assertThrows(AwsException.class, () -> service.handle("RegisterUsage", mapper.readTree(
"{\"ProductCode\":\"prod-local\",\"PublicKeyVersion\":1,\"Nonce\":\""
+ "x".repeat(256) + "\"}"), "us-east-1"));
assertEquals("ValidationException", nonce.getErrorCode());
}
@Test
void fractionalQuantityAndBlankProductCodeAreRejected() throws Exception {
long timestamp = System.currentTimeMillis() / 1000L;
assertThrows(AwsException.class, () -> service.handle("MeterUsage", mapper.readTree(
"{\"ProductCode\":\"prod-local\",\"Timestamp\":" + timestamp
+ ",\"UsageDimension\":\"requests\",\"UsageQuantity\":1.5}"), "us-east-1"));
assertThrows(AwsException.class, () -> service.handle("RegisterUsage", mapper.readTree(
"{\"ProductCode\":\"\",\"PublicKeyVersion\":1}"), "us-east-1"));
}
}