Skip to content

Commit 7bfabe1

Browse files
committed
Python: Add the REST token to the properties
1 parent 2fa48c3 commit 7bfabe1

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

python/pyiceberg/catalog/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
_ENV_CONFIG = Config()
4242

43+
TOKEN = "token"
4344
TYPE = "type"
4445
URI = "uri"
4546

python/pyiceberg/catalog/rest.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
Catalog,
3535
Identifier,
3636
Properties,
37-
PropertiesUpdateSummary,
37+
PropertiesUpdateSummary, TOKEN,
3838
)
3939
from pyiceberg.exceptions import (
4040
AuthorizationExpiredError,
@@ -87,7 +87,6 @@ class Endpoints:
8787
CREDENTIAL = "credential"
8888
GRANT_TYPE = "grant_type"
8989
SCOPE = "scope"
90-
TOKEN = "token"
9190
TOKEN_EXCHANGE = "urn:ietf:params:oauth:grant-type:token-exchange"
9291
SEMICOLON = ":"
9392
KEY = "key"
@@ -206,12 +205,13 @@ def _create_session(self) -> None:
206205
elif ssl_client_cert := ssl_client.get(CERT):
207206
self.session.cert = ssl_client_cert
208207

208+
# If we have credentials, but not a token, we want to fetch a token
209+
if TOKEN not in self.properties and CREDENTIAL in self.properties:
210+
self.properties[TOKEN] = self._fetch_access_token(self.properties[CREDENTIAL])
211+
209212
# Set Auth token for subsequent calls in the session
210213
if token := self.properties.get(TOKEN):
211214
self.session.headers[AUTHORIZATION_HEADER] = f"{BEARER_PREFIX} {token}"
212-
elif credential := self.properties.get(CREDENTIAL):
213-
token = self._fetch_access_token(credential)
214-
self.session.headers[AUTHORIZATION_HEADER] = f"{BEARER_PREFIX} {token}"
215215

216216
# Set HTTP headers
217217
self.session.headers["Content-type"] = "application/json"

python/pyiceberg/io/fsspec.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from requests import HTTPError
2828
from s3fs import S3FileSystem
2929

30+
from pyiceberg.catalog import TOKEN
3031
from pyiceberg.exceptions import SignError
3132
from pyiceberg.io import FileIO, InputFile, OutputFile
3233
from pyiceberg.typedef import Properties
@@ -35,16 +36,20 @@
3536

3637

3738
def s3v4_rest_signer(properties: Properties, request: AWSRequest, **_) -> AWSRequest:
39+
if TOKEN not in properties:
40+
raise SignError("Signer set, but token is not available")
41+
3842
signer_url = properties["uri"].rstrip("/")
39-
signer_headers = {"Authorization": f"Bearer {properties['token']}"}
43+
signer_headers = {"Authorization": f"Bearer {properties[TOKEN]}"}
4044
signer_body = {
4145
"method": request.method,
4246
"region": request.context["client_region"],
4347
"uri": request.url,
4448
"headers": {key: [val] for key, val in request.headers.items()},
4549
}
50+
51+
response = requests.post(f"{signer_url}/v1/aws/s3/sign", headers=signer_headers, json=signer_body)
4652
try:
47-
response = requests.post(f"{signer_url}/v1/aws/s3/sign", headers=signer_headers, json=signer_body)
4853
response.raise_for_status()
4954
response_json = response.json()
5055
except HTTPError as e:

0 commit comments

Comments
 (0)