mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(iam): validate RoleName in CreateRole (#5374)
Follow-up to #5289. Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
1 parent
477e3ef813
commit
cb673802e0
3 files changed
+85
No files matched your search
@@ -65,6 +65,8 @@ 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`.
|
||||
|
||||
### Policies
|
||||
|
||||
| Action | Description |
|
||||
|
||||
@@ -167,6 +167,8 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
/** CustomSuffix as AWS constrains it: 1-64 characters of {@code [\w+=,.@-]}. */
|
||||
private static final Pattern CUSTOM_SUFFIX_PATTERN = Pattern.compile("[\\w+=,.@-]{1,64}");
|
||||
private static final int ROLE_NAME_MAX_LENGTH = 64;
|
||||
/** roleNameType's character set. Excluding {@code :} is what reserves names such as {@code aws:ec2-instance} for AWS. */
|
||||
private static final Pattern ROLE_NAME_PATTERN = Pattern.compile("[\\w+=,.@-]+");
|
||||
/** groupNameType / instanceProfileNameType: 1-128 characters of {@code [\w+=,.@-]}. */
|
||||
private static final Pattern IAM_RESOURCE_NAME_PATTERN = Pattern.compile("[\\w+=,.@-]{1,128}");
|
||||
/**
|
||||
@@ -1011,6 +1013,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
public IamRole createRole(String roleName, String path, String assumeRolePolicyDocument,
|
||||
String description, int maxSessionDuration, Map<String, String> tags,
|
||||
String permissionsBoundaryArn) {
|
||||
validateRoleName(roleName);
|
||||
if (permissionsBoundaryArn != null) {
|
||||
requirePolicy(permissionsBoundaryArn); // validate before anything is created
|
||||
}
|
||||
@@ -1056,6 +1059,25 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
return roles.get(roleName);
|
||||
}
|
||||
|
||||
/** roleNameType: 1-64 characters of {@code [\w+=,.@-]}, reported the way AWS reports a model constraint. */
|
||||
private static void validateRoleName(String roleName) {
|
||||
String constraint;
|
||||
if (roleName == null) {
|
||||
constraint = "Member must not be null";
|
||||
} else if (roleName.isEmpty()) {
|
||||
constraint = "Member must have length greater than or equal to 1";
|
||||
} else if (roleName.length() > ROLE_NAME_MAX_LENGTH) {
|
||||
constraint = "Member must have length less than or equal to " + ROLE_NAME_MAX_LENGTH;
|
||||
} else if (!ROLE_NAME_PATTERN.matcher(roleName).matches()) {
|
||||
constraint = "Member must satisfy regular expression pattern: " + ROLE_NAME_PATTERN.pattern();
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
String value = roleName == null ? "null" : "'" + roleName + "'";
|
||||
throw new AwsException("ValidationError", "1 validation error detected: Value " + value
|
||||
+ " at 'roleName' failed to satisfy constraint: " + constraint, 400);
|
||||
}
|
||||
|
||||
/**
|
||||
* AWS publishes UnmodifiableEntity on twelve role actions, and its message names the linked
|
||||
* service the caller has to go through instead. This guards the eleven of them the emulator
|
||||
|
||||
@@ -9,6 +9,8 @@ import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
@@ -1560,4 +1562,63 @@ class IamIntegrationTest {
|
||||
.when().post("/").then()
|
||||
.statusCode(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(86)
|
||||
void createRoleRejectsANameOutsideTheRoleNamePattern() {
|
||||
String pattern = "Member must satisfy regular expression pattern: [\\w+=,.@-]+";
|
||||
Map<String, String> constraints = Map.of(
|
||||
"aws:ec2-instance", pattern,
|
||||
"bad/name", pattern,
|
||||
"x".repeat(65), "Member must have length less than or equal to 64");
|
||||
for (Map.Entry<String, String> entry : constraints.entrySet()) {
|
||||
String name = entry.getKey();
|
||||
given()
|
||||
.formParam("Action", "CreateRole")
|
||||
.formParam("RoleName", name)
|
||||
.formParam("AssumeRolePolicyDocument", TRUST_POLICY)
|
||||
.header("Authorization", IAM_AUTH)
|
||||
.when().post("/").then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
|
||||
.body("ErrorResponse.Error.Message", equalTo("1 validation error detected: Value '" + name
|
||||
+ "' at 'roleName' failed to satisfy constraint: " + entry.getValue()));
|
||||
|
||||
given()
|
||||
.formParam("Action", "GetRole")
|
||||
.formParam("RoleName", name)
|
||||
.header("Authorization", IAM_AUTH)
|
||||
.when().post("/").then()
|
||||
.statusCode(404);
|
||||
}
|
||||
|
||||
given()
|
||||
.formParam("Action", "CreateRole")
|
||||
.formParam("AssumeRolePolicyDocument", TRUST_POLICY)
|
||||
.header("Authorization", IAM_AUTH)
|
||||
.when().post("/").then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
|
||||
.body("ErrorResponse.Error.Message", equalTo("1 validation error detected: Value null"
|
||||
+ " at 'roleName' failed to satisfy constraint: Member must not be null"));
|
||||
|
||||
given()
|
||||
.formParam("Action", "CreateRole")
|
||||
.formParam("RoleName", "")
|
||||
.formParam("AssumeRolePolicyDocument", TRUST_POLICY)
|
||||
.header("Authorization", IAM_AUTH)
|
||||
.when().post("/").then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
|
||||
.body("ErrorResponse.Error.Message", equalTo("1 validation error detected: Value ''"
|
||||
+ " at 'roleName' failed to satisfy constraint: Member must have length greater than or equal to 1"));
|
||||
|
||||
given()
|
||||
.formParam("Action", "CreateRole")
|
||||
.formParam("RoleName", "Ok_+=,.@-" + "x".repeat(55))
|
||||
.formParam("AssumeRolePolicyDocument", TRUST_POLICY)
|
||||
.header("Authorization", IAM_AUTH)
|
||||
.when().post("/").then()
|
||||
.statusCode(200);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user