Skip to content

Commit 24eeceb

Browse files
committed
Address comments
1 parent 23b6df6 commit 24eeceb

7 files changed

Lines changed: 74 additions & 26 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,9 @@ Fields emitted in each log line:
447447
- `table`
448448
- `requestType` (`query` or `cdf_stream`)
449449
- `egressBytes`
450+
- `pricingTier`
450451
- `timestampMs`
452+
- `clientRegion` (if available)
451453

452454
To enable this feature, configure the `accessLogging` block in the server yaml.
453455

server/src/main/scala/io/delta/sharing/server/DeltaSharingService.scala

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ class DeltaSharingService(serverConfig: ServerConfig) {
213213
schema: String,
214214
table: String,
215215
actions: Seq[Object]): Unit = {
216+
if (!Option(serverConfig.getAccessLogging).exists(_.enabled)) {
217+
return
218+
}
219+
216220
val bytes = DeltaSharingService.extractEgressBytes(actions)
217221
if (bytes <= 0) {
218222
return
@@ -273,6 +277,10 @@ class DeltaSharingService(serverConfig: ServerConfig) {
273277
schema: String,
274278
table: String,
275279
actions: Seq[Object]): Unit = {
280+
if (!Option(serverConfig.getAccessLogging).exists(_.enabled)) {
281+
return
282+
}
283+
276284
val bytes = DeltaSharingService.extractEgressBytes(actions)
277285
if (bytes <= 0) {
278286
return
@@ -1120,11 +1128,26 @@ object DeltaSharingService {
11201128
}
11211129

11221130
private def isValidIp(ip: String): Boolean = {
1123-
try {
1124-
InetAddress.getByName(ip)
1125-
true
1126-
} catch {
1127-
case _: Throwable => false
1131+
// Validate IP format without DNS resolution to avoid latency and security issues
1132+
val ipv4Pattern = """^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$""".r
1133+
val ipv6Pattern = """^([0-9a-fA-F:]+)$""".r
1134+
1135+
ip match {
1136+
case ipv4Pattern(a, b, c, d) =>
1137+
Seq(a, b, c, d).forall { octet =>
1138+
val n = octet.toInt
1139+
n >= 0 && n <= 255
1140+
}
1141+
case ipv6Pattern(_) =>
1142+
try {
1143+
// For IPv6, use InetAddress but only for format validation
1144+
// This won't trigger DNS since it matches the IPv6 pattern
1145+
InetAddress.getByName(ip)
1146+
true
1147+
} catch {
1148+
case _: Throwable => false
1149+
}
1150+
case _ => false
11281151
}
11291152
}
11301153

server/src/main/scala/io/delta/sharing/server/config/ServerConfig.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,14 @@ case class AccessLoggingConfig(
144144
// Optional mapping from region codes to pricing group labels.
145145
// Keys should be uppercase location codes (for example: US, DE).
146146
// A wildcard key "*" can be used as a catch-all default.
147+
// NOTE: pricingGroups is reserved for future use and not currently applied.
147148
@BeanProperty var pricingGroups: java.util.Map[String, String],
148149
// The GCP region where this server runs (for example: us-central1).
149150
// Used for pricing tier calculation based on source→destination pairs.
150151
@BeanProperty var sourceRegion: String,
151-
// Enable GCP inter-region traffic detection via X-Envoy-Peer-Metadata header.
152-
// When true, traffic from other GCP regions is classified as inter-region (cheaper)
153-
// rather than internet egress.
152+
// Enable GCP traffic detection using GCP's published IP ranges (cloud.json).
153+
// When true, client IPs belonging to other GCP regions can be classified as inter-region
154+
// (cheaper) rather than internet egress. Set to false to disable this detection.
154155
@BeanProperty var detectGcpTraffic: Boolean) extends ConfigItem {
155156

156157
def this() = {

server/src/main/scala/io/delta/sharing/server/telemetry/AccessLogEmitter.scala

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package io.delta.sharing.server.telemetry
1818

19+
import java.util.Locale
20+
1921
import org.slf4j.LoggerFactory
2022

2123
import io.delta.sharing.server.common.JsonUtils
@@ -230,13 +232,35 @@ class JsonAccessLogEmitter extends AccessLogEmitter {
230232
logger.info(JsonUtils.toJson(logPayload))
231233
}
232234

235+
// Headers that should be redacted to prevent leaking secrets/PII
236+
private val sensitiveHeaders = Set(
237+
"authorization",
238+
"cookie",
239+
"set-cookie",
240+
"x-api-key",
241+
"x-auth-token",
242+
"proxy-authorization",
243+
"www-authenticate",
244+
"x-csrf-token",
245+
"x-xsrf-token"
246+
)
247+
233248
override def recordHeaders(entry: RequestHeadersLogEntry): Unit = {
249+
// Redact sensitive headers to prevent leaking secrets/PII
250+
val redactedHeaders = entry.headers.map { case (key, value) =>
251+
if (sensitiveHeaders.contains(key.toLowerCase(Locale.ROOT))) {
252+
key -> "[REDACTED]"
253+
} else {
254+
key -> value
255+
}
256+
}
257+
234258
val logPayload = Map(
235259
"logType" -> "REQUEST_HEADERS",
236260
"share" -> entry.share,
237261
"table" -> entry.table,
238262
"timestampMs" -> entry.timestampMs,
239-
"headers" -> entry.headers
263+
"headers" -> redactedHeaders
240264
)
241265

242266
logger.info(JsonUtils.toJson(logPayload))

server/src/main/scala/io/delta/sharing/server/telemetry/GcpIpRangeLookup.scala

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import scala.collection.mutable
2424
import scala.util.{Failure, Success, Try}
2525
import scala.util.control.NonFatal
2626

27+
import org.slf4j.LoggerFactory
28+
2729
import io.delta.sharing.server.common.JsonUtils
2830

2931
/**
@@ -37,6 +39,8 @@ import io.delta.sharing.server.common.JsonUtils
3739
*/
3840
object GcpIpRangeLookup {
3941

42+
private val logger = LoggerFactory.getLogger("delta.sharing.gcp.ip.lookup")
43+
4044
private val GCP_IP_RANGES_URL = "https://www.gstatic.com/ipranges/cloud.json"
4145
private val REFRESH_INTERVAL_HOURS = 24
4246
private val CONNECTION_TIMEOUT_MS = 10000
@@ -230,14 +234,9 @@ object GcpIpRangeLookup {
230234
cachedTrie.set(newTrie)
231235
lastRefreshTime.set(System.currentTimeMillis())
232236
isInitialized.set(true)
233-
// scalastyle:off println
234-
System.out.println(s"[GcpIpRangeLookup] Loaded ${ranges.prefixes.size} IP ranges " +
235-
s"(syncToken: ${ranges.syncToken})")
236-
// scalastyle:on println
237+
logger.info(s"Loaded ${ranges.prefixes.size} IP ranges (syncToken: ${ranges.syncToken})")
237238
case Failure(e) =>
238-
// scalastyle:off println
239-
System.err.println(s"[GcpIpRangeLookup] Failed to refresh IP ranges: ${e.getMessage}")
240-
// scalastyle:on println
239+
logger.warn(s"Failed to refresh IP ranges: ${e.getMessage}")
241240
// Keep using the old trie if we have one
242241
if (!isInitialized.get()) {
243242
// First-time failure - create empty trie
@@ -247,9 +246,7 @@ object GcpIpRangeLookup {
247246
}
248247
} catch {
249248
case NonFatal(e) =>
250-
// scalastyle:off println
251-
System.err.println(s"[GcpIpRangeLookup] Unexpected error during refresh: ${e.getMessage}")
252-
// scalastyle:on println
249+
logger.error(s"Unexpected error during refresh: ${e.getMessage}")
253250
}
254251
}
255252

server/src/main/scala/io/delta/sharing/server/telemetry/GcpPricingTier.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,10 @@ object GcpPricingTier {
355355
return (SAME_REGION, None)
356356
}
357357

358-
// If GCP traffic detection is enabled, use IP range lookup for accurate region detection
359-
if (detectGcpTraffic) {
358+
// If GCP traffic detection is enabled and the IP looks like GCP, use IP range lookup for
359+
// accurate region detection. This avoids unnecessary initialization/refresh overhead for
360+
// non-GCP internet clients.
361+
if (detectGcpTraffic && clientIp.exists(isGcpPublicIp)) {
360362
clientIp.flatMap(GcpIpRangeLookup.lookupRegion) match {
361363
case Some(clientGcpRegion) =>
362364
// We have exact GCP region from IP lookup

server/src/universal/conf/delta-sharing-server.yaml.template

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,12 @@ accessLogging:
7979
clientIpHeader: "x-forwarded-for"
8080
# Optional mapping from region codes to billing price groups.
8181
# Use uppercase keys. A wildcard key "*" can be used as a catch-all.
82-
# If empty, server applies a built-in default map (na_eu/apac/latam buckets).
82+
# NOTE: pricingGroups is reserved for future use and not currently applied.
8383
pricingGroups: {}
8484
# GCP region where this server runs (for example: us-central1).
85-
# Required for GCP pricing tier calculation.
8685
# Used to determine source→destination pairs for egress cost attribution.
8786
sourceRegion: ""
88-
# Enable GCP inter-region traffic detection via X-Envoy-Peer-Metadata header.
89-
# When true, traffic from other GCP regions is classified as inter-region (cheaper)
90-
# rather than internet egress. Set to false to disable detection.
87+
# Enable GCP traffic detection using GCP's published IP ranges (cloud.json).
88+
# When true, client IPs belonging to other GCP regions can be classified as inter-region
89+
# (cheaper) rather than internet egress. Set to false to disable this detection.
9190
detectGcpTraffic: true

0 commit comments

Comments
 (0)