| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2019 William Brown <[email protected]> |
| 3 |
# All rights reserved. |
| 4 |
# |
| 5 |
# License: GPL (version 3 or any later version). |
| 6 |
# See LICENSE for details. |
| 7 |
# --- END COPYRIGHT BLOCK --- |
| 8 |
|
| 9 |
import ldap |
| 10 |
import pytest |
| 11 |
import time |
| 12 |
from lib389.idm.user import nsUserAccounts, UserAccounts |
| 13 |
from test389.topologies import topology_st as topology |
| 14 |
from lib389.paths import Paths |
| 15 |
from lib389.utils import ds_is_older |
| 16 |
from lib389._constants import * |
| 17 |
from lib389.idm.directorymanager import DirectoryManager |
| 18 |
from lib389.idm.account import Anonymous |
| 19 |
from lib389.extended_operations import LdapSSOTokenRequest |
| 20 |
|
| 21 |
default_paths = Paths() |
| 22 |
|
| 23 |
pytestmark = pytest.mark.tier1 |
| 24 |
|
| 25 |
USER_PASSWORD = "password aouoaeu" |
| 26 |
TEST_KEY = "4PXhmtKG7iCdT9C49GoBdD92x5X1tvF3eW9bHq4ND2Q=" |
| 27 |
|
| 28 |
@pytest.mark.skipif(not default_paths.rust_enabled or ds_is_older('1.4.3.3'), reason="Auth tokens are not available in older versions") |
| 29 |
def test_ldap_auth_token_config(topology): |
| 30 |
""" Test that we are able to configure the ldapssotoken backend with various types and states. |
| 31 |
|
| 32 |
:id: e9b9360b-76df-40ef-9f45-b448df4c9eda |
| 33 |
|
| 34 |
:setup: Standalone instance |
| 35 |
|
| 36 |
:steps: |
| 37 |
1. Enable the feature |
| 38 |
2. Set a key manually. |
| 39 |
3. Regerate a key server side. |
| 40 |
4. Attempt to set invalid keys. |
| 41 |
5. Disable the feature |
| 42 |
6. Assert that key changes are rejected |
| 43 |
|
| 44 |
:expectedresults: |
| 45 |
1. Feature enables |
| 46 |
2. Key is set and accepted |
| 47 |
3. The key is regenerated and unique |
| 48 |
4. The key is rejected |
| 49 |
5. The disable functions online |
| 50 |
6. The key changes are rejected |
| 51 |
""" |
| 52 |
# Enable token |
| 53 |
topology.standalone.config.set('nsslapd-enable-ldapssotoken', 'on') # enable it. |
| 54 |
# Set a key |
| 55 |
topology.standalone.config.set('nsslapd-ldapssotoken-secret', TEST_KEY) |
| 56 |
# regen a key |
| 57 |
topology.standalone.config.remove_all('nsslapd-ldapssotoken-secret') |
| 58 |
k1 = topology.standalone.config.get_attr_val_utf8('nsslapd-ldapssotoken-secret') |
| 59 |
assert(k1 != TEST_KEY) |
| 60 |
# set an invalid key |
| 61 |
with pytest.raises(ldap.UNWILLING_TO_PERFORM): |
| 62 |
topology.standalone.config.set('nsslapd-ldapssotoken-secret', 'invalid key') |
| 63 |
with pytest.raises(ldap.UNWILLING_TO_PERFORM): |
| 64 |
topology.standalone.config.set('nsslapd-ldapssotoken-secret', '') |
| 65 |
# Disable token |
| 66 |
topology.standalone.config.set('nsslapd-enable-ldapssotoken', 'off') # disable it. |
| 67 |
# Set a key |
| 68 |
with pytest.raises(ldap.OPERATIONS_ERROR): |
| 69 |
topology.standalone.config.set('nsslapd-ldapssotoken-secret', TEST_KEY) |
| 70 |
# regen a key |
| 71 |
with pytest.raises(ldap.OPERATIONS_ERROR): |
| 72 |
topology.standalone.config.remove_all('nsslapd-ldapssotoken-secret') |
| 73 |
|
| 74 |
|
| 75 |
@pytest.mark.skipif(not default_paths.rust_enabled or ds_is_older('1.4.3.3'), reason="Auth tokens are not available in older versions") |
| 76 |
def test_ldap_auth_token_nsuser(topology): |
| 77 |
""" |
| 78 |
Test that we can generate and authenticate with authentication tokens |
| 79 |
for users in the directory, as well as security properties around these |
| 80 |
tokens. |
| 81 |
|
| 82 |
:id: 65335341-c85b-457d-ac7d-c4079ac90a60 |
| 83 |
|
| 84 |
:setup: Standalone instance |
| 85 |
|
| 86 |
:steps: |
| 87 |
1. Create an account |
| 88 |
2. Generate a token for the account |
| 89 |
3. Authenticate with the token |
| 90 |
4. Assert that a token can not be issued from a token-authed account |
| 91 |
5. Regenerate the server key |
| 92 |
6. Assert the token no longer authenticates |
| 93 |
|
| 94 |
:expectedresults: |
| 95 |
1. Account is created |
| 96 |
2. Token is generated |
| 97 |
3. Token authenticates |
| 98 |
4. Token is NOT issued |
| 99 |
5. The key is regenerated |
| 100 |
6. The token fails to bind. |
| 101 |
""" |
| 102 |
topology.standalone.enable_tls() |
| 103 |
topology.standalone.config.set('nsslapd-enable-ldapssotoken', 'on') # enable it. |
| 104 |
nsusers = nsUserAccounts(topology.standalone, DEFAULT_SUFFIX) |
| 105 |
# Create a user as dm. |
| 106 |
user = nsusers.create(properties={ |
| 107 |
'uid': 'test_nsuser', |
| 108 |
'cn': 'test_nsuser', |
| 109 |
'displayName': 'testNsuser', |
| 110 |
'legalName': 'testNsuser', |
| 111 |
'uidNumber': '1001', |
| 112 |
'gidNumber': '1001', |
| 113 |
'homeDirectory': '/home/testnsuser', |
| 114 |
'userPassword': USER_PASSWORD, |
| 115 |
}) |
| 116 |
# Create a new con and bind as the user. |
| 117 |
user_conn = user.bind(USER_PASSWORD) |
| 118 |
user_account = nsUserAccounts(user_conn, DEFAULT_SUFFIX).get('test_nsuser') |
| 119 |
# From the user_conn do an extop_s for the token |
| 120 |
token = user_account.request_sso_token() |
| 121 |
# Great! Now do a bind where the token is the pw: |
| 122 |
# user_conn_tok = user.bind(token) |
| 123 |
user_conn_tok = user.authenticate_sso_token(token) |
| 124 |
# Assert whoami. |
| 125 |
# Assert that user_conn_tok with the token can NOT get a new token. |
| 126 |
user_tok_account = nsUserAccounts(user_conn_tok, DEFAULT_SUFFIX).get('test_nsuser') |
| 127 |
with pytest.raises(ldap.UNWILLING_TO_PERFORM): |
| 128 |
user_tok_account.request_sso_token() |
| 129 |
|
| 130 |
# Check with a lowered ttl (should deny) |
| 131 |
topology.standalone.config.set('nsslapd-ldapssotoken-ttl-secs', '1') # Set a low ttl |
| 132 |
# Ensure it's past - the one time I'll allow a sleep .... |
| 133 |
time.sleep(2) |
| 134 |
with pytest.raises(ldap.INVALID_CREDENTIALS): |
| 135 |
user.authenticate_sso_token(token) |
| 136 |
topology.standalone.config.set('nsslapd-ldapssotoken-ttl-secs', '3600') # Set a reasonable |
| 137 |
|
| 138 |
# Regenerate the server token key |
| 139 |
topology.standalone.config.remove_all('nsslapd-ldapssotoken-secret') |
| 140 |
# check we fail to authenticate. |
| 141 |
with pytest.raises(ldap.INVALID_CREDENTIALS): |
| 142 |
user.authenticate_sso_token(token) |
| 143 |
|
| 144 |
@pytest.mark.skipif(not default_paths.rust_enabled or ds_is_older('1.4.3.3'), reason="Auth tokens are not available in older versions") |
| 145 |
def test_ldap_auth_token_disabled(topology): |
| 146 |
""" Assert when the feature is disabled that token operations are not able to progress |
| 147 |
|
| 148 |
:id: ccde5d0b-7f2d-49d5-b9d5-f7082f8f36a3 |
| 149 |
|
| 150 |
:setup: Standalone instance |
| 151 |
|
| 152 |
:steps: |
| 153 |
1. Create a user |
| 154 |
2. Attempt to get a token. |
| 155 |
3. Enable the feature, get a token, then disable it. |
| 156 |
4. Attempt to auth |
| 157 |
|
| 158 |
:expectedresults: |
| 159 |
1. Success |
| 160 |
2. Fails to get a token |
| 161 |
3. Token is received |
| 162 |
4. Auth fails as token is disabled. |
| 163 |
""" |
| 164 |
topology.standalone.enable_tls() |
| 165 |
topology.standalone.config.set('nsslapd-enable-ldapssotoken', 'off') # disable it. |
| 166 |
nsusers = nsUserAccounts(topology.standalone, DEFAULT_SUFFIX) |
| 167 |
# Create a user as dm. |
| 168 |
user = nsusers.create(properties={ |
| 169 |
'uid': 'test_nsuser1', |
| 170 |
'cn': 'test_nsuser1', |
| 171 |
'displayName': 'testNsuser1', |
| 172 |
'legalName': 'testNsuser1', |
| 173 |
'uidNumber': '1002', |
| 174 |
'gidNumber': '1002', |
| 175 |
'homeDirectory': '/home/testnsuser1', |
| 176 |
'userPassword': USER_PASSWORD, |
| 177 |
}) |
| 178 |
# Create a new con and bind as the user. |
| 179 |
user_conn = user.bind(USER_PASSWORD) |
| 180 |
user_account = nsUserAccounts(user_conn, DEFAULT_SUFFIX).get('test_nsuser1') |
| 181 |
# From the user_conn do an extop_s for the token |
| 182 |
with pytest.raises(ldap.PROTOCOL_ERROR): |
| 183 |
user_account.request_sso_token() |
| 184 |
# Now enable it |
| 185 |
topology.standalone.config.set('nsslapd-enable-ldapssotoken', 'on') |
| 186 |
token = user_account.request_sso_token() |
| 187 |
# Now disable |
| 188 |
topology.standalone.config.set('nsslapd-enable-ldapssotoken', 'off') |
| 189 |
# Now attempt to bind (should fail) |
| 190 |
with pytest.raises(ldap.INVALID_CREDENTIALS): |
| 191 |
user_account.authenticate_sso_token(token) |
| 192 |
|
| 193 |
|
| 194 |
@pytest.mark.skipif(not default_paths.rust_enabled or ds_is_older('1.4.3.3'), reason="Auth tokens are not available in older versions") |
| 195 |
def test_ldap_auth_token_directory_manager(topology): |
| 196 |
""" Test token auth with directory manager is denied |
| 197 |
|
| 198 |
:id: ec9aec64-3edf-4f3f-853a-7527b0c42124 |
| 199 |
|
| 200 |
:setup: Standalone instance |
| 201 |
|
| 202 |
:steps: |
| 203 |
1. Attempt to generate a token as DM |
| 204 |
|
| 205 |
:expectedresults: |
| 206 |
1. Fails |
| 207 |
""" |
| 208 |
topology.standalone.enable_tls() |
| 209 |
topology.standalone.config.set('nsslapd-enable-ldapssotoken', 'on') # enable it. |
| 210 |
|
| 211 |
dm = DirectoryManager(topology.standalone) |
| 212 |
# Try getting a token at DM, should fail. |
| 213 |
with pytest.raises(ldap.UNWILLING_TO_PERFORM): |
| 214 |
dm.request_sso_token() |
| 215 |
|
| 216 |
## test as anon (will fail) |
| 217 |
@pytest.mark.skipif(not default_paths.rust_enabled or ds_is_older('1.4.3.3'), reason="Auth tokens are not available in older versions") |
| 218 |
def test_ldap_auth_token_anonymous(topology): |
| 219 |
""" Test token auth with Anonymous is denied. |
| 220 |
|
| 221 |
:id: 966068c3-fbc6-468d-a554-18d68d1d895b |
| 222 |
|
| 223 |
:setup: Standalone instance |
| 224 |
|
| 225 |
:steps: |
| 226 |
1. Attempt to generate a token as Anonymous |
| 227 |
|
| 228 |
:expectedresults: |
| 229 |
1. Fails |
| 230 |
""" |
| 231 |
topology.standalone.enable_tls() |
| 232 |
topology.standalone.config.set('nsslapd-enable-ldapssotoken', 'on') # enable it. |
| 233 |
|
| 234 |
anon_conn = Anonymous(topology.standalone).bind() |
| 235 |
# Build the request |
| 236 |
req = LdapSSOTokenRequest() |
| 237 |
# Get the response |
| 238 |
with pytest.raises(ldap.UNWILLING_TO_PERFORM): |
| 239 |
(_, res) = anon_conn.extop_s(req, escapehatch='i am sure') |
| 240 |
|