|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +from unittest.mock import patch |
| 5 | + |
| 6 | +import pytest |
| 7 | +from keystoneauth1 import exceptions, loading |
| 8 | +from keystoneauth1.identity.v3 import oidc as upstream_oidc |
| 9 | + |
| 10 | +from keystoneauth_kubeservicetoken.oidc import ( |
| 11 | + OpenIDConnectAccessTokenFile, |
| 12 | + OpenIDConnectAccessTokenFileLoader, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +class FakeAuthRef: |
| 17 | + def __init__(self, auth_token: str, expires_soon: bool): |
| 18 | + self.auth_token = auth_token |
| 19 | + self._expires_soon = expires_soon |
| 20 | + |
| 21 | + def will_expire_soon(self, _stale_duration: int) -> bool: |
| 22 | + return self._expires_soon |
| 23 | + |
| 24 | + |
| 25 | +def _create_plugin(token_file: Path) -> OpenIDConnectAccessTokenFile: |
| 26 | + return OpenIDConnectAccessTokenFile( |
| 27 | + auth_url="https://keystone.example/v3", |
| 28 | + identity_provider="example-idp", |
| 29 | + protocol="openid", |
| 30 | + access_token_file=str(token_file), |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture |
| 35 | +def auth_options() -> dict[str, str]: |
| 36 | + return { |
| 37 | + "auth_url": "https://keystone.example/v3", |
| 38 | + "identity_provider": "example-idp", |
| 39 | + "protocol": "openid", |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | +def test_loader_options_require_access_token_file_and_not_access_token(): |
| 44 | + loader = OpenIDConnectAccessTokenFileLoader() |
| 45 | + |
| 46 | + options = {option.dest: option for option in loader.get_options()} |
| 47 | + |
| 48 | + assert "access_token_file" in options |
| 49 | + assert options["access_token_file"].required |
| 50 | + assert "access_token" not in options |
| 51 | + |
| 52 | + |
| 53 | +def test_loader_can_initialize_plugin_with_access_token_file_only( |
| 54 | + tmp_path, auth_options |
| 55 | +): |
| 56 | + token_file = tmp_path / "token" |
| 57 | + token_file.write_text("oidc-token", encoding="utf-8") |
| 58 | + |
| 59 | + loader = OpenIDConnectAccessTokenFileLoader() |
| 60 | + plugin = loader.load_from_options( |
| 61 | + **auth_options, |
| 62 | + access_token_file=str(token_file), |
| 63 | + ) |
| 64 | + |
| 65 | + assert isinstance(plugin, OpenIDConnectAccessTokenFile) |
| 66 | + assert plugin.access_token_file == str(token_file) |
| 67 | + |
| 68 | + |
| 69 | +def test_plugin_loader_is_discoverable_by_auth_type(): |
| 70 | + loader = loading.get_plugin_loader("v3oidcaccesstokenfile") |
| 71 | + |
| 72 | + assert isinstance(loader, OpenIDConnectAccessTokenFileLoader) |
| 73 | + |
| 74 | + |
| 75 | +def test_missing_access_token_file_configuration_fails(auth_options): |
| 76 | + with pytest.raises(exceptions.OptionError, match="access_token_file"): |
| 77 | + OpenIDConnectAccessTokenFile( |
| 78 | + **auth_options, |
| 79 | + access_token_file="", |
| 80 | + ) |
| 81 | + |
| 82 | + |
| 83 | +def test_auth_reads_trimmed_token_from_file_each_time(tmp_path): |
| 84 | + token_file = tmp_path / "token" |
| 85 | + token_file.write_text(" token-a\n", encoding="utf-8") |
| 86 | + |
| 87 | + plugin = _create_plugin(token_file) |
| 88 | + observed_tokens: list[str] = [] |
| 89 | + |
| 90 | + def fake_super_get_unscoped_auth_ref(self, _session): |
| 91 | + observed_tokens.append(self.access_token) |
| 92 | + return object() |
| 93 | + |
| 94 | + with patch.object( |
| 95 | + upstream_oidc.OidcAccessToken, |
| 96 | + "get_unscoped_auth_ref", |
| 97 | + autospec=True, |
| 98 | + side_effect=fake_super_get_unscoped_auth_ref, |
| 99 | + ): |
| 100 | + plugin.get_unscoped_auth_ref(session=None) |
| 101 | + token_file.write_text("token-b\n", encoding="utf-8") |
| 102 | + plugin.get_unscoped_auth_ref(session=None) |
| 103 | + |
| 104 | + assert observed_tokens == ["token-a", "token-b"] |
| 105 | + |
| 106 | + |
| 107 | +def test_auth_fails_for_missing_file(tmp_path): |
| 108 | + plugin = _create_plugin(tmp_path / "missing-token") |
| 109 | + |
| 110 | + with pytest.raises(exceptions.AuthorizationFailure, match="does not exist"): |
| 111 | + plugin._read_access_token() |
| 112 | + |
| 113 | + |
| 114 | +def test_auth_fails_for_unreadable_file(tmp_path): |
| 115 | + token_file = tmp_path / "token" |
| 116 | + token_file.write_text("token", encoding="utf-8") |
| 117 | + plugin = _create_plugin(token_file) |
| 118 | + |
| 119 | + with patch("builtins.open", side_effect=OSError("permission denied")): |
| 120 | + with pytest.raises(exceptions.AuthorizationFailure, match="Unable to read"): |
| 121 | + plugin._read_access_token() |
| 122 | + |
| 123 | + |
| 124 | +def test_auth_fails_for_empty_file(tmp_path): |
| 125 | + token_file = tmp_path / "token" |
| 126 | + token_file.write_text("\n\t", encoding="utf-8") |
| 127 | + plugin = _create_plugin(token_file) |
| 128 | + |
| 129 | + with pytest.raises(exceptions.AuthorizationFailure, match="is empty"): |
| 130 | + plugin._read_access_token() |
| 131 | + |
| 132 | + |
| 133 | +def test_file_updates_consumed_on_reauth_not_every_request(tmp_path): |
| 134 | + token_file = tmp_path / "token" |
| 135 | + token_file.write_text("token-a", encoding="utf-8") |
| 136 | + plugin = _create_plugin(token_file) |
| 137 | + |
| 138 | + plugin.auth_ref = FakeAuthRef( |
| 139 | + auth_token="cached-keystone-token", expires_soon=False |
| 140 | + ) |
| 141 | + |
| 142 | + def fake_get_auth_ref(_session): |
| 143 | + plugin.get_unscoped_auth_ref(session=None) |
| 144 | + return FakeAuthRef(auth_token=f"ks-{plugin.access_token}", expires_soon=False) |
| 145 | + |
| 146 | + with ( |
| 147 | + patch.object( |
| 148 | + upstream_oidc.OidcAccessToken, |
| 149 | + "get_unscoped_auth_ref", |
| 150 | + autospec=True, |
| 151 | + return_value=object(), |
| 152 | + ), |
| 153 | + patch.object(plugin, "get_auth_ref", side_effect=fake_get_auth_ref), |
| 154 | + ): |
| 155 | + token_file.write_text("token-b", encoding="utf-8") |
| 156 | + |
| 157 | + assert plugin.get_token(session=None) == "cached-keystone-token" |
| 158 | + |
| 159 | + plugin.auth_ref = FakeAuthRef(auth_token="expiring-token", expires_soon=True) |
| 160 | + assert plugin.get_token(session=None) == "ks-token-b" |
0 commit comments