diff options
| author | omnom62 <omnom62@outlook.com> | 2026-10-08 06:05:05 +1000 |
|---|---|---|
| committer | omnom62 <omnom62@outlook.com> | 2026-10-08 06:05:05 +1000 |
| commit | 38c86f32d1b576314a5053b48c380ce2dd7436f7 (patch) | |
| tree | d28d9fc9fa207400f0a11ce1dcf0b4f54e692851 | |
| parent | 09fcbcb4f09759bbbb067ab356706ef3d330d586 (diff) | |
| download | rest.vyos-t8989_auth_methods_rework.tar.gz rest.vyos-t8989_auth_methods_rework.zip | |
T8989: auth_methods - AI fixes - hardent8989_auth_methods_rework
| -rw-r--r-- | changelogs/fragments/t8989_auth_methods.yml | 9 | ||||
| -rw-r--r-- | docs/vyos.rest.vyos_httpapi.rst | 54 | ||||
| -rw-r--r-- | plugins/httpapi/vyos.py | 70 | ||||
| -rw-r--r-- | tests/unit/test_httpapi_vyos.py | 73 |
4 files changed, 198 insertions, 8 deletions
diff --git a/changelogs/fragments/t8989_auth_methods.yml b/changelogs/fragments/t8989_auth_methods.yml index 1f2fdd1..9044d19 100644 --- a/changelogs/fragments/t8989_auth_methods.yml +++ b/changelogs/fragments/t8989_auth_methods.yml @@ -1,4 +1,3 @@ ---- minor_changes: - vyos httpapi plugin - add support for four additional authentication methods against the VyOS REST API - ``header`` (API key sent as an @@ -10,3 +9,11 @@ minor_changes: via the new ``auth_method`` option. The existing API-key-in-the-request-body behaviour remains the default, so no change is required for existing inventories. + - vyos httpapi plugin - the OIDC token endpoint must use ``https``, because + the request carries the client secret and the response carries a bearer + token. A plain ``http`` endpoint (for example a lab identity provider) is + accepted only with the new ``oidc_allow_insecure_http`` option, which logs + a warning, and other URL schemes are never accepted. Certificate + validation of the token endpoint can be turned off for a self-signed lab + provider with ``oidc_validate_certs``, and the request is bounded by + ``oidc_timeout``. diff --git a/docs/vyos.rest.vyos_httpapi.rst b/docs/vyos.rest.vyos_httpapi.rst index 608339d..b91c229 100644 --- a/docs/vyos.rest.vyos_httpapi.rst +++ b/docs/vyos.rest.vyos_httpapi.rst @@ -89,6 +89,32 @@ Parameters <tr> <td colspan="1"> <div class="ansibleOptionAnchor" id="parameter-"></div> + <b>oidc_allow_insecure_http</b> + <a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a> + <div style="font-size: small"> + <span style="color: purple">boolean</span> + </div> + </td> + <td> + <ul style="margin: 0; padding: 0"><b>Choices:</b> + <li><div style="color: blue"><b>no</b> ←</div></li> + <li>yes</li> + </ul> + </td> + <td> + <div> ini entries: + <p>[httpapi]<br>oidc_allow_insecure_http = no</p> + </div> + <div>var: ansible_httpapi_oidc_allow_insecure_http</div> + </td> + <td> + <div>By default the OIDC token endpoint must use <code>https</code>, because the request carries the client secret and the response carries a bearer token. Set this to <code>true</code> to allow a plain <code>http</code> endpoint.</div> + <div>Intended for isolated lab setups only. With this enabled the client secret and the access token travel in cleartext and can be captured and replayed by anyone on the network path.</div> + </td> + </tr> + <tr> + <td colspan="1"> + <div class="ansibleOptionAnchor" id="parameter-"></div> <b>oidc_client_id</b> <a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a> <div style="font-size: small"> @@ -171,6 +197,32 @@ Parameters <div>Token endpoint URL of the OIDC provider. Required for auth_method <code>oidc</code>.</div> </td> </tr> + <tr> + <td colspan="1"> + <div class="ansibleOptionAnchor" id="parameter-"></div> + <b>oidc_validate_certs</b> + <a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a> + <div style="font-size: small"> + <span style="color: purple">boolean</span> + </div> + </td> + <td> + <ul style="margin: 0; padding: 0"><b>Choices:</b> + <li>no</li> + <li><div style="color: blue"><b>yes</b> ←</div></li> + </ul> + </td> + <td> + <div> ini entries: + <p>[httpapi]<br>oidc_validate_certs = yes</p> + </div> + <div>var: ansible_httpapi_oidc_validate_certs</div> + </td> + <td> + <div>Validate the TLS certificate of the OIDC token endpoint.</div> + <div>Set to <code>false</code> only for a lab identity provider that uses a self-signed certificate.</div> + </td> + </tr> </table> <br/> @@ -188,7 +240,7 @@ Status Authors ~~~~~~~ -- Evgeny Molotkov (@eomnom62) +- VyOS Community (@vyos) .. hint:: diff --git a/plugins/httpapi/vyos.py b/plugins/httpapi/vyos.py index e8121e7..b13a807 100644 --- a/plugins/httpapi/vyos.py +++ b/plugins/httpapi/vyos.py @@ -18,7 +18,7 @@ description: and cached), C(mtls) (mutual TLS, no application-level credential), and C(oidc) (a bearer token obtained from an external OpenID Connect provider via the client_credentials grant, fetched and cached). -author: Evgeny Molotkov (@eomnom62) +author: VyOS Community (@vyos) options: api_key: description: VyOS API key. Required for auth_method C(key), C(header), and C(bearer). @@ -78,18 +78,52 @@ options: ini: - section: httpapi key: oidc_timeout + oidc_allow_insecure_http: + description: + - >- + By default the OIDC token endpoint must use C(https), because the + request carries the client secret and the response carries a bearer + token. Set this to C(true) to allow a plain C(http) endpoint. + - >- + Intended for isolated lab setups only. With this enabled the client + secret and the access token travel in cleartext and can be captured + and replayed by anyone on the network path. + type: bool + default: false + vars: + - name: ansible_httpapi_oidc_allow_insecure_http + ini: + - section: httpapi + key: oidc_allow_insecure_http + oidc_validate_certs: + description: + - Validate the TLS certificate of the OIDC token endpoint. + - >- + Set to C(false) only for a lab identity provider that uses a + self-signed certificate. + type: bool + default: true + vars: + - name: ansible_httpapi_oidc_validate_certs + ini: + - section: httpapi + key: oidc_validate_certs """ import json import os import time -from urllib.parse import urlencode +from urllib.parse import urlencode, urlparse from ansible.errors import AnsibleConnectionFailure from ansible.module_utils.connection import ConnectionError from ansible.module_utils.urls import open_url from ansible.plugins.httpapi import HttpApiBase +from ansible.utils.display import Display + + +display = Display() class HttpApi(HttpApiBase): @@ -180,12 +214,40 @@ class HttpApi(HttpApiBase): # validation across the whole ecosystem). # ----------------------------------------------------------------- + def _check_oidc_scheme(self, token_url): + """The token request carries client_secret and the response + carries a bearer token, so fail closed unless the endpoint is + https. Plain http is allowed only through the explicit + oidc_allow_insecure_http opt-in (lab use), with a warning. Any + other scheme (file:, ftp:, ...) is never accepted: open_url + would otherwise follow it. + """ + scheme = urlparse(token_url).scheme + if scheme == "https": + return + if scheme == "http": + if self.get_option("oidc_allow_insecure_http"): + display.warning( + "oidc_token_url uses plain http: the OIDC client secret and the " + "access token are sent in cleartext. Use https outside a lab.", + ) + return + raise ConnectionError( + "oidc_token_url must use https because it carries the client secret " + "and the access token. For a lab identity provider set " + "oidc_allow_insecure_http=true.", + ) + raise ConnectionError( + "oidc_token_url must be an https URL (got scheme '{0}').".format(scheme), + ) + def _fetch_oidc_token(self): token_url = self.get_option("oidc_token_url") if not token_url: raise ConnectionError( "oidc_token_url is required when auth_method=oidc.", ) + self._check_oidc_scheme(token_url) body = urlencode( { "grant_type": "client_credentials", @@ -196,6 +258,9 @@ class HttpApi(HttpApiBase): timeout = self.get_option("oidc_timeout") if timeout is None: timeout = 10 + validate_certs = self.get_option("oidc_validate_certs") + if validate_certs is None: + validate_certs = True try: response = open_url( token_url, @@ -203,6 +268,7 @@ class HttpApi(HttpApiBase): headers={"Content-Type": "application/x-www-form-urlencoded"}, method="POST", timeout=timeout, + validate_certs=validate_certs, ) payload = json.loads(response.read()) except Exception as exc: diff --git a/tests/unit/test_httpapi_vyos.py b/tests/unit/test_httpapi_vyos.py index 4541c46..2ef1098 100644 --- a/tests/unit/test_httpapi_vyos.py +++ b/tests/unit/test_httpapi_vyos.py @@ -288,18 +288,21 @@ class TestSendRequestMtlsMethod(unittest.TestCase): class TestSendRequestOidcMethod(unittest.TestCase): def _plugin( self, - token_url="http://idp/token", + token_url="https://idp/token", client_id="vyos-api", client_secret="secret", + **extra, ): conn = MagicMock() plugin = HttpApi(conn) - plugin.get_option = { + options = { "auth_method": "oidc", "oidc_token_url": token_url, "oidc_client_id": client_id, "oidc_client_secret": client_secret, - }.get + } + options.update(extra) + plugin.get_option = options.get return plugin def _idp_response(self, token="oidctoken123", expires_in=3600): @@ -382,7 +385,7 @@ class TestSendRequestOidcMethod(unittest.TestCase): plugin = self._plugin() plugin.get_option = { "auth_method": "oidc", - "oidc_token_url": "http://idp/token", + "oidc_token_url": "https://idp/token", "oidc_client_id": "vyos-api", "oidc_client_secret": "secret", "oidc_timeout": 5, @@ -395,6 +398,68 @@ class TestSendRequestOidcMethod(unittest.TestCase): plugin.send_request("/retrieve", op="showConfig", path=[]) self.assertEqual(mock_open_url.call_args[1]["timeout"], 5) + def _run_token_fetch(self, plugin): + """Run one request with open_url and the plugin's display + mocked; returns (mock_open_url, mock_display).""" + target = "ansible_collections.vyos.rest.plugins.httpapi.vyos" + with patch(target + ".open_url") as mock_open_url, patch( + target + ".display", + ) as mock_display: + mock_resp = MagicMock() + mock_resp.read.return_value = self._idp_response() + mock_open_url.return_value = mock_resp + plugin.connection.send.return_value = self._retrieve_response() + plugin.send_request("/retrieve", op="showConfig", path=[]) + return mock_open_url, mock_display + + def test_oidc_https_endpoint_accepted_without_warning(self): + plugin = self._plugin(token_url="https://idp/token") + mock_open_url, mock_display = self._run_token_fetch(plugin) + self.assertEqual(mock_open_url.call_count, 1) + mock_display.warning.assert_not_called() + + def test_oidc_rejects_plain_http_by_default(self): + """The request carries client_secret and the response a bearer + token, so a plain http endpoint must fail closed -- before any + request is made.""" + plugin = self._plugin(token_url="http://idp/token") + target = "ansible_collections.vyos.rest.plugins.httpapi.vyos.open_url" + with patch(target) as mock_open_url: + with self.assertRaises(ConnectionError) as ctx: + plugin.send_request("/retrieve", op="showConfig", path=[]) + self.assertIn("https", str(ctx.exception)) + self.assertIn("oidc_allow_insecure_http", str(ctx.exception)) + mock_open_url.assert_not_called() + + def test_oidc_plain_http_allowed_with_opt_in_and_warns(self): + plugin = self._plugin(token_url="http://idp/token", oidc_allow_insecure_http=True) + mock_open_url, mock_display = self._run_token_fetch(plugin) + self.assertEqual(mock_open_url.call_count, 1) + self.assertEqual(mock_display.warning.call_count, 1) + self.assertIn("cleartext", mock_display.warning.call_args[0][0]) + + def test_oidc_rejects_other_schemes_even_with_opt_in(self): + """The opt-in only relaxes http. file:, ftp: and a missing scheme + are never followed.""" + for url in ("file:///etc/passwd", "ftp://idp/token", "idp/token"): + plugin = self._plugin(token_url=url, oidc_allow_insecure_http=True) + target = "ansible_collections.vyos.rest.plugins.httpapi.vyos.open_url" + with patch(target) as mock_open_url: + with self.assertRaises(ConnectionError) as ctx: + plugin.send_request("/retrieve", op="showConfig", path=[]) + self.assertIn("https", str(ctx.exception), url) + mock_open_url.assert_not_called() + + def test_oidc_validates_certs_by_default(self): + plugin = self._plugin() + mock_open_url, _display = self._run_token_fetch(plugin) + self.assertIs(mock_open_url.call_args[1]["validate_certs"], True) + + def test_oidc_cert_validation_can_be_disabled_for_lab(self): + plugin = self._plugin(oidc_validate_certs=False) + mock_open_url, _display = self._run_token_fetch(plugin) + self.assertIs(mock_open_url.call_args[1]["validate_certs"], False) + def test_oidc_raises_when_access_token_missing(self): plugin = self._plugin() with patch("ansible_collections.vyos.rest.plugins.httpapi.vyos.open_url") as mock_open_url: |
