zuozhiw commented on code in PR #5375: URL: https://github.com/apache/texera/pull/5375#discussion_r3654367374
########## common/observability/src/main/scala/org/apache/texera/observability/LogSanitizer.scala: ########## @@ -0,0 +1,99 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.texera.observability + +import scala.jdk.CollectionConverters._ + +/** + * Pure functions that sanitize log bodies and MDC before export: + * strip control characters, redact secrets, cap body size, and + * filter MDC to an allowlist. + */ +object LogSanitizer { + + /** Per-record body byte cap. */ + val MaxBodyBytes: Int = 16 * 1024 + + /** Suffix appended to truncated bodies. */ + val TruncatedMarker: String = "...[truncated]" + + /** C0 control characters except TAB (0x09), plus DEL (0x7F). */ + private val C0ControlRegex = "[\\x00-\\x08\\x0A-\\x1F\\x7F]".r + + /** Secret patterns, redacted from bodies. Most specific first. */ + private val SecretPatterns: Seq[scala.util.matching.Regex] = Seq( + // Bearer token + """(?i)Bearer\s+[A-Za-z0-9._\-/+=]{8,}""".r, + // password=... or password: ... + """(?i)password\s*[=:]\s*[^\s,;"']+""".r, + // AWS access key ID + """AKIA[0-9A-Z]{16}""".r, + // labelled AWS secret access key + """(?i)aws_secret_access_key\s*[=:]\s*[A-Za-z0-9/+=]{20,}""".r + ) + + /** MDC keys forwarded to OTel log attributes. Default-deny: the MDC is a + * process-wide map that any library on the classpath may write to, so + * only keys Texera's own instrumentation sets are exported. The list is + * the correlation IDs needed to join a log line to its trace and to the + * workflow/execution/user it belongs to; all are low-cardinality, + * non-sensitive identifiers (no names, emails, or payloads). + * + * Everything else is dropped. Today that is chiefly the keys Pekko's + * SLF4J bridge injects (`sourceThread`, `pekkoSource`, `pekkoAddress`, + * `pekkoTimestamp`, `sourceActorSystem`), which are redundant with the + * log body and would bloat every exported record; it also future-proofs + * against third-party or user code leaking arbitrary MDC values. + */ + val AllowedMdcKeys: Set[String] = Set( Review Comment: sounds good -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
