Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ksauzz committed Nov 19, 2019
1 parent 5f2d377 commit 6483c86
Show file tree
Hide file tree
Showing 13 changed files with 198 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ build
dist
*egg-info
__pycache__
venv
.venv
.coverage
htmlcov
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM ubuntu:18.04

RUN apt-get update && \
apt-get install -y --no-install-recommends \
python3-pip \
python3-setuptools \
gettext \
krb5-user && \
rm -rf /var/lib/apt/lists/*

WORKDIR /app
COPY README.md setup.py entrypoint.sh pytest.ini /app/
COPY krbticket /app/krbticket
COPY tests /app/tests
COPY tests/conf/krb5.conf.tmpl tests/conf/krb5.keytab /etc/
RUN chmod 755 /app/entrypoint.sh

ENV KRB5_HOST localhost

ENTRYPOINT ["/app/entrypoint.sh"]
CMD ["pytest"]
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ from krbticket import KrbTicket
ticket = KrbTicket.init("<principal>", "<keytab path>")
ticket.updater_start()
```

## Test

```
docker run --rm -p 88:88 ksauzz/docker-krb5:0.0.1
KRB5_CONFIG=tests/conf/krb5.conf.local pytest
```
7 changes: 7 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
set -eu

envsubst < /etc/krb5.conf.tmpl > /etc/krb5.conf
pip3 install -e '.[test]'

exec "$@"
14 changes: 13 additions & 1 deletion krbticket/ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,25 @@ def parseDatetime(str):

class KrbConfig():
def __init__(self, principal=None, keytab=None, kinit_bin="kinit",
klist_bin="klist", renewal_threshold=timedelta(minutes=30)):
klist_bin="klist", kdestroy_bin="kdestroy",
renewal_threshold=timedelta(minutes=30), ticket_lifetime=None):
self.principal = principal
self.keytab = keytab
self.kinit_bin = kinit_bin
self.klist_bin = klist_bin
self.kdestroy_bin = kdestroy_bin
self.renewal_threshold = renewal_threshold
self.ticket_lifetime = ticket_lifetime


class KrbCommand():
@staticmethod
def kinit(config):
commands = []
commands.append(config.kinit_bin)
if config.ticket_lifetime:
commands.append("-l")
commands.append(config.ticket_lifetime)
commands.append("-k")
commands.append("-t")
commands.append(config.keytab)
Expand All @@ -196,6 +202,12 @@ def klist(config):
commands.append(config.klist_bin)
return KrbCommand._call(config, commands)

@staticmethod
def kdestroy(config):
commands = []
commands.append(config.kdestroy_bin)
return KrbCommand._call(config, commands)

@staticmethod
def _call(config, commands):
logging.debug("Executing {}".format(" ".join(commands)))
Expand Down
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
addopts = -v -s --cov=krbticket --cov-report=html
7 changes: 7 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
with open("README.md", "r") as fh:
long_description = fh.read()

test_require = ["pytest", "pytest-cov"]
extras = {
'test': test_require
}

setuptools.setup(
name="krbticket",
version="0.0.1.3",
Expand All @@ -13,6 +18,8 @@
long_description_content_type="text/markdown",
url="https://github.com/ksauzz/krbticket",
packages=setuptools.find_packages(),
tests_require=test_require,
extras_require=extras,
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
Expand Down
26 changes: 26 additions & 0 deletions tests/conf/krb5.conf.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[libdefaults]
default_realm = EXAMPLE.COM
dns_lookup_kdc = false
dns_lookup_realm = false
ticket_lifetime = 7d
renew_lifetime = 28d
forwardable = true
default_tgs_enctypes = rc4-hmac
default_tkt_enctypes = rc4-hmac
permitted_enctypes = rc4-hmac
udp_preference_limit = 1
kdc_timeout = 3000

[realms]
EXAMPLE.COM = {
kdc = localhost
admin_server = localhost
default_domain = EXAMPLE.COM
}

[domain_realm]
example.com = EXAMPLE.COM
.example.com = EXAMPLE.COM

[logging]
default = CONSOLE
26 changes: 26 additions & 0 deletions tests/conf/krb5.conf.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[libdefaults]
default_realm = EXAMPLE.COM
dns_lookup_kdc = false
dns_lookup_realm = false
ticket_lifetime = 7d
renew_lifetime = 28d
forwardable = true
default_tgs_enctypes = rc4-hmac
default_tkt_enctypes = rc4-hmac
permitted_enctypes = rc4-hmac
udp_preference_limit = 1
kdc_timeout = 3000

[realms]
EXAMPLE.COM = {
kdc = ${KRB5_HOST}
admin_server = ${KRB5_HOST}
default_domain = EXAMPLE.COM
}

[domain_realm]
example.com = EXAMPLE.COM
.example.com = EXAMPLE.COM

[logging]
default = CONSOLE
Binary file added tests/conf/krb5.keytab
Binary file not shown.
19 changes: 19 additions & 0 deletions tests/helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pytest
from krbticket import KrbConfig

DEFAULT_PRINCIPAL = '[email protected]'
DEFAULT_KEYTAB = './tests/conf/krb5.keytab'
DEFAULT_TICKET_LIFETIME = '2s'

def assert_ticket(t1, t2):
assert t1.principal == t2.principal
assert t1.file == t2.file
assert t1.starting == t2.starting
assert t1.expires == t2.expires
assert t1.service_principal == t2.service_principal


@pytest.fixture
def config():
return KrbConfig(DEFAULT_PRINCIPAL, DEFAULT_KEYTAB, ticket_lifetime=DEFAULT_TICKET_LIFETIME)

10 changes: 10 additions & 0 deletions tests/test_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from krbticket import KrbConfig, KrbCommand
from helper import config


def test_commands(config):
KrbCommand.kdestroy(config)
KrbCommand.kinit(config)
KrbCommand.renewal(config)
KrbCommand.klist(config)

56 changes: 56 additions & 0 deletions tests/test_ticket.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from krbticket import KrbTicket, KrbCommand
from krbticket.ticket import NoCredentialFound
from helper import *
import time


def test_init(config):
KrbCommand.kdestroy(config)
ticket1 = KrbTicket.init_by_config(config)
ticket2 = KrbTicket.init(DEFAULT_PRINCIPAL, DEFAULT_KEYTAB)
assert ticket1.principal == ticket2.principal
assert ticket1.file == ticket2.file


def test_get(config):
KrbCommand.kdestroy(config)
with pytest.raises(NoCredentialFound):
KrbTicket.get(DEFAULT_PRINCIPAL, DEFAULT_KEYTAB)

assert_ticket(
KrbTicket.init_by_config(config),
KrbTicket.get(DEFAULT_KEYTAB, DEFAULT_PRINCIPAL))


def test_ticket(config):
KrbCommand.kdestroy(config)
ticket = KrbTicket.init_by_config(config)
assert ticket.config == config
assert ticket.file
assert ticket.principal == '[email protected]'
assert ticket.starting
assert ticket.expires
assert ticket.service_principal


def test_updater(config):
KrbCommand.kdestroy(config)
ticket = KrbTicket.init_by_config(config)
updater = ticket.updater(interval=1)
updater.start()
updater.stop()
time.sleep(2)
assert not updater.is_alive()


def test_renewal(config):
KrbCommand.kdestroy(config)
ticket = KrbTicket.init_by_config(config)
starting = ticket.starting
expires = ticket.expires
updater = ticket.updater(interval=1)
updater.start()
time.sleep(2)
updater.stop()
assert ticket.starting > starting
assert ticket.expires > expires

0 comments on commit 6483c86

Please sign in to comment.