ryerraguntla commented on code in PR #3103: URL: https://github.com/apache/iggy/pull/3103#discussion_r3431422225
########## core/connectors/sinks/s3_sink/src/path.rs: ########## @@ -0,0 +1,212 @@ +// 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. + +use crate::OutputFormat; +use chrono::{DateTime, Utc}; +use iggy_connector_sdk::Error; + +pub(crate) struct PathContext<'a> { + pub stream: &'a str, + pub topic: &'a str, + pub partition_id: u32, + pub first_timestamp_micros: u64, +} + +pub(crate) fn render_s3_key( + prefix: Option<&str>, + template: &str, + ctx: &PathContext<'_>, + offset_start: u64, + offset_end: u64, + format: OutputFormat, +) -> Result<String, Error> { + let rendered = render_template(template, ctx)?; + + // Partition ID is always embedded in the filename to prevent cross-partition + // key collisions (partitions have independent offset spaces starting at 0). + let filename = format!( + "{:05}-{:020}-{:020}.{}", + ctx.partition_id, + offset_start, + offset_end, + format.file_extension() + ); + + let key = match prefix { + Some(p) => { + let p = p.trim_matches('/'); + if p.is_empty() { + format!("{rendered}/{filename}") + } else { + format!("{p}/{rendered}/{filename}") + } + } + None => format!("{rendered}/{filename}"), + }; + + Ok(key) +} + +fn render_template(template: &str, ctx: &PathContext<'_>) -> Result<String, Error> { + let dt = timestamp_to_datetime(ctx.first_timestamp_micros)?; + let date = dt.format("%Y-%m-%d").to_string(); + let hour = dt.format("%H").to_string(); + let ts_millis = (ctx.first_timestamp_micros / 1_000).to_string(); + Review Comment: warn - render_template — {stream} and {topic} are interpolated verbatim with no sanitization: .``` replace("{stream}", ctx.stream) .replace("{topic}", ctx.topic) ``` Iggy stream/topic names may contain characters that produce unexpected S3 key structures (e.g., names with // or non-ASCII). S3 does not interpret .. as path traversal, but unusual names produce keys that are difficult to list or match with glob patterns. Sanitizing to [a-zA-Z0-9_-] or percent-encoding would make keys predictable. -- 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]
