From a9ca2c3d080ee98c3d0c80f9b32051d75dfd88bd Mon Sep 17 00:00:00 2001 From: amin Date: Tue, 2 Jul 2024 03:00:39 +0100 Subject: [PATCH] docs: adding subject and main --- .../config-protector/ExerciseRunner.java | 22 ++++++ .../checkpoints/config-protector/README.md | 67 +++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 subjects/java/checkpoints/config-protector/ExerciseRunner.java create mode 100644 subjects/java/checkpoints/config-protector/README.md diff --git a/subjects/java/checkpoints/config-protector/ExerciseRunner.java b/subjects/java/checkpoints/config-protector/ExerciseRunner.java new file mode 100644 index 000000000..525ab850c --- /dev/null +++ b/subjects/java/checkpoints/config-protector/ExerciseRunner.java @@ -0,0 +1,22 @@ +import java.util.Arrays; + +public class ExerciseRunner { + public static void main(String[] args) { + ConfigProtector protector = new ConfigProtector(); + + // Test case 1 + String configFile1 = "username=admin\npassword=secret\nhost=localhost\n"; + List sensitiveKeys1 = Arrays.asList("password"); + System.out.println("Protected Config 1:\n" + protector.hideSensitiveData(configFile1, sensitiveKeys1)); + + // Test case 2 + String configFile2 = "apiKey=12345\napiSecret=abcdef\nendpoint=https://api.example.com\n"; + List sensitiveKeys2 = Arrays.asList("apiKey", "apiSecret"); + System.out.println("Protected Config 2:\n" + protector.hideSensitiveData(configFile2, sensitiveKeys2)); + + // Test case 3 + String configFile3 = "username=user\npassword=pass\n"; + List sensitiveKeys3 = Arrays.asList("username", "password"); + System.out.println("Protected Config 3:\n" + protector.hideSensitiveData(configFile3, sensitiveKeys3)); + } +} \ No newline at end of file diff --git a/subjects/java/checkpoints/config-protector/README.md b/subjects/java/checkpoints/config-protector/README.md new file mode 100644 index 000000000..a3bfce83c --- /dev/null +++ b/subjects/java/checkpoints/config-protector/README.md @@ -0,0 +1,67 @@ +## Config Protector + +### Instructions + +Create a class `ConfigProtector` that provides a method to hide sensitive data in a configuration file using regex. The method should replace sensitive values with asterisks. The configuration file will be provided as a string, and the keys for the sensitive data will be given in a list. + +### Expected Class + +```java +import java.util.List; + +public class ConfigProtector { + public String hideSensitiveData(String configFile, List sensitiveKeys) { + // Implementation to hide sensitive data in the configuration file using regex + } +} +``` + +### Usage + +Here is a possible `ExerciseRunner.java` to test your class: + +```java +import java.util.Arrays; + +public class ExerciseRunner { + public static void main(String[] args) { + ConfigProtector protector = new ConfigProtector(); + + // Test case 1 + String configFile1 = "username=admin\npassword=secret\nhost=localhost\n"; + List sensitiveKeys1 = Arrays.asList("password"); + System.out.println("Protected Config 1:\n" + protector.hideSensitiveData(configFile1, sensitiveKeys1)); + + // Test case 2 + String configFile2 = "apiKey=12345\napiSecret=abcdef\nendpoint=https://api.example.com\n"; + List sensitiveKeys2 = Arrays.asList("apiKey", "apiSecret"); + System.out.println("Protected Config 2:\n" + protector.hideSensitiveData(configFile2, sensitiveKeys2)); + + // Test case 3 + String configFile3 = "username=user\npassword=pass\n"; + List sensitiveKeys3 = Arrays.asList("username", "password"); + System.out.println("Protected Config 3:\n" + protector.hideSensitiveData(configFile3, sensitiveKeys3)); + } +} +``` + +### Expected Output + +```shell +$ javac *.java -d build +$ java -cp build ExerciseRunner +Protected Config 1: +username=admin +password=****** +host=localhost + +Protected Config 2: +apiKey=***** +apiSecret=****** +endpoint=https://api.example.com + +Protected Config 3: +username=**** +password=**** +$ +```