From fea2eab08d820dc149a47d40e834b115dff3be67 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Mon, 13 Jul 2015 14:04:13 +0100 Subject: [PATCH 01/44] * Add initial Travis-CI test suite --- .travis.yml | 14 ++++++++++++++ tests/ldapass.conf | 14 ++++++++++++++ tests/ldapass_tests.py | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 .travis.yml create mode 100644 tests/ldapass.conf create mode 100644 tests/ldapass_tests.py diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..b743311 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,14 @@ +language: python + +python: + - "2.7" + - "pypy" + +install: + - pip install -r requirements.txt + +env: + global: + - LDAPASS_CONFIG=tests/ldapass.conf + +script: nosetests diff --git a/tests/ldapass.conf b/tests/ldapass.conf new file mode 100644 index 0000000..77620f1 --- /dev/null +++ b/tests/ldapass.conf @@ -0,0 +1,14 @@ +[app] +listen_addr = 0.0.0.0 +listen_port = 80 +smtp_addr = smtp-server.example.com +hostname = ldapass-hostname.example.com +database = ldapass.sql +ldap_debug = 0 + +[ldap] +addr = ldap-server.example.com +port = 389 +user = cn=Manager,dc=example,dc=com +pass = very-complicated-ldap-password +basedn = ou=People,dc=example,dc=com diff --git a/tests/ldapass_tests.py b/tests/ldapass_tests.py new file mode 100644 index 0000000..d835ae8 --- /dev/null +++ b/tests/ldapass_tests.py @@ -0,0 +1,35 @@ +from ldapass import app +import unittest + + +class TestIndex(unittest.TestCase): + + def setUp(self): + self.app = app.test_client() + self.resp = self.app.get('/') + + def tearDown(self): + pass + + def testGetResponseCode(self): + '''GET / reques should return 200 HTTP code.''' + self.assertEqual(self.resp.status_code, 200) + + def testGetHtmlText(self): + '''GET / request should return html with proper text.''' + self.assertIn( + b'Setup/Reset LDAP Password', + self.resp.data) + + def testGetHtmlForm(self): + '''GET / request should return html with proper form.''' + self.assertIn( + b'
', + self.resp.data) + self.assertIn( + b'', + self.resp.data) + +if __name__ == '__main__': + unittest.main() From 91b2a7907bcc81287dfa06752264f185736ea69e Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Mon, 13 Jul 2015 14:06:52 +0100 Subject: [PATCH 02/44] * Remove pypy from test suite, until I can get tests running fine --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index b743311..41857ed 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,6 @@ language: python python: - "2.7" - - "pypy" install: - pip install -r requirements.txt From 670c0d78c5d3d6bd38008c32b15cb8dff0b3fda1 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Mon, 13 Jul 2015 16:04:29 +0100 Subject: [PATCH 03/44] * Added initial setup.py and empty __init__.py files --- ldapass/__init__.py | 0 setup.py | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 ldapass/__init__.py create mode 100644 setup.py diff --git a/ldapass/__init__.py b/ldapass/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..bf6dfb3 --- /dev/null +++ b/setup.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python + +from os.path import join +from setuptools import setup, find_packages + +setup(name='ldapass', + version='0.1', + description='Web application for setting/changing LDAP user passwords.' + author='Bartek Rutkowski', + author_email='contact+ldapass@robakdesign.com', + license='BSD3', + url='https://github.com/bartekrutkowski/ldapass', + packages=find_packages(), + entry_points = { + 'console_scripts': ['ldapass = ldapass:main',] + }, + install_requires=['Flask', 'WTForms', 'python-ldap'], + ) From 82783776ccd2a076c2fbde235066797ff6e3e5b2 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Mon, 13 Jul 2015 16:08:45 +0100 Subject: [PATCH 04/44] * Changed the import name from app to ldapass in tests --- tests/ldapass_tests.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/ldapass_tests.py b/tests/ldapass_tests.py index d835ae8..01aa3ad 100644 --- a/tests/ldapass_tests.py +++ b/tests/ldapass_tests.py @@ -1,11 +1,11 @@ -from ldapass import app +from ldapass import ldapass import unittest class TestIndex(unittest.TestCase): def setUp(self): - self.app = app.test_client() + self.app = ldapass.app.test_client() self.resp = self.app.get('/') def tearDown(self): From 2fae810fa6eb0be583cfd23cd54607b3cec21a38 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Mon, 13 Jul 2015 16:17:47 +0100 Subject: [PATCH 05/44] * Added installing ldapass to .travis.yml --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 41857ed..1148def 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ python: - "2.7" install: + - pip install . - pip install -r requirements.txt env: From 1bdeb789d3da8f3e9380431281db3f6b96c5e769 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Mon, 13 Jul 2015 16:20:55 +0100 Subject: [PATCH 06/44] * Fix setup.py formatting --- setup.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/setup.py b/setup.py index bf6dfb3..7948034 100644 --- a/setup.py +++ b/setup.py @@ -4,15 +4,13 @@ from setuptools import setup, find_packages setup(name='ldapass', - version='0.1', - description='Web application for setting/changing LDAP user passwords.' - author='Bartek Rutkowski', - author_email='contact+ldapass@robakdesign.com', - license='BSD3', - url='https://github.com/bartekrutkowski/ldapass', - packages=find_packages(), - entry_points = { - 'console_scripts': ['ldapass = ldapass:main',] - }, + version='0.1', + description='Web application for setting/changing LDAP user passwords.' + author='Bartek Rutkowski', + author_email='contact+ldapass@robakdesign.com', + license='BSD3', + url='https://github.com/bartekrutkowski/ldapass', + packages=find_packages(), + entry_points = {'console_scripts': ['ldapass = ldapass:main',]}, install_requires=['Flask', 'WTForms', 'python-ldap'], ) From c242463115961755a65868dd207982bfc8d7f01e Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Mon, 13 Jul 2015 16:23:05 +0100 Subject: [PATCH 07/44] * Add missing coma in setup.py --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 7948034..5b91e7b 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setup(name='ldapass', version='0.1', - description='Web application for setting/changing LDAP user passwords.' + description='Web application for setting/changing LDAP user passwords.', author='Bartek Rutkowski', author_email='contact+ldapass@robakdesign.com', license='BSD3', From 86c511c676ab3787c90e289acd441b9e9c0b95ca Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Mon, 13 Jul 2015 16:30:46 +0100 Subject: [PATCH 08/44] * Removed incorrectly used quotes around __name__ in ldapass.py * Removed obsolete coma in setup.py --- ldapass/ldapass.py | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ldapass/ldapass.py b/ldapass/ldapass.py index e95cf44..799e3e4 100644 --- a/ldapass/ldapass.py +++ b/ldapass/ldapass.py @@ -13,7 +13,7 @@ from wtforms import Form, TextField, PasswordField, validators -app = Flask('__name__') +app = Flask(__name__) app.secret_key = os.urandom(128) conf = RawConfigParser() conf.read(os.environ['LDAPASS_CONFIG']) diff --git a/setup.py b/setup.py index 5b91e7b..f25b1f1 100644 --- a/setup.py +++ b/setup.py @@ -11,6 +11,6 @@ license='BSD3', url='https://github.com/bartekrutkowski/ldapass', packages=find_packages(), - entry_points = {'console_scripts': ['ldapass = ldapass:main',]}, + entry_points = {'console_scripts': ['ldapass = ldapass:main']}, install_requires=['Flask', 'WTForms', 'python-ldap'], ) From 0443981e6ac6c3a8e65265763169942ed5bbca69 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Mon, 13 Jul 2015 19:00:26 +0100 Subject: [PATCH 09/44] * Refactoring non pythonic db lenght assessment with more pythonic one * Fixed indentation in few places --- ldapass/ldapass.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/ldapass/ldapass.py b/ldapass/ldapass.py index 799e3e4..d024d19 100644 --- a/ldapass/ldapass.py +++ b/ldapass/ldapass.py @@ -101,10 +101,10 @@ def index(): db_curs.execute( "INSERT INTO mails (mail, link_id, created) VALUES \ ('{mail}', '{link_id}', '{created}')".format( - mail=form.mail.data, - link_id=link_id, - created=datetime.datetime.now() - )) + mail=form.mail.data, + link_id=link_id, + created=datetime.datetime.now() + )) flash('Email containing password reset url has been sent \ to {mail}'.format(mail=form.mail.data)) else: @@ -114,10 +114,10 @@ def index(): db_curs.execute( "REPLACE INTO mails (mail, link_id, created) VALUES \ ('{mail}', '{link_id}', '{created}')".format( - mail=form.mail.data, - link_id=link_id, - created=datetime.datetime.now() - )) + mail=form.mail.data, + link_id=link_id, + created=datetime.datetime.now() + )) flash('Email containing password reset url has been sent \ to {mail}. Previous reset urls have been \ invalidated.'.format(mail=form.mail.data)) @@ -232,7 +232,7 @@ def reset(link_id): db_curs = db_conn.cursor() db_curs.execute( "SELECT name FROM sqlite_master WHERE type='table' AND name='mails'") - if len(db_curs.fetchall()) == 0: + if not db_curs.fetchall(): print('WARNING: the SQLite file {database} doesnt exist! Sleeping for \ 10 seconds and creating the database file. KILL ME if this is an \ error!').format(database=conf.get('app', 'database')) @@ -241,7 +241,7 @@ def reset(link_id): '''create table mails ( id INTEGER PRIMARY KEY, mail VARCHAR(255) NOT NULL COLLATE NOCASE, - link_id VARCHAR(512) NOT NULL COLLATE NOCASE, + link_id VARCHAR(512) NOT NULL COLLATE NOCASE, created INTEGER DEFAULT NULL); ''') db_conn.commit() From de9a777123a18e79865caa24f066d97ca8505a54 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Mon, 13 Jul 2015 19:01:26 +0100 Subject: [PATCH 10/44] * Remove line installing requirements via pip, since the ldapass installation takes care of that via setup.py --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 1148def..e19a9d5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,6 @@ python: install: - pip install . - - pip install -r requirements.txt env: global: From 3da39afb54277c49a372abfb681974d12e5b50ec Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Mon, 13 Jul 2015 19:13:29 +0100 Subject: [PATCH 11/44] * Switch tests to pytest --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index e19a9d5..81eb4d7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,4 +10,4 @@ env: global: - LDAPASS_CONFIG=tests/ldapass.conf -script: nosetests +script: pytest From 9de66b2bbf3849be83bf5ed4521b66c09695e7bc Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Mon, 13 Jul 2015 19:14:42 +0100 Subject: [PATCH 12/44] * Fix py.test name in .travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 81eb4d7..977f531 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,4 +10,4 @@ env: global: - LDAPASS_CONFIG=tests/ldapass.conf -script: pytest +script: py.test From 882bcd445ad7096a2d93465385280ce311dd7430 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Mon, 13 Jul 2015 19:17:54 +0100 Subject: [PATCH 13/44] * Rename main test file to follow pytest discovery rules --- tests/{ldapass_tests.py => ldapass_test.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/{ldapass_tests.py => ldapass_test.py} (100%) diff --git a/tests/ldapass_tests.py b/tests/ldapass_test.py similarity index 100% rename from tests/ldapass_tests.py rename to tests/ldapass_test.py From b91b3f221832144793f37c45b7d17e4bdeb09312 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Mon, 13 Jul 2015 20:16:37 +0100 Subject: [PATCH 14/44] * Replace pytest with nosetest to see if tests will work again --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 977f531..abf91b6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,4 +10,4 @@ env: global: - LDAPASS_CONFIG=tests/ldapass.conf -script: py.test +script: nosetest From ba57c84873a9388cf7c88a1168f14cb16dc45c93 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Mon, 13 Jul 2015 20:17:48 +0100 Subject: [PATCH 15/44] * Fix nosetests name --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index abf91b6..e19a9d5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,4 +10,4 @@ env: global: - LDAPASS_CONFIG=tests/ldapass.conf -script: nosetest +script: nosetests From 516f92f50c971e1402091f17e870c3b80218a30a Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Mon, 13 Jul 2015 20:34:56 +0100 Subject: [PATCH 16/44] * Added DEBUG and TESTING to Flask config in tests --- tests/ldapass_test.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/ldapass_test.py b/tests/ldapass_test.py index 01aa3ad..c801a05 100644 --- a/tests/ldapass_test.py +++ b/tests/ldapass_test.py @@ -6,6 +6,8 @@ class TestIndex(unittest.TestCase): def setUp(self): self.app = ldapass.app.test_client() + self.app.config["DEBUG"] = True + self.app.config["TESTING"] = True self.resp = self.app.get('/') def tearDown(self): From 318fd5403b0d5214407b57e7112f8892bfc3b43e Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Mon, 13 Jul 2015 20:40:02 +0100 Subject: [PATCH 17/44] * Change DEBUG and TESTING to testing=True in tests --- tests/ldapass_test.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/ldapass_test.py b/tests/ldapass_test.py index c801a05..bacad9a 100644 --- a/tests/ldapass_test.py +++ b/tests/ldapass_test.py @@ -6,8 +6,7 @@ class TestIndex(unittest.TestCase): def setUp(self): self.app = ldapass.app.test_client() - self.app.config["DEBUG"] = True - self.app.config["TESTING"] = True + self.app.testing = True self.resp = self.app.get('/') def tearDown(self): From 13432722108209e7b6120b472780861d0597175f Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Mon, 13 Jul 2015 20:44:07 +0100 Subject: [PATCH 18/44] * Replace nosetests with py.test * Remove obsolete import and spaces around = in setup.py --- .travis.yml | 2 +- setup.py | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index e19a9d5..977f531 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,4 +10,4 @@ env: global: - LDAPASS_CONFIG=tests/ldapass.conf -script: nosetests +script: py.test diff --git a/setup.py b/setup.py index f25b1f1..55c7889 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,5 @@ #!/usr/bin/env python -from os.path import join from setuptools import setup, find_packages setup(name='ldapass', @@ -11,6 +10,6 @@ license='BSD3', url='https://github.com/bartekrutkowski/ldapass', packages=find_packages(), - entry_points = {'console_scripts': ['ldapass = ldapass:main']}, + entry_points = {'console_scripts': ['ldapass=ldapass:main']}, install_requires=['Flask', 'WTForms', 'python-ldap'], ) From 983966e16ee0c05d2cbe2d8e1f91ad9702201a41 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Mon, 13 Jul 2015 20:49:28 +0100 Subject: [PATCH 19/44] * Added pytest import and fixture for app setup --- tests/ldapass_test.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/ldapass_test.py b/tests/ldapass_test.py index bacad9a..5f68942 100644 --- a/tests/ldapass_test.py +++ b/tests/ldapass_test.py @@ -1,9 +1,11 @@ from ldapass import ldapass +import pytest import unittest class TestIndex(unittest.TestCase): + @pytest.fixture(autouse=True) def setUp(self): self.app = ldapass.app.test_client() self.app.testing = True From 58b1a57a126a16746b2a60b9be1ab5feb70a73c9 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Mon, 13 Jul 2015 21:06:25 +0100 Subject: [PATCH 20/44] * Remove 'if __name__' from tests --- tests/ldapass_test.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/ldapass_test.py b/tests/ldapass_test.py index 5f68942..95f1fef 100644 --- a/tests/ldapass_test.py +++ b/tests/ldapass_test.py @@ -33,6 +33,3 @@ def testGetHtmlForm(self): self.assertIn( b'', self.resp.data) - -if __name__ == '__main__': - unittest.main() From 9b1da82218760318d62cbe6ab84e95c37ca7d097 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Tue, 14 Jul 2015 11:56:50 +0100 Subject: [PATCH 21/44] * Add debug=True to solve the pytest failing issue --- tests/ldapass_test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/ldapass_test.py b/tests/ldapass_test.py index 95f1fef..5b8e265 100644 --- a/tests/ldapass_test.py +++ b/tests/ldapass_test.py @@ -9,6 +9,7 @@ class TestIndex(unittest.TestCase): def setUp(self): self.app = ldapass.app.test_client() self.app.testing = True + self.app.debug = True self.resp = self.app.get('/') def tearDown(self): From 52cd4b8fdf64df332b90f32bf86b7e37a529c681 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Tue, 14 Jul 2015 12:08:27 +0100 Subject: [PATCH 22/44] * Rewrite of the tests to be more pytest-like and less unittest-like --- tests/ldapass_test.py | 69 +++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 35 deletions(-) diff --git a/tests/ldapass_test.py b/tests/ldapass_test.py index 5b8e265..35b7ba4 100644 --- a/tests/ldapass_test.py +++ b/tests/ldapass_test.py @@ -1,36 +1,35 @@ -from ldapass import ldapass import pytest -import unittest - - -class TestIndex(unittest.TestCase): - - @pytest.fixture(autouse=True) - def setUp(self): - self.app = ldapass.app.test_client() - self.app.testing = True - self.app.debug = True - self.resp = self.app.get('/') - - def tearDown(self): - pass - - def testGetResponseCode(self): - '''GET / reques should return 200 HTTP code.''' - self.assertEqual(self.resp.status_code, 200) - - def testGetHtmlText(self): - '''GET / request should return html with proper text.''' - self.assertIn( - b'Setup/Reset LDAP Password', - self.resp.data) - - def testGetHtmlForm(self): - '''GET / request should return html with proper form.''' - self.assertIn( - b'', - self.resp.data) - self.assertIn( - b'', - self.resp.data) + +from ldapass import ldapass + + +@pytest.yield_fixture(autouse=True) +def flask_app(): + ldapass.app.testing = True + ldapass.app.debug = True + with ldapass.app.app_context(): + yield ldapass.app + + +@pytest.fixture +def test_client(flask_app): + return flask_app.test_client() + + +def test_get_response_code(test_client): + '''GET / reques should return 200 HTTP code.''' + resp = test_client.get('/') + assert resp.status_code == 200 + + +def test_get_html_text(test_client): + '''GET / request should return html with proper text.''' + resp = test_client.get('/') + assert b'Setup/Reset LDAP Password' in resp.data + + +def testGetHtmlForm(self): + '''GET / request should return html with proper form.''' + resp = test_client.get('/') + assert b'' in resp.data + assert b'' in resp.data From 021f191ff09ab58abb3fd19743cc729e010ba6cf Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Tue, 14 Jul 2015 12:13:25 +0100 Subject: [PATCH 23/44] * Forgot to replace self with test_client in testGetHtmlForm() --- tests/ldapass_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ldapass_test.py b/tests/ldapass_test.py index 35b7ba4..d91b05b 100644 --- a/tests/ldapass_test.py +++ b/tests/ldapass_test.py @@ -28,7 +28,7 @@ def test_get_html_text(test_client): assert b'Setup/Reset LDAP Password' in resp.data -def testGetHtmlForm(self): +def testGetHtmlForm(test_client): '''GET / request should return html with proper form.''' resp = test_client.get('/') assert b'' in resp.data From 2868ab26e46c2e272bee2602802669a9a9fe8602 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Tue, 14 Jul 2015 12:46:12 +0100 Subject: [PATCH 24/44] * Added some valid content to __init__.py file --- ldapass/__init__.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ldapass/__init__.py b/ldapass/__init__.py index e69de29..16fb447 100644 --- a/ldapass/__init__.py +++ b/ldapass/__init__.py @@ -0,0 +1,4 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +__version__ = "0.1" From f4137778a096c660d4b9e7aaf45c8679326c7c61 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Tue, 14 Jul 2015 12:50:13 +0100 Subject: [PATCH 25/44] * Minor .travis.yml adjustments --- .travis.yml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 977f531..d326c2e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,13 +1,15 @@ language: python python: - - "2.7" + - 2.7 install: - - pip install . + - pip install -q -r requirements.txt + - pip install -q -e . env: global: - LDAPASS_CONFIG=tests/ldapass.conf -script: py.test +script: + - py.test From 89e04efe7f7cb7e38cb29f7da53bbcd0c4f7ac13 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Tue, 14 Jul 2015 12:54:59 +0100 Subject: [PATCH 26/44] * Change testGetHtmlForm() test name to more pythonic form --- tests/ldapass_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ldapass_test.py b/tests/ldapass_test.py index d91b05b..1bd9ef8 100644 --- a/tests/ldapass_test.py +++ b/tests/ldapass_test.py @@ -28,7 +28,7 @@ def test_get_html_text(test_client): assert b'Setup/Reset LDAP Password' in resp.data -def testGetHtmlForm(test_client): +def test_get_html_form(test_client): '''GET / request should return html with proper form.''' resp = test_client.get('/') assert b'' in resp.data From c3c4870b3bfafad11b800ea5bc79b471b40384c7 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Tue, 14 Jul 2015 13:05:39 +0100 Subject: [PATCH 27/44] * Test removing requirements installation to see if it made tests failing --- .travis.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index d326c2e..0d4e84b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,8 +4,7 @@ python: - 2.7 install: - - pip install -q -r requirements.txt - - pip install -q -e . + - pip install . env: global: From 58cf1180e3e48c8bff1e6348d63cc1f03be1b457 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Tue, 14 Jul 2015 13:09:58 +0100 Subject: [PATCH 28/44] * Add -e to pip install to test if it fixes the tests --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 0d4e84b..6c5547e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ python: - 2.7 install: - - pip install . + - pip install -e . env: global: From 5a5e302e65f6dafb28d5cd61148662f3dee20312 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Tue, 14 Jul 2015 13:34:10 +0100 Subject: [PATCH 29/44] * Added MANIFEST.in for installing flask templates and static files * Added include_package_data=True to setup.py for installing flask templates and static files * Removed -e from pip install in travis.yml (shouldnt be needed anymore, since the app should install fine from now on) --- .travis.yml | 2 +- MANIFEST.in | 2 ++ setup.py | 24 +++++++++++++----------- 3 files changed, 16 insertions(+), 12 deletions(-) create mode 100644 MANIFEST.in diff --git a/.travis.yml b/.travis.yml index 6c5547e..0d4e84b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ python: - 2.7 install: - - pip install -e . + - pip install . env: global: diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..5473220 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,2 @@ +recursive-include ldapass/templates * +recursive-include ldapass/static * diff --git a/setup.py b/setup.py index 55c7889..1723f7b 100644 --- a/setup.py +++ b/setup.py @@ -2,14 +2,16 @@ from setuptools import setup, find_packages -setup(name='ldapass', - version='0.1', - description='Web application for setting/changing LDAP user passwords.', - author='Bartek Rutkowski', - author_email='contact+ldapass@robakdesign.com', - license='BSD3', - url='https://github.com/bartekrutkowski/ldapass', - packages=find_packages(), - entry_points = {'console_scripts': ['ldapass=ldapass:main']}, - install_requires=['Flask', 'WTForms', 'python-ldap'], - ) +setup( + name='ldapass', + version='0.1', + description='Web application for setting/changing LDAP user passwords.', + author='Bartek Rutkowski', + author_email='contact+ldapass@robakdesign.com', + license='BSD3', + url='https://github.com/bartekrutkowski/ldapass', + packages=find_packages(), + include_package_data=True, + entry_points = {'console_scripts': ['ldapass=ldapass:main']}, + install_requires=['Flask', 'WTForms', 'python-ldap'] +) From 774c904631ef890729ae864920f0686d090c8e36 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Tue, 14 Jul 2015 13:42:14 +0100 Subject: [PATCH 30/44] * Remove MANIFEST.in and instead use package_data in setup.py --- MANIFEST.in | 2 -- setup.py | 3 ++- 2 files changed, 2 insertions(+), 3 deletions(-) delete mode 100644 MANIFEST.in diff --git a/MANIFEST.in b/MANIFEST.in deleted file mode 100644 index 5473220..0000000 --- a/MANIFEST.in +++ /dev/null @@ -1,2 +0,0 @@ -recursive-include ldapass/templates * -recursive-include ldapass/static * diff --git a/setup.py b/setup.py index 1723f7b..71dabe8 100644 --- a/setup.py +++ b/setup.py @@ -12,6 +12,7 @@ url='https://github.com/bartekrutkowski/ldapass', packages=find_packages(), include_package_data=True, - entry_points = {'console_scripts': ['ldapass=ldapass:main']}, + package_data={'ldapass': ['static/*', 'templates/*']}, + entry_points={'console_scripts': ['ldapass = ldapass:main']}, install_requires=['Flask', 'WTForms', 'python-ldap'] ) From 920150fcab3f8ab56b4a56ce19fdd1c4ea6903e5 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Tue, 14 Jul 2015 13:47:30 +0100 Subject: [PATCH 31/44] * Fix package_data entries in setup.py --- setup.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 71dabe8..2a3d044 100644 --- a/setup.py +++ b/setup.py @@ -12,7 +12,13 @@ url='https://github.com/bartekrutkowski/ldapass', packages=find_packages(), include_package_data=True, - package_data={'ldapass': ['static/*', 'templates/*']}, + package_data={ + 'ldapass': ['static/css/*', + 'static/fonts/*', + 'static/js/*', + 'templates/*.html' + ] + }, entry_points={'console_scripts': ['ldapass = ldapass:main']}, install_requires=['Flask', 'WTForms', 'python-ldap'] ) From 7179061aa38d4ca203bfa1d0ce88488cf6e1ffe1 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Fri, 7 Aug 2015 13:16:26 +0100 Subject: [PATCH 32/44] * Created .gitignore file for .pyc and vim temp files --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..021454b --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.pyc +*~ +*.swp From 6d739cbfb2f138b2303e767eb5755c3d1b2f9f88 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Fri, 7 Aug 2015 13:35:18 +0100 Subject: [PATCH 33/44] * Added build status icon for TravisCI tests --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1fd3405..4d89e0a 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -ldapass +ldapass [![Build Status](https://travis-ci.org/bartekrutkowski/ldapass.svg?branch=master)](https://travis-ci.org/bartekrutkowski/ldapass) ======= **Web application for setting/changing LDAP user passwords.** From c5867b0fb86df9fa3110b81724e2f88130dc5d54 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Sat, 10 Dec 2016 18:10:50 +0000 Subject: [PATCH 34/44] * Update Python module requirements in requirements.txt --- requirements.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 3d637e3..1dfb14b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ Flask -WTForms python-ldap +sqlite3 +WTForms \ No newline at end of file From fe9be245994703bd219975ad48e59d3505162370 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Sat, 10 Dec 2016 18:11:55 +0000 Subject: [PATCH 35/44] * Update example Nginx configuration * Update example UWSGI configuration --- examples/nginx_ldapass.example.com.conf | 6 +++--- examples/uwsgi_ldapass.ini | 11 +++++------ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/examples/nginx_ldapass.example.com.conf b/examples/nginx_ldapass.example.com.conf index 913e7de..1ab5e94 100644 --- a/examples/nginx_ldapass.example.com.conf +++ b/examples/nginx_ldapass.example.com.conf @@ -6,14 +6,14 @@ server { error_log /var/log/nginx/ldapass.example.com-error.log; location /static { - alias /var/www/ldapass/app/static; + alias /var/www/ldapass/ldapass/static; } location / { try_files $uri @ldapass.example.com; } location @ldapass.example.com { - root /var/www/ldapass/app; + root /var/www/ldapass/ldapass; include uwsgi_params; uwsgi_pass 127.0.0.1:8005; } -} +} \ No newline at end of file diff --git a/examples/uwsgi_ldapass.ini b/examples/uwsgi_ldapass.ini index 7045434..83ded60 100644 --- a/examples/uwsgi_ldapass.ini +++ b/examples/uwsgi_ldapass.ini @@ -1,14 +1,13 @@ [uwsgi] -chdir = /var/www/ldapass/app -venv = /var/www/ldapass/ldapass_venv +chdir = /var/www/ldapass/ldapass module = ldapass callable = app master = true -processes = 8 -chmod-socket = 666 +processes = 2 +chmod-socket = 127.0.0.1:8005 vacuum = true -logto = /var/log/uwsgi/ldapass.log -logto2 = /var/log/uwsgi/ldapass2.log +logger = syslog log-micros = true +log-format = %(addr) - %(user) [%(ltime)] "%(method) %(uri) %(proto)" %(status) %(size) "%(referer)" "%(uagent)" From da8f85f38343a8ee49eab2abf0fda8100188c7d7 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Sat, 10 Dec 2016 18:12:54 +0000 Subject: [PATCH 36/44] * Fix and update installation instructions in README.md --- README.md | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 4d89e0a..76cab4b 100644 --- a/README.md +++ b/README.md @@ -2,33 +2,34 @@ ldapass [![Build Status](https://travis-ci.org/bartekrutkowski/ldapass.svg?branc ======= **Web application for setting/changing LDAP user passwords.** -LDAPass is a Python/Flask simple web application that aims to make Unix/Linux LDAP user account passwords manageable by their users without involvement of DevOps/SysAdmin resources in a simple, non complicated application not requiring extensive setup nor maintenance. +LDAPass is a Python/Flask simple web application that aims to make Unix/Linux user account LDAP passwords manageable by their owners without involvement of DevOps/SysAdmin resources in a simple application not requiring extensive setup or maintenance. ## Requirements -To host LDAPass on a Unix/Linux system you need the following software installed: +To run LDAPass on a Unix/Linux system you need the following software installed: - Python 2.7.x with following modules installed: - Flask - - python-ldap + - Python-LDAP - WTForms + - SQLite3 - A web server (Nginx, Apache etc., example Nginx configuration is provided) -- UWSGI 2.x +- Application Server Container (example UWSGI 2.x configuration is provided) ## Installation The following examples are assuming you are deploying LDAPass on a FreeBSD system, you should adjust your paths accordingly to your OS. -Clone the repository into proper location: +Clone the repository to the chosen location: ```sh -$ git clone git@github.com:bartekrutkowski/ldapass.git /var/www/ldapass +$ git clone https://github.com/bartekrutkowski/ldapass.git /var/www/ldapass ``` -Copy the config ldapass.conf configuration file and edit it with appropriate values: +Copy the example ldapass.conf configuration file to main app directory and edit it with appropriate values: ```sh -$ cp /var/www/ldapass/examples/ldapass.conf /var/www/ldapass/ldapass/ +$ cp /var/www/ldapass/examples/ldapass.conf /var/www/ldapass/ldapass/ldapass.conf $ vi /var/www/ldapass/ldapass/ldapass.conf ``` From fbdac1b8c2070513af114a8e1a517716af194f77 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Sat, 10 Dec 2016 18:52:52 +0000 Subject: [PATCH 37/44] * Change 'l' var name to 'ldap_conn' for better readability * Move ldap options setting outside of try block because it cant fail --- ldapass/ldapass.py | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/ldapass/ldapass.py b/ldapass/ldapass.py index d024d19..6739bf4 100644 --- a/ldapass/ldapass.py +++ b/ldapass/ldapass.py @@ -69,22 +69,21 @@ def index(): if form.validate(): ldap_uri = 'ldap://{addr}:{port}'.format( addr=conf.get('ldap', 'addr'), port=conf.get('ldap', 'port')) + ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER) try: - ldap.set_option( - ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER) - l = ldap.initialize( + ldap_conn = ldap.initialize( ldap_uri, trace_level=conf.get('app', 'ldap_debug')) - l.start_tls_s() + ldap_conn.start_tls_s() except ldap.LDAPError as error: return render_template('index.html', error=error, form=form) try: search_filter = 'mail={mail}'.format(mail=form.mail.data) - ldap_result_id = l.search( + ldap_result_id = ldap_conn.search( conf.get('ldap', 'basedn'), ldap.SCOPE_SUBTREE, search_filter, None) except ldap.LDAPError as error: return render_template('index.html', error=error, form=form) - result_type, result_data = l.result(ldap_result_id, 0) + result_type, result_data = ldap_conn.result(ldap_result_id, 0) if len(result_data) == 1: link_id = '{uuid}-{account}'.format( uuid=str(uuid.uuid4()), @@ -173,25 +172,24 @@ def reset(link_id): addr=conf.get('ldap', 'addr'), port=conf.get('ldap', 'port') ) + ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER) try: - ldap.set_option( - ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER) - l = ldap.initialize( + ldap_conn = ldap.initialize( ldap_uri, trace_level=conf.get('app', 'ldap_debug')) - l.start_tls_s() + ldap_conn.start_tls_s() except ldap.LDAPError as error: return render_template('error.html', error=error) try: search_filter = 'mail={mail}'.format(mail=db_data[0][1]) - ldap_result_id = l.search( + ldap_result_id = ldap_conn.search( conf.get('ldap', 'basedn'), ldap.SCOPE_SUBTREE, search_filter, None) - result_type, result_data = l.result(ldap_result_id, 0) - l.simple_bind_s( + result_type, result_data = ldap_conn.result(ldap_result_id, 0) + ldap_conn.simple_bind_s( conf.get('ldap', 'user'), conf.get('ldap', 'pass')) - l.passwd_s( + ldap_conn.passwd_s( 'uid={uid},{basedn}'.format( uid=result_data[0][1]['uid'][0], basedn=conf.get('ldap', 'basedn')), From 995fe4ac23442ec827b716a4accbbdd6d13bd2c0 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Sat, 10 Dec 2016 19:05:02 +0000 Subject: [PATCH 38/44] * Add missing environmental variable setup in example uwsgi_ldapass.ini file --- examples/uwsgi_ldapass.ini | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/uwsgi_ldapass.ini b/examples/uwsgi_ldapass.ini index 83ded60..90ef430 100644 --- a/examples/uwsgi_ldapass.ini +++ b/examples/uwsgi_ldapass.ini @@ -2,6 +2,7 @@ chdir = /var/www/ldapass/ldapass module = ldapass callable = app +env = LDAPASS_CONFIG=/var/www/ldapass/ldapass/ldapass.conf master = true processes = 2 From ade15750c47a6087c485c3eac488993e3103c1eb Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Sat, 10 Dec 2016 19:08:20 +0000 Subject: [PATCH 39/44] * Fix typo in README.md file --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 76cab4b..8dc628b 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ $ cp /var/www/ldapass/examples/nginx_ldapass.example.com.conf /usr/local/etc/ngi $ vi /usr/local/etc/nginx/conf.d/ldapass.example.com.conf ``` -Copy the UWSGI uwsgi_ldapass.ini configuration file into your OR UWSGI config directory and edit it with appropriate values: +Copy the UWSGI uwsgi_ldapass.ini configuration file into your OS UWSGI config directory and edit it with appropriate values: ```sh $ cp /var/www/ldapass/examples/uwsgi_ldapass.ini /usr/local/etc/uwsgi_ldapass.ini From 0849091f42490ecd7384343a27492f35ff08cab1 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Sat, 10 Dec 2016 23:10:00 +0000 Subject: [PATCH 40/44] * Fix static asset path with Jinja generator in templates/base.html --- ldapass/templates/base.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ldapass/templates/base.html b/ldapass/templates/base.html index 687ecf5..f4d8fbb 100644 --- a/ldapass/templates/base.html +++ b/ldapass/templates/base.html @@ -22,6 +22,6 @@ - + \ No newline at end of file From 6c4df00ed3d3966e566ad3da894325d686109a29 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Sun, 11 Dec 2016 13:14:10 +0000 Subject: [PATCH 41/44] * Move ldap_debug config var from [app] section to [ldap] and rename it to debug * Change ldap trace_level to use new variable name and section --- examples/ldapass.conf | 2 +- ldapass/ldapass.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/ldapass.conf b/examples/ldapass.conf index edf864d..ad74b79 100644 --- a/examples/ldapass.conf +++ b/examples/ldapass.conf @@ -4,7 +4,6 @@ listen_port = 8080 smtp_addr = smtp-server.example.com hostname = ldapass-hostname.example.com database = ldapass.sql -ldap_debug = 0 [ldap] addr = ldap-server.example.com @@ -12,3 +11,4 @@ port = 389 user = cn=Manager,dc=example,dc=com pass = very-complicated-ldap-password basedn = ou=People,dc=example,dc=com +debug = 0 \ No newline at end of file diff --git a/ldapass/ldapass.py b/ldapass/ldapass.py index 6739bf4..e76ff58 100644 --- a/ldapass/ldapass.py +++ b/ldapass/ldapass.py @@ -72,7 +72,7 @@ def index(): ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER) try: ldap_conn = ldap.initialize( - ldap_uri, trace_level=conf.get('app', 'ldap_debug')) + ldap_uri, trace_level=conf.get('ldap', 'debug')) ldap_conn.start_tls_s() except ldap.LDAPError as error: return render_template('index.html', error=error, form=form) @@ -175,7 +175,7 @@ def reset(link_id): ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER) try: ldap_conn = ldap.initialize( - ldap_uri, trace_level=conf.get('app', 'ldap_debug')) + ldap_uri, trace_level=conf.get('ldap', 'debug')) ldap_conn.start_tls_s() except ldap.LDAPError as error: return render_template('error.html', error=error) From ceeebd31a53670990762fb96bf96cf534b5c2f13 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Sun, 11 Dec 2016 13:40:43 +0000 Subject: [PATCH 42/44] * Add debug variable in [app] config section * Change Flask app.run initialization to get debug param value from config variable --- examples/ldapass.conf | 1 + ldapass/ldapass.py | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/ldapass.conf b/examples/ldapass.conf index ad74b79..80ab42f 100644 --- a/examples/ldapass.conf +++ b/examples/ldapass.conf @@ -4,6 +4,7 @@ listen_port = 8080 smtp_addr = smtp-server.example.com hostname = ldapass-hostname.example.com database = ldapass.sql +debug = False [ldap] addr = ldap-server.example.com diff --git a/ldapass/ldapass.py b/ldapass/ldapass.py index e76ff58..798ff2c 100644 --- a/ldapass/ldapass.py +++ b/ldapass/ldapass.py @@ -250,4 +250,5 @@ def reset(link_id): db_conn.close() app.run(host=conf.get('app', 'listen_addr'), - port=conf.getint('app', 'listen_port'), debug=True) + port=conf.getint('app', 'listen_port'), + debug=conf.getboolean('app', 'debug')) From aa1e85eab7e5fb4be52a1391eb9b60a91439e5ce Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Sun, 11 Dec 2016 14:35:15 +0000 Subject: [PATCH 43/44] * Move ldap.initialize outise of the try block because it cant fail --- ldapass/ldapass.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/ldapass/ldapass.py b/ldapass/ldapass.py index 798ff2c..243b4b4 100644 --- a/ldapass/ldapass.py +++ b/ldapass/ldapass.py @@ -70,9 +70,8 @@ def index(): ldap_uri = 'ldap://{addr}:{port}'.format( addr=conf.get('ldap', 'addr'), port=conf.get('ldap', 'port')) ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER) + ldap_conn = ldap.initialize(ldap_uri, trace_level=conf.get('ldap', 'debug')) try: - ldap_conn = ldap.initialize( - ldap_uri, trace_level=conf.get('ldap', 'debug')) ldap_conn.start_tls_s() except ldap.LDAPError as error: return render_template('index.html', error=error, form=form) @@ -173,9 +172,8 @@ def reset(link_id): port=conf.get('ldap', 'port') ) ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER) + ldap_conn = ldap.initialize(ldap_uri, trace_level=conf.get('ldap', 'debug')) try: - ldap_conn = ldap.initialize( - ldap_uri, trace_level=conf.get('ldap', 'debug')) ldap_conn.start_tls_s() except ldap.LDAPError as error: return render_template('error.html', error=error) From f5b7231e549b5fd085cfc6bd53d6122b99718124 Mon Sep 17 00:00:00 2001 From: Bartek Rutkowski Date: Sun, 11 Dec 2016 16:10:33 +0000 Subject: [PATCH 44/44] * Add [ldap] starttls bool variable and change ldap_conn.start_tls_s() to be conditional * Change connection related try blocks to simplify them and catch more exceptions --- examples/ldapass.conf | 1 + ldapass/ldapass.py | 39 +++++++++++++++------------------------ 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/examples/ldapass.conf b/examples/ldapass.conf index 80ab42f..5aa9c64 100644 --- a/examples/ldapass.conf +++ b/examples/ldapass.conf @@ -9,6 +9,7 @@ debug = False [ldap] addr = ldap-server.example.com port = 389 +starttls = True user = cn=Manager,dc=example,dc=com pass = very-complicated-ldap-password basedn = ou=People,dc=example,dc=com diff --git a/ldapass/ldapass.py b/ldapass/ldapass.py index 243b4b4..b5ec34e 100644 --- a/ldapass/ldapass.py +++ b/ldapass/ldapass.py @@ -71,18 +71,16 @@ def index(): addr=conf.get('ldap', 'addr'), port=conf.get('ldap', 'port')) ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER) ldap_conn = ldap.initialize(ldap_uri, trace_level=conf.get('ldap', 'debug')) + search_filter = 'mail={mail}'.format(mail=form.mail.data) + try: - ldap_conn.start_tls_s() - except ldap.LDAPError as error: - return render_template('index.html', error=error, form=form) - try: - search_filter = 'mail={mail}'.format(mail=form.mail.data) - ldap_result_id = ldap_conn.search( - conf.get('ldap', 'basedn'), ldap.SCOPE_SUBTREE, - search_filter, None) + if conf.getboolean('ldap', 'starttls'): + ldap_conn.start_tls_s() + ldap_result_id = ldap_conn.search(conf.get('ldap', 'basedn'), ldap.SCOPE_SUBTREE, search_filter, None) + result_type, result_data = ldap_conn.result(ldap_result_id, 0) except ldap.LDAPError as error: return render_template('index.html', error=error, form=form) - result_type, result_data = ldap_conn.result(ldap_result_id, 0) + if len(result_data) == 1: link_id = '{uuid}-{account}'.format( uuid=str(uuid.uuid4()), @@ -173,17 +171,13 @@ def reset(link_id): ) ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER) ldap_conn = ldap.initialize(ldap_uri, trace_level=conf.get('ldap', 'debug')) + search_filter = 'mail={mail}'.format(mail=db_data[0][1]) + try: - ldap_conn.start_tls_s() - except ldap.LDAPError as error: - return render_template('error.html', error=error) - try: - search_filter = 'mail={mail}'.format(mail=db_data[0][1]) - ldap_result_id = ldap_conn.search( - conf.get('ldap', 'basedn'), - ldap.SCOPE_SUBTREE, - search_filter, - None) + if conf.getboolean('ldap', 'starttls'): + ldap_conn.start_tls_s() + ldap_result_id = ldap_conn.search(conf.get('ldap', 'basedn'), + ldap.SCOPE_SUBTREE, search_filter, None) result_type, result_data = ldap_conn.result(ldap_result_id, 0) ldap_conn.simple_bind_s( conf.get('ldap', 'user'), conf.get('ldap', 'pass')) @@ -196,11 +190,8 @@ def reset(link_id): except ldap.LDAPError as error: error = 'LDAP error: {error}, please get in touch with \ LDAP administration.'.format(error=error) - return render_template( - 'reset.html', - error=error, - form=form - ) + return render_template('reset.html', error=error, form=form) + flash('Password for account {mail} has been changed.'.format( mail=db_data[0][1])) db_curs.execute(