-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2b39bae
commit 3edcc3d
Showing
3 changed files
with
88 additions
and
1 deletion.
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
29 changes: 29 additions & 0 deletions
29
...s/tavily/runtime/src/main/java/io/quarkiverse/langchain4j/tavily/ShadowSensitiveData.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,29 @@ | ||
package io.quarkiverse.langchain4j.tavily; | ||
|
||
import io.vertx.core.buffer.Buffer; | ||
import io.vertx.core.json.JsonObject; | ||
|
||
public class ShadowSensitiveData { | ||
|
||
private static final int NUMBER_VISIBLE_CHARS = 7; | ||
|
||
static String process(Buffer buffer, String field) { | ||
|
||
final JsonObject bodyJson = buffer.toJsonObject(); | ||
if (bodyJson.containsKey(field)) { | ||
|
||
final String apiKeyField = bodyJson.getString(field); | ||
String shadowedData; | ||
if (apiKeyField.length() < NUMBER_VISIBLE_CHARS) { | ||
shadowedData = "*".repeat(apiKeyField.length()); | ||
} else { | ||
shadowedData = apiKeyField.substring(0, NUMBER_VISIBLE_CHARS) + | ||
"*".repeat(apiKeyField.length() - NUMBER_VISIBLE_CHARS); | ||
} | ||
bodyJson.put(field, shadowedData); | ||
|
||
} | ||
return bodyJson.toString(); | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
...vily/runtime/src/test/java/io/quarkiverse/langchain4j/tavily/ShadowSensitiveDataTest.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,57 @@ | ||
package io.quarkiverse.langchain4j.tavily; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.vertx.core.buffer.Buffer; | ||
|
||
public class ShadowSensitiveDataTest { | ||
|
||
@Test | ||
public void testShadowLongApiKey() { | ||
|
||
String body = """ | ||
{ | ||
"api_key":"tvly-Lv123456789QWERTYUIOPlZXCVBNMASD" | ||
} | ||
"""; | ||
|
||
final Buffer buffer = Buffer.buffer(body); | ||
final String shadowedBody = ShadowSensitiveData.process(buffer, "api_key"); | ||
|
||
assertThat(shadowedBody) | ||
.isEqualTo("{\"api_key\":\"tvly-Lv******************************\"}"); | ||
} | ||
|
||
@Test | ||
public void testShadowSmallApiKey() { | ||
String body = """ | ||
{ | ||
"api_key":"tvly" | ||
} | ||
"""; | ||
|
||
final Buffer buffer = Buffer.buffer(body); | ||
final String shadowedBody = ShadowSensitiveData.process(buffer, "api_key"); | ||
|
||
assertThat(shadowedBody) | ||
.isEqualTo("{\"api_key\":\"****\"}"); | ||
} | ||
|
||
@Test | ||
public void testNoFieldtoShadow() { | ||
String body = """ | ||
{ | ||
"api":"tvly" | ||
} | ||
"""; | ||
|
||
final Buffer buffer = Buffer.buffer(body); | ||
final String shadowedBody = ShadowSensitiveData.process(buffer, "api_key"); | ||
|
||
assertThat(shadowedBody) | ||
.isEqualTo("{\"api\":\"tvly\"}"); | ||
} | ||
|
||
} |