mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(s3control): take TagResource and UntagResource tag condition keys from generated data (#6677)
The Service Authorization Reference lists aws:RequestTag/${TagKey} and
aws:TagKeys as condition keys for s3:TagResource, and aws:TagKeys for
s3:UntagResource. botocore's s3control model says where the request
carries them: TagResource's Tags list in the body, UntagResource's
TagKeys in the tagKeys query parameters.
With IAM enforcement on, Floci checked both S3 Control actions with no
request tag keys. A policy that allows TagResource only for certain tag
values, or UntagResource only for certain keys, matched no request, so
a request it allows was denied, and a ForAllValues condition on
aws:TagKeys allowed UntagResource whatever keys it named.
tools/aws/regen_s3control_tag_condition_keys.py now generates
s3control-tag-condition-keys.json from the two sources, as the DynamoDB
condition key table is generated: every s3 action the reference gives
either key, and every S3 Control operation's request members that feed
them, found as the tag-on-create table finds tag members.
IamConditionContextResolver reads it for a request on an S3 Control
route. The make targets check the members offline against the pinned
botocore in CI and the actions online against AWS in the IAM data drift
workflow. S3's own bucket and object tag keys stay as written, since
they were measured against AWS and do not all match the reference.
This commit is contained in:
1 parent
8ad890148a
commit
f926968167
11 files changed
+853
-9
No files matched your search
@@ -13,6 +13,7 @@ import io.github.hectorvent.floci.services.ec2.Ec2Service;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Tag;
|
||||
import io.github.hectorvent.floci.services.iam.IamActionRegistry;
|
||||
import io.github.hectorvent.floci.services.iam.ResourceArnBuilder;
|
||||
import io.github.hectorvent.floci.services.s3.S3ControlTagConditionKeys;
|
||||
import io.github.hectorvent.floci.services.s3.S3ObjectActions;
|
||||
import io.github.hectorvent.floci.services.s3.S3ObjectKeys;
|
||||
import io.github.hectorvent.floci.services.s3.S3Service;
|
||||
@@ -296,6 +297,12 @@ public class IamConditionContextResolver {
|
||||
// ── S3 ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
private Map<String, List<String>> s3ConditionContext(String action, ContainerRequestContext ctx) {
|
||||
// S3 Control's request tag keys come from the generated table; S3's own below were measured
|
||||
// against AWS, and do not all match the Service Reference.
|
||||
if (IamActionRegistry.isS3ControlRequest("s3", ctx)) {
|
||||
return S3ControlTagConditionKeys.conditions(action, () -> bodyText(ctx),
|
||||
ctx.getUriInfo().getQueryParameters());
|
||||
}
|
||||
Map<String, List<String>> conditions = s3ActionConditionContext(action, ctx);
|
||||
// A version action is checked only for a request naming a version, and carries its id.
|
||||
if (S3ObjectActions.isVersionAction(action) && ctx.getUriInfo() != null) {
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.VendoredResources;
|
||||
import io.github.hectorvent.floci.core.common.XmlParser;
|
||||
import jakarta.ws.rs.core.MultivaluedMap;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* The request tag condition keys of an S3 Control request, {@code aws:RequestTag/<key>} and
|
||||
* {@code aws:TagKeys}, read from the table {@code tools/aws/regen_s3control_tag_condition_keys.py}
|
||||
* generates: the actions the Service Reference gives either key, and the request members botocore's
|
||||
* {@code s3control} model carries them in. S3 Control's routes are checked as the action of their
|
||||
* operation's name, so an action names its operation's members.
|
||||
*/
|
||||
public final class S3ControlTagConditionKeys {
|
||||
|
||||
private static final String REQUEST_TAG = "aws:RequestTag/${TagKey}";
|
||||
private static final String TAG_KEYS = "aws:TagKeys";
|
||||
private static final String REQUEST_TAG_PREFIX = "aws:RequestTag/";
|
||||
|
||||
private static final JsonNode TABLE = VendoredResources.read("aws/s3control-tag-condition-keys.json",
|
||||
"S3 Control tag condition keys", input -> {
|
||||
JsonNode document = new ObjectMapper().readTree(input);
|
||||
if (!document.path("actions").isObject() || document.path("actions").isEmpty()
|
||||
|| !document.path("members").isObject() || document.path("members").isEmpty()) {
|
||||
throw new IllegalStateException("Malformed S3 Control tag condition key table");
|
||||
}
|
||||
return document;
|
||||
});
|
||||
|
||||
private S3ControlTagConditionKeys() {}
|
||||
|
||||
/**
|
||||
* The request tag keys {@code action} takes, from the members of the request that carry them:
|
||||
* the tags in {@code body}, read as {@code S3ControlController} reads them, and the tag keys in
|
||||
* {@code query}. Null when the action takes neither key or the request carries no tags.
|
||||
*/
|
||||
public static Map<String, List<String>> conditions(String action, Supplier<String> body,
|
||||
MultivaluedMap<String, String> query) {
|
||||
JsonNode members = TABLE.path("members").path(action == null ? "" : action);
|
||||
Map<String, List<String>> conditions = new LinkedHashMap<>();
|
||||
if (takes(action, REQUEST_TAG)) {
|
||||
for (JsonNode member : members.path(REQUEST_TAG)) {
|
||||
bodyTags(member, body).forEach((key, value) -> conditions.put(REQUEST_TAG_PREFIX + key, List.of(value)));
|
||||
}
|
||||
}
|
||||
if (takes(action, TAG_KEYS)) {
|
||||
List<String> keys = new ArrayList<>();
|
||||
for (JsonNode member : members.path(TAG_KEYS)) {
|
||||
List<String> named = "querystring".equals(member.path("location").asText())
|
||||
? query.getOrDefault(member.path("member").asText(), List.of())
|
||||
: List.copyOf(bodyTags(member, body).keySet());
|
||||
named.stream().filter(key -> !keys.contains(key)).forEach(keys::add);
|
||||
}
|
||||
if (!keys.isEmpty()) {
|
||||
conditions.put(TAG_KEYS, keys);
|
||||
}
|
||||
}
|
||||
return conditions.isEmpty() ? null : conditions;
|
||||
}
|
||||
|
||||
private static boolean takes(String action, String key) {
|
||||
for (JsonNode taken : TABLE.path("actions").path(action == null ? "" : action)) {
|
||||
if (key.equals(taken.asText())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** The tags a body member holds, by the element its list wraps each tag in. */
|
||||
private static Map<String, String> bodyTags(JsonNode member, Supplier<String> body) {
|
||||
String path = member.path("member").asText();
|
||||
int open = path.indexOf('[');
|
||||
if (open < 0 || !member.has("key") || !member.has("value")) {
|
||||
return Map.of();
|
||||
}
|
||||
String element = path.substring(open + 1, path.length() - 1);
|
||||
return XmlParser.extractPairs(body.get(), element.isEmpty() ? "member" : element,
|
||||
member.path("key").asText(), member.path("value").asText());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user