-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ add support for reading client credentials from the local H2O…
… CLI config file (#68) * feat: ✨ add support for reading clientcredentials form the local H2O CLI config - implment config path lookusp - implement config loading - extend credentials loading to accept tokens loaded from the config - extend main discover functions to accept config path and utilize RESOLVES h2oai/cloud-discovery#437 * REVIEW: tomli python_version squote
- Loading branch information
Showing
9 changed files
with
263 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
"""Helper module for importing backported packages. So that the rest of the | ||
modules is not cluttered with conditional imports. | ||
""" | ||
|
||
import sys | ||
|
||
# We use version check instead of try/except because mypy does not like it. | ||
if sys.version_info >= (3, 11): | ||
import tomllib # noqa: F401 | ||
else: | ||
import tomli as tomllib # noqa: F401 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import dataclasses | ||
import types | ||
from typing import Mapping | ||
from typing import Optional | ||
|
||
from h2o_discovery._internal.compat import tomllib | ||
|
||
|
||
def _empty_tokens_factory() -> Mapping[str, str]: | ||
return types.MappingProxyType({}) | ||
|
||
|
||
@dataclasses.dataclass(frozen=True) | ||
class Config: | ||
"""Internal representation of the H2O CLI Configuration.""" | ||
|
||
#: Map of found tokens in the configuration file. in the `{"client-id": "token"}` | ||
#: format. | ||
tokens: Mapping[str, str] = dataclasses.field(default_factory=_empty_tokens_factory) | ||
|
||
|
||
def load_config(path: Optional[str] = None) -> Config: | ||
"""Loads the H2O CLI config from the specified path. | ||
If no path is specified, an empty configuration is returned. | ||
""" | ||
|
||
if not path: | ||
return Config() | ||
|
||
with open(path, "rb") as f: | ||
data = tomllib.load(f) | ||
|
||
client_id = data.get("ClientID") | ||
token = data.get("Token") | ||
platform_client_id = data.get("PlatformClientID") | ||
platform_token = data.get("PlatformToken") | ||
|
||
tokens = {} | ||
if client_id is not None and token is not None: | ||
tokens[client_id] = token | ||
if platform_client_id is not None and platform_token is not None: | ||
tokens[platform_client_id] = platform_token | ||
|
||
return Config(tokens=types.MappingProxyType(tokens)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
tests/_internal/lookup/test_determine_local_config_path.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from h2o_discovery._internal import lookup | ||
|
||
|
||
def test_determine_local_config_path_param(): | ||
# When | ||
path = lookup.determine_local_config_path("test-path") | ||
|
||
# Then | ||
assert path == "test-path" | ||
|
||
|
||
def test_determine_local_config_path_env(monkeypatch): | ||
# Given | ||
monkeypatch.setenv("H2OCONFIG", "test-path-from-env") | ||
|
||
# When | ||
path = lookup.determine_local_config_path() | ||
|
||
# Then | ||
assert path == "test-path-from-env" | ||
|
||
|
||
def test_determine_local_config_path_default_location_does_not_exist( | ||
monkeypatch, tmp_path | ||
): | ||
# Given | ||
home = tmp_path / "home" / "test-user" | ||
home.mkdir(parents=True) | ||
monkeypatch.setenv("HOME", str(home)) | ||
|
||
# When | ||
path = lookup.determine_local_config_path() | ||
|
||
# Then | ||
assert path is None | ||
|
||
|
||
def test_determine_local_config_path_default_location_does_exists( | ||
monkeypatch, tmp_path | ||
): | ||
# Given | ||
home = tmp_path / "home" / "test-user" | ||
home.mkdir(parents=True) | ||
monkeypatch.setenv("HOME", str(home)) | ||
|
||
config_dir = home / ".h2oai" | ||
config_dir.mkdir(parents=True) | ||
config_file = config_dir / "h2o-cli-config.toml" | ||
config_file.touch() | ||
|
||
# When | ||
path = lookup.determine_local_config_path() | ||
|
||
# Then | ||
assert path == str(config_file) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import pytest | ||
|
||
from h2o_discovery._internal import config | ||
|
||
|
||
def config_test_cases(): | ||
yield pytest.param("", config.Config(tokens={}), id="empty config") | ||
yield pytest.param( | ||
""" | ||
ClientID = "client-id" | ||
Token = "TestToken" | ||
PlatformClientID = "platform-client-id" | ||
PlatformToken = "TestPlatformToken" | ||
""", | ||
config.Config( | ||
tokens={"client-id": "TestToken", "platform-client-id": "TestPlatformToken"} | ||
), | ||
id="full config", | ||
) | ||
yield pytest.param( | ||
""" | ||
ClientID = "client-id" | ||
Token = "TestToken" | ||
""", | ||
config.Config(tokens={"client-id": "TestToken"}), | ||
id="cli token only", | ||
) | ||
yield pytest.param( | ||
""" | ||
PlatformClientID = "platform-client-id" | ||
PlatformToken = "TestPlatformToken" | ||
""", | ||
config.Config(tokens={"platform-client-id": "TestPlatformToken"}), | ||
id="platform token only", | ||
) | ||
|
||
|
||
@pytest.mark.parametrize("config_content,expected_results", config_test_cases()) | ||
def test_load_config(config_content, expected_results, tmp_path): | ||
# Given | ||
config_file = tmp_path / "config.toml" | ||
config_file.write_text(config_content) | ||
|
||
# When | ||
result = config.load_config(config_file) | ||
|
||
# Then | ||
assert result == expected_results | ||
|
||
|
||
def test_load_config_no_path(tmp_path): | ||
# When | ||
result = config.load_config() | ||
|
||
# Then | ||
assert result == config.Config() |