Skip to content

Commit 7a9e212

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

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
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 & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
from pyiceberg import __version__
3232
from pyiceberg.catalog import (
33+
TOKEN,
3334
URI,
3435
Catalog,
3536
Identifier,
@@ -87,7 +88,6 @@ class Endpoints:
8788
CREDENTIAL = "credential"
8889
GRANT_TYPE = "grant_type"
8990
SCOPE = "scope"
90-
TOKEN = "token"
9191
TOKEN_EXCHANGE = "urn:ietf:params:oauth:grant-type:token-exchange"
9292
SEMICOLON = ":"
9393
KEY = "key"
@@ -206,12 +206,13 @@ def _create_session(self) -> None:
206206
elif ssl_client_cert := ssl_client.get(CERT):
207207
self.session.cert = ssl_client_cert
208208

209+
# If we have credentials, but not a token, we want to fetch a token
210+
if TOKEN not in self.properties and CREDENTIAL in self.properties:
211+
self.properties[TOKEN] = self._fetch_access_token(self.properties[CREDENTIAL])
212+
209213
# Set Auth token for subsequent calls in the session
210214
if token := self.properties.get(TOKEN):
211215
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}"
215216

216217
# Set HTTP headers
217218
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)