-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
issue #3437 add instanceIdentifier to remote index messages and other…
… review fixes Signed-off-by: Robin Arnold <robin.arnold@ibm.com>
- Loading branch information
1 parent
5aa9325
commit c446a15
Showing
22 changed files
with
343 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
fhir-core/src/main/java/com/ibm/fhir/core/util/LogSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* (C) Copyright IBM Corp. 2022 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.ibm.fhir.core.util; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Static support functions related to logging | ||
*/ | ||
public class LogSupport { | ||
private static final String MASK = "*****"; | ||
private static final Pattern PASSWORD_EQ_PATTERN = Pattern.compile("[^\"]password[= ]*\"([^\"]*)\"", Pattern.CASE_INSENSITIVE); | ||
private static final Pattern PASSWORD_PATTERN = Pattern.compile("\"password\"[: ]*\"([^\"]*)\"", Pattern.CASE_INSENSITIVE); | ||
|
||
/** | ||
* Hide any text in quotes following the token "password" to avoid writing secrets to log files | ||
* @param input | ||
* @return | ||
*/ | ||
public static String hidePassword(String input) { | ||
String result = hidePassword(input, PASSWORD_EQ_PATTERN); | ||
result = hidePassword(result, PASSWORD_PATTERN); | ||
return result; | ||
} | ||
|
||
/** | ||
* Replace any text matching the given pattern with the MASK value | ||
* @param input | ||
* @param pattern | ||
* @return | ||
*/ | ||
private static String hidePassword(String input, Pattern pattern) { | ||
final Matcher m = pattern.matcher(input); | ||
final StringBuffer result = new StringBuffer(); | ||
while (m.find()) { | ||
final String match = m.group(); | ||
final int start = m.start(); | ||
m.appendReplacement(result, | ||
match.substring(0, | ||
m.start(1) - start) | ||
+ MASK | ||
+ match.substring(m.end(1) - start, m.end() - start)); | ||
} | ||
m.appendTail(result); | ||
return result.toString(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
fhir-core/src/test/java/com/ibm/fhir/core/util/test/LogSupportTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* (C) Copyright IBM Corp. 2022 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.ibm.fhir.core.util.test; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
|
||
import org.testng.annotations.Test; | ||
|
||
import com.ibm.fhir.core.util.LogSupport; | ||
|
||
/** | ||
* Unit tests for {@link LogSupport} methods | ||
*/ | ||
public class LogSupportTest { | ||
|
||
@Test | ||
public void testPassReplaceEquals() { | ||
assertEquals(LogSupport.hidePassword("something password=\"change-password\" something else"), "something password=\"*****\" something else"); | ||
} | ||
|
||
@Test | ||
public void testPassReplaceEqualsWithSpace() { | ||
assertEquals(LogSupport.hidePassword("something password = \"change-password\" something else"), "something password = \"*****\" something else"); | ||
} | ||
|
||
@Test | ||
public void testPassReplaceJson() { | ||
assertEquals(LogSupport.hidePassword("something \"password\": \"change-password\" something else"), "something \"password\": \"*****\" something else"); | ||
} | ||
|
||
@Test | ||
public void testPassReplaceJsonCompact() { | ||
assertEquals(LogSupport.hidePassword("something \"password\":\"change-password\" something else"), "something \"password\":\"*****\" something else"); | ||
} | ||
|
||
@Test | ||
public void testPassReplaceMixed() { | ||
final String src = "org.apache.kafka.common.security.plain.PlainLoginModule required username=\"token\" password=\"change-password-\";"; | ||
final String tgt = src.replace("change-password-", "*****"); | ||
assertEquals(LogSupport.hidePassword(src), tgt); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
fhir-persistence/src/main/java/com/ibm/fhir/persistence/helper/RemoteIndexSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* (C) Copyright IBM Corp. 2022 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.ibm.fhir.persistence.helper; | ||
|
||
import java.time.Instant; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.logging.Logger; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.google.gson.JsonDeserializer; | ||
import com.google.gson.JsonPrimitive; | ||
import com.google.gson.JsonSerializer; | ||
import com.ibm.fhir.persistence.index.RemoteIndexMessage; | ||
|
||
/** | ||
* Utility methods supporting the fhir-remote-index consumer | ||
*/ | ||
public class RemoteIndexSupport { | ||
private static final Logger logger = Logger.getLogger(RemoteIndexSupport.class.getName()); | ||
private static final DateTimeFormatter formatter = DateTimeFormatter.ISO_INSTANT; | ||
|
||
/** | ||
* Get an instance of Gson configured to support serialization/deserialization of | ||
* remote index messages (sent through Kafka as strings) | ||
* @return | ||
*/ | ||
public static Gson getGson() { | ||
Gson gson = new GsonBuilder() | ||
.registerTypeAdapter(Instant.class, (JsonSerializer<Instant>) (value, type, context) -> | ||
new JsonPrimitive(formatter.format(value)) | ||
) | ||
.registerTypeAdapter(Instant.class, (JsonDeserializer<Instant>) (jsonElement, type, context) -> | ||
formatter.parse(jsonElement.getAsString(), Instant::from) | ||
) | ||
.create(); | ||
|
||
return gson; | ||
} | ||
|
||
/** | ||
* Unmarshall the JSON payload parameter as a RemoteIndexMessage | ||
* @param jsonPayload | ||
* @return | ||
*/ | ||
public static RemoteIndexMessage unmarshall(String jsonPayload) { | ||
try { | ||
Gson gson = getGson(); | ||
return gson.fromJson(jsonPayload, RemoteIndexMessage.class); | ||
} catch (Throwable t) { | ||
// We need to sink this error to avoid poison messages from | ||
// blocking the queues. | ||
// TODO. Perhaps push this to a dedicated error topic | ||
logger.severe("Not a RemoteIndexMessage. Ignoring: '" + jsonPayload + "'"); | ||
} | ||
return null; | ||
|
||
} | ||
|
||
/** | ||
* Marshall the RemoteIndexMessage to a JSON string | ||
* @param message | ||
* @return | ||
*/ | ||
public static String marshallToString(RemoteIndexMessage message) { | ||
Gson gson = getGson(); | ||
return gson.toJson(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.