Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(bedrock): list guardrails in the tagging api and route its writes (#6646)

bercec4ad788e62cb
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ResourceGroupsTaggingDiscoveryTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ResourceGroupsTaggingDiscoveryTest.java
index 3550c7292c..4bad9efebb 100644
--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ResourceGroupsTaggingDiscoveryTest.java
+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ResourceGroupsTaggingDiscoveryTest.java
@@ -9,6 +9,8 @@ import software.amazon.awssdk.core.exception.SdkException;
import software.amazon.awssdk.services.apigateway.ApiGatewayClient;
import software.amazon.awssdk.services.apigateway.model.CreateApiKeyResponse;
import software.amazon.awssdk.services.apigateway.model.GetApiKeyResponse;
+import software.amazon.awssdk.services.bedrock.BedrockClient;
+import software.amazon.awssdk.services.bedrock.model.CreateGuardrailResponse;
import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;
import software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogGroupsResponse;
import software.amazon.awssdk.services.cloudwatchlogs.model.LogGroup;
@@ -46,6 +48,7 @@ class ResourceGroupsTaggingDiscoveryTest {
private static LambdaClient lambda;
private static CloudWatchLogsClient logs;
private static ApiGatewayClient apiGateway;
+ private static BedrockClient bedrock;
@BeforeAll
static void setup() {
@@ -54,6 +57,7 @@ class ResourceGroupsTaggingDiscoveryTest {
lambda = TestFixtures.lambdaClient();
logs = TestFixtures.cloudWatchLogsClient();
apiGateway = TestFixtures.apiGatewayClient();
+ bedrock = TestFixtures.bedrockClient();
}
@AfterAll
@@ -64,6 +68,7 @@ class ResourceGroupsTaggingDiscoveryTest {
lambda.close();
logs.close();
apiGateway.close();
+ bedrock.close();
}
}
@@ -154,6 +159,45 @@ class ResourceGroupsTaggingDiscoveryTest {
}
}
+ @Test
+ @DisplayName("Bedrock guardrails are found by type and tag, and tagging API writes are the guardrail's own tags")
+ void bedrockGuardrailIsDiscoveredAndTaggingApiWritesReachIt() {
+ String run = TestFixtures.uniqueName("run");
+ CreateGuardrailResponse created = bedrock.createGuardrail(request -> request
+ .name(TestFixtures.uniqueName("tag-discovery-guardrail"))
+ .blockedInputMessaging("Input blocked.")
+ .blockedOutputsMessaging("Output blocked.")
+ .tags(software.amazon.awssdk.services.bedrock.model.Tag.builder().key("Run").value(run).build()));
+ String arn = created.guardrailArn();
+
+ try {
+ GetResourcesResponse matched = tagging.getResources(request -> request
+ .resourceTypeFilters("bedrock:guardrail")
+ .tagFilters(tagFilter("Run", run)));
+
+ assertThat(matched.resourceTagMappingList()).singleElement().satisfies(mapping -> {
+ assertThat(mapping.resourceARN()).isEqualTo(arn);
+ assertThat(tags(mapping)).containsOnly(Map.entry("Run", run));
+ });
+
+ TagResourcesResponse tagged = tagging.tagResources(request -> request
+ .resourceARNList(arn)
+ .tags(Map.of("Owner", "compat")));
+
+ assertThat(tagged.failedResourcesMap()).isEmpty();
+ assertThat(guardrailTags(arn)).containsOnly(Map.entry("Run", run), Map.entry("Owner", "compat"));
+
+ UntagResourcesResponse untagged = tagging.untagResources(request -> request
+ .resourceARNList(arn)
+ .tagKeys("Owner"));
+
+ assertThat(untagged.failedResourcesMap()).isEmpty();
+ assertThat(guardrailTags(arn)).containsOnly(Map.entry("Run", run));
+ } finally {
+ bedrock.deleteGuardrail(request -> request.guardrailIdentifier(created.guardrailId()));
+ }
+ }
+
@Test
@DisplayName("Lambda functions tagged on CreateFunction are found by type and tag, and the ARN resolves")
void lambdaFunctionIsDiscoveredByTag() {
@@ -285,6 +329,12 @@ class ResourceGroupsTaggingDiscoveryTest {
return mapping.tags().stream().collect(Collectors.toMap(Tag::key, Tag::value));
}
+ private static Map<String, String> guardrailTags(String arn) {
+ return bedrock.listTagsForResource(request -> request.resourceARN(arn)).tags().stream()
+ .collect(Collectors.toMap(software.amazon.awssdk.services.bedrock.model.Tag::key,
+ software.amazon.awssdk.services.bedrock.model.Tag::value));
+ }
+
private static void deleteApiKeyAfterFailure(String id) {
try {
apiGateway.deleteApiKey(request -> request.apiKey(id));
diff --git a/docs/services/bedrock.md b/docs/services/bedrock.md
index 4f46d438a3..516bcc60b5 100644
--- a/docs/services/bedrock.md
+++ b/docs/services/bedrock.md
@@ -67,6 +67,10 @@ request, so `CreateGuardrail` and `TagResource` both raise `TooManyTagsException
the resulting total would pass 50. Replacing the value of a tag key the guardrail
already carries does not add to that total.
+Guardrails are listed by the Resource Groups Tagging API (resource type `bedrock:guardrail`)
+and Resource Explorer with their own tags, and `TagResources` / `UntagResources` there change
+those tags, as `TagResource` and `UntagResource` do.
+
## Not implemented
The rest of the Bedrock control plane is absent rather than stubbed, because
diff --git a/docs/services/resource-explorer.md b/docs/services/resource-explorer.md
index 5c40b43dd6..bc7cac957e 100644
--- a/docs/services/resource-explorer.md
+++ b/docs/services/resource-explorer.md
@@ -85,6 +85,7 @@ map as empty.
| ACM | `acm:certificate` |
| Amazon MQ | `mq:broker` |
| API Gateway | `apigateway:restapis`, `apigateway:restapis/stages`, `apigateway:apikeys`, `apigateway:usageplans`, `apigateway:domainnames` |
+| Bedrock | `bedrock:guardrail` |
| CloudFormation | `cloudformation:stack` |
| CloudWatch Logs | `logs:log-group` |
| Cognito | `cognito-idp:userpool` |
diff --git a/src/main/java/io/github/hectorvent/floci/services/bedrock/BedrockService.java b/src/main/java/io/github/hectorvent/floci/services/bedrock/BedrockService.java
index ff71d1a7ab..4017083780 100644
--- a/src/main/java/io/github/hectorvent/floci/services/bedrock/BedrockService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/bedrock/BedrockService.java
@@ -9,6 +9,9 @@ import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.core.common.Pagination;
import io.github.hectorvent.floci.core.common.RegionResolver;
+import io.github.hectorvent.floci.core.resource.ExplorerResource;
+import io.github.hectorvent.floci.core.resource.ResourceProvider;
+import io.github.hectorvent.floci.core.resource.SupportedResourceType;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.bedrock.model.Guardrail;
@@ -25,6 +28,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
+import java.util.Set;
import java.util.UUID;
import java.util.regex.Pattern;
@@ -39,7 +43,7 @@ import java.util.regex.Pattern;
* this signing name but serves the {@code /model/...} routes.
*/
@ApplicationScoped
-public class BedrockService {
+public class BedrockService implements ResourceProvider {
public static final String DRAFT_VERSION = "DRAFT";
public static final String READY = "READY";
@@ -274,6 +278,30 @@ public class BedrockService {
guardrails.put(storageKey(region, guardrail.getGuardrailId(), DRAFT_VERSION), guardrail);
}
+ // Resource discovery (Resource Groups Tagging API, Resource Explorer)
+
+ @Override
+ public List<ExplorerResource> getResources() {
+ List<ExplorerResource> resources = new ArrayList<>();
+ for (String key : guardrails.keys()) {
+ // Versions share the draft's ARN, and tags live on the draft, so only the draft is listed.
+ if (!key.endsWith("::" + DRAFT_VERSION)) {
+ continue;
+ }
+ guardrails.get(key).ifPresent(guardrail -> resources.add(new ExplorerResource(
+ guardrail.getGuardrailArn(), "bedrock:guardrail", "bedrock",
+ key.substring(0, key.indexOf("::")), guardrail.getAccountId(),
+ guardrail.getCreatedAt() != null ? guardrail.getCreatedAt() : Instant.now(),
+ guardrail.getTags() != null ? guardrail.getTags() : Map.of())));
+ }
+ return resources;
+ }
+
+ @Override
+ public Set<SupportedResourceType> getSupportedResourceTypes() {
+ return Set.of(new SupportedResourceType("bedrock:guardrail", "bedrock", true));
+ }
+
// Helpers
private void applyPolicies(Guardrail guardrail, JsonNode request, String region) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/bedrock/BedrockTagHandler.java b/src/main/java/io/github/hectorvent/floci/services/bedrock/BedrockTagHandler.java
new file mode 100644
index 0000000000..f22cd92684
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/bedrock/BedrockTagHandler.java
@@ -0,0 +1,48 @@
+package io.github.hectorvent.floci.services.bedrock;
+
+import io.github.hectorvent.floci.core.common.TagHandler;
+import io.github.hectorvent.floci.core.common.TaggingApiOnly;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Lets Resource Groups Tagging API writes reach a guardrail's own tags (#6235).
+ *
+ * <p>A separate {@link TaggingApiOnly} bean because Bedrock tags through its own
+ * {@code TagResource} action: a default-qualified handler would also serve guardrails on
+ * {@code /tags/{arn}}, which AWS does not define.
+ */
+@ApplicationScoped
+@TaggingApiOnly
+public class BedrockTagHandler implements TagHandler {
+
+ private final BedrockService bedrockService;
+
+ @Inject
+ public BedrockTagHandler(BedrockService bedrockService) {
+ this.bedrockService = bedrockService;
+ }
+
+ @Override
+ public String serviceKey() {
+ return "bedrock";
+ }
+
+ @Override
+ public Map<String, String> listTags(String region, String arn) {
+ return bedrockService.listTags(arn, region);
+ }
+
+ @Override
+ public void tagResource(String region, String arn, Map<String, String> tags) {
+ bedrockService.tagResource(arn, tags, region);
+ }
+
+ @Override
+ public void untagResource(String region, String arn, List<String> tagKeys) {
+ bedrockService.untagResource(arn, tagKeys, region);
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/resourcegroupstagging/ResourceGroupsTaggingDiscoveryIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/resourcegroupstagging/ResourceGroupsTaggingDiscoveryIntegrationTest.java
index 097985197b..8f3c3996b8 100644
--- a/src/test/java/io/github/hectorvent/floci/services/resourcegroupstagging/ResourceGroupsTaggingDiscoveryIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/resourcegroupstagging/ResourceGroupsTaggingDiscoveryIntegrationTest.java
@@ -647,6 +647,46 @@ class ResourceGroupsTaggingDiscoveryIntegrationTest {
""".formatted(queueUrl)).then().statusCode(200);
}
+ @Test
+ void bedrockGuardrailIsDiscoveredAndTaggingApiWritesReachIt() {
+ String marker = unique();
+ String value = unique();
+ Response created = given()
+ .contentType("application/json")
+ .body("""
+ {"name": "discovery-%s", "blockedInputMessaging": "in", "blockedOutputsMessaging": "out",
+ "tags": [{"key": "fd", "value": "%s"}]}
+ """.formatted(marker, marker))
+ .when()
+ .post("/guardrails");
+ created.then().statusCode(202);
+ String arn = created.path("guardrailArn");
+ String guardrailId = created.path("guardrailId");
+
+ getResources(markerFilter("bedrock:guardrail", marker))
+ .body("ResourceTagMappingList.ResourceARN", contains(arn));
+
+ tagAddedThroughTaggingApi(arn, value);
+ guardrailTags(arn)
+ .body("tags.find { it.key == 'fd' }.value", equalTo(marker))
+ .body("tags.find { it.key == 'added' }.value", equalTo(value));
+ addedTagIsListedOnce(arn, value);
+
+ untagAddedThroughTaggingApi(arn);
+ guardrailTags(arn)
+ .body("tags.key", not(hasItem("added")));
+ addedTagIsNotListed(arn);
+
+ // Bedrock tags through its own actions, so the shared tags paths still reject a guardrail.
+ for (String path : List.of("/tags/{arn}", "/v1/tags/{arn}")) {
+ tagsPathRejectsArn(path, arn);
+ }
+
+ given().when().delete("/guardrails/" + guardrailId).then().statusCode(202);
+ getResources(markerFilter("bedrock:guardrail", marker))
+ .body("ResourceTagMappingList", empty());
+ }
+
@Test
void taggingApiReportsAnotherAccountsArn() {
String name = "discovery-" + unique();
@@ -996,6 +1036,18 @@ class ResourceGroupsTaggingDiscoveryIntegrationTest {
.statusCode(200);
}
+ private static ValidatableResponse guardrailTags(String arn) {
+ return given()
+ .contentType("application/json")
+ .body("""
+ {"resourceARN": "%s"}
+ """.formatted(arn))
+ .when()
+ .post("/listTagsForResource")
+ .then()
+ .statusCode(200);
+ }
+
private static ValidatableResponse lambdaTags(String arn) {
return given()
.when()