Skip to content

Commit

Permalink
Shadows api key from tavily logs
Browse files Browse the repository at this point in the history
  • Loading branch information
lordofthejars authored and jmartisk committed Oct 2, 2024
1 parent 2b39bae commit 3edcc3d
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ private String bodyToString(Buffer body) {
if (body == null) {
return "";
}
return body.toString();

return ShadowSensitiveData.process(body, "api_key");
}

private String inOneLine(io.vertx.core.MultiMap headers) {
Expand Down
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();
}

}
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\"}");
}

}

0 comments on commit 3edcc3d

Please sign in to comment.