Skip to content

Commit e9cb2e7

Browse files
Merge branch 'master' into SNOW-1943092-migrate-JDBC-auth-test
2 parents 008c65e + ef81582 commit e9cb2e7

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

src/main/java/net/snowflake/client/jdbc/SnowflakeFileTransferAgent.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
import net.snowflake.client.log.ArgSupplier;
7171
import net.snowflake.client.log.SFLogger;
7272
import net.snowflake.client.log.SFLoggerFactory;
73+
import net.snowflake.client.util.SecretDetector;
7374
import net.snowflake.common.core.FileCompressionType;
7475
import net.snowflake.common.core.RemoteStoreFileEncryptionMaterial;
7576
import net.snowflake.common.core.SqlState;
@@ -1337,7 +1338,8 @@ private static JsonNode parseCommandInGS(SFStatement statement, String command)
13371338
}
13381339

13391340
JsonNode jsonNode = (JsonNode) result;
1340-
logger.debug("Response: {}", jsonNode.toString());
1341+
1342+
logger.debug("Response: {}", SecretDetector.maskSecrets(jsonNode.toString()));
13411343

13421344
SnowflakeUtil.checkErrorAndThrowException(jsonNode);
13431345
return jsonNode;

src/main/java/net/snowflake/client/util/SecretDetector.java

+23-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ public class SecretDetector {
7979
"(token|assertion content)" + "(['\"\\s:=]+)" + "([a-z0-9=/_\\-+]{8,})",
8080
Pattern.CASE_INSENSITIVE);
8181

82+
private static final Pattern ENCRYPTION_MATERIAL_PATTERN =
83+
Pattern.compile("\"encryptionMaterial\"\\s*:\\s*\\{.*?\\}", Pattern.CASE_INSENSITIVE);
84+
8285
// only attempt to find secrets in its leading 100Kb SNOW-30961
8386
private static final int MAX_LENGTH = 100 * 1000;
8487

@@ -222,7 +225,9 @@ public static String maskSASToken(String text) {
222225
public static String maskSecrets(String text) {
223226
return filterAccessTokens(
224227
filterConnectionTokens(
225-
filterPassword(filterSASTokens(filterAWSKeys(filterOAuthTokens(text))))));
228+
filterPassword(
229+
filterSASTokens(
230+
filterAWSKeys(filterOAuthTokens(filterEncryptionMaterial(text)))))));
226231
}
227232

228233
/**
@@ -283,6 +288,23 @@ public static String filterAccessTokens(String message) {
283288
return message;
284289
}
285290

291+
/**
292+
* Filter encryption material that may be buried inside a JSON string.
293+
*
294+
* @param message the message text which may contain encryption material
295+
* @return Return filtered message
296+
*/
297+
public static String filterEncryptionMaterial(String message) {
298+
Matcher matcher =
299+
ENCRYPTION_MATERIAL_PATTERN.matcher(
300+
message.length() <= MAX_LENGTH ? message : message.substring(0, MAX_LENGTH));
301+
302+
if (matcher.find()) {
303+
return matcher.replaceAll("\"encryptionMaterial\" : ****");
304+
}
305+
return message;
306+
}
307+
286308
public static JSONObject maskJsonObject(JSONObject json) {
287309
for (Map.Entry<String, Object> entry : json.entrySet()) {
288310
if (entry.getValue() instanceof String) {

src/test/java/net/snowflake/client/util/SecretDetectorTest.java

+23
Original file line numberDiff line numberDiff line change
@@ -421,4 +421,27 @@ public void testMaskJacksonObject() {
421421
"Nested Jackson array node is not masked successfully",
422422
maskedNestedArrayStr.equals(SecretDetector.maskJacksonNode(objNode4).toString()));
423423
}
424+
425+
@Test
426+
public void testEncryptionMaterialFilter() throws Exception {
427+
String messageText =
428+
"{\"data\":"
429+
+ "{\"autoCompress\":true,"
430+
+ "\"overwrite\":false,"
431+
+ "\"clientShowEncryptionParameter\":true,"
432+
+ "\"encryptionMaterial\":{\"queryStageMasterKey\":\"asdfasdfasdfasdf==\",\"queryId\":\"01b6f5ba-0002-0181-0000-11111111da\",\"smkId\":1111},"
433+
+ "\"stageInfo\":{\"locationType\":\"AZURE\", \"region\":\"eastus2\"}";
434+
435+
String filteredMessageText =
436+
"{\"data\":"
437+
+ "{\"autoCompress\":true,"
438+
+ "\"overwrite\":false,"
439+
+ "\"clientShowEncryptionParameter\":true,"
440+
+ "\"encryptionMaterial\" : ****,"
441+
+ "\"stageInfo\":{\"locationType\":\"AZURE\", \"region\":\"eastus2\"}";
442+
443+
String result = SecretDetector.filterEncryptionMaterial(messageText);
444+
445+
assertEquals(filteredMessageText, result);
446+
}
424447
}

0 commit comments

Comments
 (0)