|
4 | 4 | import pytest
|
5 | 5 | from django.conf import settings
|
6 | 6 | from django.test.utils import override_settings
|
| 7 | +from onelogin.saml2.settings import OneLogin_Saml2_Settings |
| 8 | +from social_core.backends.saml import SAMLAuth, SAMLIdentityProvider |
7 | 9 |
|
8 | 10 | from ansible_base.authentication.authenticator_plugins.saml import AuthenticatorPlugin
|
9 | 11 | from ansible_base.authentication.session import SessionAuthentication
|
| 12 | +from ansible_base.authentication.social_auth import AuthenticatorConfigTestStrategy, AuthenticatorStorage |
10 | 13 | from ansible_base.lib.utils.encryption import ENCRYPTED_STRING
|
11 | 14 | from ansible_base.lib.utils.response import get_fully_qualified_url, get_relative_url
|
12 | 15 |
|
13 | 16 | authenticated_test_page = "authenticator-list"
|
14 | 17 |
|
| 18 | +idp_string = 'IdP' |
| 19 | + |
| 20 | + |
| 21 | +def get_valid_saml_security_settings(saml_configuration): |
| 22 | + """ |
| 23 | + Helper function to get valid SAML security settings dynamically. |
| 24 | + This mirrors the logic in SAMLConfiguration.validate() method. |
| 25 | + """ |
| 26 | + # Create a minimal configuration for getting valid security settings |
| 27 | + attrs = saml_configuration.copy() |
| 28 | + attrs['ENABLED_IDPS'] = { |
| 29 | + idp_string: { |
| 30 | + 'url': 'https://example.com/sso', |
| 31 | + 'x509cert': attrs.get('IDP_X509_CERT', ''), |
| 32 | + 'entity_id': 'https://example.com/entity', |
| 33 | + 'attr_username': 'username', |
| 34 | + 'attr_user_permanent_id': 'uid', |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + saml_auth = SAMLAuth(AuthenticatorConfigTestStrategy(AuthenticatorStorage(), additional_settings=attrs)) |
| 39 | + saml_auth.redirect_uri = attrs['CALLBACK_URL'] |
| 40 | + idp = SAMLIdentityProvider(idp_string, **attrs['ENABLED_IDPS'][idp_string]) |
| 41 | + config = saml_auth.generate_saml_config(idp=idp) |
| 42 | + |
| 43 | + settings = OneLogin_Saml2_Settings(settings=config) |
| 44 | + settings._security = {} |
| 45 | + settings._add_default_values() |
| 46 | + return sorted(settings._security.keys()) |
| 47 | + |
15 | 48 |
|
16 | 49 | @mock.patch("rest_framework.views.APIView.authentication_classes", [SessionAuthentication])
|
17 | 50 | @mock.patch("ansible_base.authentication.authenticator_plugins.saml.AuthenticatorPlugin.authenticate")
|
@@ -58,6 +91,26 @@ def test_saml_auth_successful(authenticate, unauthenticated_api_client, saml_aut
|
58 | 91 | {"IDP_ATTR_USERNAME": "This field is required.", "IDP_ATTR_USER_PERMANENT_ID": "This field is required."},
|
59 | 92 | id="missing IDP_ATTR_USERNAME and IDP_ATTR_USER_PERMANENT_ID",
|
60 | 93 | ),
|
| 94 | + pytest.param( |
| 95 | + {"SECURITY_CONFIG": {"invalidKey1": "value1", "invalidKey2": "value2"}}, |
| 96 | + { |
| 97 | + "SECURITY_CONFIG": ( |
| 98 | + "Invalid keys: invalidKey1, invalidKey2, " |
| 99 | + "Valid keys: allowRepeatAttributeName, authnRequestsSigned, digestAlgorithm, " |
| 100 | + "failOnAuthnContextMismatch, logoutRequestSigned, logoutResponseSigned, " |
| 101 | + "metadataCacheDuration, metadataValidUntil, nameIdEncrypted, rejectDeprecatedAlgorithm, " |
| 102 | + "requestedAuthnContext, requestedAuthnContextComparison, signMetadata, signatureAlgorithm, " |
| 103 | + "wantAssertionsEncrypted, wantAssertionsSigned, wantAttributeStatement, wantMessagesSigned, " |
| 104 | + "wantNameId, wantNameIdEncrypted" |
| 105 | + ) |
| 106 | + }, |
| 107 | + id="invalid security config keys", |
| 108 | + ), |
| 109 | + pytest.param( |
| 110 | + {"SECURITY_CONFIG": {"zebra": "value1", "alpha": "value2", "beta": "value3"}}, |
| 111 | + {"SECURITY_CONFIG": ("Invalid keys: alpha, beta, zebra")}, |
| 112 | + id="invalid security config keys are sorted alphabetically", |
| 113 | + ), |
61 | 114 | ],
|
62 | 115 | )
|
63 | 116 | def test_saml_create_authenticator_error_handling(
|
@@ -99,6 +152,50 @@ def test_saml_create_authenticator_error_handling(
|
99 | 152 | assert value in response.data[key]
|
100 | 153 |
|
101 | 154 |
|
| 155 | +def test_saml_security_config_validation_with_dynamic_keys(admin_api_client, saml_configuration): |
| 156 | + """ |
| 157 | + Test that SAML security config validation uses dynamically obtained valid keys |
| 158 | + and that invalid keys are sorted alphabetically in error messages. |
| 159 | + """ |
| 160 | + # Get the valid security settings dynamically |
| 161 | + valid_keys = get_valid_saml_security_settings(saml_configuration) |
| 162 | + |
| 163 | + # Test with invalid keys that should be sorted alphabetically |
| 164 | + invalid_keys = ["zebra", "alpha", "beta"] |
| 165 | + security_config = {key: f"value_{key}" for key in invalid_keys} |
| 166 | + |
| 167 | + saml_configuration["SECURITY_CONFIG"] = security_config |
| 168 | + |
| 169 | + url = get_relative_url("authenticator-list") |
| 170 | + data = { |
| 171 | + "name": "SAML authenticator with invalid security config", |
| 172 | + "enabled": True, |
| 173 | + "create_objects": True, |
| 174 | + "remove_users": True, |
| 175 | + "configuration": saml_configuration, |
| 176 | + "type": "ansible_base.authentication.authenticator_plugins.saml", |
| 177 | + } |
| 178 | + |
| 179 | + response = admin_api_client.post(url, data=data, format="json") |
| 180 | + assert response.status_code == 400 |
| 181 | + |
| 182 | + # Verify the response contains security config errors |
| 183 | + assert "SECURITY_CONFIG" in response.data |
| 184 | + error_message = str(response.data["SECURITY_CONFIG"][0]) |
| 185 | + |
| 186 | + # Verify invalid keys are sorted alphabetically |
| 187 | + expected_invalid_keys = "alpha, beta, zebra" |
| 188 | + assert f"Invalid keys: {expected_invalid_keys}" in error_message |
| 189 | + |
| 190 | + # Verify valid keys are included and sorted |
| 191 | + expected_valid_keys = ", ".join(valid_keys) |
| 192 | + assert f"Valid keys: {expected_valid_keys}" in error_message |
| 193 | + |
| 194 | + # Verify that the valid keys we got dynamically match what's in the error message |
| 195 | + # This ensures our helper function works correctly |
| 196 | + assert expected_valid_keys in error_message |
| 197 | + |
| 198 | + |
102 | 199 | def test_saml_create_authenticator_does_not_leak_private_key_on_error(
|
103 | 200 | admin_api_client,
|
104 | 201 | saml_configuration,
|
|
0 commit comments