|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 2 | +# contributor license agreements. See the NOTICE file distributed with |
| 3 | +# this work for additional information regarding copyright ownership. |
| 4 | +# The ASF licenses this file to you under the Apache License, Version 2.0 |
| 5 | +# (the "License"); you may not use this file except in compliance with |
| 6 | +# the License. You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +import os |
| 16 | +import unittest |
| 17 | +import requests |
| 18 | +import urllib3 |
| 19 | +from requests.auth import HTTPBasicAuth |
| 20 | + |
| 21 | +######################################################## |
| 22 | +# This test is verifying the behavior of the RemoteAuthProvider. |
| 23 | +# It is using the 'auth/api/v1/pre' endpoint to get the actor ID and group headers. |
| 24 | +# It is using the 'guest' user to get the guest user headers. |
| 25 | +# It is using the 'admin' user to get the admin user headers. |
| 26 | +# It is verifying that the actor ID and group headers are correct. |
| 27 | +# It is verifying that the actor ID and group headers are not empty. |
| 28 | +# It is verifying that the actor ID and group headers are not None. |
| 29 | +######################################################## |
| 30 | +# Suppress InsecureRequestWarning |
| 31 | +urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) |
| 32 | + |
| 33 | +class TestRemoteAuth(unittest.TestCase): |
| 34 | + def setUp(self): |
| 35 | + self.base_url = os.environ.get("KNOX_GATEWAY_URL", "https://localhost:8443/") |
| 36 | + if not self.base_url.endswith("/"): |
| 37 | + self.base_url += "/" |
| 38 | + self.topology_url = self.base_url + "gateway/remoteauth/auth/api/v1/pre" |
| 39 | + |
| 40 | + def test_remote_auth_success(self): |
| 41 | + """ |
| 42 | + Verify that valid credentials result in successful authentication |
| 43 | + and correct identity assertion using knoxldap as remote auth. |
| 44 | + """ |
| 45 | + print(f"\nTesting remote auth success against {self.topology_url}") |
| 46 | + # The RemoteAuthFilter forwards Authorization header to knoxldap. |
| 47 | + # knoxldap accepts guest:guest-password |
| 48 | + response = requests.get( |
| 49 | + self.topology_url, |
| 50 | + auth=HTTPBasicAuth('guest', 'guest-password'), |
| 51 | + verify=False, |
| 52 | + timeout=30 |
| 53 | + ) |
| 54 | + print(f"Status Code: {response.status_code}") |
| 55 | + print(f"Headers: {response.headers}") |
| 56 | + self.assertEqual(response.status_code, 200) |
| 57 | + |
| 58 | + # RemoteAuthFilter sets principal from x-knox-actor-username (guest) |
| 59 | + # KNOX-AUTH-SERVICE (pre endpoint) echoes principal in X-Knox-Actor-ID (default) |
| 60 | + self.assertIn('X-Knox-Actor-ID', response.headers) |
| 61 | + self.assertEqual(response.headers['X-Knox-Actor-ID'], 'guest') |
| 62 | + |
| 63 | + def test_remote_auth_admin_groups(self): |
| 64 | + """ |
| 65 | + Verify admin user gets multiple groups from knoxldap mapping |
| 66 | + """ |
| 67 | + print(f"\nTesting remote auth admin against {self.topology_url}") |
| 68 | + response = requests.get( |
| 69 | + self.topology_url, |
| 70 | + auth=HTTPBasicAuth('admin', 'admin-password'), |
| 71 | + verify=False, |
| 72 | + timeout=30 |
| 73 | + ) |
| 74 | + self.assertEqual(response.status_code, 200) |
| 75 | + self.assertEqual(response.headers['X-Knox-Actor-ID'], 'admin') |
| 76 | + |
| 77 | + # knoxldap maps admin to: longGroupName1,longGroupName2,longGroupName3,longGroupName4 |
| 78 | + # RemoteAuthFilter picks these up from x-knox-actor-groups-* |
| 79 | + # And KNOX-AUTH-SERVICE echoes them back in X-Knox-Actor-Groups-* |
| 80 | + |
| 81 | + group_headers = [h for h in response.headers if h.lower().startswith('x-knox-actor-groups')] |
| 82 | + all_groups = [] |
| 83 | + for h in group_headers: |
| 84 | + all_groups.extend(response.headers[h].split(',')) |
| 85 | + |
| 86 | + print(f"Found groups: {all_groups}") |
| 87 | + self.assertIn('longGroupName1', all_groups) |
| 88 | + self.assertIn('longGroupName2', all_groups) |
| 89 | + |
| 90 | + def test_remote_auth_failure(self): |
| 91 | + """ |
| 92 | + Verify invalid credentials result in 401 |
| 93 | + """ |
| 94 | + print(f"\nTesting remote auth failure against {self.topology_url}") |
| 95 | + response = requests.get( |
| 96 | + self.topology_url, |
| 97 | + auth=HTTPBasicAuth('baduser', 'badpass'), |
| 98 | + verify=False, |
| 99 | + timeout=30 |
| 100 | + ) |
| 101 | + print(f"Status Code: {response.status_code}") |
| 102 | + # When remote auth fails (knoxldap returns 401), RemoteAuthFilter should return 401 |
| 103 | + self.assertEqual(response.status_code, 401) |
| 104 | + |
| 105 | +if __name__ == '__main__': |
| 106 | + unittest.main() |
0 commit comments