Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

6.5 KB
Raw Blame
# 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.

import math
from dataclasses import dataclass, field
from typing import Set

# Tag carried by the metalake discovery tools. Defined here because
# Setting is what interprets --include-tool-tags; tools/metalake.py
# imports it so the tag and the check below cannot drift apart.
METALAKE_TOOL_TAG = "metalake"


@dataclass
class DefaultSetting:
    default_gravitino_uri: str = "http://127.0.0.1:8090"
    default_transport: str = "stdio"
    default_mcp_url: str = "http://127.0.0.1:8000/mcp"


@dataclass
class Setting:  # pylint: disable=too-many-instance-attributes
    # Default metalake, used by any tool call that does not name one itself
    # via the `metalake` argument. Optional on every transport: a deployment
    # serving several metalakes can leave it unset and let each call choose.
    metalake: str = ""
    gravitino_uri: str = DefaultSetting.default_gravitino_uri
    tags: Set[str] = field(default_factory=set)
    transport: str = DefaultSetting.default_transport
    mcp_url: str = DefaultSetting.default_mcp_url
    # Static authorization credential. A bare value is treated as an OAuth2 Bearer
    # token; a value containing a valid scheme and credential (``Basic ...``) is
    # forwarded as an Authorization credential. Sent on every request in stdio
    # mode; in HTTP mode it is only the fallback used when an incoming request
    # carries no Authorization header (per-request identity takes priority).
    # Empty string means anonymous (no Authorization header sent).
    # repr=False keeps the raw value out of the dataclass-generated __repr__.
    token: str = field(default="", repr=False)
    # TLS certificate/key paths for serving the HTTP endpoint over HTTPS.
    # Both must be set to enable TLS; empty means plain HTTP.
    tls_cert: str = ""
    tls_key: str = ""
    # OAuth2 client-credentials for the service hop to Gravitino. All three of
    # endpoint / client id / client secret must be set together. Ignored when
    # ``token`` is also set (--token wins).
    oauth_token_endpoint: str = ""
    oauth_client_id: str = ""
    oauth_client_secret: str = field(default="", repr=False)
    oauth_scope: str = ""
    # HTTP only: reject requests with no Authorization when service OAuth or
    # --token is configured instead of falling back to the service identity.
    no_service_identity_fallback: bool = False
    gravitino_read_timeout: float = 60.0

    def __post_init__(self) -> None:
        # A whitespace-only --metalake (e.g. a shell-quoting mistake) must be
        # treated as "no default configured", the same as an empty string,
        # rather than silently used as a nonsensical metalake name.
        self.metalake = self.metalake.strip()
        if (
            not math.isfinite(self.gravitino_read_timeout)
            or self.gravitino_read_timeout <= 0
        ):
            raise ValueError(
                "Gravitino read timeout must be a finite positive number of seconds"
            )

    def has_oauth_client(self) -> bool:
        """Return True when client-credentials is fully configured."""
        return bool(
            self.oauth_token_endpoint.strip()
            and self.oauth_client_id.strip()
            and self.oauth_client_secret.strip()
        )

    def exposes_metalake_discovery(self) -> bool:
        """Whether the `list_metalakes` tool is reachable in this deployment.

        --include-tool-tags is an allowlist, so a tag filter that omits
        "metalake" hides the discovery tool. Callers use this to avoid telling
        an agent to call a tool it cannot see.
        """
        return not self.tags or METALAKE_TOOL_TAG in self.tags

    def has_service_identity(self) -> bool:
        """Return True when a static token or OAuth client-credentials is set."""
        return bool(self.token.strip()) or self.has_oauth_client()

    def validate_oauth(self) -> None:
        """Reject a partial OAuth client-credentials configuration."""
        filled = [
            bool(self.oauth_token_endpoint.strip()),
            bool(self.oauth_client_id.strip()),
            bool(self.oauth_client_secret.strip()),
        ]
        if any(filled) and not all(filled):
            raise ValueError(
                "OAuth client credentials requires "
                "--oauth-token-endpoint, --oauth-client-id, and "
                "--oauth-client-secret together "
                "(or GRAVITINO_OAUTH_TOKEN_ENDPOINT / "
                "GRAVITINO_OAUTH_CLIENT_ID / GRAVITINO_OAUTH_CLIENT_SECRET)."
            )
        if self.oauth_scope.strip() and not all(filled):
            raise ValueError(
                "OAuth scope (--oauth-scope / GRAVITINO_OAUTH_SCOPE) requires "
                "a complete client-credentials configuration "
                "(--oauth-token-endpoint, --oauth-client-id, and "
                "--oauth-client-secret)."
            )

    def __str__(self) -> str:
        # Mirror startup_authorization: a whitespace-only token is anonymous on
        # the wire, so it must not be logged as a configured identity.
        token_display = "***" if self.token.strip() else ""
        secret_display = "***" if self.oauth_client_secret.strip() else ""
        return (
            f"Setting(metalake={self.metalake}, gravitino_uri={self.gravitino_uri}, "
            f"tags={self.tags}, transport={self.transport}, mcp_url={self.mcp_url}, "
            f"gravitino_read_timeout={self.gravitino_read_timeout}, "
            f"token={token_display}, tls_cert={self.tls_cert}, "
            f"tls_key={self.tls_key}, "
            f"oauth_token_endpoint={self.oauth_token_endpoint}, "
            f"oauth_client_id={self.oauth_client_id}, "
            f"oauth_client_secret={secret_display}, "
            f"oauth_scope={self.oauth_scope}, "
            f"no_service_identity_fallback="
            f"{self.no_service_identity_fallback})"
        )