mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(iam): validate RoleName in every role action (#5391)
Every action whose RoleName is roleNameType now answers a bad name with ValidationError instead of NoSuchEntity, as CreateRole does. Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
1 parent
60837956de
commit
b2b083ee91
4 files changed
+79
-3
No files matched your search
@@ -65,7 +65,7 @@ type.
|
||||
| UntagRole | Removes tags from an IAM role. |
|
||||
| ListRoleTags | Lists tags stored for an IAM role. |
|
||||
|
||||
`CreateRole` checks `RoleName` as AWS does: 1-64 characters of `[\w+=,.@-]`, otherwise `ValidationError`. A colon is never valid, so no role can take the name of the EC2 instance identity role, `aws:ec2-instance`.
|
||||
`CreateRole` and every action that takes a `RoleName` (`GetRole`, `DeleteRole`, `UpdateRole`, `TagRole`, `AttachRolePolicy`, `PutRolePolicy` and the rest) check it as AWS does: 1-64 characters of `[\w+=,.@-]`, otherwise `ValidationError`. A colon is never valid, so no role can take the name of the EC2 instance identity role, `aws:ec2-instance`.
|
||||
|
||||
### Policies
|
||||
|
||||
|
||||
@@ -1306,7 +1306,7 @@ public class IamQueryHandler {
|
||||
}
|
||||
|
||||
private Response handleGetRole(MultivaluedMap<String, String> params) {
|
||||
IamRole role = iamService.getRole(getParam(params, "RoleName"));
|
||||
IamRole role = iamService.getValidatedRole(getParam(params, "RoleName"));
|
||||
String result = new XmlBuilder().start("Role").raw(roleXml(role, true)).end("Role").build();
|
||||
return Response.ok(AwsQueryResponse.envelope("GetRole", AwsNamespaces.IAM, result)).build();
|
||||
}
|
||||
|
||||
@@ -1049,6 +1049,11 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
"The role with name " + roleName + " cannot be found.", 404));
|
||||
}
|
||||
|
||||
public IamRole getValidatedRole(String roleName) {
|
||||
validateRoleName(roleName);
|
||||
return getRole(roleName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Looks up a role by name in a specific account's namespace, without throwing when absent.
|
||||
*
|
||||
@@ -1132,6 +1137,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
}
|
||||
|
||||
public void deleteRole(String roleName) {
|
||||
validateRoleName(roleName);
|
||||
IamRole role = getRole(roleName);
|
||||
requireNotServiceLinked(role, roleName);
|
||||
if (!role.getAttachedPolicyArns().isEmpty() || !role.getInlinePolicies().isEmpty()) {
|
||||
@@ -1220,6 +1226,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
if (roleName == null) {
|
||||
throw new AwsException("NoSuchEntity", "The request must include RoleName.", 404);
|
||||
}
|
||||
validateRoleName(roleName);
|
||||
IamRole role = getRole(roleName);
|
||||
// The path cannot classify a role — CreateRole will put an ordinary one under the
|
||||
// service-role prefix — so only roles minted here are deletable through this action. The
|
||||
@@ -1256,6 +1263,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
}
|
||||
|
||||
public void updateRole(String roleName, String description, int maxSessionDuration) {
|
||||
validateRoleName(roleName);
|
||||
IamRole role = getRole(roleName);
|
||||
requireNotServiceLinked(role, roleName);
|
||||
if (description != null) role.setDescription(description);
|
||||
@@ -1264,6 +1272,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
}
|
||||
|
||||
public void updateAssumeRolePolicy(String roleName, String policyDocument) {
|
||||
validateRoleName(roleName);
|
||||
IamRole role = getRole(roleName);
|
||||
requireNotServiceLinked(role, roleName);
|
||||
role.setAssumeRolePolicyDocument(policyDocument);
|
||||
@@ -1278,6 +1287,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
* that check and this call would silently receive the update meant for the original role.
|
||||
*/
|
||||
public void updateAssumeRolePolicy(String roleName, String policyDocument, String expectedRoleId) {
|
||||
validateRoleName(roleName);
|
||||
IamRole role = getRole(roleName);
|
||||
if (expectedRoleId != null && !expectedRoleId.equals(role.getRoleId())) {
|
||||
throw new AwsException("EntityAlreadyExists",
|
||||
@@ -1289,6 +1299,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
}
|
||||
|
||||
public void tagRole(String roleName, Map<String, String> newTags) {
|
||||
validateRoleName(roleName);
|
||||
synchronized (tagLock) {
|
||||
IamRole role = getRole(roleName);
|
||||
role.setTags(mergeTagsWithinQuota(role.getTags(), newTags, "TagsPerRole", true));
|
||||
@@ -1297,6 +1308,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
}
|
||||
|
||||
public void untagRole(String roleName, List<String> tagKeys) {
|
||||
validateRoleName(roleName);
|
||||
synchronized (tagLock) {
|
||||
IamRole role = getRole(roleName);
|
||||
removeTagsCaseInsensitive(role.getTags(), tagKeys);
|
||||
@@ -1305,6 +1317,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
}
|
||||
|
||||
public Map<String, String> listRoleTags(String roleName) {
|
||||
validateRoleName(roleName);
|
||||
return getRole(roleName).getTags();
|
||||
}
|
||||
|
||||
@@ -1761,6 +1774,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
// =========================================================================
|
||||
|
||||
public void attachRolePolicy(String roleName, String policyArn) {
|
||||
validateRoleName(roleName);
|
||||
IamRole role = getRole(roleName);
|
||||
requireNotServiceLinked(role, roleName);
|
||||
IamPolicy policy = requirePolicy(policyArn);
|
||||
@@ -1772,6 +1786,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
}
|
||||
|
||||
public void detachRolePolicy(String roleName, String policyArn) {
|
||||
validateRoleName(roleName);
|
||||
IamRole role = getRole(roleName);
|
||||
requireNotServiceLinked(role, roleName);
|
||||
if (!role.getAttachedPolicyArns().remove(policyArn)) {
|
||||
@@ -1783,6 +1798,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
}
|
||||
|
||||
public List<IamPolicy> listAttachedRolePolicies(String roleName, String pathPrefix) {
|
||||
validateRoleName(roleName);
|
||||
return getRole(roleName).getAttachedPolicyArns().stream()
|
||||
.flatMap(arn -> resolvePolicy(arn).stream())
|
||||
.filter(p -> pathPrefix == null || p.getPath().startsWith(pathPrefix))
|
||||
@@ -1860,6 +1876,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
// =========================================================================
|
||||
|
||||
public void putRolePolicy(String roleName, String policyName, String policyDocument) {
|
||||
validateRoleName(roleName);
|
||||
IamRole role = getRole(roleName);
|
||||
requireNotServiceLinked(role, roleName);
|
||||
Map<String, String> inlinePolicies = role.getInlinePolicies();
|
||||
@@ -1889,6 +1906,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
}
|
||||
|
||||
public String getRolePolicy(String roleName, String policyName) {
|
||||
validateRoleName(roleName);
|
||||
IamRole role = getRole(roleName);
|
||||
String doc = role.getInlinePolicies().get(policyName);
|
||||
if (doc == null) {
|
||||
@@ -1899,6 +1917,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
}
|
||||
|
||||
public void deleteRolePolicy(String roleName, String policyName) {
|
||||
validateRoleName(roleName);
|
||||
IamRole role = getRole(roleName);
|
||||
requireNotServiceLinked(role, roleName);
|
||||
if (role.getInlinePolicies().remove(policyName) == null) {
|
||||
@@ -1909,6 +1928,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
}
|
||||
|
||||
public List<String> listRolePolicies(String roleName) {
|
||||
validateRoleName(roleName);
|
||||
return new ArrayList<>(getRole(roleName).getInlinePolicies().keySet());
|
||||
}
|
||||
|
||||
@@ -2067,6 +2087,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
}
|
||||
|
||||
public void addRoleToInstanceProfile(String instanceProfileName, String roleName) {
|
||||
validateRoleName(roleName);
|
||||
InstanceProfile profile = getInstanceProfile(instanceProfileName);
|
||||
requireNotServiceLinked(getRole(roleName), roleName);
|
||||
List<String> roleNames = profile.getRoleNames();
|
||||
@@ -2083,6 +2104,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
}
|
||||
|
||||
public void removeRoleFromInstanceProfile(String instanceProfileName, String roleName) {
|
||||
validateRoleName(roleName);
|
||||
InstanceProfile profile = getInstanceProfile(instanceProfileName);
|
||||
// Tolerates an already-deleted role, so guard only what is still there.
|
||||
roles.get(roleName).ifPresent(role -> requireNotServiceLinked(role, roleName));
|
||||
@@ -2091,6 +2113,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
}
|
||||
|
||||
public List<InstanceProfile> listInstanceProfilesForRole(String roleName) {
|
||||
validateRoleName(roleName);
|
||||
getRole(roleName); // validates existence
|
||||
return instanceProfiles.scan(k -> true).stream()
|
||||
.filter(p -> p.getRoleNames().contains(roleName))
|
||||
@@ -4865,6 +4888,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
}
|
||||
|
||||
public void putRolePermissionsBoundary(String roleName, String permissionsBoundaryArn) {
|
||||
validateRoleName(roleName);
|
||||
IamRole role = getRole(roleName);
|
||||
requireNotServiceLinked(role, roleName);
|
||||
requirePolicy(permissionsBoundaryArn); // validate policy exists
|
||||
@@ -4874,6 +4898,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
}
|
||||
|
||||
public void deleteRolePermissionsBoundary(String roleName) {
|
||||
validateRoleName(roleName);
|
||||
IamRole role = getRole(roleName);
|
||||
requireNotServiceLinked(role, roleName);
|
||||
if (role.getPermissionsBoundaryArn() == null) {
|
||||
|
||||
@@ -9,6 +9,7 @@ import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
@@ -1589,7 +1590,15 @@ class IamIntegrationTest {
|
||||
.formParam("RoleName", name)
|
||||
.header("Authorization", IAM_AUTH)
|
||||
.when().post("/").then()
|
||||
.statusCode(404);
|
||||
.statusCode(400);
|
||||
|
||||
given()
|
||||
.formParam("Action", "ListRoles")
|
||||
.header("Authorization", IAM_AUTH)
|
||||
.when().post("/").then()
|
||||
.statusCode(200)
|
||||
.body("ListRolesResponse.ListRolesResult.Roles.member.RoleName.flatten()",
|
||||
not(hasItem(name)));
|
||||
}
|
||||
|
||||
given()
|
||||
@@ -1621,4 +1630,46 @@ class IamIntegrationTest {
|
||||
.when().post("/").then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(87)
|
||||
void roleActionsRejectANameOutsideTheRoleNamePattern() {
|
||||
String name = "aws:ec2-instance";
|
||||
String message = "1 validation error detected: Value '" + name + "' at 'roleName' failed to satisfy"
|
||||
+ " constraint: Member must satisfy regular expression pattern: [\\w+=,.@-]+";
|
||||
Map<String, Map<String, String>> actions = new LinkedHashMap<>();
|
||||
actions.put("GetRole", Map.of());
|
||||
actions.put("DeleteRole", Map.of());
|
||||
actions.put("UpdateRole", Map.of());
|
||||
actions.put("UpdateAssumeRolePolicy", Map.of("PolicyDocument", TRUST_POLICY));
|
||||
actions.put("TagRole", Map.of("Tags.member.1.Key", "k", "Tags.member.1.Value", "v"));
|
||||
actions.put("UntagRole", Map.of("TagKeys.member.1", "k"));
|
||||
actions.put("ListRoleTags", Map.of());
|
||||
actions.put("AttachRolePolicy", Map.of("PolicyArn", "arn:aws:iam::aws:policy/ReadOnlyAccess"));
|
||||
actions.put("DetachRolePolicy", Map.of("PolicyArn", "arn:aws:iam::aws:policy/ReadOnlyAccess"));
|
||||
actions.put("ListAttachedRolePolicies", Map.of());
|
||||
actions.put("PutRolePolicy", Map.of("PolicyName", "p", "PolicyDocument", TRUST_POLICY));
|
||||
actions.put("GetRolePolicy", Map.of("PolicyName", "p"));
|
||||
actions.put("DeleteRolePolicy", Map.of("PolicyName", "p"));
|
||||
actions.put("ListRolePolicies", Map.of());
|
||||
actions.put("ListInstanceProfilesForRole", Map.of());
|
||||
actions.put("PutRolePermissionsBoundary",
|
||||
Map.of("PermissionsBoundary", "arn:aws:iam::aws:policy/ReadOnlyAccess"));
|
||||
actions.put("DeleteRolePermissionsBoundary", Map.of());
|
||||
actions.put("AddRoleToInstanceProfile", Map.of("InstanceProfileName", "ip"));
|
||||
actions.put("RemoveRoleFromInstanceProfile", Map.of("InstanceProfileName", "ip"));
|
||||
actions.put("DeleteServiceLinkedRole", Map.of());
|
||||
|
||||
for (Map.Entry<String, Map<String, String>> action : actions.entrySet()) {
|
||||
given()
|
||||
.formParam("Action", action.getKey())
|
||||
.formParam("RoleName", name)
|
||||
.formParams(action.getValue())
|
||||
.header("Authorization", IAM_AUTH)
|
||||
.when().post("/").then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
|
||||
.body("ErrorResponse.Error.Message", equalTo(message));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user