CTTY commented on code in PR #2660:
URL: https://github.com/apache/iceberg-rust/pull/2660#discussion_r3867102311


##########
crates/catalog/rest/src/auth/oauth2.rs:
##########
@@ -219,7 +219,11 @@ fn attach_bearer(req: &mut HttpRequest, token: 
&SensitiveString) -> Result<()> {
             .with_source(e)
         })?;
     value.set_sensitive(true);
-    req.headers_mut().insert(http::header::AUTHORIZATION, value);
+    // Java's `OAuth2Util.AuthSession` puts the header only if absent, leaving 
a
+    // configured `header.authorization` in place.
+    if !req.headers().contains_key(http::header::AUTHORIZATION) {
+        req.headers_mut().insert(http::header::AUTHORIZATION, value);
+    }

Review Comment:
   Should this be put under a different PR?



##########
crates/catalog/rest/src/auth/sigv4/signer.rs:
##########
@@ -0,0 +1,866 @@
+// 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 chrono::{DateTime, Utc};
+use hmac::{Hmac, Mac};
+use iceberg::sensitive::SensitiveString;
+use iceberg::{Error, ErrorKind, Result};
+use sha2::{Digest, Sha256};
+
+/// Hex SHA-256 of the empty string.
+const EMPTY_BODY_HEX_SHA256: &str =
+    "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
+
+/// How the payload hash is encoded in the `x-amz-content-sha256` header.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub enum PayloadHashMode {
+    /// Iceberg Java's RESTSigV4 style: base64 header for non-empty bodies, hex
+    /// for empty; the canonical request always uses hex.
+    IcebergRest,
+    /// Standard AWS SigV4 style: hex everywhere (e.g. AWS Glue).
+    StandardAws,
+}
+
+/// Derives the AWS SigV4 signing key.
+fn hmac_sha256(key: &[u8], data: &[u8]) -> Vec<u8> {
+    let mut mac = <Hmac<Sha256> as Mac>::new_from_slice(key).expect("HMAC 
takes a key of any size");
+    mac.update(data);
+    mac.finalize().into_bytes().to_vec()
+}
+
+fn hex_sha256(data: &[u8]) -> String {
+    encode_hex(&Sha256::digest(data))
+}
+
+fn hex_hmac_sha256(key: &[u8], data: &[u8]) -> String {
+    encode_hex(&hmac_sha256(key, data))
+}
+
+fn encode_hex(bytes: &[u8]) -> String {
+    bytes.iter().map(|byte| format!("{byte:02x}")).collect()
+}
+
+fn base64_encode(bytes: &[u8]) -> String {
+    base64::engine::Engine::encode(&base64::engine::general_purpose::STANDARD, 
bytes)
+}
+
+fn signing_key(secret: &str, date: &str, region: &str, service: &str) -> 
Vec<u8> {
+    let k_date = hmac_sha256(format!("AWS4{secret}").as_bytes(), 
date.as_bytes());
+    let k_region = hmac_sha256(&k_date, region.as_bytes());
+    let k_service = hmac_sha256(&k_region, service.as_bytes());
+    hmac_sha256(&k_service, b"aws4_request")
+}
+
+/// Computes the value of the `x-amz-content-sha256` header.
+fn content_sha256_header(body: &[u8], mode: PayloadHashMode) -> String {
+    match mode {
+        PayloadHashMode::StandardAws => hex_sha256(body),
+        PayloadHashMode::IcebergRest => {
+            if body.is_empty() {
+                EMPTY_BODY_HEX_SHA256.to_string()
+            } else {
+                base64_encode(&Sha256::digest(body))
+            }
+        }
+    }
+}
+
+/// Builds the SigV4 canonical request. `headers` are (lowercased, trimmed)
+/// pairs; `payload_hash` is always the hex sha256 of the body.
+fn canonical_request(
+    method: &str,
+    canonical_uri: &str,
+    canonical_query: &str,
+    headers: &[(String, String)],
+    payload_hash: &str,
+) -> String {
+    let mut sorted = headers.to_vec();
+    sorted.sort_by(|a, b| a.0.cmp(&b.0));
+
+    let canonical_headers: String = sorted.iter().map(|(k, v)| 
format!("{k}:{v}\n")).collect();
+    let signed_headers = sorted
+        .iter()
+        .map(|(k, _)| k.as_str())
+        .collect::<Vec<_>>()
+        .join(";");
+
+    format!(
+        
"{method}\n{canonical_uri}\n{canonical_query}\n{canonical_headers}\n{signed_headers}\n{payload_hash}"
+    )
+}
+
+/// Builds the SigV4 string-to-sign.
+fn string_to_sign(amz_date: &str, scope: &str, canonical_request: &str) -> 
String {
+    format!(
+        "AWS4-HMAC-SHA256\n{amz_date}\n{scope}\n{}",
+        hex_sha256(canonical_request.as_bytes())
+    )
+}
+
+/// Static AWS-style credentials used for SigV4 signing of catalog requests.
+#[derive(Clone)]
+pub struct AwsCredentials {
+    /// AWS access key id.
+    pub access_key_id: String,
+    /// AWS secret access key.
+    pub secret_access_key: SensitiveString,
+    /// Optional STS session token.
+    pub session_token: Option<SensitiveString>,
+}
+
+/// AWS SigV4 signer following Iceberg Java's `RESTSigV4AuthSession`: it adds 
the
+/// required amz headers and signs all request headers except a small 
blacklist.
+#[derive(Clone)]
+pub struct SigV4Signer {

Review Comment:
   I understand that there are behavior differences between java and rust aws 
sdks, but I'm still hesitant of maintaining a handrolled sigv4 signer in the 
iceberg repo. Have we explored other alternatives?



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to