Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce LoggingMarkers marker for logging sensitive data #2051

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,9 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<kotlin.compiler.jvmTarget>${java.version}</kotlin.compiler.jvmTarget>

<!-- production dependencies -->
<spring-boot.version>3.3.6</spring-boot.version>
Expand Down Expand Up @@ -344,6 +345,10 @@
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<configuration>
<jvmTarget>${java.version}</jvmTarget>
<javaParameters>true</javaParameters>
</configuration>
<executions>
<execution>
<id>compile</id>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.lang.NonNull;

import static org.springframework.ai.util.LoggingMarkers.SENSITIVE_DATA_MARKER;

/**
* An implementation of {@link StructuredOutputConverter} that transforms the LLM output
* to a specific object type using JSON schema. This converter works by generating a JSON
Expand Down Expand Up @@ -143,7 +145,7 @@ private void generateSchema() {
this.jsonSchema = objectWriter.writeValueAsString(jsonNode);
}
catch (JsonProcessingException e) {
logger.error("Could not pretty print json schema for jsonNode: " + jsonNode);
logger.error("Could not pretty print json schema for jsonNode: {}", jsonNode);
throw new RuntimeException("Could not pretty print json schema for " + this.type, e);
}
}
Expand Down Expand Up @@ -180,7 +182,8 @@ public T convert(@NonNull String text) {
return (T) this.objectMapper.readValue(text, this.objectMapper.constructType(this.type));
}
catch (JsonProcessingException e) {
logger.error("Could not parse the given text to the desired target type:" + text + " into " + this.type);
logger.error(SENSITIVE_DATA_MARKER,
"Could not parse the given text to the desired target type: \"{}\" into {}", text, this.type);
throw new RuntimeException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.springframework.ai.util;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will need to add copywrite statement. will add it when merging.


import org.slf4j.Marker;
import org.slf4j.MarkerFactory;

/**
* Utility class that provides predefined SLF4J {@link Marker} instances used in logging
* operations within the application. <br>
* This class is not intended to be instantiated, but is open for extension.
*/
public class LoggingMarkers {

/**
* Marker used to identify log statements associated with <strong>sensitive
* data</strong>, such as:
* <ul>
* <li>Internal business information</li>
* <li>Employee data</li>
* <li>Customer non-regulated data</li>
* <li>Business processes and logic</li>
* <li>etc.</li>
* </ul>
* Typically, logging this information should be avoided.
*/
public static final Marker SENSITIVE_DATA_MARKER = MarkerFactory.getMarker("SENSITIVE");

/**
* Marker used to identify log statements associated with <strong>restricted
* data</strong>, such as:
* <ul>
* <li>Authentication credentials</li>
* <li>Keys and secrets</li>
* <li>Core intellectual property</li>
* <li>Critical security configs</li>
* <li>Trade secrets</li>
* <li>etc.</li>
* </ul>
* Logging of such information is usually prohibited in any circumstances.
*/
public static final Marker RESTRICTED_DATA_MARKER = MarkerFactory.getMarker("RESTRICTED");

/**
* Marker used to identify log statements associated with <strong>regulated
* data</strong>, such as:
* <ul>
* <li>PCI (credit card data)</li>
* <li>PHI (health information)</li>
* <li>PII (personally identifiable info)</li>
* <li>Financial records</li>
* <li>Compliance-controlled data</li>
* <li>etc.</li>
* </ul>
* Logging of such information should be avoided.
*/
public static final Marker REGULATED_DATA_MARKER = MarkerFactory.getMarker("REGULATED");

/**
* Marker used to identify log statements associated with <strong>public
* data</strong>, such as:
* <ul>
* <li>Public documentation</li>
* <li>Marketing materials</li>
* <li>etc.</li>
* </ul>
* There are no restriction for logging such information.
*/
public static final Marker PUBLIC_DATA_MARKER = MarkerFactory.getMarker("PUBLIC");

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,32 @@

package org.springframework.ai.converter;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import org.slf4j.LoggerFactory;
import org.springframework.core.ParameterizedTypeReference;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.springframework.ai.util.LoggingMarkers.SENSITIVE_DATA_MARKER;

/**
* @author Sebastian Ullrich
Expand All @@ -45,9 +52,21 @@
@ExtendWith(MockitoExtension.class)
class BeanOutputConverterTest {

private ListAppender<ILoggingEvent> logAppender;

@Mock
private ObjectMapper objectMapperMock;

@BeforeEach
void beforeEach() {

var logger = (Logger) LoggerFactory.getLogger(BeanOutputConverter.class);

logAppender = new ListAppender<>();
logAppender.start();
logger.addAppender(logAppender);
}

@Test
void shouldHavePreConfiguredDefaultObjectMapper() {
var converter = new BeanOutputConverter<>(new ParameterizedTypeReference<TestClass>() {
Expand Down Expand Up @@ -135,6 +154,19 @@ void convertClassType() {
assertThat(testClass.getSomeString()).isEqualTo("some value");
}

@Test
void failToConvertInvalidJson() {
var converter = new BeanOutputConverter<>(TestClass.class);
assertThatThrownBy(() -> converter.convert("{invalid json")).hasCauseInstanceOf(JsonParseException.class);
assertThat(logAppender.list).hasSize(1);
final var loggingEvent = logAppender.list.get(0);
assertThat(loggingEvent.getFormattedMessage())
.isEqualTo("Could not parse the given text to the desired target type: \"{invalid json\" into "
+ TestClass.class);

assertThat(loggingEvent.getMarkerList()).contains(SENSITIVE_DATA_MARKER);
}

@Test
void convertClassWithDateType() {
var converter = new BeanOutputConverter<>(TestClassWithDateProperty.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ you'll need to develop a low-level client API class. This often involves utilizi
Ensure your client conforms to the link:https://docs.spring.io/spring-ai/reference/api/generic-model.html[Generic Model API].
Use existing request and response classes if your model's inputs and outputs are supported.
If not, create new classes for the Generic Model API and establish a new Java package.
Be careful with logging Personally Identifiable Information (PII), mark it with https://github.com/spring-projects/spring-ai/tree/main/spring-ai-core/src/main/java/org/springframework/ai/util/LoggingMarkers.java[`PII_MARKER`] Slf4j marker.

. *Implement Auto-Configuration and a Spring Boot Starter*: This step involves creating the
necessary auto-configuration and Spring Boot Starter to easily instantiate the new model with
Expand Down