The JsonResourceChecker class is responsible for verifying the structure and content of a JSON file to ensure that it meets specific criteria. It checks whether the JSON file contains a valid structure with required fields and verifies that the "Resource" field does not contain a single asterisk '*'. The class contains a static method verifyInputJson(File inputFile) that when called returns false if the File contains a single 'asterisk' in Resource field and true if it contains any other content. Method throws a Runtime Exception if the file doesn't follow the AWS::IAM::Role Policy structure.
{
"PolicyName": "root",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Sid": "IamListAccess",
"Effect": "Allow",
"Action": [
"iam:ListRoles",
"iam:ListUsers"
],
"Resource": "*"
}
]
}
}To use the JsonResourceChecker class, follow these steps:
-
Clone the Repository Clone the repository containing the
JsonResourceCheckerclass into your local environment. -
Input JSON File Place your input JSON file (
input.json) in thesrc/main/resourcesdirectory within the project. -
Run the Main Method Open the
JsonResourceCheckerclass in your preferred Java development environment (e.g., IntelliJ IDEA, Eclipse). -
Run the Main Method Execute the
mainmethod in theJsonResourceCheckerclass. This will load the input JSON file, verify its structure and content, and print the result to the console.
You can run the JsonResourceChecker program by executing the main method in the JsonResourceChecker class. Ensure that your project setup includes the required dependencies (e.g., Jackson ObjectMapper for JSON processing).
public class JsonResourceChecker {
public static void main(String[] args) {
String resourcePath = "input.json"; // Relative path within the resources directory
File inputFile = FileResourceLoader.getResourceFile(resourcePath, JsonResourceChecker.class);
if (inputFile == null) {
System.err.println("Input JSON file not found: " + resourcePath);
return;
}
try {
boolean result = verifyInputJson(inputFile);
System.out.println("Resource field does not contain a single asterisk: " + result);
} catch (IOException e) {
System.err.println("Error reading or parsing JSON file: " + e.getMessage());
}
}
}- Ensure the input JSON file (
input.json) is located in thesrc/main/resourcesdirectory. - Run the
mainmethod in theJsonResourceCheckerclass. - Check the console output to see if the "Resource" field in the JSON file meets the required criteria.
- Make sure to replace
input.jsonwith your own JSON file name if it differs. - The
JsonResourceCheckerclass uses the Jackson library (ObjectMapper) for JSON parsing. Ensure the library is correctly configured in your project.
The JsonResourceCheckerTest class is used to test the functionality of the JsonResourceChecker class, which verifies the structure and content of JSON files.
-
testVerifyInputJson_ContainingAsteriskReturnFalse
- Verifies that
verifyInputJsonmethod returnsfalsewhen the input JSON file contains a single asterisk ('*') in the "Resource" field.
- Verifies that
-
testVerifyInputJson_NotContainingAsteriskReturnTrue
- Verifies that
verifyInputJsonmethod returnstruewhen the input JSON file does not contain a single asterisk in the "Resource" field.
- Verifies that
-
testVerifyInputJson_InvalidJsonMissingStatement
- Tests the behavior of
verifyInputJsonmethod when the input JSON file is missing the "Statement" field, which is required. - Expects a
RuntimeExceptionwith a specific error message related to the missing "Statement" field.
- Tests the behavior of
-
testVerifyInputJson_InvalidJsonMissingPolicyDocument
- Tests the behavior of
verifyInputJsonmethod when the input JSON file is missing the "PolicyDocument" field, which is required. - Expects a
RuntimeExceptionwith a specific error message related to the missing "PolicyDocument" field.
- Tests the behavior of
-
testVerifyInputJson_InvalidJsonMissingResource
- Tests the behavior of
verifyInputJsonmethod when the input JSON file is missing the "Resource" field within the "Statement" array. - Expects a
RuntimeExceptionwith a specific error message related to the missing "Resource" field.
- Tests the behavior of
-
Setup
- Ensure that you have the required dependencies and test framework configured (e.g., JUnit 5).
-
File Preparation
- Prepare the test JSON files (
valid_input.json,no_asterisk.json,missing_statement.json,missing_policy.json,missing_resource.json) and place them in the appropriate location within the resources directory (src/test/resources).
- Prepare the test JSON files (
-
Running Tests
- Open the
JsonResourceCheckerTestclass in your IDE. - Run each test method individually or execute all test methods to validate the behavior of the
JsonResourceCheckerclass under different scenarios.
- Open the