| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright: (c) 2020, Jordan Borean (@jborean93) <[email protected]> |
| 3 |
# MIT License (see LICENSE or https://opensource.org/licenses/MIT) |
| 4 |
|
| 5 |
import collections |
| 6 |
import re |
| 7 |
import typing |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
import spnego |
| 12 |
import spnego._gss |
| 13 |
import spnego.iov |
| 14 |
from spnego.exceptions import InvalidCredentialError, NoContextError |
| 15 |
|
| 16 |
|
| 17 |
def test_gss_sasl_description_fail(mocker, monkeypatch): |
| 18 |
gssapi = pytest.importorskip("gssapi") |
| 19 |
SASLResult = collections.namedtuple("SASLResult", ["mech_description"]) |
| 20 |
|
| 21 |
mock_inquire_sasl = mocker.MagicMock() |
| 22 |
mock_inquire_sasl.side_effect = [Exception, SASLResult(b"result")] |
| 23 |
monkeypatch.setattr(gssapi.raw, "inquire_saslname_for_mech", mock_inquire_sasl) |
| 24 |
|
| 25 |
actual = spnego._gss._gss_sasl_description(gssapi.OID.from_int_seq("1.2.3")) |
| 26 |
assert actual is None |
| 27 |
|
| 28 |
actual = spnego._gss._gss_sasl_description(gssapi.OID.from_int_seq("1.2.3")) |
| 29 |
assert actual is None |
| 30 |
|
| 31 |
actual = spnego._gss._gss_sasl_description(gssapi.OID.from_int_seq("1.2.3.4")) |
| 32 |
assert actual == b"result" |
| 33 |
|
| 34 |
actual = spnego._gss._gss_sasl_description(gssapi.OID.from_int_seq("1.2.3.4")) |
| 35 |
assert actual == b"result" |
| 36 |
|
| 37 |
assert mock_inquire_sasl.call_count == 2 |
| 38 |
|
| 39 |
|
| 40 |
def test_build_iov_list(kerb_cred): |
| 41 |
c = spnego._gss.GSSAPIProxy(kerb_cred.user_princ, protocol="kerberos") |
| 42 |
actual = c._build_iov_list( |
| 43 |
[ |
| 44 |
(spnego.iov.BufferType.header, b"\x01"), |
| 45 |
(spnego.iov.BufferType.data, 1), |
| 46 |
(spnego.iov.BufferType.padding, True), |
| 47 |
spnego.iov.BufferType.header, |
| 48 |
spnego.iov.BufferType.stream, |
| 49 |
b"\x02", |
| 50 |
], |
| 51 |
c._convert_iov_buffer, |
| 52 |
) |
| 53 |
|
| 54 |
assert len(actual) == 6 |
| 55 |
assert actual[0] == (spnego.iov.BufferType.header, False, b"\x01") |
| 56 |
assert actual[1] == (spnego.iov.BufferType.data, False, b"\x00") |
| 57 |
assert actual[2] == (spnego.iov.BufferType.padding, True, None) |
| 58 |
assert actual[3] == (spnego.iov.BufferType.header, True, None) |
| 59 |
assert actual[4] == (spnego.iov.BufferType.stream, False, None) |
| 60 |
assert actual[5] == (spnego.iov.BufferType.data, False, b"\x02") |
| 61 |
|
| 62 |
|
| 63 |
def test_gssapi_query_message_sizes_fail(kerb_cred): |
| 64 |
c = spnego._gss.GSSAPIProxy(kerb_cred.user_princ, protocol="kerberos") |
| 65 |
|
| 66 |
with pytest.raises(NoContextError, match="Cannot get message sizes until context has been established"): |
| 67 |
c.query_message_sizes() |
| 68 |
|
| 69 |
|
| 70 |
def test_gssapi_wrap_no_context(kerb_cred): |
| 71 |
c = spnego._gss.GSSAPIProxy(kerb_cred.user_princ, protocol="kerberos") |
| 72 |
|
| 73 |
with pytest.raises(NoContextError, match="Cannot wrap until context has been established"): |
| 74 |
c.wrap(b"data") |
| 75 |
|
| 76 |
|
| 77 |
def test_gssapi_wrap_iov_no_context(kerb_cred): |
| 78 |
c = spnego._gss.GSSAPIProxy(kerb_cred.user_princ, protocol="kerberos") |
| 79 |
|
| 80 |
with pytest.raises(NoContextError, match="Cannot wrap until context has been established"): |
| 81 |
c.wrap_iov([]) |
| 82 |
|
| 83 |
|
| 84 |
def test_gssapi_wrap_winrm_no_context(kerb_cred): |
| 85 |
c = spnego._gss.GSSAPIProxy(kerb_cred.user_princ, protocol="kerberos") |
| 86 |
|
| 87 |
with pytest.raises(NoContextError, match="Cannot wrap until context has been established"): |
| 88 |
c.wrap_winrm(b"data") |
| 89 |
|
| 90 |
|
| 91 |
def test_gssapi_unwrap_no_context(kerb_cred): |
| 92 |
c = spnego._gss.GSSAPIProxy(kerb_cred.user_princ, protocol="kerberos") |
| 93 |
|
| 94 |
with pytest.raises(NoContextError, match="Cannot unwrap until context has been established"): |
| 95 |
c.unwrap(b"data") |
| 96 |
|
| 97 |
|
| 98 |
def test_gssapi_unwrap_iov_no_context(kerb_cred): |
| 99 |
c = spnego._gss.GSSAPIProxy(kerb_cred.user_princ, protocol="kerberos") |
| 100 |
|
| 101 |
with pytest.raises(NoContextError, match="Cannot unwrap until context has been established"): |
| 102 |
c.unwrap_iov([]) |
| 103 |
|
| 104 |
|
| 105 |
def test_gssapi_unwrap_winrm_no_context(kerb_cred): |
| 106 |
c = spnego._gss.GSSAPIProxy(kerb_cred.user_princ, protocol="kerberos") |
| 107 |
|
| 108 |
with pytest.raises(NoContextError, match="Cannot unwrap until context has been established"): |
| 109 |
c.unwrap_winrm(b"header", b"data") |
| 110 |
|
| 111 |
|
| 112 |
def test_gssapi_sign_no_context(kerb_cred): |
| 113 |
c = spnego._gss.GSSAPIProxy(kerb_cred.user_princ, protocol="kerberos") |
| 114 |
|
| 115 |
with pytest.raises(NoContextError, match="Cannot sign until context has been established"): |
| 116 |
c.sign(b"data") |
| 117 |
|
| 118 |
|
| 119 |
def test_gssapi_verify_no_context(kerb_cred): |
| 120 |
c = spnego._gss.GSSAPIProxy(kerb_cred.user_princ, protocol="kerberos") |
| 121 |
|
| 122 |
with pytest.raises(NoContextError, match="Cannot verify until context has been established"): |
| 123 |
c.verify(b"data", b"mic") |
| 124 |
|
| 125 |
|
| 126 |
def test_build_iov_list_invalid_tuple(kerb_cred): |
| 127 |
c = spnego._gss.GSSAPIProxy(kerb_cred.user_princ, protocol="kerberos") |
| 128 |
|
| 129 |
expected = "IOV entry tuple must contain 2 values, the type and data, see IOVBuffer." |
| 130 |
with pytest.raises(ValueError, match=expected): |
| 131 |
c._build_iov_list([(1, 2, 3)], c._convert_iov_buffer) # type: ignore[list-item] # we are testing this |
| 132 |
|
| 133 |
|
| 134 |
def test_build_iov_list_invalid_buffer_type(kerb_cred): |
| 135 |
c = spnego._gss.GSSAPIProxy(kerb_cred.user_princ, protocol="kerberos") |
| 136 |
|
| 137 |
expected = "IOV entry[0] must specify the BufferType as an int" |
| 138 |
with pytest.raises(ValueError, match=re.escape(expected)): |
| 139 |
c._build_iov_list([(b"", b"")], c._convert_iov_buffer) # type: ignore[list-item] # we are testing this |
| 140 |
|
| 141 |
|
| 142 |
def test_build_iov_list_invalid_data(kerb_cred): |
| 143 |
c = spnego._gss.GSSAPIProxy(kerb_cred.user_princ, protocol="kerberos") |
| 144 |
|
| 145 |
expected = "IOV entry[1] must specify the buffer bytes, length of the buffer, or whether it is auto allocated." |
| 146 |
with pytest.raises(ValueError, match=re.escape(expected)): |
| 147 |
c._build_iov_list([(1, "data")], c._convert_iov_buffer) # type: ignore[list-item] # we are testing this |
| 148 |
|
| 149 |
|
| 150 |
def test_build_iov_list_invalid_value(kerb_cred): |
| 151 |
c = spnego._gss.GSSAPIProxy(kerb_cred.user_princ, protocol="kerberos") |
| 152 |
|
| 153 |
expected = "IOV entry must be a IOVBuffer tuple, int, or bytes" |
| 154 |
with pytest.raises(ValueError, match=re.escape(expected)): |
| 155 |
c._build_iov_list([None], c._convert_iov_buffer) # type: ignore[list-item] # we are testing this |
| 156 |
|
| 157 |
|
| 158 |
def test_no_gssapi_library(monkeypatch): |
| 159 |
monkeypatch.setattr(spnego._gss, "HAS_GSSAPI", False) |
| 160 |
|
| 161 |
with pytest.raises(ImportError, match="GSSAPIProxy requires the Python gssapi library"): |
| 162 |
spnego._gss.GSSAPIProxy() |
| 163 |
|
| 164 |
|
| 165 |
@pytest.mark.skipif(not spnego._gss.HAS_GSSAPI, reason="Requires the gssapi library to be installed for testing") |
| 166 |
def test_gssapi_no_valid_acceptor_cred(): |
| 167 |
server_creds: typing.List[typing.Tuple[spnego.Credential, str]] = [ |
| 168 |
(spnego.KerberosCCache("ccache"), "kerberos"), |
| 169 |
(spnego.KerberosKeytab("user_princ", "ccache"), "kerberos"), |
| 170 |
(spnego.NTLMHash("user_princ"), "ntlm"), |
| 171 |
] |
| 172 |
for cred, protocol in server_creds: |
| 173 |
if protocol not in spnego._gss.GSSAPIProxy.available_protocols(): |
| 174 |
continue |
| 175 |
|
| 176 |
with pytest.raises(InvalidCredentialError, match="No applicable credentials available"): |
| 177 |
spnego._gss.GSSAPIProxy(cred, protocol=protocol, usage="accept") |