mirror of
https://github.com/floci-io/floci.git
synced 2026-12-05 02:58:00 -05:00
fix(iam): enforce tag constraints across every IAM tagging action (#4485)
extractTags collapsed Tags.member.N into a map before any count was taken, so a 51-member list with one repeated key slipped through as 50 tags, and TagUser, TagRole, TagPolicy and TagOpenIDConnectProvider bounded nothing. The request-shape rules now live in the handler, on the members as sent: tagListType and tagKeyListType are capped at 50 for every tag and untag action, keys must be 1-128 characters and values at most 256, both matching IAM's tag character pattern, with errors naming the offending member index. Users, roles, policies and OIDC providers now also enforce the 50-tag per-resource quota (LimitExceeded, as instance and SAML providers already did), and the tag read-modify-write runs under a lock so two requests that each fit cannot together push a resource past it. Closes #4407
This commit is contained in:
1 parent
1451693cda
commit
64375691ac
5 files changed
+577
-65
No files matched your search
@@ -27,6 +27,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
@@ -38,6 +39,11 @@ import java.util.stream.Stream;
|
||||
public class IamQueryHandler {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(IamQueryHandler.class);
|
||||
private static final int MAX_TAG_LIST_MEMBERS = 50;
|
||||
private static final int MAX_TAG_KEY_LENGTH = 128;
|
||||
private static final int MAX_TAG_VALUE_LENGTH = 256;
|
||||
private static final Pattern TAG_KEY_PATTERN = Pattern.compile("[\\p{L}\\p{Z}\\p{N}_.:/=+\\-@]+");
|
||||
private static final Pattern TAG_VALUE_PATTERN = Pattern.compile("[\\p{L}\\p{Z}\\p{N}_.:/=+\\-@]*");
|
||||
|
||||
private final IamService iamService;
|
||||
private final IamPolicyEvaluator policyEvaluator;
|
||||
@@ -380,7 +386,6 @@ public class IamQueryHandler {
|
||||
}
|
||||
|
||||
private Response handleCreateSAMLProvider(MultivaluedMap<String, String> params, String authorization) {
|
||||
checkSamlTagMembers(params);
|
||||
SAMLProvider provider = samlProviderService.create(regionResolver.getPartition(),
|
||||
accountResolver.resolve(authorization), getParam(params, "Name"),
|
||||
getParam(params, "SAMLMetadataDocument"), extractTags(params));
|
||||
@@ -417,7 +422,6 @@ public class IamQueryHandler {
|
||||
|
||||
private Response handleTagSAMLProvider(MultivaluedMap<String, String> params, String authorization) {
|
||||
String accountId = accountResolver.resolve(authorization);
|
||||
checkSamlTagMembers(params);
|
||||
samlProviderService.tag(accountId, getParam(params, "SAMLProviderArn"), extractTags(params));
|
||||
return Response.ok(AwsQueryResponse.envelopeNoResult("TagSAMLProvider", AwsNamespaces.IAM)).build();
|
||||
}
|
||||
@@ -1710,33 +1714,61 @@ public class IamQueryHandler {
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* Enforces {@code tagListType}'s {@code max: 50} on the members as sent. extractTags collapses
|
||||
* repeated keys into a map, and AWS resolves a repeated key by overwriting rather than
|
||||
* rejecting, so counting the map would let a 51-member list through whenever any key repeats.
|
||||
* Every IAM request that carries tags types them as {@code tagListType}, whose {@code max: 50}
|
||||
* binds the members as sent, so the count is taken here, before the map collapses repeated keys.
|
||||
*/
|
||||
private void checkSamlTagMembers(MultivaluedMap<String, String> params) {
|
||||
int members = 0;
|
||||
while (params.getFirst("Tags.member." + (members + 1) + ".Key") != null) {
|
||||
members++;
|
||||
}
|
||||
if (members > SAMLProviderService.MAX_TAGS_PER_SAML_PROVIDER) {
|
||||
throw new AwsException("ValidationError",
|
||||
"Value at 'tags' failed to satisfy constraint: Member must have length "
|
||||
+ "less than or equal to " + SAMLProviderService.MAX_TAGS_PER_SAML_PROVIDER, 400);
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, String> extractTags(MultivaluedMap<String, String> params) {
|
||||
Map<String, String> tags = new HashMap<>();
|
||||
List<Map.Entry<String, String>> members = new ArrayList<>();
|
||||
for (int i = 1; ; i++) {
|
||||
String key = params.getFirst("Tags.member." + i + ".Key");
|
||||
String value = params.getFirst("Tags.member." + i + ".Value");
|
||||
if (key == null) break;
|
||||
tags.put(key, value != null ? value : "");
|
||||
members.add(Map.entry(key, value != null ? value : ""));
|
||||
}
|
||||
checkListLength(members.size(), "tags");
|
||||
Map<String, String> tags = new HashMap<>();
|
||||
for (int i = 0; i < members.size(); i++) {
|
||||
String key = members.get(i).getKey();
|
||||
String value = members.get(i).getValue();
|
||||
String at = "tags." + (i + 1) + ".member";
|
||||
if (key.isEmpty()) {
|
||||
throw tagValidationError(key, at + ".key", "Member must have length greater than or equal to 1");
|
||||
}
|
||||
if (key.codePointCount(0, key.length()) > MAX_TAG_KEY_LENGTH) {
|
||||
throw tagValidationError(key, at + ".key",
|
||||
"Member must have length less than or equal to " + MAX_TAG_KEY_LENGTH);
|
||||
}
|
||||
if (!TAG_KEY_PATTERN.matcher(key).matches()) {
|
||||
throw tagValidationError(key, at + ".key",
|
||||
"Member must satisfy regular expression pattern: " + TAG_KEY_PATTERN.pattern());
|
||||
}
|
||||
if (value.codePointCount(0, value.length()) > MAX_TAG_VALUE_LENGTH) {
|
||||
throw tagValidationError(value, at + ".value",
|
||||
"Member must have length less than or equal to " + MAX_TAG_VALUE_LENGTH);
|
||||
}
|
||||
if (!TAG_VALUE_PATTERN.matcher(value).matches()) {
|
||||
throw tagValidationError(value, at + ".value",
|
||||
"Member must satisfy regular expression pattern: " + TAG_VALUE_PATTERN.pattern());
|
||||
}
|
||||
tags.put(key, value);
|
||||
}
|
||||
return tags;
|
||||
}
|
||||
|
||||
private static void checkListLength(int members, String param) {
|
||||
if (members > MAX_TAG_LIST_MEMBERS) {
|
||||
throw new AwsException("ValidationError",
|
||||
"1 validation error detected: Value at '" + param + "' failed to satisfy constraint: "
|
||||
+ "Member must have length less than or equal to " + MAX_TAG_LIST_MEMBERS, 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static AwsException tagValidationError(String value, String at, String constraint) {
|
||||
return new AwsException("ValidationError",
|
||||
"1 validation error detected: Value '" + value + "' at '" + at + "' failed to satisfy constraint: "
|
||||
+ constraint, 400);
|
||||
}
|
||||
|
||||
private List<String> getMemberList(MultivaluedMap<String, String> params, String name) {
|
||||
List<String> values = new ArrayList<>();
|
||||
for (int i = 1; ; i++) {
|
||||
@@ -1754,6 +1786,7 @@ public class IamQueryHandler {
|
||||
if (key == null) break;
|
||||
keys.add(key);
|
||||
}
|
||||
checkListLength(keys.size(), "tagKeys");
|
||||
return keys;
|
||||
}
|
||||
|
||||
|
||||
@@ -98,6 +98,12 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
/** Guards the read-modify-write in the OIDC provider mutators. */
|
||||
private final Object oidcProviderLock = new Object();
|
||||
|
||||
/**
|
||||
* Guards the tag read-modify-write on users, roles, policies and instance profiles, so two
|
||||
* requests that each fit the per-resource quota cannot together push a resource past it.
|
||||
*/
|
||||
private final Object tagLock = new Object();
|
||||
|
||||
/** CSPRNG for long-term secret access keys; ordinary resource IDs keep using {@link ThreadLocalRandom}. */
|
||||
private final SecureRandom secureRandom = new SecureRandom();
|
||||
|
||||
@@ -119,8 +125,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
/** pathType: a bare slash, or a slash-delimited run of {@code !}-{@code ~}. */
|
||||
private static final Pattern IAM_PATH_PATTERN = Pattern.compile("(/)|(/[\\x21-\\x7E]+/)");
|
||||
private static final int IAM_PATH_MAX_LENGTH = 512;
|
||||
/** {@code tagListType} / {@code tagKeyListType} are both {@code max: 50}. */
|
||||
private static final int MAX_TAGS_PER_INSTANCE_PROFILE = 50;
|
||||
private static final int MAX_TAGS_PER_RESOURCE = 50;
|
||||
private static final String ROOT_FEATURES_KEY = "org-root-features";
|
||||
private static final String CREDENTIAL_REPORT_KEY = "credential-report";
|
||||
/** AWS generates a fresh report only if the most recent one is older than this. */
|
||||
@@ -514,15 +519,19 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
}
|
||||
|
||||
public void tagUser(String userName, Map<String, String> newTags) {
|
||||
IamUser user = getUser(userName);
|
||||
user.getTags().putAll(newTags);
|
||||
users.put(userName, user);
|
||||
synchronized (tagLock) {
|
||||
IamUser user = getUser(userName);
|
||||
user.setTags(mergeTagsWithinQuota(user.getTags(), newTags, "TagsPerUser"));
|
||||
users.put(userName, user);
|
||||
}
|
||||
}
|
||||
|
||||
public void untagUser(String userName, List<String> tagKeys) {
|
||||
IamUser user = getUser(userName);
|
||||
tagKeys.forEach(user.getTags()::remove);
|
||||
users.put(userName, user);
|
||||
synchronized (tagLock) {
|
||||
IamUser user = getUser(userName);
|
||||
tagKeys.forEach(user.getTags()::remove);
|
||||
users.put(userName, user);
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, String> listUserTags(String userName) {
|
||||
@@ -874,15 +883,19 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
}
|
||||
|
||||
public void tagRole(String roleName, Map<String, String> newTags) {
|
||||
IamRole role = getRole(roleName);
|
||||
role.getTags().putAll(newTags);
|
||||
roles.put(roleName, role);
|
||||
synchronized (tagLock) {
|
||||
IamRole role = getRole(roleName);
|
||||
role.setTags(mergeTagsWithinQuota(role.getTags(), newTags, "TagsPerRole"));
|
||||
roles.put(roleName, role);
|
||||
}
|
||||
}
|
||||
|
||||
public void untagRole(String roleName, List<String> tagKeys) {
|
||||
IamRole role = getRole(roleName);
|
||||
tagKeys.forEach(role.getTags()::remove);
|
||||
roles.put(roleName, role);
|
||||
synchronized (tagLock) {
|
||||
IamRole role = getRole(roleName);
|
||||
tagKeys.forEach(role.getTags()::remove);
|
||||
roles.put(roleName, role);
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, String> listRoleTags(String roleName) {
|
||||
@@ -1241,16 +1254,20 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
|
||||
public void tagPolicy(String policyArn, Map<String, String> newTags) {
|
||||
rejectIfAwsManaged(policyArn);
|
||||
IamPolicy policy = getPolicy(policyArn);
|
||||
policy.getTags().putAll(newTags);
|
||||
policies.put(policyArn, policy);
|
||||
synchronized (tagLock) {
|
||||
IamPolicy policy = getPolicy(policyArn);
|
||||
policy.setTags(mergeTagsWithinQuota(policy.getTags(), newTags, "TagsPerPolicy"));
|
||||
policies.put(policyArn, policy);
|
||||
}
|
||||
}
|
||||
|
||||
public void untagPolicy(String policyArn, List<String> tagKeys) {
|
||||
rejectIfAwsManaged(policyArn);
|
||||
IamPolicy policy = getPolicy(policyArn);
|
||||
tagKeys.forEach(policy.getTags()::remove);
|
||||
policies.put(policyArn, policy);
|
||||
synchronized (tagLock) {
|
||||
IamPolicy policy = getPolicy(policyArn);
|
||||
tagKeys.forEach(policy.getTags()::remove);
|
||||
policies.put(policyArn, policy);
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, String> listPolicyTags(String policyArn) {
|
||||
@@ -2062,9 +2079,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
}
|
||||
synchronized (oidcProviderLock) {
|
||||
OpenIDConnectProvider provider = getOpenIDConnectProvider(arn);
|
||||
Map<String, String> merged = new LinkedHashMap<>(provider.getTags());
|
||||
merged.putAll(newTags);
|
||||
provider.setTags(merged);
|
||||
provider.setTags(mergeTagsWithinQuota(provider.getTags(), newTags, "TagsPerOpenIdConnectProvider"));
|
||||
oidcProviders.put(arn, provider);
|
||||
}
|
||||
}
|
||||
@@ -2806,34 +2821,33 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
}
|
||||
|
||||
public void tagInstanceProfile(String instanceProfileName, Map<String, String> newTags) {
|
||||
// Request shape before resource lookup, matching untagInstanceProfile and AWS's order.
|
||||
// Name shape before resource lookup, matching untagInstanceProfile.
|
||||
validateIamResourceName(instanceProfileName, "InstanceProfileName");
|
||||
if (newTags != null && newTags.size() > MAX_TAGS_PER_INSTANCE_PROFILE) {
|
||||
throw new AwsException("ValidationError",
|
||||
"Value at 'tags' failed to satisfy constraint: Member must have length "
|
||||
+ "less than or equal to " + MAX_TAGS_PER_INSTANCE_PROFILE, 400);
|
||||
synchronized (tagLock) {
|
||||
InstanceProfile profile = getInstanceProfile(instanceProfileName);
|
||||
profile.setTags(mergeTagsWithinQuota(profile.getTags(), newTags, "TagsPerInstanceProfile"));
|
||||
instanceProfiles.put(instanceProfileName, profile);
|
||||
}
|
||||
InstanceProfile profile = getInstanceProfile(instanceProfileName);
|
||||
Map<String, String> merged = new LinkedHashMap<>(profile.getTags());
|
||||
}
|
||||
|
||||
private static Map<String, String> mergeTagsWithinQuota(Map<String, String> current,
|
||||
Map<String, String> newTags, String quota) {
|
||||
Map<String, String> merged = new LinkedHashMap<>(current);
|
||||
merged.putAll(newTags == null ? Map.of() : newTags);
|
||||
if (merged.size() > MAX_TAGS_PER_INSTANCE_PROFILE) {
|
||||
if (merged.size() > MAX_TAGS_PER_RESOURCE) {
|
||||
throw new AwsException("LimitExceeded",
|
||||
"Cannot exceed quota for TagsPerInstanceProfile: " + MAX_TAGS_PER_INSTANCE_PROFILE, 409);
|
||||
"Cannot exceed quota for " + quota + ": " + MAX_TAGS_PER_RESOURCE, 409);
|
||||
}
|
||||
profile.getTags().putAll(newTags == null ? Map.of() : newTags);
|
||||
instanceProfiles.put(instanceProfileName, profile);
|
||||
return merged;
|
||||
}
|
||||
|
||||
public void untagInstanceProfile(String instanceProfileName, List<String> tagKeys) {
|
||||
validateIamResourceName(instanceProfileName, "InstanceProfileName");
|
||||
if (tagKeys != null && tagKeys.size() > MAX_TAGS_PER_INSTANCE_PROFILE) {
|
||||
throw new AwsException("ValidationError",
|
||||
"Value at 'tagKeys' failed to satisfy constraint: Member must have length "
|
||||
+ "less than or equal to " + MAX_TAGS_PER_INSTANCE_PROFILE, 400);
|
||||
synchronized (tagLock) {
|
||||
InstanceProfile profile = getInstanceProfile(instanceProfileName);
|
||||
tagKeys.forEach(profile.getTags()::remove);
|
||||
instanceProfiles.put(instanceProfileName, profile);
|
||||
}
|
||||
InstanceProfile profile = getInstanceProfile(instanceProfileName);
|
||||
tagKeys.forEach(profile.getTags()::remove);
|
||||
instanceProfiles.put(instanceProfileName, profile);
|
||||
}
|
||||
|
||||
public Map<String, String> listInstanceProfileTags(String instanceProfileName) {
|
||||
|
||||
@@ -183,11 +183,6 @@ public class SAMLProviderService {
|
||||
}
|
||||
|
||||
public void untag(String accountId, String arn, List<String> tagKeys) {
|
||||
if (tagKeys != null && tagKeys.size() > MAX_TAGS_PER_SAML_PROVIDER) {
|
||||
throw new AwsException("ValidationError",
|
||||
"Value at 'tagKeys' failed to satisfy constraint: Member must have length "
|
||||
+ "less than or equal to " + MAX_TAGS_PER_SAML_PROVIDER, 400);
|
||||
}
|
||||
synchronized (providerLock) {
|
||||
SAMLProvider provider = getForAccount(accountId, arn);
|
||||
Map<String, String> remaining = new LinkedHashMap<>(provider.getTags());
|
||||
|
||||
Reference in new issue
Block a user