mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(cur): check every request against the model's constraints (#8236)
* fix(cur): check every request against the model's constraints * fix(cur): refuse report names the emitter cannot write to S3 * test(iam): modify a report definition with a billing view ARN the model accepts
This commit is contained in:
1 parent
392d79ef2f
commit
68a5f2a7f0
10 files changed
+361
-88
No files matched your search
@@ -29,8 +29,8 @@
|
||||
# from botocore's ECR model by tools/aws/regen_ecr_shapes.py.
|
||||
# Athena request constraints for every operation are generated from botocore's Athena model
|
||||
# by tools/aws/regen_athena_shapes.py.
|
||||
# src/main/resources/aws/cur-request-shapes.json, the Cost and Usage Reports tag operations'
|
||||
# request constraints, is generated from botocore's CUR model by tools/aws/regen_cur_shapes.py.
|
||||
# src/main/resources/aws/cur-request-shapes.json, the Cost and Usage Reports request constraints
|
||||
# for every operation, is generated from botocore's CUR model by tools/aws/regen_cur_shapes.py.
|
||||
# src/main/resources/aws/payment-cryptography-request-shapes.json, the Payment Cryptography
|
||||
# request constraints, is generated from botocore's model by
|
||||
# tools/aws/regen_payment_cryptography_shapes.py.
|
||||
|
||||
+16
-12
@@ -20,21 +20,25 @@ real Parquet artifacts in Floci's S3 service via the `floci-duck` sidecar.
|
||||
|
||||
## Validation rules
|
||||
|
||||
- `ReportName`: alphanumerics + `-_`, max 256 chars
|
||||
- `Tags`: up to 200 in one request, each a `Key` of 1 to 128 characters and a `Value` of up to 256. A report holds at most 50 tags, so a `PutReportDefinition` or `TagResource` that would leave it with more fails with `ValidationException`
|
||||
Every request is checked by `RequestConstraintCheck` against the types, required members, bounds,
|
||||
patterns and enums generated from botocore's CUR service model, so an invalid request fails with
|
||||
`ValidationException` before it reaches the report store. `tools/aws/regen_cur_shapes.py` vendors
|
||||
them; `make aws-data-sync` regenerates them and `make aws-data-check` checks for drift. Among them:
|
||||
|
||||
The tag operations, and the `Tags` of `PutReportDefinition`, are checked by `RequestConstraintCheck`
|
||||
against the types, required members and bounds generated from botocore's CUR service model.
|
||||
`tools/aws/regen_cur_shapes.py` vendors them; `make aws-data-sync` regenerates them and
|
||||
`make aws-data-check` checks for drift. The 50-tag limit is documented in the model's prose only,
|
||||
so Floci enforces it separately.
|
||||
- `TimeUnit`: `HOURLY` / `DAILY` / `MONTHLY`
|
||||
- `ReportName`: letters, digits and `!-_.*'()`, max 256 chars
|
||||
- `Tags`: up to 200 in one request, each a `Key` of 1 to 128 characters and a `Value` of up to 256
|
||||
- `TimeUnit`, `ReportVersioning`, `AdditionalArtifacts`, `AdditionalSchemaElements` and `S3Region`: the model's enums
|
||||
- Required: `ReportName`, `TimeUnit`, `Format`, `Compression`, `AdditionalSchemaElements`, `S3Bucket`, `S3Prefix`, `S3Region`
|
||||
|
||||
Floci also enforces:
|
||||
|
||||
- A report holds at most 50 tags, so a `PutReportDefinition` or `TagResource` that would leave it with
|
||||
more fails with `ValidationException`. The limit is documented in the model's prose only.
|
||||
- `Format`: `Parquet` (CSV emission not yet implemented; `textORcsv` returns `ValidationException`)
|
||||
- `Compression`: `Parquet` (`ZIP` / `GZIP` not yet implemented)
|
||||
- `ReportVersioning`: `CREATE_NEW_REPORT` / `OVERWRITE_REPORT`
|
||||
- `AdditionalArtifacts`: subset of `REDSHIFT` / `QUICKSIGHT` / `ATHENA`
|
||||
- `AdditionalSchemaElements`: subset of `RESOURCES` / `SPLIT_COST_ALLOCATION_DATA` / `MANUAL_DISCOUNT_COMPATIBILITY`
|
||||
- Required: `ReportName`, `TimeUnit`, `Format`, `Compression`, `S3Bucket`, `S3Region`
|
||||
- `ReportName` and `S3Prefix`: letters, digits, `-`, `_` and `.` (and `/` in a prefix), the characters of the
|
||||
S3 key the report is written to. A name or prefix with `!`, `*`, `'`, `(` or `)`, which the model allows,
|
||||
returns `ValidationException`
|
||||
|
||||
## Storage keys
|
||||
|
||||
|
||||
@@ -57,7 +57,6 @@ public class CurJsonHandler {
|
||||
}
|
||||
|
||||
private Response handlePutReportDefinition(JsonNode request, String region) {
|
||||
CurRequestShapes.checkTags(request.get("Tags"));
|
||||
ReportDefinition def = parseReportDefinition(request.path("ReportDefinition"));
|
||||
def.setTags(parseTags(request.path("Tags")));
|
||||
ReportDefinition created = service.putReportDefinition(def, region);
|
||||
@@ -136,10 +135,6 @@ public class CurJsonHandler {
|
||||
}
|
||||
|
||||
private ReportDefinition parseReportDefinition(JsonNode node) {
|
||||
if (node == null || !node.isObject() || node.isEmpty()) {
|
||||
throw new AwsException("ValidationException",
|
||||
"ReportDefinition is required.", 400);
|
||||
}
|
||||
ReportDefinition d = new ReportDefinition();
|
||||
d.setReportName(stringOrNull(node, "ReportName"));
|
||||
d.setTimeUnit(stringOrNull(node, "TimeUnit"));
|
||||
|
||||
@@ -4,8 +4,8 @@ import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.floci.aws.core.common.RequestConstraintCheck;
|
||||
|
||||
/**
|
||||
* Checks the Cost and Usage Reports tag operations, and the {@code Tags} of {@code PutReportDefinition},
|
||||
* against the constraints generated by {@code tools/aws/regen_cur_shapes.py}.
|
||||
* Checks every Cost and Usage Reports request against the constraints generated by
|
||||
* {@code tools/aws/regen_cur_shapes.py}.
|
||||
*/
|
||||
final class CurRequestShapes {
|
||||
|
||||
@@ -21,11 +21,4 @@ final class CurRequestShapes {
|
||||
CHECK.check(operation, request);
|
||||
}
|
||||
}
|
||||
|
||||
/** Checks a request's {@code Tags} member, when it has one, as a {@code TagList}. */
|
||||
static void checkTags(JsonNode tags) {
|
||||
if (tags != null && !tags.isNull()) {
|
||||
CHECK.checkShape("TagList", tags);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,6 @@ import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* AWS CUR (Cost and Usage Report) management plane.
|
||||
@@ -43,11 +42,6 @@ public class CurService {
|
||||
/** AWS hard limit per account: 5 report definitions. */
|
||||
private static final int MAX_REPORTS_PER_ACCOUNT = 5;
|
||||
private static final int MAX_TAGS_PER_REPORT = 50;
|
||||
/** AWS allows the report name to contain alphanumerics, hyphens, and underscores. */
|
||||
private static final Pattern REPORT_NAME_PATTERN =
|
||||
Pattern.compile("[A-Za-z0-9_-]+");
|
||||
|
||||
private static final Set<String> ALLOWED_TIME_UNITS = Set.of("HOURLY", "DAILY", "MONTHLY");
|
||||
/**
|
||||
* Floci only emits Parquet at the moment. Real CUR also accepts
|
||||
* {@code textORcsv} but Floci's emission engine writes Parquet
|
||||
@@ -57,9 +51,6 @@ public class CurService {
|
||||
*/
|
||||
private static final Set<String> ALLOWED_FORMATS = Set.of("Parquet");
|
||||
private static final Set<String> ALLOWED_COMPRESSIONS = Set.of("Parquet");
|
||||
private static final Set<String> ALLOWED_VERSIONING = Set.of("CREATE_NEW_REPORT", "OVERWRITE_REPORT");
|
||||
private static final Set<String> ALLOWED_ARTIFACTS = Set.of("REDSHIFT", "QUICKSIGHT", "ATHENA");
|
||||
private static final Set<String> ALLOWED_SCHEMA_ELEMENTS = Set.of("RESOURCES", "SPLIT_COST_ALLOCATION_DATA", "MANUAL_DISCOUNT_COMPATIBILITY");
|
||||
|
||||
private final StorageBackend<String, ReportDefinition> store;
|
||||
private final RegionResolver regionResolver;
|
||||
@@ -273,51 +264,17 @@ public class CurService {
|
||||
return store.keys().size();
|
||||
}
|
||||
|
||||
/**
|
||||
* The checks the model leaves to Floci: its types, required members, patterns, lengths and enums are checked in
|
||||
* {@link CurJsonHandler#handle} from {@code cur-request-shapes.json}.
|
||||
*/
|
||||
private void validateReportDefinition(ReportDefinition d) {
|
||||
if (d == null) {
|
||||
throw new AwsException("ValidationException", "ReportDefinition is required.", 400);
|
||||
}
|
||||
requireNonEmpty(d.getReportName(), "ReportName");
|
||||
if (!REPORT_NAME_PATTERN.matcher(d.getReportName()).matches()) {
|
||||
throw new AwsException("ValidationException",
|
||||
"ReportName may only contain alphanumeric characters, hyphens, and underscores.", 400);
|
||||
}
|
||||
if (d.getReportName().length() > 256) {
|
||||
throw new AwsException("ValidationException",
|
||||
"ReportName must be at most 256 characters long.", 400);
|
||||
}
|
||||
requireOneOf(d.getTimeUnit(), ALLOWED_TIME_UNITS, "TimeUnit");
|
||||
requireOneOf(d.getFormat(), ALLOWED_FORMATS, "Format");
|
||||
requireOneOf(d.getCompression(), ALLOWED_COMPRESSIONS, "Compression");
|
||||
requireNonEmpty(d.getS3Bucket(), "S3Bucket");
|
||||
// The report name is a segment of the key ParquetEmitter writes, which takes fewer characters than the model.
|
||||
S3DestinationValidation.requireSafeKeySegment(d.getReportName(), "ReportName");
|
||||
S3DestinationValidation.requireValidBucketName(d.getS3Bucket(), "S3Bucket");
|
||||
if (d.getS3Prefix() != null) {
|
||||
S3DestinationValidation.requireSafeKeySegment(d.getS3Prefix(), "S3Prefix");
|
||||
}
|
||||
if (d.getS3Prefix() == null) {
|
||||
d.setS3Prefix("");
|
||||
}
|
||||
requireNonEmpty(d.getS3Region(), "S3Region");
|
||||
if (d.getReportVersioning() != null && !ALLOWED_VERSIONING.contains(d.getReportVersioning())) {
|
||||
throw new AwsException("ValidationException",
|
||||
"ReportVersioning must be one of " + ALLOWED_VERSIONING, 400);
|
||||
}
|
||||
if (d.getAdditionalArtifacts() != null) {
|
||||
for (String artifact : d.getAdditionalArtifacts()) {
|
||||
if (!ALLOWED_ARTIFACTS.contains(artifact)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"AdditionalArtifacts must be one of " + ALLOWED_ARTIFACTS, 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (d.getAdditionalSchemaElements() != null) {
|
||||
for (String element : d.getAdditionalSchemaElements()) {
|
||||
if (!ALLOWED_SCHEMA_ELEMENTS.contains(element)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"AdditionalSchemaElements must be one of " + ALLOWED_SCHEMA_ELEMENTS, 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
S3DestinationValidation.requireSafeKeySegment(d.getS3Prefix(), "S3Prefix");
|
||||
}
|
||||
|
||||
private static String compositeKey(String region, String reportName) {
|
||||
@@ -332,7 +289,6 @@ public class CurService {
|
||||
}
|
||||
|
||||
private static void requireOneOf(String value, Set<String> allowed, String field) {
|
||||
requireNonEmpty(value, field);
|
||||
if (!allowed.contains(value)) {
|
||||
throw new AwsException("ValidationException",
|
||||
field + " must be one of " + allowed, 400);
|
||||
|
||||
@@ -4,11 +4,108 @@
|
||||
"botocore": "botocore 1.43.118 (installed package)"
|
||||
},
|
||||
"operations": {
|
||||
"DeleteReportDefinition": "DeleteReportDefinitionRequest",
|
||||
"DescribeReportDefinitions": "DescribeReportDefinitionsRequest",
|
||||
"ListTagsForResource": "ListTagsForResourceRequest",
|
||||
"ModifyReportDefinition": "ModifyReportDefinitionRequest",
|
||||
"PutReportDefinition": "PutReportDefinitionRequest",
|
||||
"TagResource": "TagResourceRequest",
|
||||
"UntagResource": "UntagResourceRequest"
|
||||
},
|
||||
"shapes": {
|
||||
"AWSRegion": {
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"af-south-1",
|
||||
"ap-east-1",
|
||||
"ap-south-1",
|
||||
"ap-south-2",
|
||||
"ap-southeast-1",
|
||||
"ap-southeast-2",
|
||||
"ap-southeast-3",
|
||||
"ap-northeast-1",
|
||||
"ap-northeast-2",
|
||||
"ap-northeast-3",
|
||||
"ca-central-1",
|
||||
"eu-central-1",
|
||||
"eu-central-2",
|
||||
"eu-west-1",
|
||||
"eu-west-2",
|
||||
"eu-west-3",
|
||||
"eu-north-1",
|
||||
"eu-south-1",
|
||||
"eu-south-2",
|
||||
"me-central-1",
|
||||
"me-south-1",
|
||||
"sa-east-1",
|
||||
"us-east-1",
|
||||
"us-east-2",
|
||||
"us-west-1",
|
||||
"us-west-2",
|
||||
"cn-north-1",
|
||||
"cn-northwest-1"
|
||||
]
|
||||
},
|
||||
"AdditionalArtifact": {
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"REDSHIFT",
|
||||
"QUICKSIGHT",
|
||||
"ATHENA"
|
||||
]
|
||||
},
|
||||
"AdditionalArtifactList": {
|
||||
"type": "list",
|
||||
"member": "AdditionalArtifact"
|
||||
},
|
||||
"BillingViewArn": {
|
||||
"type": "string",
|
||||
"max": 128,
|
||||
"pattern": "(arn:aws(-cn)?:billing::[0-9]{12}:billingview/)?[a-zA-Z0-9_\\+=\\.\\-@].{1,30}"
|
||||
},
|
||||
"CompressionFormat": {
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"ZIP",
|
||||
"GZIP",
|
||||
"Parquet"
|
||||
]
|
||||
},
|
||||
"DeleteReportDefinitionRequest": {
|
||||
"type": "structure",
|
||||
"required": [
|
||||
"ReportName"
|
||||
],
|
||||
"members": {
|
||||
"ReportName": "ReportName"
|
||||
}
|
||||
},
|
||||
"DescribeReportDefinitionsRequest": {
|
||||
"type": "structure",
|
||||
"members": {
|
||||
"MaxResults": "MaxResults",
|
||||
"NextToken": "GenericString"
|
||||
}
|
||||
},
|
||||
"GenericString": {
|
||||
"type": "string",
|
||||
"max": 256,
|
||||
"pattern": "[A-Za-z0-9_\\.\\-=]*"
|
||||
},
|
||||
"LastDelivery": {
|
||||
"type": "string",
|
||||
"min": 16,
|
||||
"max": 20,
|
||||
"pattern": "[0-9]{8}[T][0-9]{6}([Z]|[+-][0-9]{4})"
|
||||
},
|
||||
"LastStatus": {
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"SUCCESS",
|
||||
"ERROR_PERMISSIONS",
|
||||
"ERROR_NO_BUCKET"
|
||||
]
|
||||
},
|
||||
"ListTagsForResourceRequest": {
|
||||
"type": "structure",
|
||||
"required": [
|
||||
@@ -18,11 +115,111 @@
|
||||
"ReportName": "ReportName"
|
||||
}
|
||||
},
|
||||
"MaxResults": {
|
||||
"type": "integer",
|
||||
"min": 5,
|
||||
"max": 5
|
||||
},
|
||||
"ModifyReportDefinitionRequest": {
|
||||
"type": "structure",
|
||||
"required": [
|
||||
"ReportName",
|
||||
"ReportDefinition"
|
||||
],
|
||||
"members": {
|
||||
"ReportName": "ReportName",
|
||||
"ReportDefinition": "ReportDefinition"
|
||||
}
|
||||
},
|
||||
"PutReportDefinitionRequest": {
|
||||
"type": "structure",
|
||||
"required": [
|
||||
"ReportDefinition"
|
||||
],
|
||||
"members": {
|
||||
"ReportDefinition": "ReportDefinition",
|
||||
"Tags": "TagList"
|
||||
}
|
||||
},
|
||||
"RefreshClosedReports": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"ReportDefinition": {
|
||||
"type": "structure",
|
||||
"required": [
|
||||
"ReportName",
|
||||
"TimeUnit",
|
||||
"Format",
|
||||
"Compression",
|
||||
"AdditionalSchemaElements",
|
||||
"S3Bucket",
|
||||
"S3Prefix",
|
||||
"S3Region"
|
||||
],
|
||||
"members": {
|
||||
"ReportName": "ReportName",
|
||||
"TimeUnit": "TimeUnit",
|
||||
"Format": "ReportFormat",
|
||||
"Compression": "CompressionFormat",
|
||||
"AdditionalSchemaElements": "SchemaElementList",
|
||||
"S3Bucket": "S3Bucket",
|
||||
"S3Prefix": "S3Prefix",
|
||||
"S3Region": "AWSRegion",
|
||||
"AdditionalArtifacts": "AdditionalArtifactList",
|
||||
"RefreshClosedReports": "RefreshClosedReports",
|
||||
"ReportVersioning": "ReportVersioning",
|
||||
"BillingViewArn": "BillingViewArn",
|
||||
"ReportStatus": "ReportStatus"
|
||||
}
|
||||
},
|
||||
"ReportFormat": {
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"textORcsv",
|
||||
"Parquet"
|
||||
]
|
||||
},
|
||||
"ReportName": {
|
||||
"type": "string",
|
||||
"max": 256,
|
||||
"pattern": "[0-9A-Za-z!\\-_.*\\'()]+"
|
||||
},
|
||||
"ReportStatus": {
|
||||
"type": "structure",
|
||||
"members": {
|
||||
"lastDelivery": "LastDelivery",
|
||||
"lastStatus": "LastStatus"
|
||||
}
|
||||
},
|
||||
"ReportVersioning": {
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"CREATE_NEW_REPORT",
|
||||
"OVERWRITE_REPORT"
|
||||
]
|
||||
},
|
||||
"S3Bucket": {
|
||||
"type": "string",
|
||||
"max": 256,
|
||||
"pattern": "[A-Za-z0-9_\\.\\-]+"
|
||||
},
|
||||
"S3Prefix": {
|
||||
"type": "string",
|
||||
"max": 256,
|
||||
"pattern": "[0-9A-Za-z!\\-_.*\\'()/]*"
|
||||
},
|
||||
"SchemaElement": {
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"RESOURCES",
|
||||
"SPLIT_COST_ALLOCATION_DATA",
|
||||
"MANUAL_DISCOUNT_COMPATIBILITY"
|
||||
]
|
||||
},
|
||||
"SchemaElementList": {
|
||||
"type": "list",
|
||||
"member": "SchemaElement"
|
||||
},
|
||||
"Tag": {
|
||||
"type": "structure",
|
||||
"required": [
|
||||
@@ -69,6 +266,14 @@
|
||||
"max": 256,
|
||||
"pattern": ".*"
|
||||
},
|
||||
"TimeUnit": {
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"HOURLY",
|
||||
"DAILY",
|
||||
"MONTHLY"
|
||||
]
|
||||
},
|
||||
"UntagResourceRequest": {
|
||||
"type": "structure",
|
||||
"required": [
|
||||
|
||||
@@ -136,7 +136,7 @@ class CurIntegrationTest {
|
||||
"\"ReportName\":\"modify-target\"," +
|
||||
"\"TimeUnit\":\"MONTHLY\",\"Format\":\"Parquet\",\"Compression\":\"Parquet\"," +
|
||||
"\"AdditionalSchemaElements\":[\"RESOURCES\"]," +
|
||||
"\"S3Bucket\":\"new-bucket\",\"S3Region\":\"us-east-1\"" +
|
||||
"\"S3Bucket\":\"new-bucket\",\"S3Prefix\":\"reports/\",\"S3Region\":\"us-east-1\"" +
|
||||
"}}";
|
||||
given().contentType(CONTENT_TYPE)
|
||||
.header("X-Amz-Target", "AWSOrigamiServiceGatewayService.ModifyReportDefinition")
|
||||
@@ -155,7 +155,7 @@ class CurIntegrationTest {
|
||||
"\"ReportName\":\"missing-report\"," +
|
||||
"\"TimeUnit\":\"DAILY\",\"Format\":\"Parquet\",\"Compression\":\"Parquet\"," +
|
||||
"\"AdditionalSchemaElements\":[]," +
|
||||
"\"S3Bucket\":\"valid-bucket\",\"S3Region\":\"us-east-1\"}}";
|
||||
"\"S3Bucket\":\"valid-bucket\",\"S3Prefix\":\"reports/\",\"S3Region\":\"us-east-1\"}}";
|
||||
given().contentType(CONTENT_TYPE)
|
||||
.header("X-Amz-Target", "AWSOrigamiServiceGatewayService.ModifyReportDefinition")
|
||||
.header("Authorization", AUTH).body(modify)
|
||||
@@ -263,7 +263,7 @@ class CurIntegrationTest {
|
||||
"\"ReportName\":\"csv-attempt\"," +
|
||||
"\"TimeUnit\":\"DAILY\",\"Format\":\"textORcsv\",\"Compression\":\"GZIP\"," +
|
||||
"\"AdditionalSchemaElements\":[]," +
|
||||
"\"S3Bucket\":\"valid-bucket\",\"S3Region\":\"us-east-1\"}}";
|
||||
"\"S3Bucket\":\"valid-bucket\",\"S3Prefix\":\"reports/\",\"S3Region\":\"us-east-1\"}}";
|
||||
given().contentType(CONTENT_TYPE)
|
||||
.header("X-Amz-Target", "AWSOrigamiServiceGatewayService.PutReportDefinition")
|
||||
.header("Authorization", AUTH).body(body)
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
package io.floci.aws.services.cur;
|
||||
|
||||
import io.floci.aws.testing.AwsTestClients;
|
||||
import io.floci.aws.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.costandusagereport.CostAndUsageReportClient;
|
||||
import software.amazon.awssdk.services.costandusagereport.model.CompressionFormat;
|
||||
import software.amazon.awssdk.services.costandusagereport.model.ReportFormat;
|
||||
import software.amazon.awssdk.services.costandusagereport.model.SchemaElement;
|
||||
import software.amazon.awssdk.services.costandusagereport.model.TimeUnit;
|
||||
import software.amazon.awssdk.services.costandusagereport.model.ValidationException;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/** Every CUR request is checked against the constraints of the botocore model. */
|
||||
@QuarkusTest
|
||||
class CurRequestConstraintsIntegrationTest {
|
||||
|
||||
private static final String ACCOUNT = "100000008230";
|
||||
|
||||
private CostAndUsageReportClient cur;
|
||||
private String reportName;
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void createClient() {
|
||||
cur = AwsTestClients.create(CostAndUsageReportClient.builder(), ACCOUNT, Region.US_EAST_1);
|
||||
reportName = "constraints-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void deleteReportAndCloseClient() {
|
||||
try {
|
||||
cur.deleteReportDefinition(r -> r.reportName(reportName));
|
||||
} finally {
|
||||
cur.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void aReportNameWithADotIsAccepted() {
|
||||
reportName = "report.v1." + UUID.randomUUID().toString().substring(0, 8);
|
||||
|
||||
putReport();
|
||||
|
||||
assertEquals(1, storedReports());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aReportNameFlociCannotWriteToS3IsRefusedAndNotStored() {
|
||||
reportName = "report(" + UUID.randomUUID().toString().substring(0, 8) + ")";
|
||||
|
||||
assertThrows(ValidationException.class, this::putReport);
|
||||
assertEquals(0, storedReports());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDefinitionWithoutTheS3PrefixTheModelRequiresIsRefused() {
|
||||
given()
|
||||
.contentType("application/x-amz-json-1.1")
|
||||
.header("X-Amz-Target", "AWSOrigamiServiceGatewayService.PutReportDefinition")
|
||||
.header("Authorization",
|
||||
"AWS4-HMAC-SHA256 Credential=" + ACCOUNT + "/20260101/us-east-1/cur/aws4_request")
|
||||
.body("{\"ReportDefinition\":{\"ReportName\":\"" + reportName + "\",\"TimeUnit\":\"DAILY\","
|
||||
+ "\"Format\":\"Parquet\",\"Compression\":\"Parquet\","
|
||||
+ "\"AdditionalSchemaElements\":[\"RESOURCES\"],\"S3Bucket\":\"constraints-bucket\","
|
||||
+ "\"S3Region\":\"us-east-1\"}}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", containsString("ValidationException"))
|
||||
.body("message", containsString("S3Prefix"));
|
||||
assertEquals(0, storedReports());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aReportNameThatIsNotAStringIsRefusedAndDeletesNothing() {
|
||||
putReport();
|
||||
|
||||
given()
|
||||
.contentType("application/x-amz-json-1.1")
|
||||
.header("X-Amz-Target", "AWSOrigamiServiceGatewayService.DeleteReportDefinition")
|
||||
.header("Authorization",
|
||||
"AWS4-HMAC-SHA256 Credential=" + ACCOUNT + "/20260101/us-east-1/cur/aws4_request")
|
||||
.body("{\"ReportName\":5}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"));
|
||||
assertEquals(1, storedReports());
|
||||
}
|
||||
|
||||
private void putReport() {
|
||||
cur.putReportDefinition(r -> r.reportDefinition(d -> d.reportName(reportName).timeUnit(TimeUnit.DAILY)
|
||||
.format(ReportFormat.PARQUET).compression(CompressionFormat.PARQUET)
|
||||
.additionalSchemaElements(SchemaElement.RESOURCES).s3Bucket("constraints-bucket")
|
||||
.s3Prefix("reports").s3Region("us-east-1")));
|
||||
}
|
||||
|
||||
private long storedReports() {
|
||||
return cur.describeReportDefinitions(r -> { }).reportDefinitions().stream()
|
||||
.filter(definition -> definition.reportName().equals(reportName)).count();
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1233,7 +1233,7 @@ class IamRequestResourceEnforcementIntegrationTest {
|
||||
+ "}"), "cur:PutReportDefinition");
|
||||
cur(user, "PutReportDefinition", "{\"ReportDefinition\":" + reportDefinition(name, null) + "}").statusCode(200);
|
||||
cur(user, "ModifyReportDefinition", "{\"ReportName\":\"" + name + "\",\"ReportDefinition\":"
|
||||
+ reportDefinition(name, arn + "-other") + "}").statusCode(200);
|
||||
+ reportDefinition(name, "arn:aws:billing::" + ACCOUNT + ":billingview/primary") + "}").statusCode(200);
|
||||
refusedJson(cur(user, "ModifyReportDefinition", "{\"ReportName\":\"" + name + "-other\",\"ReportDefinition\":"
|
||||
+ reportDefinition(name + "-other", arn) + "}"), "cur:ModifyReportDefinition");
|
||||
cur(user, "DeleteReportDefinition", "{\"ReportName\":\"" + name + "\"}").statusCode(200);
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Vendor the Cost and Usage Reports tag operations' request constraints from botocore's Apache-2.0 service model.
|
||||
"""Vendor the Cost and Usage Reports request constraints from botocore's Apache-2.0 service model.
|
||||
|
||||
CurRequestShapes checks TagResource, UntagResource and ListTagsForResource, and the Tags of
|
||||
PutReportDefinition, through RequestConstraintCheck using the model's types, required members and bounds.
|
||||
CurRequestShapes checks every operation's request through RequestConstraintCheck using the model's
|
||||
types, required members, bounds, patterns and enums.
|
||||
|
||||
Run from anywhere in the repo:
|
||||
python3 tools/aws/regen_cur_shapes.py
|
||||
@@ -17,12 +17,11 @@ import request_shapes
|
||||
|
||||
OUTPUT = request_shapes.REPO_ROOT / "src/main/resources/aws/cur-request-shapes.json"
|
||||
MODEL = Path("cur/2017-01-06")
|
||||
OPERATIONS = ("ListTagsForResource", "TagResource", "UntagResource")
|
||||
|
||||
|
||||
def main(argv: list[str] | None = None) -> int:
|
||||
return request_shapes.main(argv, script=__file__, model=MODEL, output=OUTPUT,
|
||||
description=__doc__, operations=OPERATIONS)
|
||||
description=__doc__, constraints=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in new issue
Block a user