mirror of
https://github.com/floci-io/floci.git
synced 2026-12-02 09:47:56 -05:00
fix(iam): generate resource and request tag condition context (#7589)
This commit is contained in:
1 parent
cf4b3f0e82
commit
c3058a8de4
9 files changed
+863
-59
No files matched your search
+48
-8
@@ -29,6 +29,7 @@ import jakarta.enterprise.inject.Instance;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.MultivaluedHashMap;
|
||||
import jakarta.ws.rs.core.MultivaluedMap;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
@@ -48,6 +49,7 @@ import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Builds the IAM request context: the condition keys a policy's Condition block can match,
|
||||
@@ -235,7 +237,8 @@ public class IamConditionContextResolver {
|
||||
private Map<String, List<String>> iamConditionContext(String action, ContainerRequestContext ctx) {
|
||||
Map<String, String> members = IamConditionKeys.members(action);
|
||||
Map<String, String> targets = IamConditionKeys.targets(action);
|
||||
if (members.isEmpty() && targets.isEmpty()) {
|
||||
Map<String, IamConditionKeys.RequestTags> requestTags = IamConditionKeys.requestTags(action);
|
||||
if (members.isEmpty() && targets.isEmpty() && requestTags.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
Map<String, String> form = formParameters(ctx);
|
||||
@@ -246,24 +249,61 @@ public class IamConditionContextResolver {
|
||||
conditions.put(key, List.of(value));
|
||||
}
|
||||
});
|
||||
// iam:PermissionsBoundary on an existing role or user is the boundary stored on it, left off when it has none.
|
||||
targets.forEach((key, target) -> {
|
||||
String value = storedPermissionsBoundary(target, form.get(target));
|
||||
if (value != null) {
|
||||
conditions.put(key, List.of(value));
|
||||
if (key.contains("${TagKey}")) {
|
||||
Map<String, String> tags = storedTargetValue(target, form.get(target), IamRole::getTags, IamUser::getTags);
|
||||
if (tags != null) {
|
||||
tags.forEach((tag, value) -> conditions.put(key.replace("${TagKey}", tag), List.of(value)));
|
||||
}
|
||||
} else {
|
||||
String value = storedTargetValue(target, form.get(target),
|
||||
IamRole::getPermissionsBoundaryArn, IamUser::getPermissionsBoundaryArn);
|
||||
if (value != null) {
|
||||
conditions.put(key, List.of(value));
|
||||
}
|
||||
}
|
||||
});
|
||||
if (!requestTags.isEmpty()) {
|
||||
addIamRequestTags(conditions, form, requestTags);
|
||||
}
|
||||
return conditions.isEmpty() ? null : conditions;
|
||||
}
|
||||
|
||||
private String storedPermissionsBoundary(String target, String name) {
|
||||
private static void addIamRequestTags(Map<String, List<String>> conditions, Map<String, String> form,
|
||||
Map<String, IamConditionKeys.RequestTags> requestTags) {
|
||||
MultivaluedMap<String, String> parameters = new MultivaluedHashMap<>();
|
||||
form.forEach(parameters::putSingle);
|
||||
requestTags.forEach((key, fields) -> {
|
||||
List<String> keys = new ArrayList<>();
|
||||
for (AwsQueryLists.IndexedMember member : AwsQueryLists.members(parameters, fields.member(), null)) {
|
||||
String tag = form.get(member.prefix() + (fields.keyMember() == null ? "" : "." + fields.keyMember()));
|
||||
if (tag == null) {
|
||||
continue;
|
||||
}
|
||||
if ("aws:TagKeys".equals(key)) {
|
||||
if (!keys.contains(tag)) {
|
||||
keys.add(tag);
|
||||
}
|
||||
} else {
|
||||
conditions.put(key.replace("${TagKey}", tag),
|
||||
List.of(form.getOrDefault(member.prefix() + "." + fields.valueMember(), "")));
|
||||
}
|
||||
}
|
||||
if (!keys.isEmpty()) {
|
||||
conditions.put(key, keys);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private <T> T storedTargetValue(String target, String name,
|
||||
Function<IamRole, T> roleValue, Function<IamUser, T> userValue) {
|
||||
if (name == null || !iamService.isResolvable()) {
|
||||
return null;
|
||||
}
|
||||
String account = requestContext.getAccountId();
|
||||
return switch (target) {
|
||||
case "RoleName" -> iamService.get().findRole(account, name).map(IamRole::getPermissionsBoundaryArn).orElse(null);
|
||||
case "UserName" -> iamService.get().findUser(account, name).map(IamUser::getPermissionsBoundaryArn).orElse(null);
|
||||
case "RoleName" -> iamService.get().findRole(account, name).map(roleValue).orElse(null);
|
||||
case "UserName" -> iamService.get().findUser(account, name).map(userValue).orElse(null);
|
||||
default -> throw new IllegalStateException("Unknown IAM condition key target: " + target);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* The {@code iam:} condition keys an IAM request carries in its own members, and those read from the
|
||||
@@ -13,34 +14,43 @@ import java.util.Map;
|
||||
*/
|
||||
final class IamConditionKeys {
|
||||
|
||||
private record Table(Map<String, Map<String, String>> members, Map<String, Map<String, String>> targets) {}
|
||||
record RequestTags(String member, String keyMember, String valueMember) {}
|
||||
|
||||
private record Table(Map<String, Map<String, String>> members, Map<String, Map<String, String>> targets,
|
||||
Map<String, Map<String, RequestTags>> requestTags) {}
|
||||
|
||||
private static final Table TABLE = VendoredResources.read(
|
||||
"aws/iam-condition-keys.json", "IAM condition keys", input -> {
|
||||
JsonNode document = new ObjectMapper().readTree(input);
|
||||
return new Table(section(document.path("read"), "member"),
|
||||
section(document.path("fromTarget"), "target"));
|
||||
return new Table(section(document.path("read"), entry -> text(entry, "member")),
|
||||
section(document.path("fromTarget"), entry -> text(entry, "target")),
|
||||
section(document.path("requestTags"), entry -> new RequestTags(text(entry, "member"),
|
||||
entry.has("keyMember") ? text(entry, "keyMember") : null,
|
||||
entry.has("keyMember") ? text(entry, "valueMember") : null)));
|
||||
});
|
||||
|
||||
private IamConditionKeys() {}
|
||||
|
||||
private static Map<String, Map<String, String>> section(JsonNode entries, String field) {
|
||||
private static <T> Map<String, Map<String, T>> section(JsonNode entries, Function<JsonNode, T> value) {
|
||||
if (!entries.isArray() || entries.isEmpty()) {
|
||||
throw new IllegalStateException("Malformed IAM condition key table");
|
||||
}
|
||||
Map<String, Map<String, String>> byAction = new LinkedHashMap<>();
|
||||
Map<String, Map<String, T>> byAction = new LinkedHashMap<>();
|
||||
for (JsonNode entry : entries) {
|
||||
if (!entry.path("action").isTextual() || !entry.path("key").isTextual()
|
||||
|| !entry.path(field).isTextual()) {
|
||||
throw new IllegalStateException("Malformed IAM condition key entry: " + entry);
|
||||
}
|
||||
byAction.computeIfAbsent(entry.get("action").asText(), action -> new LinkedHashMap<>())
|
||||
.put(entry.get("key").asText(), entry.get(field).asText());
|
||||
byAction.computeIfAbsent(text(entry, "action"), action -> new LinkedHashMap<>())
|
||||
.put(text(entry, "key"), value.apply(entry));
|
||||
}
|
||||
byAction.replaceAll((action, keys) -> Map.copyOf(keys));
|
||||
return Map.copyOf(byAction);
|
||||
}
|
||||
|
||||
private static String text(JsonNode entry, String field) {
|
||||
if (!entry.path(field).isTextual()) {
|
||||
throw new IllegalStateException("Malformed IAM condition key entry: " + entry);
|
||||
}
|
||||
return entry.get(field).asText();
|
||||
}
|
||||
|
||||
/** Each condition key a request for {@code action} carries, to the member that holds it; empty when none. */
|
||||
static Map<String, String> members(String action) {
|
||||
return TABLE.members().getOrDefault(action, Map.of());
|
||||
@@ -53,4 +63,8 @@ final class IamConditionKeys {
|
||||
static Map<String, String> targets(String action) {
|
||||
return TABLE.targets().getOrDefault(action, Map.of());
|
||||
}
|
||||
|
||||
static Map<String, RequestTags> requestTags(String action) {
|
||||
return TABLE.requestTags().getOrDefault(action, Map.of());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user