mrcnc commented on code in PR #3751:
URL: https://github.com/apache/iceberg-python/pull/3751#discussion_r4681490840


##########
pyiceberg/catalog/rest/credential_provider.py:
##########
@@ -0,0 +1,201 @@
+# 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.
+from __future__ import annotations
+
+import logging
+import threading
+from collections.abc import Callable
+from dataclasses import dataclass, field, replace
+from random import randint
+from time import time
+from typing import TYPE_CHECKING
+from urllib.parse import urlparse
+
+from pyiceberg.catalog.rest.scan_planning import StorageCredential
+from pyiceberg.io import (
+    GCS_TOKEN,
+    GCS_TOKEN_EXPIRES_AT_MS,
+    S3_ACCESS_KEY_ID,
+    S3_SECRET_ACCESS_KEY,
+    S3_SESSION_TOKEN,
+    S3_SESSION_TOKEN_EXPIRES_AT_MS,
+)
+from pyiceberg.typedef import Properties
+
+if TYPE_CHECKING:
+    from pyiceberg.catalog.rest import LoadCredentialsResponse
+
+logger = logging.getLogger(__name__)
+
+REFRESH_CREDENTIALS_ENABLED = "client.refresh-credentials-enabled"
+_REFRESH_MARGIN_MS = 300_000
+_INITIAL_REFRESH_RETRY_WINDOW_MS = 30_000
+_MAX_REFRESH_RETRY_WINDOW_MS = 120_000
+
+
+@dataclass(frozen=True)
+class _CredentialPolicy:
+    required_properties: tuple[str, ...]
+    expiration_properties: tuple[str, ...]
+
+    def expiration_ms(self, config: Properties) -> int | None:
+        for key in self.expiration_properties:
+            if key in config:
+                try:
+                    return int(config[key])
+                except (TypeError, ValueError, OverflowError):
+                    raise ValueError(f"Invalid expiration property: {key}") 
from None
+        return None
+
+    def normalize(self, config: Properties) -> Properties:
+        properties = dict(config)
+        if (expiration := self.expiration_ms(properties)) is not None:
+            for key in self.required_properties:
+                if not properties.get(key):
+                    raise ValueError(f"Missing vended credential property: 
{key}")
+            for key in self.expiration_properties:
+                properties.pop(key, None)
+            properties[self.expiration_properties[0]] = str(expiration)
+        return properties
+
+
+_S3_POLICY = _CredentialPolicy((S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY, 
S3_SESSION_TOKEN), (S3_SESSION_TOKEN_EXPIRES_AT_MS,))
+_GCS_POLICY = _CredentialPolicy((GCS_TOKEN,), (GCS_TOKEN_EXPIRES_AT_MS, 
"gcs.oauth2.token_expires_at"))
+_POLICIES_BY_SCHEME = {
+    "s3": _S3_POLICY,
+    "s3a": _S3_POLICY,
+    "s3n": _S3_POLICY,
+    "gs": _GCS_POLICY,
+    "gcs": _GCS_POLICY,
+}
+
+
+def is_s3_credential_expired(config: Properties, threshold_seconds: int = 300) 
-> bool:

Review Comment:
   I don't believe this will be used anymore since _CredentialPolicy was added, 
so can we remove it?



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