From 301d8dfd0f5c280f9c22afb65263d174a61ab5b7 Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Sat, 19 Oct 2024 21:36:08 -0500 Subject: [PATCH 1/8] switch from travis to GHA --- .github/workflows/tox.yaml | 84 ++++++++++++++++++++++++++++++++++++++ .travis.yml | 32 --------------- 2 files changed, 84 insertions(+), 32 deletions(-) create mode 100644 .github/workflows/tox.yaml delete mode 100644 .travis.yml diff --git a/.github/workflows/tox.yaml b/.github/workflows/tox.yaml new file mode 100644 index 0000000..10d334f --- /dev/null +++ b/.github/workflows/tox.yaml @@ -0,0 +1,84 @@ +name: Tox CI + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +jobs: + lint: + runs-on: ubuntu-latest + steps: + - name: Checkout repo + uses: actions/checkout@v4 + + - name: Setup Python + uses: actions/setup-python@v5 + with: + python-version: "3.8" + + - name: Install tox + run: pip install --user tox + + - name: Lint + run: tox -e lint + + test: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.8", "3.9", "3.10", "3.11"] + services: + mongo: + image: mongo:7.0 + ports: + - 27017:27017 + steps: + - name: Checkout repo + uses: actions/checkout@v4 + + - name: Setup Python + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + + - name: Install tox + run: pip install --user tox + + - name: Select tox env + id: tox-env + run: echo tox-env=py${{ matrix.python-version }} | tr -d '.' >> ${GITHUB_OUTPUT} + + - name: Test + run: tox -e ${{ steps.tox-env.outputs.tox-env }} + + set_merge_ok: + name: Set Merge OK + if: always() && !contains(needs.*.result, 'failure') && !contains(needs.*.result, 'cancelled') + needs: + - lint + - test + outputs: + merge_ok: ${{ steps.set_merge_ok.outputs.merge_ok }} + runs-on: ubuntu-latest + steps: + - id: set_merge_ok + run: echo 'merge_ok=true' >> ${GITHUB_OUTPUT} + + merge_ok: + name: Merge OK + if: always() + needs: + - set_merge_ok + runs-on: ubuntu-latest + steps: + - run: | + merge_ok="${{ needs.set_merge_ok.outputs.merge_ok }}" + if [[ "${merge_ok}" == "true" ]]; then + echo "Merge OK" + exit 0 + else + echo "Merge NOT OK" + exit 1 + fi diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 1d18328..0000000 --- a/.travis.yml +++ /dev/null @@ -1,32 +0,0 @@ -language: python -python: 2.7 -sudo: false - -env: - global: - - CACHE_NAME=JOB1 -matrix: - include: - - env: TOX_ENV=lint - python: 2.7 - - env: TOX_ENV=py27 - python: 2.7 - - env: TOX_ENV=py36 - python: 3.6 - -services: - - mongodb - -cache: - pip: true - directories: - - virtualenv/ - # NOTE: Caching .tox speeds up py3 build for 30-60 seconds, but causes issues when dependencies - # are updated so it's disabled - #- .tox/ - -install: - - pip install tox - -script: - - tox -e $TOX_ENV From 28808acc4ea80cd56a532e07d1191a4afd83e308 Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Sat, 19 Oct 2024 21:37:46 -0500 Subject: [PATCH 2/8] sync test reqs w/ st2.git --- lint-configs/python/.pylintrc | 7 +------ test-requirements.txt | 4 ++-- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/lint-configs/python/.pylintrc b/lint-configs/python/.pylintrc index 8e2d3c5..2ac0894 100644 --- a/lint-configs/python/.pylintrc +++ b/lint-configs/python/.pylintrc @@ -1,14 +1,9 @@ [MESSAGES CONTROL] # C0111 Missing docstring # I0011 Warning locally suppressed using disable-msg -# I0012 Warning locally suppressed using disable-msg -# W0704 Except doesn't do anything Used when an except clause does nothing but "pass" and there is no "else" clause -# W0142 Used * or * magic* Used when a function or method is called using *args or **kwargs to dispatch arguments. # W0212 Access to a protected member %s of a client class -# W0232 Class has no __init__ method Used when a class has no __init__ method, neither its parent classes. # W0613 Unused argument %r Used when a function or method argument is not used. # W0702 No exception's type specified Used when an except clause doesn't specify exceptions type to catch. -# R0201 Method could be a function # W0614 Unused import XYZ from wildcard import # R0914 Too many local variables # R0912 Too many branches @@ -16,7 +11,7 @@ # R0913 Too many arguments # R0904 Too many public methods # E0211: Method has no argument -disable=C0103,C0111,I0011,I0012,W0704,W0142,W0212,W0232,W0613,W0702,R0201,W0614,R0914,R0912,R0915,R0913,R0904,R0801 +disable=C0103,C0111,I0011,W0212,W0613,W0702,W0614,R0914,R0912,R0915,R0913,R0904,R0801 [TYPECHECK] # Note: This modules are manipulated during the runtime so we can't detect all the properties during diff --git a/test-requirements.txt b/test-requirements.txt index 4cdc397..3ae305e 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,6 +1,6 @@ -mock==3.0.5 +mock==5.1.0 nose>=1.3.7 pep8==1.7.1 -pylint==1.9.4 +pylint~=3.1.0 st2flake8==0.1.0 unittest2 From e9cc45ef232b48e08c0aded3c388c18a3427580f Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Sat, 19 Oct 2024 21:40:26 -0500 Subject: [PATCH 3/8] add description for pypi --- setup.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/setup.py b/setup.py index 1773a6f..5c2e9e9 100644 --- a/setup.py +++ b/setup.py @@ -28,10 +28,15 @@ version = parse_version_string(INIT_FILE) install_reqs, dep_links = fetch_requirements(REQUIREMENTS_FILE) +with open("README.md", "r") as fh: + long_description = fh.read() + setup( name='st2-auth-backend-mongodb', version=version, description='StackStorm authentication backend which reads credentials from a MongoDB collection.', + long_description=long_description, + long_description_content_type="text/markdown", author='StackStorm, Inc.', author_email='info@stackstorm.com', url='https://github.com/StackStorm/st2-auth-backend-mongodb', From a2da44f18a95ef8cac969c2569df04b9474f83c0 Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Sat, 19 Oct 2024 21:41:59 -0500 Subject: [PATCH 4/8] unpin st2flake8 test dep and add flake8 --- test-requirements.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test-requirements.txt b/test-requirements.txt index 3ae305e..3777fe6 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,6 +1,7 @@ +flake8==7.0.0 mock==5.1.0 nose>=1.3.7 pep8==1.7.1 pylint~=3.1.0 -st2flake8==0.1.0 +st2flake8 unittest2 From 25ec8238bc5ac61e9bfd0bc3d44935e8a3b69001 Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Sat, 19 Oct 2024 21:46:37 -0500 Subject: [PATCH 5/8] switch from nose to pytest --- setup.py | 1 - test-requirements.txt | 3 +-- tests/unit/test_mongodb_backend.py | 6 +++--- tox.ini | 20 ++++++++++++-------- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/setup.py b/setup.py index 5c2e9e9..423d7e4 100644 --- a/setup.py +++ b/setup.py @@ -60,7 +60,6 @@ include_package_data=True, install_requires=install_reqs, dependency_links=dep_links, - test_suite='tests', entry_points={ 'st2auth.backends.backend': [ 'mongodb = st2auth_mongodb_backend.mongodb:MongoDBAuthenticationBackend', diff --git a/test-requirements.txt b/test-requirements.txt index 3777fe6..79c982e 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,7 +1,6 @@ flake8==7.0.0 mock==5.1.0 -nose>=1.3.7 pep8==1.7.1 pylint~=3.1.0 +pytest st2flake8 -unittest2 diff --git a/tests/unit/test_mongodb_backend.py b/tests/unit/test_mongodb_backend.py index 1ab80d4..f67e157 100644 --- a/tests/unit/test_mongodb_backend.py +++ b/tests/unit/test_mongodb_backend.py @@ -15,12 +15,12 @@ import sys -import unittest2 +import unittest from st2auth_mongodb_backend.mongodb import MongoDBAuthenticationBackend -class MongoDBAuthenticationBackendTestCase(unittest2.TestCase): +class MongoDBAuthenticationBackendTestCase(unittest.TestCase): hash_function = MongoDBAuthenticationBackend._hash_function fixtures = [ { @@ -55,4 +55,4 @@ def test_authenticate(self): self.assertTrue(self._backend.authenticate(username='test1', password='testpassword')) if __name__ == '__main__': - sys.exit(unittest2.main()) + sys.exit(unittest.main()) diff --git a/tox.ini b/tox.ini index f3bb992..e469917 100644 --- a/tox.ini +++ b/tox.ini @@ -1,18 +1,22 @@ [tox] -envlist = py27,py36,lint +envlist = py38,py39,py310,py311,lint [testenv] deps = -r{toxinidir}/test-requirements.txt +commands = pytest -[testenv:py27] -basepython = python2.7 -commands = python setup.py test +[testenv:py38] +basepython = python3.8 -[testenv:py36] -basepython = python3.6 -commands = python setup.py test +[testenv:py39] +basepython = python3.9 + +[testenv:py310] +basepython = python3.10 + +[testenv:py311] +basepython = python3.11 [testenv:lint] -deps = -r{toxinidir}/test-requirements.txt commands = flake8 --config ./lint-configs/python/.flake8 st2auth_mongodb_backend/ pylint -E --rcfile=./lint-configs/python/.pylintrc st2auth_mongodb_backend/ From dd217e7498b711072637e740da6e0dc4265ad71f Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Sat, 19 Oct 2024 21:47:17 -0500 Subject: [PATCH 6/8] bump version to 0.3.0 for py upgrade --- setup.py | 6 ++++-- st2auth_mongodb_backend/__init__.py | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/setup.py b/setup.py index 423d7e4..1b267b1 100644 --- a/setup.py +++ b/setup.py @@ -47,12 +47,14 @@ 'License :: OSI Approved :: Apache Software License', 'Programming Language :: Python', 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: 3.8', + 'Programming Language :: Python :: 3.9', + 'Programming Language :: Python :: 3.10', + 'Programming Language :: Python :: 3.11', 'Intended Audience :: Developers', 'Environment :: Console', ], + python_requires='>=3.8', platforms=['Any'], scripts=[], provides=['st2auth_mongodb_backend'], diff --git a/st2auth_mongodb_backend/__init__.py b/st2auth_mongodb_backend/__init__.py index 4a4bb9b..9ed4abb 100644 --- a/st2auth_mongodb_backend/__init__.py +++ b/st2auth_mongodb_backend/__init__.py @@ -21,4 +21,4 @@ 'MongoDBAuthenticationBackend' ] -__version__ = '0.2.0' +__version__ = '0.3.0' From 1ef44e23d86672d1afbe6d6016316b513cf079f1 Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Sat, 19 Oct 2024 21:55:42 -0500 Subject: [PATCH 7/8] sync pymongo req w/ st2.git --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 3bc60d2..ada7c65 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -pymongo>=2.8.1,<3.0 +pymongo>=4.0.0,<4.7 From 6397f431b9f9c8114d4906e2850a97a723df042d Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Sat, 19 Oct 2024 22:17:36 -0500 Subject: [PATCH 8/8] pymongo4 updates --- st2auth_mongodb_backend/mongodb.py | 20 ++++++++++++++------ tests/unit/test_mongodb_backend.py | 7 +++---- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/st2auth_mongodb_backend/mongodb.py b/st2auth_mongodb_backend/mongodb.py index 99b46f1..19ada28 100644 --- a/st2auth_mongodb_backend/mongodb.py +++ b/st2auth_mongodb_backend/mongodb.py @@ -42,7 +42,7 @@ class MongoDBAuthenticationBackend(object): _collection_name = 'users' _indexes = [ - ('username', pymongo.ASCENDING) + pymongo.IndexModel([('username', pymongo.ASCENDING)], unique=True) ] _hash_function = hashlib.sha256 @@ -53,12 +53,17 @@ def __init__(self, db_host='localhost', db_port=27017, db_name='st2auth', db_use self._db_port = db_port self._db_username = db_username self._db_password = db_password + self._db_indexes_created = False - self._client = MongoClient(host=self._db_host, port=self._db_port, tz_aware=True) - self._db = self._client[db_name] - + kwargs = {} if self._db_username: - self._db.authenticate(name=self._db_username, password=self._db_password) + kwargs["username"] = self._db_username + kwargs["password"] = self._db_password + + self._client = MongoClient( + host=self._db_host, port=self._db_port, tz_aware=True, **kwargs + ) + self._db = self._client[db_name] self._collection = self._db[self._collection_name] self._ensure_indexes() @@ -87,4 +92,7 @@ def get_user(self, username): pass def _ensure_indexes(self): - self._collection.ensure_index(self._indexes, unique=True) + if self._db_indexes_created: + return + self._collection.create_indexes(self._indexes) + self._db_indexes_created = True diff --git a/tests/unit/test_mongodb_backend.py b/tests/unit/test_mongodb_backend.py index f67e157..1076c8d 100644 --- a/tests/unit/test_mongodb_backend.py +++ b/tests/unit/test_mongodb_backend.py @@ -34,15 +34,14 @@ def setUp(self): self._backend = MongoDBAuthenticationBackend(db_name='st2authtest') # Clear database - self._backend._collection.remove() + self._backend._collection.delete_many({}) # Add fixtures - for fixture in self.fixtures: - self._backend._collection.insert(fixture) + self._backend._collection.insert_many(self.fixtures) def tearDown(self): # Clear database - self._backend._collection.remove() + self._backend._collection.delete_many({}) def test_authenticate(self): # Inexistent user