|
| 1 | +# /// script |
| 2 | +# requires-python = "==3.13.*" |
| 3 | +# dependencies = [ |
| 4 | +# "authlib~=1.6.6", |
| 5 | +# "click~=8.3.0", |
| 6 | +# "requests~=2.32.5", |
| 7 | +# ] |
| 8 | +# /// |
| 9 | +"""Provides a set of commands for interacting with a Lakekeeper instance |
| 10 | +
|
| 11 | +Note: This script is used by both local, dev deployments and the ansible scripts. |
| 12 | +""" |
| 13 | + |
| 14 | +from collections import namedtuple |
| 15 | +import dataclasses |
| 16 | +import json |
| 17 | +from pathlib import Path |
| 18 | +import logging |
| 19 | +from typing import Any, Dict, Callable, Iterable, Sequence |
| 20 | + |
| 21 | +from authlib.integrations.requests_client import OAuth2Session |
| 22 | +import click |
| 23 | +import requests |
| 24 | + |
| 25 | +LOGGER = logging.getLogger(__name__) |
| 26 | +LOGGER_FILENAME = f"{Path(__file__).name}.log" |
| 27 | +LOGGER_FORMAT = "%(asctime)s|%(message)s" |
| 28 | + |
| 29 | +OPENID_SCOPE_DEFAULT = "openid profile" |
| 30 | +REQUESTS_TIMEOUT_DEFAULT = 60.0 |
| 31 | +REQUESTS_DEFAULT_KWARGS: Dict[str, Any] = { |
| 32 | + "timeout": REQUESTS_TIMEOUT_DEFAULT, |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +def create_oauth2_session( |
| 37 | + token_endpoint: str, |
| 38 | + client_id: str, |
| 39 | + client_secret: str, |
| 40 | + scope: str = OPENID_SCOPE_DEFAULT, |
| 41 | +) -> OAuth2Session: |
| 42 | + """Create an requests.Session that is able to request and inject |
| 43 | + a token for authentication |
| 44 | +
|
| 45 | + Args: |
| 46 | + token_endpoint: URL of auth serve token endpoint |
| 47 | + client_id: The client ID for authentication |
| 48 | + client_secret: The client secret for authentication |
| 49 | + scope: The requested scope (default: "openid profile") |
| 50 | +
|
| 51 | + Returns: |
| 52 | + The new OAuth2Session |
| 53 | + """ |
| 54 | + |
| 55 | + return OAuth2Session( |
| 56 | + token_endpoint=token_endpoint, |
| 57 | + client_id=client_id, |
| 58 | + client_secret=client_secret, |
| 59 | + scope=scope, |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +class LakekeeperRestV1: |
| 64 | + def __init__( |
| 65 | + self, |
| 66 | + server_url: str, |
| 67 | + openid_provider_url: str, |
| 68 | + client_id: str, |
| 69 | + client_secret: str, |
| 70 | + scope: str, |
| 71 | + ) -> None: |
| 72 | + """ |
| 73 | + Args: |
| 74 | +
|
| 75 | + server_url: URL for Lakekeeper instance |
| 76 | + """ |
| 77 | + self._server_url = server_url |
| 78 | + openid_config_resp = requests.get( |
| 79 | + openid_provider_url + "/.well-known/openid-configuration" |
| 80 | + ) |
| 81 | + openid_config_resp.raise_for_status() |
| 82 | + self._session = create_oauth2_session( |
| 83 | + openid_config_resp.json()["token_endpoint"], |
| 84 | + client_id, |
| 85 | + client_secret, |
| 86 | + scope=f"{OPENID_SCOPE_DEFAULT} {scope}", |
| 87 | + ) |
| 88 | + |
| 89 | + @property |
| 90 | + def catalog_url(self) -> str: |
| 91 | + return self.server_url + "/catalog/v1" |
| 92 | + |
| 93 | + @property |
| 94 | + def management_url(self) -> str: |
| 95 | + return self.server_url + "/management/v1" |
| 96 | + |
| 97 | + @property |
| 98 | + def server_url(self) -> str: |
| 99 | + return self._server_url |
| 100 | + |
| 101 | + def bootstrap(self): |
| 102 | + """Bootstrap the lakekeeper instance using the given access_token. |
| 103 | +
|
| 104 | + Raises an excception if bootstrapping is unsuccessful.""" |
| 105 | + if self.is_bootstrapped(): |
| 106 | + LOGGER.info("Server already bootstrapped. Skipping bootstrap step.") |
| 107 | + return |
| 108 | + |
| 109 | + LOGGER.info("Bootstrapping server.") |
| 110 | + response = self._session.request( |
| 111 | + "POST", |
| 112 | + self.management_url + "/bootstrap", |
| 113 | + json={ |
| 114 | + "accept-terms-of-use": True, |
| 115 | + "is-operator": True, |
| 116 | + }, |
| 117 | + ) |
| 118 | + response.raise_for_status() |
| 119 | + LOGGER.info("Server bootstrapped successfully.") |
| 120 | + |
| 121 | + def is_bootstrapped(self) -> bool: |
| 122 | + response = self._session.request("GET", self.management_url + "/info") |
| 123 | + response.raise_for_status() |
| 124 | + return response.json()["bootstrapped"] |
| 125 | + |
| 126 | + |
| 127 | +def main(): |
| 128 | + pass |
| 129 | + |
| 130 | + |
| 131 | +# session = create_oauth2_session( |
| 132 | +# "http://localhost:50080/auth/realms/analytics-data-platform/protocol/openid-connect/token", |
| 133 | +# "machine-infra", |
| 134 | +# "s3cr3t", |
| 135 | +# scope=f"{OPENID_SCOPE_DEFAULT} lakekeeper", |
| 136 | +# ) |
| 137 | +# session.fetch_token() |
| 138 | +# resp = session.request("GET", "http://localhost:50080/iceberg/management/v1/info") |
| 139 | +# resp.raise_for_status() |
| 140 | +# print(resp.json()) |
| 141 | + |
| 142 | +if __name__ == "__main__": |
| 143 | + main() |
0 commit comments